Skip to content

Commit f804ff0

Browse files
committed
Support selector function as optimization.splitChunks.chunks option
1 parent 4428efe commit f804ff0

File tree

4 files changed

+111
-13
lines changed

4 files changed

+111
-13
lines changed

lib/optimize/SplitChunksPlugin.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,17 @@ module.exports = class SplitChunksPlugin {
305305
if (chunkIndices.length < cacheGroup.minChunks) continue;
306306
// Select chunks by configuration
307307
const selectedChunks =
308-
cacheGroup.chunks === "initial"
309-
? Array.from(chunkCombination).filter(chunk =>
310-
chunk.canBeInitial()
311-
)
312-
: cacheGroup.chunks === "async"
313-
? Array.from(chunkCombination).filter(
314-
chunk => !chunk.canBeInitial()
308+
typeof cacheGroup.chunks === "function"
309+
? cacheGroup.chunks(Array.from(chunkCombination))
310+
: cacheGroup.chunks === "initial"
311+
? Array.from(chunkCombination).filter(chunk =>
312+
chunk.canBeInitial()
315313
)
316-
: Array.from(chunkCombination);
314+
: cacheGroup.chunks === "async"
315+
? Array.from(chunkCombination).filter(
316+
chunk => !chunk.canBeInitial()
317+
)
318+
: Array.from(chunkCombination);
317319
// Break if minimum number of chunks is not reached
318320
if (selectedChunks.length < cacheGroup.minChunks) continue;
319321
// Determine name for split chunk

schemas/WebpackOptions.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,10 +1337,17 @@
13371337
"properties": {
13381338
"chunks": {
13391339
"description": "Select chunks for determining shared modules (defaults to \"async\", \"initial\" and \"all\" requires adding these chunks to the HTML)",
1340-
"enum": [
1341-
"initial",
1342-
"async",
1343-
"all"
1340+
"oneOf": [
1341+
{
1342+
"enum": [
1343+
"initial",
1344+
"async",
1345+
"all"
1346+
]
1347+
},
1348+
{
1349+
"instanceof": "Function"
1350+
}
13441351
]
13451352
},
13461353
"minSize": {

test/statsCases/split-chunks/expected.txt

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,72 @@ Child name-too-long:
257257
> ./c cccccccccccccccccccccccccccccc
258258
[0] ./d.js 20 bytes {1} {10} {11} {12} [built]
259259
[1] ./f.js 20 bytes {2} {11} {12} [built]
260-
[5] ./c.js 72 bytes {7} {12} [built]
260+
[5] ./c.js 72 bytes {7} {12} [built]
261+
Child default:
262+
Entrypoint main = default/main.js
263+
Entrypoint a = default/a.js
264+
Entrypoint b = default/vendors~async-a~async-b~async-c~b~c.js default/vendors~async-a~async-b~b.js default/b.js
265+
Entrypoint c = default/vendors~async-a~async-b~async-c~b~c.js default/vendors~async-c~c.js default/c.js
266+
chunk {0} default/vendors~async-a~async-b~async-c~b~c.js (vendors~async-a~async-b~async-c~b~c) 20 bytes <{9}> ={1}= ={11}= ={12}= ={2}= ={3}= ={4}= ={6}= ={7}= ={8}= >{2}< >{5}< [initial] [rendered] split chunk (cache group: vendors) (name: vendors~async-a~async-b~async-c~b~c)
267+
> ./b b
268+
> ./c c
269+
> ./a [8] ./index.js 1:0-47
270+
> ./b [8] ./index.js 2:0-47
271+
> ./c [8] ./index.js 3:0-47
272+
[2] ./node_modules/x.js 20 bytes {0} {10} [built]
273+
chunk {1} default/async-a~async-b~async-c~b~c.js (async-a~async-b~async-c~b~c) 20 bytes <{9}> ={0}= ={2}= ={3}= ={4}= ={6}= ={7}= ={8}= >{2}< >{5}< [rendered] split chunk (cache group: default) (name: async-a~async-b~async-c~b~c)
274+
> ./a [8] ./index.js 1:0-47
275+
> ./b [8] ./index.js 2:0-47
276+
> ./c [8] ./index.js 3:0-47
277+
[0] ./d.js 20 bytes {1} {10} {11} {12} [built]
278+
chunk {2} default/async-b~async-c~async-g~b~c.js (async-b~async-c~async-g~b~c) 20 bytes <{0}> <{1}> <{10}> <{3}> <{6}> <{9}> ={0}= ={1}= ={3}= ={4}= ={5}= ={7}= ={8}= [rendered] split chunk (cache group: default) (name: async-b~async-c~async-g~b~c)
279+
> ./g [] 6:0-47
280+
> ./g [] 6:0-47
281+
> ./b [8] ./index.js 2:0-47
282+
> ./c [8] ./index.js 3:0-47
283+
[1] ./f.js 20 bytes {2} {11} {12} [built]
284+
chunk {3} default/vendors~async-a~async-b~b.js (vendors~async-a~async-b~b) 20 bytes <{9}> ={0}= ={1}= ={11}= ={2}= ={6}= ={7}= >{2}< >{5}< [initial] [rendered] split chunk (cache group: vendors) (name: vendors~async-a~async-b~b)
285+
> ./b b
286+
> ./a [8] ./index.js 1:0-47
287+
> ./b [8] ./index.js 2:0-47
288+
[3] ./node_modules/y.js 20 bytes {3} {10} [built]
289+
chunk {4} default/vendors~async-c~c.js (vendors~async-c~c) 20 bytes <{9}> ={0}= ={1}= ={12}= ={2}= ={8}= [initial] [rendered] split chunk (cache group: vendors) (name: vendors~async-c~c)
290+
> ./c c
291+
> ./c [8] ./index.js 3:0-47
292+
[7] ./node_modules/z.js 20 bytes {4} [built]
293+
chunk {5} default/async-g.js (async-g) 34 bytes <{0}> <{1}> <{10}> <{3}> <{6}> ={2}= [rendered]
294+
> ./g [] 6:0-47
295+
> ./g [] 6:0-47
296+
[9] ./g.js 34 bytes {5} [built]
297+
chunk {6} default/async-a.js (async-a) 156 bytes <{9}> ={0}= ={1}= ={3}= >{2}< >{5}< [rendered]
298+
> ./a [8] ./index.js 1:0-47
299+
[6] ./a.js + 1 modules 156 bytes {6} {10} [built]
300+
| ./a.js 121 bytes [built]
301+
| ./e.js 20 bytes [built]
302+
chunk {7} default/async-b.js (async-b) 72 bytes <{9}> ={0}= ={1}= ={2}= ={3}= [rendered]
303+
> ./b [8] ./index.js 2:0-47
304+
[4] ./b.js 72 bytes {7} {11} [built]
305+
chunk {8} default/async-c.js (async-c) 72 bytes <{9}> ={0}= ={1}= ={2}= ={4}= [rendered]
306+
> ./c [8] ./index.js 3:0-47
307+
[5] ./c.js 72 bytes {8} {12} [built]
308+
chunk {9} default/main.js (main) 147 bytes >{0}< >{1}< >{2}< >{3}< >{4}< >{6}< >{7}< >{8}< [entry] [rendered]
309+
> ./ main
310+
[8] ./index.js 147 bytes {9} [built]
311+
chunk {10} default/a.js (a) 216 bytes >{2}< >{5}< [entry] [rendered]
312+
> ./a a
313+
[0] ./d.js 20 bytes {1} {10} {11} {12} [built]
314+
[2] ./node_modules/x.js 20 bytes {0} {10} [built]
315+
[3] ./node_modules/y.js 20 bytes {3} {10} [built]
316+
[6] ./a.js + 1 modules 156 bytes {6} {10} [built]
317+
| ./a.js 121 bytes [built]
318+
| ./e.js 20 bytes [built]
319+
chunk {11} default/b.js (b) 112 bytes ={0}= ={3}= [entry] [rendered]
320+
> ./b b
321+
[0] ./d.js 20 bytes {1} {10} {11} {12} [built]
322+
[1] ./f.js 20 bytes {2} {11} {12} [built]
323+
[4] ./b.js 72 bytes {7} {11} [built]
324+
chunk {12} default/c.js (c) 112 bytes ={0}= ={4}= [entry] [rendered]
325+
> ./c c
326+
[0] ./d.js 20 bytes {1} {10} {11} {12} [built]
327+
[1] ./f.js 20 bytes {2} {11} {12} [built]
328+
[5] ./c.js 72 bytes {8} {12} [built]

test/statsCases/split-chunks/webpack.config.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,26 @@ module.exports = [
9898
}
9999
},
100100
stats
101+
},
102+
103+
{
104+
name: "default",
105+
mode: "production",
106+
entry: {
107+
main: "./",
108+
a: "./a",
109+
b: "./b",
110+
c: "./c"
111+
},
112+
output: {
113+
filename: "default/[name].js"
114+
},
115+
optimization: {
116+
splitChunks: {
117+
minSize: 0,
118+
chunks: chunks => chunks.filter(chunk => chunk.name !== "a")
119+
}
120+
},
121+
stats
101122
}
102123
];

0 commit comments

Comments
 (0)