Skip to content

Commit

Permalink
fix!: reuseExistingChunk should be false by default (#7489)
Browse files Browse the repository at this point in the history
* fix: reuseExistingChunk should be false by default

* docs: add docs for reuseExistingChunk

* Update website/docs/zh/plugins/webpack/split-chunks-plugin.mdx

Co-authored-by: neverland <chenjiahan.jait@bytedance.com>

* Update website/docs/zh/plugins/webpack/split-chunks-plugin.mdx

Co-authored-by: neverland <chenjiahan.jait@bytedance.com>

* docs: modify some statements

---------

Co-authored-by: neverland <chenjiahan.jait@bytedance.com>
  • Loading branch information
JSerFeng and chenjiahan authored Aug 8, 2024
1 parent 32c3d09 commit f2ec947
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl From<RawSplitChunksOptions> for rspack_plugin_split_chunks::PluginOptions {
.automatic_name_delimiter
.unwrap_or(overall_automatic_name_delimiter.clone()),
filename: v.filename.map(Filename::from),
reuse_existing_chunk: v.reuse_existing_chunk.unwrap_or(true),
reuse_existing_chunk: v.reuse_existing_chunk.unwrap_or(false),
// TODO(hyf0): the non-enforced default value should be 30
// I would set align default value with Webpack when the options is exposed to users
max_async_requests: u32::MAX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ it("should have files", () => {
expect(files).toContain("a.js");
expect(files).toContain("b.js");
expect(files).toContain("common-common1_js.js");
expect(files).toContain("otherName1.js");
expect(files).toContain("otherName2.js");
expect(files).toContain("common-common2_js.js");
expect(files).toContain("common-common3_js.js");
expect(files).toContain("shared-shared-shared1_js.js");
expect(files).toContain("shared-theName.js");
expect(files).toContain("shared-shared-shared2_js.js");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 42;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import fs from 'fs'
import path from 'path'

it('should have extra chunk', () => {
expect(fs.existsSync(path.resolve(__dirname, 'foo-index_js.js'))).toBe(true)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
target: "node",
output: {
filename: "[name].js"
},
optimization: {
chunkIds: 'named',
splitChunks: {
minSize: 1,
chunks: "all",
cacheGroups: {
foo: {
minSize: 0,
}
}
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import("../../../..").TConfigCaseConfig} */
module.exports = {
findBundle: function (i, options) {
return ["main.js"];
}
};
59 changes: 32 additions & 27 deletions tests/webpack-test/__snapshots__/StatsTestCases.basictest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,10 @@ multiple-vendors:
multiple-vendors (Rspack x.x.x) compiled successfully
all:
Entrypoint main 9.17 KiB = all/main.js
Entrypoint a 11.2 KiB = all/272.js 1.33 KiB all/a.js 9.91 KiB
Entrypoint main 10.7 KiB = all/605.js 821 bytes all/main.js 9.88 KiB
Entrypoint a 11.2 KiB = all/272.js 1.33 KiB all/a.js 9.88 KiB
Entrypoint b 5.91 KiB = all/726.js 1.18 KiB all/b.js 4.73 KiB
Entrypoint c 5.91 KiB = all/29.js 1.18 KiB all/c.js 4.73 KiB
chunk (runtime: main) all/async-c.js (async-c) (id hint: vendors) 20 bytes [rendered]
> ./c ./index.js 3:0-47
./node_modules/z.js 20 bytes [built] [code generated]
chunk (runtime: main) all/async-a.js (async-a) (id hint: vendors) 20 bytes [rendered]
> ./a ./index.js 1:0-47
./e.js 20 bytes [built] [code generated]
chunk (runtime: main) all/async-b.js (async-b) (id hint: vendors) 20 bytes [rendered]
> ./a ./index.js 1:0-47
> ./b ./index.js 2:0-47
./node_modules/y.js 20 bytes [built] [code generated]
chunk (runtime: a, main) all/272.js (id hint: vendors) 165 bytes [initial] [rendered] split chunk (cache group: vendors)
> ./a ./index.js 1:0-47
> ./a a
Expand All @@ -197,6 +187,13 @@ all:
> ./c ./index.js 3:0-47
> ./c c
./c.js 116 bytes [built] [code generated]
chunk (runtime: main) all/310.js (id hint: vendors) 20 bytes [rendered] split chunk (cache group: vendors)
> ./a ./index.js 1:0-47
> ./b ./index.js 2:0-47
./node_modules/y.js 20 bytes [built] [code generated]
chunk (runtime: main) all/416.js (id hint: vendors) 20 bytes [rendered] split chunk (cache group: vendors)
> ./c ./index.js 3:0-47
./node_modules/z.js 20 bytes [built] [code generated]
chunk (runtime: main) all/457.js (id hint: vendors) 40 bytes [rendered] split chunk (cache group: vendors)
> ./a ./index.js 1:0-47
> ./b ./index.js 2:0-47
Expand All @@ -208,22 +205,27 @@ all:
> ./c ./index.js 3:0-47
> ./g ./a.js 6:0-47
./f.js 20 bytes [built] [code generated]
chunk (runtime: main) all/605.js (id hint: vendors) 147 bytes [initial] [rendered] split chunk (cache group: vendors)
> ./ main
./index.js 147 bytes [built] [code generated]
chunk (runtime: main) all/697.js (id hint: vendors) 20 bytes [rendered] split chunk (cache group: vendors)
> ./a ./index.js 1:0-47
./e.js 20 bytes [built] [code generated]
chunk (runtime: b, main) all/726.js (id hint: vendors) 116 bytes [initial] [rendered] split chunk (cache group: vendors)
> ./b ./index.js 2:0-47
> ./b b
./b.js 116 bytes [built] [code generated]
chunk (runtime: a) all/a.js (a) 8.09 KiB [entry] [rendered]
chunk (runtime: a) all/a.js (a) 8.06 KiB [entry] [rendered]
> ./a a
chunk (runtime: b) all/b.js (b) 3.16 KiB [entry] [rendered]
> ./b b
chunk (runtime: c) all/c.js (c) 3.16 KiB [entry] [rendered]
> ./c c
chunk (runtime: main) all/main.js (main) (id hint: vendors) 147 bytes (javascript) 7.06 KiB (runtime) [entry] [rendered]
> ./ main
./index.js 147 bytes [built] [code generated]
chunk (runtime: a, main) all/async-g.js (async-g) (id hint: vendors) 45 bytes [rendered]
chunk (runtime: a, main) all/889.js (id hint: vendors) 45 bytes [rendered] split chunk (cache group: vendors)
> ./g ./a.js 6:0-47
./g.js 45 bytes [built] [code generated]
chunk (runtime: main) all/main.js (main) 8.06 KiB [entry] [rendered]
> ./ main
all (Rspack x.x.x) compiled successfully"
`;

Expand Down Expand Up @@ -536,29 +538,32 @@ Rspack x.x.x compiled with 2 warnings"
`;
exports[`StatsTestCases should print correct stats for issue-7577 1`] = `
"asset a-runtime~main-6f980b3fe4e873c8018a.js 3.69 KiB [emitted] [immutable] (name: runtime~main)
asset a-main-74eb98039f994af619b3.js 323 bytes [emitted] [immutable] (name: main) (id hint: all)
Entrypoint main 4 KiB = a-runtime~main-6f980b3fe4e873c8018a.js 3.69 KiB a-main-74eb98039f994af619b3.js 323 bytes
"asset a-runtime~main-c4a0be3dc26f064b4283.js 3.69 KiB [emitted] [immutable] (name: runtime~main)
asset a-main-b9f0cc0473486a71c3d1.js 387 bytes [emitted] [immutable] (name: main)
asset a-all-a_js-46301f7a16444535097a.js 128 bytes [emitted] [immutable] (id hint: all)
Entrypoint main 4.19 KiB = a-runtime~main-c4a0be3dc26f064b4283.js 3.69 KiB a-all-a_js-46301f7a16444535097a.js 128 bytes a-main-b9f0cc0473486a71c3d1.js 387 bytes
runtime modules 2.59 KiB 3 modules
./a.js 18 bytes [built] [code generated]
Rspack x.x.x compiled successfully in X.23
asset b-runtime~main-841c6f8661ffb8487676.js 4.39 KiB [emitted] [immutable] (name: runtime~main)
asset b-main-7a912c9e72d35b5ccb33.js 779 bytes [emitted] [immutable] (name: main) (id hint: all)
asset b-runtime~main-fe10e0b865c55df180b8.js 4.39 KiB [emitted] [immutable] (name: runtime~main)
asset b-all-b_js-242da217bb553f79303f.js 461 bytes [emitted] [immutable] (id hint: all)
asset b-main-236b715ba81f9430f0c7.js 420 bytes [emitted] [immutable] (name: main)
asset b-vendors-node_modules_vendor_js-97d43f84c65cc0e25938.js 173 bytes [emitted] [immutable] (id hint: vendors)
Entrypoint main 5.32 KiB = b-runtime~main-841c6f8661ffb8487676.js 4.39 KiB b-vendors-node_modules_vendor_js-97d43f84c65cc0e25938.js 173 bytes b-main-7a912c9e72d35b5ccb33.js 779 bytes
Entrypoint main 5.42 KiB = b-runtime~main-fe10e0b865c55df180b8.js 4.39 KiB b-vendors-node_modules_vendor_js-97d43f84c65cc0e25938.js 173 bytes b-all-b_js-242da217bb553f79303f.js 461 bytes b-main-236b715ba81f9430f0c7.js 420 bytes
runtime modules 3.17 KiB 5 modules
cacheable modules 40 bytes
./b.js 17 bytes [built] [code generated]
./node_modules/vendor.js 23 bytes [built] [code generated]
Rspack x.x.x compiled successfully in X.23
assets by chunk 1.29 KiB (id hint: all)
asset c-main-badd31d63df8ff8ef439.js 833 bytes [emitted] [immutable] (name: main) (id hint: all)
assets by chunk 870 bytes (id hint: all)
asset c-all-b_js-a364a38577953cc2ab7c.js 488 bytes [emitted] [immutable] (id hint: all)
asset c-runtime~main-758a5f5cc29c2a3b8870.js 10.8 KiB [emitted] [immutable] (name: runtime~main)
asset c-all-c_js-a54d408465645a2aea4f.js 382 bytes [emitted] [immutable] (id hint: all)
asset c-runtime~main-13146ab0d777c036e368.js 10.8 KiB [emitted] [immutable] (name: runtime~main)
asset c-main-4bdbd2cfa296de1ab23e.js 643 bytes [emitted] [immutable] (name: main)
asset c-vendors-node_modules_vendor_js-97d43f84c65cc0e25938.js 173 bytes [emitted] [immutable] (id hint: vendors)
Entrypoint main 11.6 KiB = c-runtime~main-758a5f5cc29c2a3b8870.js 10.8 KiB c-main-badd31d63df8ff8ef439.js 833 bytes
Entrypoint main 11.8 KiB = c-runtime~main-13146ab0d777c036e368.js 10.8 KiB c-all-c_js-a54d408465645a2aea4f.js 382 bytes c-main-4bdbd2cfa296de1ab23e.js 643 bytes
runtime modules 9.38 KiB 13 modules
cacheable modules 101 bytes
./c.js 61 bytes [built] [code generated]
Expand Down
21 changes: 21 additions & 0 deletions website/docs/en/plugins/webpack/split-chunks-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,24 @@ module.exports = {
},
};
```

#### splitChunks.cacheGroups.\{cacheGroup\}.reuseExistingChunk

- **Type:** `boolean`
- **Default** `false`

Whether to reuse existing chunks when possible. If so, after splitting, the newly created chunk contains modules that are exactly the same as those in the original chunk, the original chunk will be reused, and no new chunk will be generated, which may affect the final filename of the chunk. For example:

```
chunk Foo: [ module A, module B ]
chunk Bar: [ module B ]
cacheGroup: {
test: /B/,
chunks: 'all'
}
```

In chunks Foo and Bar, the module B, due to the configuration of cacheGroup, will be split into a new chunk that only contains module B. This new chunk is identical in terms of the modules it contains with chunk Bar, so chunk Bar can be directly reused.

If the setting of reuseExistingChunk is set to `false`, then the module B in chunks Bar and Foo will be moved to a new chunk, and chunk Bar, since it no longer contains any modules, will be deleted as an empty chunk.
21 changes: 21 additions & 0 deletions website/docs/zh/plugins/webpack/split-chunks-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,24 @@ module.exports = {
},
};
```

#### splitChunks.cacheGroups.\{cacheGroup\}.reuseExistingChunk

- **类型:** `boolean`
- **默认值:** `false`

是否重用现有的 chunk。如果经过拆分后新产生的 chunk 中包含的模块和原 chunk 中包含的模块完全一致,则原 chunk 会被复用,不会生成新的 chunk,这可能会影响 chunk 最终的文件名。举例:

```
chunk Foo: [ module A, module B ]
chunk Bar: [ module B ]
cacheGroup: {
test: /B/,
chunks: 'all'
}
```

由于 cacheGroup 的配置,在 chunk Foo 和 chunk Bar 中的 module B 会被拆分到一个新的 chunk 中,该 chunk 只包含 module B,这个新 chunk 和 chunk Bar 包含的 module 完全一样,因此可以直接复用 chunk Bar。

如果设置 reuseExistingChunk 为 `false`,那么 chunk Bar 和 chunk Foo 中的 module B 会被移到新 chunk 中,chunk Bar 由于不包含任何 module,会作为空 chunk 被删除。

2 comments on commit f2ec947

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rsbuild ❌ failure
examples ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Benchmark detail: Open

Name Base (2024-08-08 2bd905d) Current Change
10000_development-mode + exec 2.32 s ± 27 ms 2.33 s ± 35 ms +0.46 %
10000_development-mode_hmr + exec 701 ms ± 7.3 ms 713 ms ± 4.7 ms +1.73 %
10000_production-mode + exec 2.85 s ± 23 ms 2.88 s ± 42 ms +1.21 %
arco-pro_development-mode + exec 1.89 s ± 75 ms 1.87 s ± 41 ms -0.70 %
arco-pro_development-mode_hmr + exec 434 ms ± 2.4 ms 434 ms ± 4 ms +0.08 %
arco-pro_production-mode + exec 3.52 s ± 286 ms 3.55 s ± 283 ms +0.81 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.54 s ± 235 ms 3.53 s ± 259 ms -0.32 %
threejs_development-mode_10x + exec 1.72 s ± 15 ms 1.72 s ± 16 ms +0.41 %
threejs_development-mode_10x_hmr + exec 826 ms ± 9.5 ms 830 ms ± 8.4 ms +0.48 %
threejs_production-mode_10x + exec 5.53 s ± 29 ms 5.51 s ± 21 ms -0.31 %

Please sign in to comment.