Skip to content

Commit d688f4d

Browse files
authored
feat: split chunks overall filename (#9243)
1 parent 38fbd20 commit d688f4d

File tree

16 files changed

+143
-57
lines changed

16 files changed

+143
-57
lines changed

crates/node_binding/binding.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,6 +2213,7 @@ export interface RawSplitChunkSizes {
22132213
export interface RawSplitChunksOptions {
22142214
fallbackCacheGroup?: RawFallbackCacheGroupOptions
22152215
name?: string | false | Function
2216+
filename?: JsFilename
22162217
cacheGroups?: Array<RawCacheGroupOptions>
22172218
/** What kind of chunks should be selected. */
22182219
chunks?: RegExp | 'async' | 'initial' | 'all' | Function

crates/rspack_binding_values/src/raw_options/raw_split_chunks/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct RawSplitChunksOptions {
3333
#[napi(ts_type = "string | false | Function")]
3434
#[debug(skip)]
3535
pub name: Option<RawChunkOptionName>,
36+
pub filename: Option<JsFilename>,
3637
pub cache_groups: Option<Vec<RawCacheGroupOptions>>,
3738
/// What kind of chunks should be selected.
3839
#[napi(ts_type = "RegExp | 'async' | 'initial' | 'all' | Function")]
@@ -107,6 +108,8 @@ impl From<RawSplitChunksOptions> for rspack_plugin_split_chunks::PluginOptions {
107108

108109
let mut cache_groups = vec![];
109110

111+
let overall_filename = raw_opts.filename.map(Filename::from);
112+
110113
let overall_chunk_filter = raw_opts.chunks.map(create_chunks_filter);
111114

112115
let overall_min_chunks = raw_opts.min_chunks.unwrap_or(1);
@@ -214,7 +217,10 @@ impl From<RawSplitChunksOptions> for rspack_plugin_split_chunks::PluginOptions {
214217
automatic_name_delimiter: v
215218
.automatic_name_delimiter
216219
.unwrap_or(overall_automatic_name_delimiter.clone()),
217-
filename: v.filename.map(Filename::from),
220+
filename: v
221+
.filename
222+
.map(Filename::from)
223+
.or_else(|| overall_filename.clone()),
218224
reuse_existing_chunk: v.reuse_existing_chunk.unwrap_or(false),
219225
max_async_requests: v.max_async_requests.unwrap_or(f64::INFINITY),
220226
max_initial_requests: v.max_initial_requests.unwrap_or(f64::INFINITY),
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import "./shared1";
2+
import "./common1";
3+
4+
it("should be able to load the split chunk on demand (shared)", () => {
5+
return import(/* webpackChunkName: "theName" */ "./shared2");
6+
});
7+
8+
it("should be able to load the split chunk on demand (common)", () => {
9+
return Promise.all([
10+
import(/* webpackChunkName: "otherName1" */ "./common2"),
11+
import(/* webpackChunkName: "otherName2" */ "./common3")
12+
]);
13+
});
14+
15+
it("should have files", async () => {
16+
const fs = require("fs");
17+
const path = require("path");
18+
const files = await fs.promises.readdir(__dirname);
19+
expect(files).toContain("shared-shared-shared1_js.js");
20+
expect(files).toContain("shared-shared-shared2_js.js");
21+
expect(files).toContain("splitted-chunks");
22+
const innerFiles = await fs.promises.readdir(path.resolve(__dirname, "splitted-chunks"));
23+
expect(innerFiles).toContain("common-common1_js.js");
24+
expect(innerFiles).toContain("common-common2_js.js");
25+
expect(innerFiles).toContain("common-common3_js.js");
26+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "./shared1";
2+
import "./shared2";
3+
import "./common1";
4+
import "./common2";
5+
import "./common3";

packages/rspack-test-tools/tests/configCases/split-chunks/custom-filename-overall/common1.js

Whitespace-only changes.

packages/rspack-test-tools/tests/configCases/split-chunks/custom-filename-overall/common2.js

Whitespace-only changes.

packages/rspack-test-tools/tests/configCases/split-chunks/custom-filename-overall/common3.js

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/** @type {import("@rspack/core").Configuration} */
2+
module.exports = {
3+
mode: "development",
4+
entry: {
5+
a: "./a",
6+
b: "./b"
7+
},
8+
output: {
9+
filename: "[name].js",
10+
libraryTarget: "commonjs2"
11+
},
12+
optimization: {
13+
chunkIds: "named",
14+
splitChunks: {
15+
filename: "splitted-chunks/[name].js",
16+
cacheGroups: {
17+
shared: {
18+
chunks: "all",
19+
test: /shared/,
20+
filename: "shared-[name].js",
21+
enforce: true
22+
},
23+
common: {
24+
chunks: "all",
25+
test: /common/,
26+
enforce: true
27+
}
28+
}
29+
}
30+
}
31+
};

packages/rspack-test-tools/tests/configCases/split-chunks/custom-filename-overall/shared1.js

Whitespace-only changes.

packages/rspack-test-tools/tests/configCases/split-chunks/custom-filename-overall/shared2.js

Whitespace-only changes.

0 commit comments

Comments
 (0)