Skip to content

Commit 85acfeb

Browse files
committed
allow to use new options minSize, maxRequests
1 parent 4b81320 commit 85acfeb

File tree

5 files changed

+102
-52
lines changed

5 files changed

+102
-52
lines changed

lib/WebpackOptionsApply.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,13 @@ class WebpackOptionsApply extends OptionsApply {
288288
if(options.optimization.concatenateModules)
289289
new ModuleConcatenationPlugin().apply(compiler);
290290
if(options.optimization.asyncCommonsChunks) {
291-
new AutomaticCommonsChunksPlugin({
292-
initialChunks: false
293-
}).apply(compiler);
291+
new AutomaticCommonsChunksPlugin(Object.assign(
292+
{},
293+
options.optimization.asyncCommonsChunks,
294+
{
295+
initialChunks: false
296+
}
297+
)).apply(compiler);
294298
}
295299
if(options.optimization.initialCommonsChunks || options.optimization.initialVendorsChunk) {
296300
let nameOption = options.optimization.initialVendorsChunk;
@@ -326,11 +330,15 @@ class WebpackOptionsApply extends OptionsApply {
326330
}
327331
}))(nameOption);
328332
}
329-
new AutomaticCommonsChunksPlugin({
330-
initialChunks: true,
331-
onlyNamed: !options.optimization.initialCommonsChunks,
332-
name: nameOption
333-
}).apply(compiler);
333+
new AutomaticCommonsChunksPlugin(Object.assign(
334+
{},
335+
options.optimization.initialCommonsChunks,
336+
{
337+
initialChunks: true,
338+
onlyNamed: !options.optimization.initialCommonsChunks,
339+
name: nameOption
340+
}
341+
)).apply(compiler);
334342
}
335343
if(options.optimization.noEmitOnErrors)
336344
new NoEmitOnErrorsPlugin().apply(compiler);

lib/optimize/AutomaticCommonsChunksPlugin.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ const sortByIdentifier = (a, b) => {
1212
return 0;
1313
};
1414

15-
const getMaxBlockRequests = chunk => {
16-
return chunk.mapBlocks(block => block.chunks.length).reduce(Math.max, 0);
15+
const getRequests = chunk => {
16+
return Math.max(
17+
chunk.mapBlocks(block => block.chunks.length).reduce(Math.max, 0),
18+
chunk.mapEntrypoints(ep => ep.chunks.length).reduce(Math.max, 0)
19+
);
1720
};
1821

1922
module.exports = class AutomaticCommonsChunksPlugin {
2023
constructor(options) {
2124
this.options = Object.assign({}, {
2225
initialChunks: false,
2326
minSize: 30000,
24-
maxRequests: 10,
27+
maxRequests: 4,
2528
onlyNamed: false,
2629
name: undefined
2730
}, options);
@@ -112,7 +115,7 @@ module.exports = class AutomaticCommonsChunksPlugin {
112115
// only use selected chunks
113116
if(!indexMap.get(chunk)) continue;
114117
// respect max requests when not a named chunk
115-
if(!chunkName && getMaxBlockRequests(chunk) >= this.options.maxRequests) continue;
118+
if(!chunkName && getRequests(chunk) >= this.options.maxRequests) continue;
116119
splitted = true;
117120
chunk.split(newChunk);
118121
for(const module of modules) {

schemas/WebpackOptions.json

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,11 +1325,51 @@
13251325
},
13261326
"asyncCommonsChunks": {
13271327
"description": "Automatically deduplicate modules in on-demand chunks by creating additional async-loaded shared chunks",
1328-
"type": "boolean"
1328+
"oneOf": [
1329+
{
1330+
"type": "boolean"
1331+
},
1332+
{
1333+
"type": "object",
1334+
"additionalProperties": false,
1335+
"properties": {
1336+
"minSize": {
1337+
"description": "Specify the minimum size of commons chunks at which they are created",
1338+
"type": "number",
1339+
"minimum": 0
1340+
},
1341+
"maxRequests": {
1342+
"description": "Specify the maximum number of requests that should occur at a split points (when on the limit bigger chunks are preferred)",
1343+
"type": "number",
1344+
"minimum": 0
1345+
}
1346+
}
1347+
}
1348+
]
13291349
},
13301350
"initialCommonsChunks": {
13311351
"description": "Automatically deduplicate modules in initial chunks by creating additional initial-loaded shared chunks (These chunks need to be added to the HTML)",
1332-
"type": "boolean"
1352+
"oneOf": [
1353+
{
1354+
"type": "boolean"
1355+
},
1356+
{
1357+
"type": "object",
1358+
"additionalProperties": false,
1359+
"properties": {
1360+
"minSize": {
1361+
"description": "Specify the minimum size of commons chunks at which they are created (when on the limit bigger chunks are preferred)",
1362+
"type": "number",
1363+
"minimum": 0
1364+
},
1365+
"maxRequests": {
1366+
"description": "Specify the maximum number of requests that should occur at startup",
1367+
"type": "number",
1368+
"minimum": 0
1369+
}
1370+
}
1371+
}
1372+
]
13331373
},
13341374
"initialVendorsChunk": {
13351375
"description": "Automatically extract vendor modules (inside of node_modules) into separate chunk assuming they change less often ",

test/statsCases/async-commons-chunk-auto/expected.txt

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,49 @@ Entrypoint main = main.js
22
Entrypoint a = libs.js vendors.js a.js
33
Entrypoint b = libs.js vendors.js b.js
44
Entrypoint c = libs.js vendors.js c.js
5-
chunk {0} 0.chunk.js 182 bytes {4} [rendered]
5+
chunk {0} 0.chunk.js 40 bytes {7} [rendered] commons chunk
6+
[0] ./d.js 20 bytes {0} {8} {9} {10} [built]
7+
[1] ./node_modules/x.js 20 bytes {0} {11} [built]
8+
chunk {1} 1.chunk.js 20 bytes {0+2+3} {7} {8+11+12} [rendered] commons chunk
9+
[2] ./f.js 20 bytes {1} {9} {10} [built]
10+
chunk {2} 2.chunk.js 20 bytes {7} [rendered] commons chunk
11+
[3] ./node_modules/y.js 20 bytes {2} {12} [built]
12+
chunk {3} 3.chunk.js 122 bytes {7} [rendered]
613
> [8] ./index.js 1:0-13
7-
[0] ./d.js 20 bytes {0} {1} {2} {5} {6} {7} [built]
8-
[1] ./node_modules/x.js 20 bytes {0} {1} {2} {8} [built]
9-
[3] ./node_modules/y.js 20 bytes {0} {1} {9} [built]
10-
[7] ./a.js + 1 modules 122 bytes {0} {5} [built]
14+
[7] ./a.js + 1 modules 122 bytes {3} {8} [built]
1115
| ./a.js 87 bytes [built]
1216
| ./e.js 20 bytes [built]
13-
chunk {1} 1.chunk.js 152 bytes {4} [rendered]
17+
chunk {4} 4.chunk.js 72 bytes {7} [rendered]
1418
> [8] ./index.js 2:0-13
15-
[0] ./d.js 20 bytes {0} {1} {2} {5} {6} {7} [built]
16-
[1] ./node_modules/x.js 20 bytes {0} {1} {2} {8} [built]
17-
[2] ./f.js 20 bytes {1} {2} {3} {6} {7} [built]
18-
[3] ./node_modules/y.js 20 bytes {0} {1} {9} [built]
19-
[5] ./b.js 72 bytes {1} {6} [built]
20-
chunk {2} 2.chunk.js 152 bytes {4} [rendered]
19+
[5] ./b.js 72 bytes {4} {9} [built]
20+
chunk {5} 5.chunk.js 92 bytes {7} [rendered]
2121
> [8] ./index.js 3:0-13
22-
[0] ./d.js 20 bytes {0} {1} {2} {5} {6} {7} [built]
23-
[1] ./node_modules/x.js 20 bytes {0} {1} {2} {8} [built]
24-
[2] ./f.js 20 bytes {1} {2} {3} {6} {7} [built]
25-
[4] ./node_modules/z.js 20 bytes {2} {9} [built]
26-
[6] ./c.js 72 bytes {2} {7} [built]
27-
chunk {3} 3.chunk.js 54 bytes {0} {5+8+9} [rendered]
22+
[4] ./node_modules/z.js 20 bytes {5} {12} [built]
23+
[6] ./c.js 72 bytes {5} {10} [built]
24+
chunk {6} 6.chunk.js 34 bytes {0+2+3} {8+11+12} [rendered]
2825
> [] 6:0-13
29-
[2] ./f.js 20 bytes {1} {2} {3} {6} {7} [built]
30-
[9] ./g.js 34 bytes {3} [built]
31-
chunk {4} main.js (main) 45 bytes [entry] [rendered]
26+
[9] ./g.js 34 bytes {6} [built]
27+
chunk {7} main.js (main) 45 bytes [entry] [rendered]
3228
> main [8] ./index.js
33-
[8] ./index.js 45 bytes {4} [built]
34-
chunk {5} a.js (a) 142 bytes [entry] [rendered]
29+
[8] ./index.js 45 bytes {7} [built]
30+
chunk {8} a.js (a) 142 bytes [entry] [rendered]
3531
> a []
36-
[0] ./d.js 20 bytes {0} {1} {2} {5} {6} {7} [built]
37-
[7] ./a.js + 1 modules 122 bytes {0} {5} [built]
32+
[0] ./d.js 20 bytes {0} {8} {9} {10} [built]
33+
[7] ./a.js + 1 modules 122 bytes {3} {8} [built]
3834
| ./a.js 87 bytes [built]
3935
| ./e.js 20 bytes [built]
40-
chunk {6} b.js (b) 112 bytes [entry] [rendered]
36+
chunk {9} b.js (b) 112 bytes [entry] [rendered]
4137
> b [5] ./b.js
42-
[0] ./d.js 20 bytes {0} {1} {2} {5} {6} {7} [built]
43-
[2] ./f.js 20 bytes {1} {2} {3} {6} {7} [built]
44-
[5] ./b.js 72 bytes {1} {6} [built]
45-
chunk {7} c.js (c) 112 bytes [entry] [rendered]
38+
[0] ./d.js 20 bytes {0} {8} {9} {10} [built]
39+
[2] ./f.js 20 bytes {1} {9} {10} [built]
40+
[5] ./b.js 72 bytes {4} {9} [built]
41+
chunk {10} c.js (c) 112 bytes [entry] [rendered]
4642
> c [6] ./c.js
47-
[0] ./d.js 20 bytes {0} {1} {2} {5} {6} {7} [built]
48-
[2] ./f.js 20 bytes {1} {2} {3} {6} {7} [built]
49-
[6] ./c.js 72 bytes {2} {7} [built]
50-
chunk {8} libs.js (libs) 20 bytes [initial] [rendered] vendors chunk
51-
[1] ./node_modules/x.js 20 bytes {0} {1} {2} {8} [built]
52-
chunk {9} vendors.js (vendors) 40 bytes [initial] [rendered] vendors chunk
53-
[3] ./node_modules/y.js 20 bytes {0} {1} {9} [built]
54-
[4] ./node_modules/z.js 20 bytes {2} {9} [built]
43+
[0] ./d.js 20 bytes {0} {8} {9} {10} [built]
44+
[2] ./f.js 20 bytes {1} {9} {10} [built]
45+
[6] ./c.js 72 bytes {5} {10} [built]
46+
chunk {11} libs.js (libs) 20 bytes [initial] [rendered] vendors chunk
47+
[1] ./node_modules/x.js 20 bytes {0} {11} [built]
48+
chunk {12} vendors.js (vendors) 40 bytes [initial] [rendered] vendors chunk
49+
[3] ./node_modules/y.js 20 bytes {2} {12} [built]
50+
[4] ./node_modules/z.js 20 bytes {5} {12} [built]

test/statsCases/async-commons-chunk-auto/webpack.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ module.exports = {
1111
chunkFilename: "[name].chunk.js"
1212
},
1313
optimization: {
14+
asyncCommonsChunks: {
15+
minSize: 1 // enforce all
16+
},
1417
initialVendorsChunk: {
1518
"libs": /[\\/](xyz|x)/,
1619
vendors: path.resolve(__dirname, "node_modules")

0 commit comments

Comments
 (0)