Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support passing RegExp to splitChunks.chunks #17332

Merged
merged 8 commits into from Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

I'm confused about the error messages of CI. I did create a test here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR, I will finish it

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