Skip to content

Commit 3f8a94f

Browse files
committed
Extract normalizeChunksFilter helper
1 parent 42b2b70 commit 3f8a94f

3 files changed

Lines changed: 27 additions & 18 deletions

File tree

lib/optimize/SplitChunksPlugin.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ module.exports = class SplitChunksPlugin {
8585

8686
static normalizeOptions(options = {}) {
8787
return {
88-
chunks: options.chunks || "all",
88+
chunksFilter: SplitChunksPlugin.normalizeChunksFilter(
89+
options.chunks || "all"
90+
),
8991
minSize: options.minSize || 0,
9092
minChunks: options.minChunks || 1,
9193
maxAsyncRequests: options.maxAsyncRequests || 1,
@@ -135,6 +137,19 @@ module.exports = class SplitChunksPlugin {
135137
if (typeof name === "function") return name;
136138
}
137139

140+
static normalizeChunksFilter(chunks) {
141+
if (chunks === "initial") {
142+
return chunk => chunk.canBeInitial();
143+
}
144+
if (chunks === "async") {
145+
return chunk => !chunk.canBeInitial();
146+
}
147+
if (chunks === "all") {
148+
return () => true;
149+
}
150+
if (typeof chunks === "function") return chunks;
151+
}
152+
138153
static normalizeCacheGroups({ cacheGroups, automaticNameDelimiter }) {
139154
if (typeof cacheGroups === "function") {
140155
return cacheGroups;
@@ -174,7 +189,9 @@ module.exports = class SplitChunksPlugin {
174189
name: option.name,
175190
automaticNameDelimiter
176191
}),
177-
chunks: option.chunks,
192+
chunksFilter: SplitChunksPlugin.normalizeChunksFilter(
193+
option.chunks
194+
),
178195
enforce: option.enforce,
179196
minSize: option.minSize,
180197
minChunks: option.minChunks,
@@ -264,7 +281,8 @@ module.exports = class SplitChunksPlugin {
264281
const cacheGroup = {
265282
key: cacheGroupSource.key,
266283
priority: cacheGroupSource.priority || 0,
267-
chunks: cacheGroupSource.chunks || this.options.chunks,
284+
chunksFilter:
285+
cacheGroupSource.chunksFilter || this.options.chunksFilter,
268286
minSize:
269287
cacheGroupSource.minSize !== undefined
270288
? cacheGroupSource.minSize
@@ -304,18 +322,9 @@ module.exports = class SplitChunksPlugin {
304322
// Break if minimum number of chunks is not reached
305323
if (chunkIndices.length < cacheGroup.minChunks) continue;
306324
// Select chunks by configuration
307-
const selectedChunks =
308-
typeof cacheGroup.chunks === "function"
309-
? cacheGroup.chunks(Array.from(chunkCombination))
310-
: cacheGroup.chunks === "initial"
311-
? Array.from(chunkCombination).filter(chunk =>
312-
chunk.canBeInitial()
313-
)
314-
: cacheGroup.chunks === "async"
315-
? Array.from(chunkCombination).filter(
316-
chunk => !chunk.canBeInitial()
317-
)
318-
: Array.from(chunkCombination);
325+
const selectedChunks = Array.from(chunkCombination).filter(
326+
cacheGroup.chunksFilter
327+
);
319328
// Break if minimum number of chunks is not reached
320329
if (selectedChunks.length < cacheGroup.minChunks) continue;
321330
// Determine name for split chunk

test/statsCases/split-chunks/expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ Child name-too-long:
258258
[0] ./d.js 20 bytes {1} {10} {11} {12} [built]
259259
[1] ./f.js 20 bytes {2} {11} {12} [built]
260260
[5] ./c.js 72 bytes {7} {12} [built]
261-
Child chunks-selector:
261+
Child custom-chunks-filter:
262262
Entrypoint main = default/main.js
263263
Entrypoint a = default/a.js
264264
Entrypoint b = default/vendors~async-a~async-b~async-c~b~c.js default/vendors~async-a~async-b~b.js default/b.js

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ module.exports = [
101101
},
102102

103103
{
104-
name: "chunks-selector",
104+
name: "custom-chunks-filter",
105105
mode: "production",
106106
entry: {
107107
main: "./",
@@ -115,7 +115,7 @@ module.exports = [
115115
optimization: {
116116
splitChunks: {
117117
minSize: 0,
118-
chunks: chunks => chunks.filter(chunk => chunk.name !== "a")
118+
chunks: chunk => chunk.name !== "a"
119119
}
120120
},
121121
stats

0 commit comments

Comments
 (0)