|
1 | 1 | # css-chunks-html-webpack-plugin
|
2 |
| -Injecting css chunks extracted using extract-css-chunks-webpack-plugin to HTML for html-webpack-plugin |
| 2 | +Plugin for injecting css chunks, extracted using extract-css-chunks-webpack-plugin, to HTML for html-webpack-plugin |
| 3 | + |
| 4 | +Use in conjunction with |
| 5 | +[extract-css-chunks-webpack-plugin](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin) and |
| 6 | +[babel-plugin-dual-import](https://github.com/faceyspacey/babel-plugin-dual-import) |
| 7 | +to inject CSS chunks paths into your HTML file with |
| 8 | +[html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin). |
| 9 | + |
| 10 | +## Recommended Installation |
| 11 | + |
| 12 | +```bash |
| 13 | +npm install --save-dev css-chunks-html-webpack-plugin extract-css-chunks-webpack-plugin babel-plugin-dual-import html-webpack-plugin |
| 14 | +``` |
| 15 | + |
| 16 | +*webpack.config.js* |
| 17 | +```js |
| 18 | + const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); |
| 19 | + const HtmlWebpackPlugin = require('html-webpack-plugin'); |
| 20 | + const CssChunkHashPlugin = require('../lib'); |
| 21 | + |
| 22 | + |
| 23 | + module.exports = { |
| 24 | + // your other options |
| 25 | + plugins: [ |
| 26 | + new ExtractCssChunks(), |
| 27 | + new CssChunkHashPlugin({ inject: true }), |
| 28 | + new HtmlWebpackPlugin(), |
| 29 | + ] |
| 30 | + }; |
| 31 | + |
| 32 | +``` |
| 33 | + |
| 34 | +## Configuration |
| 35 | + |
| 36 | +You can pass an object of configuration options to CssChunkHashPlugin. Allowed values are as follows: |
| 37 | + |
| 38 | +* `inject`: `true` | `false` whether to inject chunks paths object into HTML, used for manually adding chunks paths using custom teamplte for HtmlWebpackPlugin (default `true`) |
| 39 | + |
| 40 | +The CSS chunks paths map is saved in `htmlWebpackPlugin.files.cssHash` in your template. So if you want to manually add CSS chunks map you can do (if you are using EJS): |
| 41 | + |
| 42 | +```ejs |
| 43 | +<script type="text/javascript"> |
| 44 | + window.__CSS_CHUNKS__ = JSON.parse('<%= JSON.stringify(htmlWebpackPlugin.files.cssHash) %>') |
| 45 | +</script> |
| 46 | +``` |
| 47 | + |
| 48 | +Otherwise, if you set `inject: true` the script tag with `window.__CSS_CHUNKS__` will be injected in `head` or `body` according your HtmlWebpackPlugin configuration. |
| 49 | + |
| 50 | +## Example |
| 51 | + |
| 52 | +There is a basic example of usage in [examples][examples] |
| 53 | + |
| 54 | +# Contribution |
| 55 | + |
| 56 | +You're free to contribute to this project by submitting [issues](https://github.com/mike1808/css-chunks-html-webpack-plugin/issues) and/or [pull requests](https://github.com/mike1808/css-chunks-html-webpack-plugin/pulls). |
| 57 | + |
| 58 | +# License |
| 59 | + |
| 60 | +This project is licensed under [MIT](LICENSE). |
0 commit comments