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

fix!: reuseExistingChunk should be false by default #7489

Merged
merged 5 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
JSerFeng marked this conversation as resolved.
Show resolved Hide resolved
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),
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved
// 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");
});
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved
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. For example:
JSerFeng marked this conversation as resolved.
Show resolved Hide resolved

```
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。举例
JSerFeng marked this conversation as resolved.
Show resolved Hide resolved

```
chunk Foo: [ module A, module B ]
chunk Bar: [ module B ]

cacheGroup: {
test: /B/,
chunks: 'all'
}
```

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

如果设置 reuseExistingChunk 为 `false` 那么 chunk Bar 和 chunk Foo 中的 module B 会被移到新 chunk 中,chunk Bar 由于不包含任何 module,会作为空 chunk 被删除。
JSerFeng marked this conversation as resolved.
Show resolved Hide resolved
Loading