Skip to content

Commit 7a2d3d8

Browse files
authored
Merge pull request #17332 from hyf0/hyf_2348902389048230
feat: support passing `RegExp` to `splitChunks.chunks`
2 parents 5636399 + 851b6fa commit 7a2d3d8

11 files changed

Lines changed: 76 additions & 4 deletions

File tree

declarations/WebpackOptions.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,7 @@ export interface OptimizationSplitChunksOptions {
17821782
*/
17831783
chunks?:
17841784
| ("initial" | "async" | "all")
1785+
| RegExp
17851786
| ((chunk: import("../lib/Chunk")) => boolean);
17861787
/**
17871788
* Sets the size types which are used when a number is used for sizes.
@@ -1804,6 +1805,7 @@ export interface OptimizationSplitChunksOptions {
18041805
*/
18051806
chunks?:
18061807
| ("initial" | "async" | "all")
1808+
| RegExp
18071809
| ((chunk: import("../lib/Chunk")) => boolean);
18081810
/**
18091811
* Maximal size hint for the on-demand chunks.
@@ -1897,6 +1899,7 @@ export interface OptimizationSplitChunksCacheGroup {
18971899
*/
18981900
chunks?:
18991901
| ("initial" | "async" | "all")
1902+
| RegExp
19001903
| ((chunk: import("../lib/Chunk")) => boolean);
19011904
/**
19021905
* Ignore minimum size, minimum chunks and maximum requests and always create chunks for this cache group.

lib/optimize/SplitChunksPlugin.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,11 @@ const normalizeChunksFilter = chunks => {
424424
if (chunks === "all") {
425425
return ALL_CHUNK_FILTER;
426426
}
427+
if (chunks instanceof RegExp) {
428+
return chunk => {
429+
return chunk.name ? chunks.test(chunk.name) : false;
430+
};
431+
}
427432
if (typeof chunks === "function") {
428433
return chunks;
429434
}

schemas/WebpackOptions.check.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemas/WebpackOptions.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,6 +2625,10 @@
26252625
{
26262626
"enum": ["initial", "async", "all"]
26272627
},
2628+
{
2629+
"instanceof": "RegExp",
2630+
"tsType": "RegExp"
2631+
},
26282632
{
26292633
"instanceof": "Function",
26302634
"tsType": "((chunk: import('../lib/Chunk')) => boolean)"
@@ -2872,6 +2876,10 @@
28722876
{
28732877
"enum": ["initial", "async", "all"]
28742878
},
2879+
{
2880+
"instanceof": "RegExp",
2881+
"tsType": "RegExp"
2882+
},
28752883
{
28762884
"instanceof": "Function",
28772885
"tsType": "((chunk: import('../lib/Chunk')) => boolean)"
@@ -2911,6 +2919,10 @@
29112919
{
29122920
"enum": ["initial", "async", "all"]
29132921
},
2922+
{
2923+
"instanceof": "RegExp",
2924+
"tsType": "RegExp"
2925+
},
29142926
{
29152927
"instanceof": "Function",
29162928
"tsType": "((chunk: import('../lib/Chunk')) => boolean)"

test/__snapshots__/Cli.basictest.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5134,6 +5134,12 @@ Object {
51345134
"all",
51355135
],
51365136
},
5137+
Object {
5138+
"description": "Select chunks for determining shared modules (defaults to \\"async\\", \\"initial\\" and \\"all\\" requires adding these chunks to the HTML).",
5139+
"multiple": false,
5140+
"path": "optimization.splitChunks.chunks",
5141+
"type": "RegExp",
5142+
},
51375143
],
51385144
"description": "Select chunks for determining shared modules (defaults to \\"async\\", \\"initial\\" and \\"all\\" requires adding these chunks to the HTML).",
51395145
"multiple": false,
@@ -5204,6 +5210,12 @@ Object {
52045210
"all",
52055211
],
52065212
},
5213+
Object {
5214+
"description": "Select chunks for determining shared modules (defaults to \\"async\\", \\"initial\\" and \\"all\\" requires adding these chunks to the HTML).",
5215+
"multiple": false,
5216+
"path": "optimization.splitChunks.fallbackCacheGroup.chunks",
5217+
"type": "RegExp",
5218+
},
52075219
],
52085220
"description": "Select chunks for determining shared modules (defaults to \\"async\\", \\"initial\\" and \\"all\\" requires adding these chunks to the HTML).",
52095221
"multiple": false,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'bar.js'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './bar'
2+
3+
export default 'foo.js'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
it('should run', async () => {
2+
await import(/* webpackChunkName: "foo" */ "./foo");
3+
4+
const bar = __STATS__.modules.find(m => m.name.includes("bar.js"));
5+
6+
expect(bar.chunks).toEqual(["split-foo"]);
7+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
findBundle: function (i, options) {
3+
return ["split-foo.js", "foo.js", "main.js"];
4+
}
5+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/** @type {import("../../../../").Configuration} */
2+
module.exports = {
3+
mode: "development",
4+
entry: {
5+
main: "./index"
6+
},
7+
output: {
8+
filename: "[name].js"
9+
},
10+
optimization: {
11+
splitChunks: {
12+
cacheGroups: {
13+
default: false,
14+
defaultVendors: false,
15+
bar: {
16+
chunks: /foo/,
17+
test: /bar\.js/,
18+
name: "split-foo",
19+
minSize: 1
20+
}
21+
}
22+
}
23+
}
24+
};

0 commit comments

Comments
 (0)