Skip to content

Commit

Permalink
Merge pull request #17332 from hyf0/hyf_2348902389048230
Browse files Browse the repository at this point in the history
feat: support passing `RegExp` to `splitChunks.chunks`
  • Loading branch information
TheLarkInn committed Jun 7, 2023
2 parents 5636399 + 851b6fa commit 7a2d3d8
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 4 deletions.
3 changes: 3 additions & 0 deletions declarations/WebpackOptions.d.ts
Expand Up @@ -1782,6 +1782,7 @@ export interface OptimizationSplitChunksOptions {
*/
chunks?:
| ("initial" | "async" | "all")
| RegExp
| ((chunk: import("../lib/Chunk")) => boolean);
/**
* Sets the size types which are used when a number is used for sizes.
Expand All @@ -1804,6 +1805,7 @@ export interface OptimizationSplitChunksOptions {
*/
chunks?:
| ("initial" | "async" | "all")
| RegExp
| ((chunk: import("../lib/Chunk")) => boolean);
/**
* Maximal size hint for the on-demand chunks.
Expand Down Expand Up @@ -1897,6 +1899,7 @@ export interface OptimizationSplitChunksCacheGroup {
*/
chunks?:
| ("initial" | "async" | "all")
| RegExp
| ((chunk: import("../lib/Chunk")) => boolean);
/**
* Ignore minimum size, minimum chunks and maximum requests and always create chunks for this cache group.
Expand Down
5 changes: 5 additions & 0 deletions lib/optimize/SplitChunksPlugin.js
Expand Up @@ -424,6 +424,11 @@ const normalizeChunksFilter = chunks => {
if (chunks === "all") {
return ALL_CHUNK_FILTER;
}
if (chunks instanceof RegExp) {
return chunk => {
return chunk.name ? chunks.test(chunk.name) : false;
};
}
if (typeof chunks === "function") {
return chunks;
}
Expand Down
2 changes: 1 addition & 1 deletion schemas/WebpackOptions.check.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions schemas/WebpackOptions.json
Expand Up @@ -2625,6 +2625,10 @@
{
"enum": ["initial", "async", "all"]
},
{
"instanceof": "RegExp",
"tsType": "RegExp"
},
{
"instanceof": "Function",
"tsType": "((chunk: import('../lib/Chunk')) => boolean)"
Expand Down Expand Up @@ -2872,6 +2876,10 @@
{
"enum": ["initial", "async", "all"]
},
{
"instanceof": "RegExp",
"tsType": "RegExp"
},
{
"instanceof": "Function",
"tsType": "((chunk: import('../lib/Chunk')) => boolean)"
Expand Down Expand Up @@ -2911,6 +2919,10 @@
{
"enum": ["initial", "async", "all"]
},
{
"instanceof": "RegExp",
"tsType": "RegExp"
},
{
"instanceof": "Function",
"tsType": "((chunk: import('../lib/Chunk')) => boolean)"
Expand Down
12 changes: 12 additions & 0 deletions test/__snapshots__/Cli.basictest.js.snap
Expand Up @@ -5134,6 +5134,12 @@ Object {
"all",
],
},
Object {
"description": "Select chunks for determining shared modules (defaults to \\"async\\", \\"initial\\" and \\"all\\" requires adding these chunks to the HTML).",
"multiple": false,
"path": "optimization.splitChunks.chunks",
"type": "RegExp",
},
],
"description": "Select chunks for determining shared modules (defaults to \\"async\\", \\"initial\\" and \\"all\\" requires adding these chunks to the HTML).",
"multiple": false,
Expand Down Expand Up @@ -5204,6 +5210,12 @@ Object {
"all",
],
},
Object {
"description": "Select chunks for determining shared modules (defaults to \\"async\\", \\"initial\\" and \\"all\\" requires adding these chunks to the HTML).",
"multiple": false,
"path": "optimization.splitChunks.fallbackCacheGroup.chunks",
"type": "RegExp",
},
],
"description": "Select chunks for determining shared modules (defaults to \\"async\\", \\"initial\\" and \\"all\\" requires adding these chunks to the HTML).",
"multiple": false,
Expand Down
1 change: 1 addition & 0 deletions test/configCases/split-chunks/issue-17332/bar.js
@@ -0,0 +1 @@
export default 'bar.js'
3 changes: 3 additions & 0 deletions test/configCases/split-chunks/issue-17332/foo.js
@@ -0,0 +1,3 @@
import './bar'

export default 'foo.js'
7 changes: 7 additions & 0 deletions test/configCases/split-chunks/issue-17332/index.js
@@ -0,0 +1,7 @@
it('should run', async () => {
await import(/* webpackChunkName: "foo" */ "./foo");

const bar = __STATS__.modules.find(m => m.name.includes("bar.js"));

expect(bar.chunks).toEqual(["split-foo"]);
})
5 changes: 5 additions & 0 deletions test/configCases/split-chunks/issue-17332/test.config.js
@@ -0,0 +1,5 @@
module.exports = {
findBundle: function (i, options) {
return ["split-foo.js", "foo.js", "main.js"];
}
};
24 changes: 24 additions & 0 deletions test/configCases/split-chunks/issue-17332/webpack.config.js
@@ -0,0 +1,24 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
mode: "development",
entry: {
main: "./index"
},
output: {
filename: "[name].js"
},
optimization: {
splitChunks: {
cacheGroups: {
default: false,
defaultVendors: false,
bar: {
chunks: /foo/,
test: /bar\.js/,
name: "split-foo",
minSize: 1
}
}
}
}
};
6 changes: 3 additions & 3 deletions types.d.ts
Expand Up @@ -8691,7 +8691,7 @@ declare interface OptimizationSplitChunksCacheGroup {
/**
* Select chunks for determining cache group content (defaults to "initial", "initial" and "all" requires adding these chunks to the HTML).
*/
chunks?: "all" | "initial" | "async" | ((chunk: Chunk) => boolean);
chunks?: RegExp | "all" | "initial" | "async" | ((chunk: Chunk) => boolean);

/**
* Ignore minimum size, minimum chunks and maximum requests and always create chunks for this cache group.
Expand Down Expand Up @@ -8818,7 +8818,7 @@ declare interface OptimizationSplitChunksOptions {
/**
* Select chunks for determining shared modules (defaults to "async", "initial" and "all" requires adding these chunks to the HTML).
*/
chunks?: "all" | "initial" | "async" | ((chunk: Chunk) => boolean);
chunks?: RegExp | "all" | "initial" | "async" | ((chunk: Chunk) => boolean);

/**
* Sets the size types which are used when a number is used for sizes.
Expand All @@ -8841,7 +8841,7 @@ declare interface OptimizationSplitChunksOptions {
/**
* Select chunks for determining shared modules (defaults to "async", "initial" and "all" requires adding these chunks to the HTML).
*/
chunks?: "all" | "initial" | "async" | ((chunk: Chunk) => boolean);
chunks?: RegExp | "all" | "initial" | "async" | ((chunk: Chunk) => boolean);
/**
* Maximal size hint for the on-demand chunks.
*/
Expand Down

0 comments on commit 7a2d3d8

Please sign in to comment.