Skip to content

Commit 5236091

Browse files
author
Michael Manukyan
committed
add impl
1 parent 1d88a3d commit 5236091

27 files changed

+6281
-1
lines changed

.editorconfig

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
indent_size = 2
8+
indent_style = space
9+
insert_final_newline = true
10+
max_line_length = 120
11+
trim_trailing_whitespace = true

.eslintrc.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = {
2+
"env": {
3+
"browser": true,
4+
"commonjs": true,
5+
"es6": true
6+
},
7+
"extends": "eslint:recommended",
8+
"rules": {
9+
"indent": [
10+
"error",
11+
4
12+
],
13+
"linebreak-style": [
14+
"error",
15+
"unix"
16+
],
17+
"quotes": [
18+
"error",
19+
"single"
20+
],
21+
"semi": [
22+
"error",
23+
"always"
24+
]
25+
}
26+
};

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ typings/
5757
# dotenv environment variables file
5858
.env
5959

60+
.idea

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
examples

README.md

+59-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,60 @@
11
# 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).

examples/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Examples
2+
3+
This is a sample example of usage of CssChunkHashPlugin. We have 4 `.js` and `.css` files.
4+
The entry `a.js` file on button click dynamically imports `b.js` which in turn dynamically loads `c.js` and the last dynamically loads `d.js`
5+
6+
The result of the build is 8 chunks for each file `.js` and `.css` file.
7+
8+
To run the build:
9+
10+
```bash
11+
npm install
12+
../node_modules/.bin/webpack --config webpack.config.js
13+
```

examples/a.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.a {
2+
color: red;
3+
}

examples/a.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import styles from './a.css';
2+
3+
4+
const button = document.createElement('button');
5+
button.className = styles.a;
6+
button.innerText = 'load modules';
7+
button.addEventListener('click', () => {
8+
button.innerText = 'loading...';
9+
import('./b.js')
10+
.then(() => {
11+
button.innerText = 'loaded';
12+
});
13+
});
14+
15+
document.body.appendChild(button);
16+
17+

examples/b.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.a {
2+
color: green;
3+
}

examples/b.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import styles from './b.css';
2+
3+
4+
import('./c.js');

examples/c.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.c {
2+
color: blue;
3+
}

examples/c.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import styles from './c.css';
2+
3+
4+
import('./d.js');

examples/d.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.d {
2+
color: gold;
3+
}

examples/d.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import styles from './d.css';
2+
3+
const anchor = document.createElement('a');
4+
anchor.className = styles.d;
5+
anchor.innerText = 'Hi!';
6+
anchor.href = 'https://github.com/mike1808/css-chunks-html-webpack-plugin';
7+
8+
document.body.appendChild(anchor);
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
webpackJsonp([0],{
2+
3+
/***/ 4:
4+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
5+
6+
"use strict";
7+
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
8+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_babel_plugin_dual_import_importCss_js__ = __webpack_require__(0);
9+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_babel_plugin_dual_import_importCss_js___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_babel_plugin_dual_import_importCss_js__);
10+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__b_css__ = __webpack_require__(6);
11+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__b_css___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_1__b_css__);
12+
13+
14+
15+
Promise.all([__webpack_require__.e/* import() */(2).then(__webpack_require__.bind(null, 7)), __WEBPACK_IMPORTED_MODULE_0_babel_plugin_dual_import_importCss_js___default()('c')]).then(function (proms) {
16+
return proms[0];
17+
});
18+
19+
/***/ }),
20+
21+
/***/ 6:
22+
/***/ (function(module, exports) {
23+
24+
// removed by extract-text-webpack-plugin
25+
module.exports = {"a":"b__a--35CkO"};
26+
27+
/***/ })
28+
29+
});
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
webpackJsonp([1],{
2+
3+
/***/ 10:
4+
/***/ (function(module, exports) {
5+
6+
// removed by extract-text-webpack-plugin
7+
module.exports = {"d":"d__d--2-Mju"};
8+
9+
/***/ }),
10+
11+
/***/ 9:
12+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
13+
14+
"use strict";
15+
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
16+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__d_css__ = __webpack_require__(10);
17+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__d_css___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__d_css__);
18+
19+
20+
var anchor = document.createElement('a');
21+
anchor.className = __WEBPACK_IMPORTED_MODULE_0__d_css___default.a.d;
22+
anchor.innerText = 'Hi!';
23+
anchor.href = 'https://github.com/mike1808/css-chunks-html-webpack-plugin';
24+
25+
document.body.appendChild(anchor);
26+
27+
/***/ })
28+
29+
});
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
webpackJsonp([2],{
2+
3+
/***/ 7:
4+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
5+
6+
"use strict";
7+
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
8+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_babel_plugin_dual_import_importCss_js__ = __webpack_require__(0);
9+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_babel_plugin_dual_import_importCss_js___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_babel_plugin_dual_import_importCss_js__);
10+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__c_css__ = __webpack_require__(8);
11+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__c_css___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_1__c_css__);
12+
13+
14+
15+
Promise.all([__webpack_require__.e/* import() */(1).then(__webpack_require__.bind(null, 9)), __WEBPACK_IMPORTED_MODULE_0_babel_plugin_dual_import_importCss_js___default()('d')]).then(function (proms) {
16+
return proms[0];
17+
});
18+
19+
/***/ }),
20+
21+
/***/ 8:
22+
/***/ (function(module, exports) {
23+
24+
// removed by extract-text-webpack-plugin
25+
module.exports = {"c":"c__c--1O-Px"};
26+
27+
/***/ })
28+
29+
});

0 commit comments

Comments
 (0)