Skip to content

Commit 0347498

Browse files
committed
added chunkhash example
1 parent e5c11c7 commit 0347498

8 files changed

Lines changed: 303 additions & 0 deletions

File tree

‎examples/chunkhash/README.md‎

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
A common challenge with combining `[chunkhash]` and Code Splitting is that the entry chunk includes the webpack runtime and with it the chunkhash mappings. This means it's always updated and the `[chunkhash]` is pretty useless, because this chunk won't be cached.
2+
3+
A very simple solution to this problem is to create another chunk which contains only the webpack runtime (including chunkhash map). This can be archieved by the CommonsChunkPlugin (or if the CommonsChunkPlugin is already used by passing multiple names to the CommonChunkPlugin). To avoid the additional request for another chunk, this pretty small chunk can be inlined into the HTML page.
4+
5+
The configuration required for this is:
6+
7+
* use `[chunkhash]` in `output.filename` (Note that this example doesn't do this because of the example generator infrastructure, but you should)
8+
* use `[chunkhash]` in `output.chunkFilename`
9+
* `CommonsChunkPlugin`
10+
11+
# example.js
12+
13+
``` javascript
14+
import vendor from "./vendor";
15+
// some module
16+
System.import("./async1");
17+
System.import("./async2");
18+
```
19+
20+
# vendor.js
21+
22+
``` javascript
23+
// some vendor lib (should be in common chunk)
24+
export default 123;
25+
```
26+
27+
# webpack.config.js
28+
29+
``` javascript
30+
var path = require("path");
31+
var webpack = require("../../");
32+
module.exports = {
33+
entry: {
34+
main: "./example",
35+
common: ["./vendor"] // optional
36+
},
37+
output: {
38+
path: path.join(__dirname, "js"),
39+
filename: "[name].chunkhash.js",
40+
chunkFilename: "[chunkhash].js"
41+
},
42+
plugins: [
43+
new webpack.optimize.CommonsChunkPlugin({
44+
names: ["common", "manifest"]
45+
})
46+
/* without the "common" chunk:
47+
new webpack.optimize.CommonsChunkPlugin({
48+
name: "manifest"
49+
})
50+
*/
51+
]
52+
};
53+
```
54+
55+
# index.html
56+
57+
``` javascript
58+
<html>
59+
<head>
60+
</head>
61+
<body>
62+
63+
<!-- inlined minimized file "manifest.[chunkhash].js" -->
64+
<script>
65+
!function(e){function r(o){if(n[o])return n[o].exports;var t=n[o]={exports:{},id:o,loaded:!1};return e[o].call(t.exports,t,t.exports,r),t.loaded=!0,t.exports}var o=window.webpackJsonp;window.webpackJsonp=function(n,a,i){for(var d,c,f=0,u=[];f<n.length;f++)c=n[f],t[c]&&u.push(t[c][0]),t[c]=0;for(d in a)e[d]=a[d];for(o&&o(n,a);u.length;)u.shift()();return i+1?r(i):void 0};var n={},t={2:0};r.e=function(e){function o(){a.onerror=a.onload=null,clearTimeout(i);var r=t[e];0!==r&&(r&&r[1](new Error("Loading chunk "+e+" failed.")),t[e]=void 0)}if(0===t[e])return Promise.resolve();if(t[e])return t[e][2];var n=document.getElementsByTagName("head")[0],a=document.createElement("script");a.type="text/javascript",a.charset="utf-8",a.async=!0,a.timeout=12e4,a.src=r.p+""+{0:"16274d569fd1e7a2eda7",1:"509642492f90d26144b5",3:"9876f9ef706cd5c5f139",4:"7b8badb0178210fedc90"}[e]+".js";var i=setTimeout(o,12e4);a.onerror=a.onload=o,n.appendChild(a);var d=new Promise(function(r,o){t[e]=[r,o]});return t[e][2]=d},r.m=e,r.c=n,r.oe=function(e){throw e},r.p="js/"}([]);
66+
</script>
67+
68+
<!-- optional when using the CommonChunkPlugin for vendor modules -->
69+
<script src="js/common.[chunkhash].js"></script>
70+
71+
<script src="js/main.[chunkhash].js"></script>
72+
73+
</body>
74+
</html>
75+
```
76+
77+
# js/common.[chunkhash].js
78+
79+
``` javascript
80+
webpackJsonp([3,2],[
81+
/* 0 */
82+
/*!*******************!*\
83+
!*** ./vendor.js ***!
84+
\*******************/
85+
/***/ function(module, exports, __webpack_require__) {
86+
87+
// some vendor lib (should be in common chunk)
88+
/* harmony default export */ exports["default"] = 123
89+
90+
91+
/***/ },
92+
/* 1 */,
93+
/* 2 */,
94+
/* 3 */,
95+
/* 4 */
96+
/*!********************!*\
97+
!*** multi common ***!
98+
\********************/
99+
/***/ function(module, exports, __webpack_require__) {
100+
101+
module.exports = __webpack_require__(/*! ./vendor */0);
102+
103+
104+
/***/ }
105+
],[4]);
106+
```
107+
108+
# js/main.[chunkhash].js
109+
110+
``` javascript
111+
webpackJsonp([4,2],{
112+
113+
/***/ 3:
114+
/*!********************!*\
115+
!*** ./example.js ***!
116+
\********************/
117+
/***/ function(module, exports, __webpack_require__) {
118+
119+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__vendor__ = __webpack_require__(/*! ./vendor */ 0);
120+
121+
// some module
122+
__webpack_require__.e/* System.import */(1).then(__webpack_require__.bind(null, /*! ./async1 */ 1));
123+
__webpack_require__.e/* System.import */(0).then(__webpack_require__.bind(null, /*! ./async2 */ 2));
124+
125+
126+
/***/ }
127+
128+
},[3]);
129+
```
130+
131+
# Info
132+
133+
## Uncompressed
134+
135+
```
136+
Hash: 223f819918db476ed5ff
137+
Version: webpack 2.0.0-beta
138+
Time: 130ms
139+
Asset Size Chunks Chunk Names
140+
16274d569fd1e7a2eda7.js 184 bytes 0, 2 [emitted]
141+
509642492f90d26144b5.js 190 bytes 1, 2 [emitted]
142+
manifest.chunkhash.js 4.2 kB 2 [emitted] manifest
143+
common.chunkhash.js 517 bytes 3, 2 [emitted] common
144+
main.chunkhash.js 513 bytes 4, 2 [emitted] main
145+
chunk {0} 16274d569fd1e7a2eda7.js 29 bytes {4} [rendered]
146+
> [3] ./example.js 4:0-25
147+
[2] ./async2.js 29 bytes {0} [built]
148+
./async2 [3] ./example.js 4:0-25
149+
chunk {1} 509642492f90d26144b5.js 29 bytes {4} [rendered]
150+
> [3] ./example.js 3:0-25
151+
[1] ./async1.js 29 bytes {1} [built]
152+
./async1 [3] ./example.js 3:0-25
153+
chunk {2} manifest.chunkhash.js (manifest) 0 bytes [rendered]
154+
chunk {3} common.chunkhash.js (common) 97 bytes {2} [rendered]
155+
> common [4] multi common
156+
[0] ./vendor.js 69 bytes {3} [built]
157+
harmony import ./vendor [3] ./example.js 1:0-30
158+
single entry ./vendor [4] multi common
159+
[4] multi common 28 bytes {3} [built]
160+
chunk {4} main.chunkhash.js (main) 104 bytes {3} [rendered]
161+
> main [3] ./example.js
162+
[3] ./example.js 104 bytes {4} [built]
163+
```
164+
165+
## Minimized (uglify-js, no zip)
166+
167+
```
168+
Hash: 223f819918db476ed5ff
169+
Version: webpack 2.0.0-beta
170+
Time: 317ms
171+
Asset Size Chunks Chunk Names
172+
16274d569fd1e7a2eda7.js 40 bytes 0, 2 [emitted]
173+
509642492f90d26144b5.js 39 bytes 1, 2 [emitted]
174+
manifest.chunkhash.js 1.05 kB 2 [emitted] manifest
175+
common.chunkhash.js 95 bytes 3, 2 [emitted] common
176+
main.chunkhash.js 106 bytes 4, 2 [emitted] main
177+
chunk {0} 16274d569fd1e7a2eda7.js 29 bytes {4} [rendered]
178+
> [3] ./example.js 4:0-25
179+
[2] ./async2.js 29 bytes {0} [built]
180+
./async2 [3] ./example.js 4:0-25
181+
chunk {1} 509642492f90d26144b5.js 29 bytes {4} [rendered]
182+
> [3] ./example.js 3:0-25
183+
[1] ./async1.js 29 bytes {1} [built]
184+
./async1 [3] ./example.js 3:0-25
185+
chunk {2} manifest.chunkhash.js (manifest) 0 bytes [rendered]
186+
chunk {3} common.chunkhash.js (common) 97 bytes {2} [rendered]
187+
> common [4] multi common
188+
[0] ./vendor.js 69 bytes {3} [built]
189+
harmony import ./vendor [3] ./example.js 1:0-30
190+
single entry ./vendor [4] multi common
191+
[4] multi common 28 bytes {3} [built]
192+
chunk {4} main.chunkhash.js (main) 104 bytes {3} [rendered]
193+
> main [3] ./example.js
194+
[3] ./example.js 104 bytes {4} [built]
195+
```

‎examples/chunkhash/async1.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// some async loaded module

‎examples/chunkhash/async2.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// some async loaded module

‎examples/chunkhash/build.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
global.NO_TARGET_ARGS = true;
2+
require("../build-common");

‎examples/chunkhash/example.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import vendor from "./vendor";
2+
// some module
3+
System.import("./async1");
4+
System.import("./async2");

‎examples/chunkhash/template.md‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
A common challenge with combining `[chunkhash]` and Code Splitting is that the entry chunk includes the webpack runtime and with it the chunkhash mappings. This means it's always updated and the `[chunkhash]` is pretty useless, because this chunk won't be cached.
2+
3+
A very simple solution to this problem is to create another chunk which contains only the webpack runtime (including chunkhash map). This can be archieved by the CommonsChunkPlugin (or if the CommonsChunkPlugin is already used by passing multiple names to the CommonChunkPlugin). To avoid the additional request for another chunk, this pretty small chunk can be inlined into the HTML page.
4+
5+
The configuration required for this is:
6+
7+
* use `[chunkhash]` in `output.filename` (Note that this example doesn't do this because of the example generator infrastructure, but you should)
8+
* use `[chunkhash]` in `output.chunkFilename`
9+
* `CommonsChunkPlugin`
10+
11+
# example.js
12+
13+
``` javascript
14+
{{example.js}}
15+
```
16+
17+
# vendor.js
18+
19+
``` javascript
20+
{{vendor.js}}
21+
```
22+
23+
# webpack.config.js
24+
25+
``` javascript
26+
{{webpack.config.js}}
27+
```
28+
29+
# index.html
30+
31+
``` javascript
32+
<html>
33+
<head>
34+
</head>
35+
<body>
36+
37+
<!-- inlined minimized file "manifest.[chunkhash].js" -->
38+
<script>
39+
{{min:js/manifest.chunkhash.js}}
40+
</script>
41+
42+
<!-- optional when using the CommonChunkPlugin for vendor modules -->
43+
<script src="js/common.[chunkhash].js"></script>
44+
45+
<script src="js/main.[chunkhash].js"></script>
46+
47+
</body>
48+
</html>
49+
```
50+
51+
# js/common.[chunkhash].js
52+
53+
``` javascript
54+
{{js/common.chunkhash.js}}
55+
```
56+
57+
# js/main.[chunkhash].js
58+
59+
``` javascript
60+
{{js/main.chunkhash.js}}
61+
```
62+
63+
# Info
64+
65+
## Uncompressed
66+
67+
```
68+
{{stdout}}
69+
```
70+
71+
## Minimized (uglify-js, no zip)
72+
73+
```
74+
{{min:stdout}}
75+
```

‎examples/chunkhash/vendor.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// some vendor lib (should be in common chunk)
2+
export default 123;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var path = require("path");
2+
var webpack = require("../../");
3+
module.exports = {
4+
entry: {
5+
main: "./example",
6+
common: ["./vendor"] // optional
7+
},
8+
output: {
9+
path: path.join(__dirname, "js"),
10+
filename: "[name].chunkhash.js",
11+
chunkFilename: "[chunkhash].js"
12+
},
13+
plugins: [
14+
new webpack.optimize.CommonsChunkPlugin({
15+
names: ["common", "manifest"]
16+
})
17+
/* without the "common" chunk:
18+
new webpack.optimize.CommonsChunkPlugin({
19+
name: "manifest"
20+
})
21+
*/
22+
]
23+
};

0 commit comments

Comments
 (0)