Skip to content

Commit 1679218

Browse files
committed
fixed CommonsChunkPlugin
and added two explict vendor chunks example
1 parent 91366de commit 1679218

11 files changed

Lines changed: 306 additions & 3 deletions

File tree

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
# webpack.config.js
2+
3+
``` javascript
4+
var path = require("path");
5+
var CommonsChunkPlugin = require("../../lib/optimize/CommonsChunkPlugin");
6+
module.exports = {
7+
entry: {
8+
vendor1: "./vendor1",
9+
vendor2: "./vendor2",
10+
pageA: "./pageA",
11+
pageB: "./pageB",
12+
pageC: "./pageC"
13+
},
14+
output: {
15+
path: path.join(__dirname, "js"),
16+
filename: "[name].js"
17+
},
18+
plugins: [
19+
new CommonsChunkPlugin({
20+
names: ["vendor2", "vendor1"],
21+
minChunks: Infinity
22+
})
23+
]
24+
}
25+
```
26+
27+
# js/vendor1.js
28+
29+
``` javascript
30+
/******/ (function(modules) { // webpackBootstrap
31+
/******/ // install a JSONP callback for chunk loading
32+
/******/ var parentJsonpFunction = window["webpackJsonp"];
33+
/******/ window["webpackJsonp"] = function webpackJsonpCallback(chunkIds, moreModules) {
34+
/******/ // add "moreModules" to the modules object,
35+
/******/ // then flag all "chunkIds" as loaded and fire callback
36+
/******/ var moduleId, chunkId, i = 0, callbacks = [];
37+
/******/ for(;i < chunkIds.length; i++) {
38+
/******/ chunkId = chunkIds[i];
39+
/******/ if(installedChunks[chunkId])
40+
/******/ callbacks.push.apply(callbacks, installedChunks[chunkId]);
41+
/******/ installedChunks[chunkId] = 0;
42+
/******/ }
43+
/******/ for(moduleId in moreModules) {
44+
/******/ modules[moduleId] = moreModules[moduleId];
45+
/******/ }
46+
/******/ if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules);
47+
/******/ while(callbacks.length)
48+
/******/ callbacks.shift().call(null, __webpack_require__);
49+
/******/ if(moreModules[0]) {
50+
/******/ installedModules[0] = 0;
51+
/******/ return __webpack_require__(0);
52+
/******/ }
53+
/******/ };
54+
55+
/******/ // The module cache
56+
/******/ var installedModules = {};
57+
58+
/******/ // object to store loaded and loading chunks
59+
/******/ // "0" means "already loaded"
60+
/******/ // Array means "loading", array contains callbacks
61+
/******/ var installedChunks = {
62+
/******/ 0:0
63+
/******/ };
64+
65+
/******/ // The require function
66+
/******/ function __webpack_require__(moduleId) {
67+
68+
/******/ // Check if module is in cache
69+
/******/ if(installedModules[moduleId])
70+
/******/ return installedModules[moduleId].exports;
71+
72+
/******/ // Create a new module (and put it into the cache)
73+
/******/ var module = installedModules[moduleId] = {
74+
/******/ exports: {},
75+
/******/ id: moduleId,
76+
/******/ loaded: false
77+
/******/ };
78+
79+
/******/ // Execute the module function
80+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
81+
82+
/******/ // Flag the module as loaded
83+
/******/ module.loaded = true;
84+
85+
/******/ // Return the exports of the module
86+
/******/ return module.exports;
87+
/******/ }
88+
89+
/******/ // This file contains only the entry chunk.
90+
/******/ // The chunk loading function for additional chunks
91+
/******/ __webpack_require__.e = function requireEnsure(chunkId, callback) {
92+
/******/ // "0" is the signal for "already loaded"
93+
/******/ if(installedChunks[chunkId] === 0)
94+
/******/ return callback.call(null, __webpack_require__);
95+
96+
/******/ // an array means "currently loading".
97+
/******/ if(installedChunks[chunkId] !== undefined) {
98+
/******/ installedChunks[chunkId].push(callback);
99+
/******/ } else {
100+
/******/ // start chunk loading
101+
/******/ installedChunks[chunkId] = [callback];
102+
/******/ var head = document.getElementsByTagName('head')[0];
103+
/******/ var script = document.createElement('script');
104+
/******/ script.type = 'text/javascript';
105+
/******/ script.charset = 'utf-8';
106+
/******/ script.async = true;
107+
108+
/******/ script.src = __webpack_require__.p + "" + chunkId + "." + ({"1":"vendor2","2":"pageC","3":"pageB","4":"pageA"}[chunkId]||chunkId) + ".js";
109+
/******/ head.appendChild(script);
110+
/******/ }
111+
/******/ };
112+
113+
/******/ // expose the modules object (__webpack_modules__)
114+
/******/ __webpack_require__.m = modules;
115+
116+
/******/ // expose the module cache
117+
/******/ __webpack_require__.c = installedModules;
118+
119+
/******/ // __webpack_public_path__
120+
/******/ __webpack_require__.p = "js/";
121+
122+
/******/ // Load entry module and return exports
123+
/******/ return __webpack_require__(0);
124+
/******/ })
125+
/************************************************************************/
126+
/******/ ([
127+
/* 0 */
128+
/*!********************!*\
129+
!*** ./vendor1.js ***!
130+
\********************/
131+
/***/ function(module, exports, __webpack_require__) {
132+
133+
module.exports = "Vendor1";
134+
135+
/***/ }
136+
/******/ ]);
137+
```
138+
139+
# js/vendor2.js
140+
141+
``` javascript
142+
webpackJsonp([1],[
143+
/* 0 */
144+
/*!********************!*\
145+
!*** ./vendor2.js ***!
146+
\********************/
147+
/***/ function(module, exports, __webpack_require__) {
148+
149+
module.exports = "Vendor2";
150+
151+
/***/ }
152+
]);
153+
```
154+
155+
# js/pageA.js
156+
157+
``` javascript
158+
webpackJsonp([4],[
159+
/* 0 */
160+
/*!******************!*\
161+
!*** ./pageA.js ***!
162+
\******************/
163+
/***/ function(module, exports, __webpack_require__) {
164+
165+
module.exports = "pageA";
166+
167+
/***/ }
168+
]);
169+
```
170+
171+
# Info
172+
173+
## Uncompressed
174+
175+
```
176+
Hash: eb19ed22782f5af61ef3
177+
Version: webpack 1.8.10
178+
Time: 62ms
179+
Asset Size Chunks Chunk Names
180+
vendor1.js 3.87 kB 0 [emitted] vendor1
181+
vendor2.js 201 bytes 1 [emitted] vendor2
182+
pageC.js 193 bytes 2 [emitted] pageC
183+
pageB.js 193 bytes 3 [emitted] pageB
184+
pageA.js 193 bytes 4 [emitted] pageA
185+
chunk {0} vendor1.js (vendor1) 27 bytes [rendered]
186+
> vendor1 [0] ./vendor1.js
187+
[0] ./vendor1.js 27 bytes {0} [built]
188+
chunk {1} vendor2.js (vendor2) 27 bytes {0} [rendered]
189+
> vendor2 [0] ./vendor2.js
190+
[0] ./vendor2.js 27 bytes {1} [built]
191+
chunk {2} pageC.js (pageC) 25 bytes {1} [rendered]
192+
> pageC [0] ./pageC.js
193+
[0] ./pageC.js 25 bytes {2} [built]
194+
chunk {3} pageB.js (pageB) 25 bytes {1} [rendered]
195+
> pageB [0] ./pageB.js
196+
[0] ./pageB.js 25 bytes {3} [built]
197+
chunk {4} pageA.js (pageA) 25 bytes {1} [rendered]
198+
> pageA [0] ./pageA.js
199+
[0] ./pageA.js 25 bytes {4} [built]
200+
```
201+
202+
## Minimized (uglify-js, no zip)
203+
204+
```
205+
Hash: eb19ed22782f5af61ef3
206+
Version: webpack 1.8.10
207+
Time: 156ms
208+
Asset Size Chunks Chunk Names
209+
vendor1.js 817 bytes 0 [emitted] vendor1
210+
vendor2.js 57 bytes 1 [emitted] vendor2
211+
pageC.js 55 bytes 2 [emitted] pageC
212+
pageB.js 55 bytes 3 [emitted] pageB
213+
pageA.js 55 bytes 4 [emitted] pageA
214+
chunk {0} vendor1.js (vendor1) 27 bytes [rendered]
215+
> vendor1 [0] ./vendor1.js
216+
[0] ./vendor1.js 27 bytes {0} [built]
217+
chunk {1} vendor2.js (vendor2) 27 bytes {0} [rendered]
218+
> vendor2 [0] ./vendor2.js
219+
[0] ./vendor2.js 27 bytes {1} [built]
220+
chunk {2} pageC.js (pageC) 25 bytes {1} [rendered]
221+
> pageC [0] ./pageC.js
222+
[0] ./pageC.js 25 bytes {2} [built]
223+
chunk {3} pageB.js (pageB) 25 bytes {1} [rendered]
224+
> pageB [0] ./pageB.js
225+
[0] ./pageB.js 25 bytes {3} [built]
226+
chunk {4} pageA.js (pageA) 25 bytes {1} [rendered]
227+
> pageA [0] ./pageA.js
228+
[0] ./pageA.js 25 bytes {4} [built]
229+
```
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");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<head></head>
3+
<body>
4+
<script src="js/vendor.js" charset="utf-8"></script>
5+
<script src="js/pageA.js" charset="utf-8"></script>
6+
</body>
7+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "pageA";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "pageB";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "pageC";
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# webpack.config.js
2+
3+
``` javascript
4+
{{webpack.config.js}}
5+
```
6+
7+
# js/vendor1.js
8+
9+
``` javascript
10+
{{js/vendor1.js}}
11+
```
12+
13+
# js/vendor2.js
14+
15+
``` javascript
16+
{{js/vendor2.js}}
17+
```
18+
19+
# js/pageA.js
20+
21+
``` javascript
22+
{{js/pageA.js}}
23+
```
24+
25+
# Info
26+
27+
## Uncompressed
28+
29+
```
30+
{{stdout}}
31+
```
32+
33+
## Minimized (uglify-js, no zip)
34+
35+
```
36+
{{min:stdout}}
37+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "Vendor1";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "Vendor2";
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var path = require("path");
2+
var CommonsChunkPlugin = require("../../lib/optimize/CommonsChunkPlugin");
3+
module.exports = {
4+
entry: {
5+
vendor1: "./vendor1",
6+
vendor2: "./vendor2",
7+
pageA: "./pageA",
8+
pageB: "./pageB",
9+
pageC: "./pageC"
10+
},
11+
output: {
12+
path: path.join(__dirname, "js"),
13+
filename: "[name].js"
14+
},
15+
plugins: [
16+
new CommonsChunkPlugin({
17+
names: ["vendor2", "vendor1"],
18+
minChunks: Infinity
19+
})
20+
]
21+
}

0 commit comments

Comments
 (0)