webpack-for-babel-plugin

babel 6 plugin which allows to use webpack loaders

Usage no npm install needed!

<script type="module">
  import webpackForBabelPlugin from 'https://cdn.skypack.dev/webpack-for-babel-plugin';
</script>

README

Build Status

babel-plugin-webpack-loaders

This babel 6 plugin allows you to use webpack loaders in babel. It's now easy to run universal apps on the server without additional build steps and to create libraries as usual with babel src --out-dir lib command.

For now this plugin is at alpha quality and tested on webpack loaders I use in my projects. These loaders are file-loader, url-loader, css-loader, style-loader, sass-loader, postcss-loader. Plugin supports all webpack features like loaders chaining, webpack plugins, and all loaders params. It's easy because this plugin just uses webpack.

There are two examples here:

How it works

You need to create webpack config file, like this

// webpack.config.js css-modules loader example
module.exports = {
  output: {
    // YOU NEED TO SET libraryTarget: 'commonjs2'
    libraryTarget: 'commonjs2',
    path: './build',
  },
  module: {
    loaders: [
      {
        test: /\.css$/,
        loaders: [
          'style-loader',
          'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]--[hash:base64:5]',
          'postcss-loader',
        ],
      },
    ],
  },
};

You need to add to .babelrc next lines like here

{
  "presets": ["es2015"],
  "env": {
    "EXAMPLES_RUN": {
      "plugins": [
        [
          "babel-plugin-webpack-loaders",
          {
            "config": "./webpack.config.js",
            "verbose": false,
          }
        ]
      ]
    }
  }
}

And now you can include css with support of css-modules into js file

.main {
  display: flex;
}

.item {
  flex: 1 1 auto;
}
import css from './some.css';
console.log(css);

Run NODE_ENV=EXAMPLES_RUN babel-node your.js and you get console output

{ main: 'some__main--2kmsh', item: 'some__item--ocDmM' }

you can test this and other examples just cloning this repo and running next commands

npm install

# example above
npm run example-run

# library example - build library with a lot of modules
npm run example-build
# and now you can use your library using just node
node build/myCoolLibrary/myCoolLibrary.js

# test sources are also good examples
npm run test

Install

npm install babel-plugin-webpack-loaders

Examples

webpack configs

examples

.babelrc example

tests

Why

The source of inspiration of this plugin is babel-plugin-css-modules-transform

  • But I love to write css with sass (sometimes with less or with scss), and it's easy to configure with webpack loaders, with full support of images and fonts preloading as base64 (using url-loader).

  • Webpack already has a lot of loaders with a lot of params, so I could reuse them

  • I have a lot of webpack configs where all troubles with params and settings was solved

How plugin works internally

Plugin tests all require pathes with test regexps from webpack config loaders,