UNPKG

6.74 kBMarkdownView Raw
1[![Build Status](https://travis-ci.org/istarkov/babel-plugin-webpack-loaders.svg?branch=master)](https://travis-ci.org/istarkov/babel-plugin-webpack-loaders)
2
3# babel-plugin-webpack-loaders
4
5This babel 6 plugin allows you to use webpack loaders in babel.
6It'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.
7It just replaces `require - import` statements with `webpack loaders` results. Look at babel build output [diff](https://github.com/istarkov/babel-plugin-webpack-loaders/commit/2a7a6d1e61ea3d052b34afd5c3abc46f075d277c#diff-4) to get the idea.
8
9For now this plugin is at alpha quality and tested on webpack loaders I use in my projects.
10These loaders are `file-loader`, `url-loader`, `css-loader`, `style-loader`, `sass-loader`, `postcss-loader`.
11Plugin supports all webpack features like loaders chaining, webpack plugins, and all loaders params. It's easy because this plugin just uses webpack.
12
13There are three examples here:
14
15- [runtime css-modules example](https://github.com/istarkov/babel-plugin-webpack-loaders/blob/master/examples/runExample/run.js) with simple [webpack config](https://github.com/istarkov/babel-plugin-webpack-loaders/blob/master/examples_webpack_configs/run.webpack.config.js),
16run it with `npm run example-run`
17
18- [library example](https://github.com/istarkov/babel-plugin-webpack-loaders/blob/master/examples/myCoolLibrary/myCoolLibrary.js) with [multi loaders-plugins webpack config](https://github.com/istarkov/babel-plugin-webpack-loaders/blob/master/examples_webpack_configs/lib.webpack.config.js),
19build it with `npm run example-build` and execute with `node build/myCoolLibrary/myCoolLibrary.js`, assets and code will be placed at `./build/myCoolLibrary` folder.
20
21 Here is [output diff](https://github.com/istarkov/babel-plugin-webpack-loaders/commit/2a7a6d1e61ea3d052b34afd5c3abc46f075d277c#diff-4) of this [library example](https://github.com/istarkov/babel-plugin-webpack-loaders/blob/master/examples/myCoolLibrary/myCoolLibrary.js) build without and with current plugin.
22
23- [minimal-example-standalone-repo](https://github.com/istarkov/minimal-example-for-babel-plugin-webpack-loaders)
24
25## Warning
26
27**Do not run this plugin as part of webpack frontend configuration. This plugin is intended only for backend compilation.**
28
29
30# How it works
31
32Look at [minimal-example](https://github.com/istarkov/minimal-example-for-babel-plugin-webpack-loaders)
33
34- You need to create [webpack config](https://github.com/istarkov/minimal-example-for-babel-plugin-webpack-loaders/blob/master/webpack.config.js) file.
35
36- You need add to `.babelrc` [next lines](https://github.com/istarkov/minimal-example-for-babel-plugin-webpack-loaders/blob/master/.babelrc#L1-L16)
37
38- Now you can run [next javascript file](https://github.com/istarkov/minimal-example-for-babel-plugin-webpack-loaders/blob/master/example.js)
39
40 ```javascript
41 // example.js
42 import css from './example.css';
43 console.log('css-modules result:', css);
44 ```
45
46 with command `NODE_ENV=EXAMPLE ./node_modules/.bin/babel-node ./example.js` and you get the console output
47
48 ```javascript
49 css-modules result: { main: 'example__main--zYOjd', item: 'example__item--W9XoN' }
50 ```
51
52- Here I placed [output diff](https://github.com/istarkov/babel-plugin-webpack-loaders/commit/2a7a6d1e61ea3d052b34afd5c3abc46f075d277c#diff-4)
53of this [babel library](https://github.com/istarkov/babel-plugin-webpack-loaders/blob/master/examples/myCoolLibrary/myCoolLibrary.js) build without and with plugin.
54As you can see plugin just replaces require with loaders results. [All loaders](https://github.com/istarkov/babel-plugin-webpack-loaders/blob/example-output/build/myCoolLibrary/assets/myCoolStyle.css#L12) and [plugins](https://github.com/istarkov/babel-plugin-webpack-loaders/blob/example-output/build/myCoolLibrary/assets/myCoolStyle.css#L4) applied to generated assets
55
56
57# Install
58
59```shell
60npm install --save-dev babel-cli babel-plugin-webpack-loaders
61```
62
63# Examples
64
65[webpack configs](https://github.com/istarkov/babel-plugin-webpack-loaders/tree/master/examples_webpack_configs),
66[examples](https://github.com/istarkov/babel-plugin-webpack-loaders/tree/master/examples),
67[.babelrc example](https://github.com/istarkov/babel-plugin-webpack-loaders/blob/master/.babelrc),
68[tests](https://github.com/istarkov/babel-plugin-webpack-loaders/tree/master/test),
69[minimal-example-repo](https://github.com/istarkov/minimal-example-for-babel-plugin-webpack-loaders)
70
71you can test local examples just cloning this repo and running next commands
72
73```shell
74npm install
75# example above
76npm run example-run
77# library example - build library with a lot of modules
78npm run example-build
79# and now you can use your library using just node
80node build/myCoolLibrary/myCoolLibrary.js
81# test sources are also good examples
82npm run test
83```
84
85# Why
86
87The source of inspiration of this plugin is [babel-plugin-css-modules-transform](https://github.com/michalkvasnicak/babel-plugin-css-modules-transform)
88
89- But I love to write css with sass
90- I just like webpack and its loaders (chaining, plugins, settings).
91- I want to opensource an UI library which heavily uses css-modules + sass and other webpack loaders.
92 Library contains of many small modules and every module must be available to users independently like `lodash/blabla/blublu`.
93 Now I can replace heavy build file with this plugin and just one command `babel src --out-dir lib`
94
95# How plugin works internally
96
97Plugin tests all `require` pathes with [test regexps](https://github.com/istarkov/babel-plugin-webpack-loaders/blob/master/src/plugin.js#L91) from webpack config loaders,
98
99- for each successful test plugin [synchronously executes webpack](https://github.com/istarkov/babel-plugin-webpack-loaders/blob/master/src/runWebPackSync.js#L15-L16),
100
101- using babel-parse plugin [parses webpack output](https://github.com/istarkov/babel-plugin-webpack-loaders/blob/master/src/plugin.js#L7),
102
103- plugin [replaces](https://github.com/istarkov/babel-plugin-webpack-loaders/blob/master/src/plugin.js#L104) require ast with parse ast output.
104
105# Verbose mode in config
106
107By default babel caches compiled files, if you need to view webpack stdout output, run commands with
108`BABEL_DISABLE_CACHE=1` prefix
109
110# Thanks to
111
112[Felix Kling](https://github.com/fkling) and his [astexplorer](https://github.com/fkling/astexplorer)
113
114[James Kyle](https://github.com/thejameskyle) and his [babel-plugin-handbook](https://github.com/thejameskyle/babel-plugin-handbook)
115
116[Michal Kvasničák](https://github.com/michalkvasnicak) and his [babel-plugin-css-modules-transform](https://github.com/michalkvasnicak/babel-plugin-css-modules-transform)