Skip to content

Commit 4f9fc01

Browse files
committed
Added deepChildren support from ArcEglos' pull request and fixed failing tests
1 parent b8f181f commit 4f9fc01

18 files changed

Lines changed: 708 additions & 16 deletions

File tree

‎examples/common-chunk-grandchildren/README.md‎

Lines changed: 469 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("../build-common");
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var main = function() {
2+
console.log("Main class");
3+
require.ensure([], () => {
4+
const page = require("./pageA");
5+
page();
6+
});
7+
require.ensure([], () => {
8+
const page = require("./pageB");
9+
page();
10+
});
11+
};
12+
13+
main();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var reusableComponent = require("./reusableComponent");
2+
3+
module.exports = function() {
4+
console.log("Page A");
5+
reusableComponent();
6+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = function() {
2+
console.log("Page B");
3+
require.ensure([], ()=>{
4+
const page = require("./pageC");
5+
page();
6+
});
7+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var reusableComponent = require("./reusableComponent");
2+
3+
module.exports = function() {
4+
console.log("Page C");
5+
reusableComponent();
6+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function() {
2+
console.log("reusable Component");
3+
};
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
This example illustrates how common modules from deep ancestors of an entry point can be split into a seperate common chunk
2+
3+
* `pageA` and `pageB` are dynamically required
4+
* `pageC` and `pageA` both require the `reusableComponent`
5+
* `pageB` dynamically requires `PageC`
6+
7+
You can see that webpack outputs four files/chunks:
8+
9+
* `output.js` is the entry chunk and contains
10+
* the module system
11+
* chunk loading logic
12+
* the entry point `example.js`
13+
* module `reusableComponent`
14+
* `0.output.js` is an additional chunk
15+
* module `pageC`
16+
* `1.output.js` is an additional chunk
17+
* module `pageB`
18+
* `2.output.js` is an additional chunk
19+
* module `pageA`
20+
21+
22+
# example.js
23+
24+
``` javascript
25+
{{example.js}}
26+
```
27+
28+
# pageA.js
29+
30+
``` javascript
31+
{{pageA.js}}
32+
```
33+
34+
# pageB.js
35+
36+
``` javascript
37+
{{pageB.js}}
38+
```
39+
40+
# pageC.js
41+
42+
``` javascript
43+
{{pageC.js}}
44+
```
45+
46+
# reusableComponent.js
47+
48+
``` javascript
49+
{{reusableComponent.js}}
50+
```
51+
52+
# webpack.config.js
53+
54+
``` javascript
55+
{{webpack.config.js}}
56+
```
57+
58+
# js/output.js
59+
60+
``` javascript
61+
{{js/output.js}}
62+
```
63+
64+
# js/0.output.js
65+
66+
``` javascript
67+
{{js/0.output.js}}
68+
```
69+
70+
# js/1.output.js
71+
72+
``` javascript
73+
{{js/1.output.js}}
74+
```
75+
76+
# js/2.output.js
77+
78+
``` javascript
79+
{{js/2.output.js}}
80+
```
81+
82+
# Info
83+
84+
## Uncompressed
85+
86+
```
87+
{{stdout}}
88+
```
89+
90+
## Minimized (uglify-js, no zip)
91+
92+
```
93+
{{min:stdout}}
94+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var webpack = require("../../");
2+
3+
module.exports = {
4+
entry: {
5+
main: ["./example.js"]
6+
},
7+
plugins: [
8+
new webpack.optimize.CommonsChunkPlugin({
9+
name: "main",
10+
minChunks: 2,
11+
children: true,
12+
deepChildren: true,
13+
})
14+
]
15+
};

‎lib/optimize/CommonsChunkPlugin.js‎

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
"use strict";
66
let nextIdent = 0;
7+
78
class CommonsChunkPlugin {
89
constructor(options) {
910
if(arguments.length > 1) {
@@ -31,6 +32,7 @@ The available options are:
3132
this.minChunks = normalizedOptions.minChunks;
3233
this.selectedChunks = normalizedOptions.selectedChunks;
3334
this.children = normalizedOptions.children;
35+
this.deepChildren = normalizedOptions.deepChildren;
3436
this.async = normalizedOptions.async;
3537
this.minSize = normalizedOptions.minSize;
3638
this.ident = __filename + (nextIdent++);
@@ -76,6 +78,7 @@ You can however specify the name of the async chunk by passing the desired strin
7678
minChunks: options.minChunks,
7779
selectedChunks: options.chunks,
7880
children: options.children,
81+
deepChildren: options.deepChildren,
7982
async: options.async,
8083
minSize: options.minSize
8184
};
@@ -211,6 +214,37 @@ You can however specify the name of the async chunk by passing the desired strin
211214
Take a look at the "name"/"names" or async/children option.`);
212215
}
213216

217+
getAffectedUnnamedChunks(affectedChunks, targetChunk, asyncOption) {
218+
let chunks = targetChunk.chunks;
219+
chunks && chunks.forEach((chunk) => {
220+
if(chunk.isInitial()) {
221+
return;
222+
}
223+
// If all the parents of a chunk are either
224+
// a) the target chunk we started with
225+
// b) themselves affected chunks
226+
// we can assume that this chunk is an affected chunk too, as there is no way a chunk that
227+
// isn't only depending on the target chunk is a parent of the chunk tested
228+
if(asyncOption || chunk.parents.every((parentChunk) => parentChunk === targetChunk || affectedChunks.indexOf(parentChunk) >= 0)) {
229+
// This check not only dedupes the affectedChunks but also guarantees we avoid endless loops
230+
if(affectedChunks.findIndex(usedChunk => usedChunk === chunk) < 1) {
231+
// We mutate the affected chunks before going deeper, so the deeper levels and other branches
232+
// Have the information of this chunk being affected for their assertion if a chunk shouldnt
233+
// be affected
234+
affectedChunks.push(chunk);
235+
236+
// We recurse down to all the children of the chunk, applying the same assumption.
237+
// This guarantees that if a chunk should be an affected chunk,
238+
// at the latest the last connection to the same chunk meets the
239+
// condition to add it to the affected chunks.
240+
if(this.deepChildren === true) {
241+
this.getAffectedUnnamedChunks(affectedChunks, chunk, asyncOption);
242+
}
243+
}
244+
}
245+
});
246+
}
247+
214248
getAffectedChunks(compilation, allChunks, targetChunk, targetChunks, currentIndex, selectedChunks, asyncOption, children) {
215249
const asyncOrNoSelectedChunk = children || asyncOption;
216250

@@ -223,22 +257,9 @@ Take a look at the "name"/"names" or async/children option.`);
223257
}
224258

225259
if(asyncOrNoSelectedChunk) {
226-
// nothing to do here
227-
if(!targetChunk.chunks) {
228-
return [];
229-
}
230-
231-
return targetChunk.chunks.filter((chunk) => {
232-
// we only are interested in on-demand chunks
233-
if(chunk.isInitial())
234-
return false;
235-
236-
// we can only move modules from this chunk if the "commonChunk" is the only parent
237-
if(!asyncOption)
238-
return chunk.parents.length === 1;
239-
240-
return true;
241-
});
260+
let affectedChunks = [];
261+
this.getAffectedUnnamedChunks(affectedChunks, targetChunk, asyncOption);
262+
return affectedChunks;
242263
}
243264

244265
/**

0 commit comments

Comments
 (0)