Skip to content

Commit 8dcd915

Browse files
authored
docs: improve splitChunks.chunks and update glossary (#10990)
* docs: improve `splitChunks.chunks` and update glossary * docs: fix
1 parent 5604372 commit 8dcd915

File tree

4 files changed

+180
-20
lines changed

4 files changed

+180
-20
lines changed

website/docs/en/misc/glossary.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ An "asset module" is a special module type used to process static assets, such a
1212

1313
- [Asset modules](/guide/features/asset-module)
1414

15+
## async chunk
16+
17+
An async chunk refers to a [chunk](#chunk) that is loaded asynchronously. When you use dynamic imports, Rspack will bundle the module into an async chunk.
18+
1519
## bundle
1620

1721
Historically bundlers produced a single output file called a "bundle." The concept of chunks was introduced later as part of a feature for automatically decomposing bundles into smaller files that can be loaded on demand.
@@ -49,6 +53,10 @@ Code splitting is a technique that allows you to split your code into multiple c
4953

5054
Dependency is parsed from the transformed code of a module. It is used to store the import relationships of modules for recursively generating the module graph. When code generation, it can inject code of module imports and exports. It can also be used for code replacement and injecting runtime requirements.
5155

56+
## initial chunk
57+
58+
Initial chunk is a [chunk](#chunk) that is loaded synchronously, including the entry module of the page and the modules imported statically.
59+
5260
## loader
5361

5462
In bundling terminology, a loader is like a plugin but specifically tasked with transforming module content. For example, we can use a loader to transform a TypeScript module into a JavaScript module, or to transform a CSS module into a JavaScript module that injects the CSS into the page.

website/docs/en/plugins/webpack/split-chunks-plugin.mdx

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import { ApiMeta } from '../../../../components/ApiMeta';
55

66
# SplitChunksPlugin
77

8-
## Default
8+
SplitChunksPlugin is a built-in plugin that splits code into multiple [chunks](/misc/glossary#chunk) to optimize application loading performance and achieve better caching strategies and parallel loading.
99

10-
Out of the box `SplitChunksPlugin` should work well for most users.
10+
SplitChunksPlugin can be configured through the [optimization.splitChunks](/config/optimization#optimizationsplitchunks) option, and you typically don't need to manually register this plugin.
11+
12+
## Default behavior
13+
14+
Rspack has a built-in configuration for `SplitChunksPlugin` that works well for most scenarios.
1115

1216
By default it only affects on-demand chunks, because changing initial chunks would affect the script tags the HTML file should include to run the project.
1317

@@ -92,20 +96,29 @@ export default {
9296

9397
#### splitChunks.cacheGroups.\{cacheGroup\}.chunks
9498

95-
- **Type:** `'initial' | 'all' | 'async' | RegExp | ((chunk: Chunk) => bool)`
96-
- **Default:** `'async'`
99+
- **Type:**
97100

98-
This indicates which chunks will be selected for optimization.
101+
```ts
102+
type OptimizationSplitChunksChunks =
103+
| 'initial'
104+
| 'async'
105+
| 'all'
106+
| RegExp
107+
| ((chunk: Chunk) => boolean);
108+
```
99109

100-
When a string is provided, valid values are `all`, `async`, and `initial`. Providing `all` can be particularly powerful, because it means that chunks can be shared even between async and non-async chunks.
110+
- **Default:** `'async'`
111+
112+
This option controls which chunks should be selected for code splitting. When a string is provided, the possible values are `all`, `async` and `initial`.
101113

102-
Alternatively, you may provide a function for more control. The return value will indicate whether to include each chunk.
114+
- `all`: Split all types of chunks, including [initial chunks](/misc/glossary#initial-chunk) and [async chunks](/misc/glossary#async-chunk).
115+
- `initial`: Only split initial chunks.
116+
- `async`: Only split async chunks.
103117

104-
You can also pass a regular expression, which is a short for `(chunk) => typeof chunk.name === "string" && regex.test(chunk.name)`.
118+
Generally, setting it to `all` can help reduce duplicate modules being bundled, as it means chunks can be shared between initial chunks and async chunks.
105119

106120
```js title="rspack.config.mjs"
107121
export default {
108-
//...
109122
optimization: {
110123
splitChunks: {
111124
// include all types of chunks
@@ -115,6 +128,64 @@ export default {
115128
};
116129
```
117130

131+
:::tip
132+
When using [Module Federation](/guide/features/module-federation), if the application uses `exposes` to expose remote modules, `chunks: 'all'` cannot be used as it would break the remote module splitting.
133+
:::
134+
135+
The `chunks` option can be set to a regular expression, which is a shorthand for `(chunk) => typeof chunk.name === "string" && regex.test(chunk.name)`.
136+
137+
```js title="rspack.config.mjs"
138+
export default {
139+
optimization: {
140+
splitChunks: {
141+
// equivalent to `chunks: (chunk) => typeof chunk.name === "string" && /foo/.test(chunk.name)`
142+
chunks: /foo/,
143+
},
144+
},
145+
};
146+
```
147+
148+
The `chunks` option can be set to a function for more fine-grained control. The function receives a `chunk` parameter, and returning `true` means the chunk participates in splitting (modules within it may be extracted into new chunks), while returning `false` means the chunk doesn't participate in splitting (remains as is).
149+
150+
```js title="rspack.config.mjs"
151+
export default {
152+
optimization: {
153+
splitChunks: {
154+
chunks(chunk) {
155+
// exclude `foo` chunk
156+
return chunk.name !== 'foo';
157+
},
158+
},
159+
},
160+
};
161+
```
162+
163+
:::warning
164+
Using the function type of `chunks` will significantly reduce build performance, as the function needs to be called for each module, resulting in huge cross-language communication overhead between Rust and JavaScript. Therefore, we do not recommend using the function type.
165+
:::
166+
167+
You can configure `chunks` individually for each cacheGroup, for example:
168+
169+
```js title="rspack.config.mjs"
170+
export default {
171+
optimization: {
172+
splitChunks: {
173+
cacheGroups: {
174+
groupA: {
175+
chunks: 'all',
176+
},
177+
groupB: {
178+
chunks: 'initial',
179+
},
180+
groupC: {
181+
chunks: 'async',
182+
},
183+
},
184+
},
185+
},
186+
};
187+
```
188+
118189
### splitChunks.maxAsyncRequests
119190

120191
- **Type:** `number`

website/docs/zh/misc/glossary.mdx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Asset 模块是一种特殊的模块类型,用来处理静态资源,例如
1212

1313
- [资源模块](/guide/features/asset-module)
1414

15+
## async chunk
16+
17+
Async chunk 指的是被异步加载的 [chunk](#chunk),当你使用动态导入时,Rspack 会将该模块打包到 async chunk 中。
18+
1519
## bundle splitting
1620

1721
Bundle splitting 是一种允许你将代码分割或合并到多个 bundle 的技术,这对于并行请求和更好的浏览器缓存很有用,它不用于减少初始化 bundle 的大小。
@@ -29,7 +33,7 @@ Rspack 中的内置模块类型指的是那些不需要依赖 loader 和 plugin
2933

3034
## chunk
3135

32-
Chunk 是一组绑定在一起的模块。rspack 会将相互关联的模块打包成一个 chunk,然后生成对应的文件。
36+
Chunk 是一组绑定在一起的模块。Rspack 会将相互关联的模块打包成一个 chunk,然后生成对应的文件。
3337

3438
## chunk graph(chunk 图)
3539

@@ -45,6 +49,10 @@ Code splitting 是一种技术,它允许你将你的代码分割成多个块
4549

4650
Dependency 是从模块编译后的代码解析出来的,它用于存储模块的导入关系,以便递归生成模块图。在生成产物代码时,它可以注入模块的导入和导出相关代码,也可以用于代码替换和注入运行时依赖。
4751

52+
## initial chunk
53+
54+
Initial chunk 是被同步加载的 [chunk](#chunk),包含页面的入口模块和被静态导入的模块。
55+
4856
## loader
4957

5058
Loader 是用来转换模块内容的。例如,我们可以使用 loader 将 TypeScript 模块转化为 JavaScript 模块,或者将 CSS 模块转化为 JavaScript 模块,将 CSS 注入到页面中。

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

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import { ApiMeta } from '../../../../components/ApiMeta';
55

66
# SplitChunksPlugin
77

8-
## 默认值
8+
SplitChunksPlugin 是一个内置插件,用于将代码拆分成多个 [chunk](/misc/glossary#chunk),以优化应用的加载性能,实现更好的缓存策略和并行加载效果。
99

10-
开箱即用的 `SplitChunksPlugin` 对于大部分用户来说非常友好。
10+
SplitChunksPlugin 可以通过 [optimization.splitChunks](/config/optimization#optimizationsplitchunks) 选项进行配置,通常你不需要手动注册该插件。
11+
12+
## 默认行为
13+
14+
Rspack 内置了开箱即用的 `SplitChunksPlugin` 配置,适用于大部分场景。
1115

1216
默认情况下,它只会影响到按需加载的 chunk,因为初始 chunk 会影响到项目的 HTML 文件中的脚本标签。
1317

14-
rspack 将根据以下条件自动拆分 chunk:
18+
Rspack 将根据以下条件自动拆分 chunk:
1519

1620
- 新的 chunk 可以被共享,或者模块来自于 `node_modules` 文件夹
1721
- 新的 chunk 体积大于 20kb(在进行 min+gz 之前的体积)
@@ -20,7 +24,7 @@ rspack 将根据以下条件自动拆分 chunk:
2024

2125
## 配置
2226

23-
rspack 为希望对该功能进行更多控制的开发者提供了一组选项。
27+
Rspack 为希望对该功能进行更多控制的开发者提供了一组选项。
2428

2529
:::warning
2630
Rspack 的默认配置符合 Web 性能最佳实践,但是项目的最佳策略可能有所不同。如果要更改配置,则应评估所做更改的影响,以确保有真正的收益。
@@ -90,18 +94,29 @@ export default {
9094

9195
#### splitChunks.cacheGroups.\{cacheGroup\}.chunks
9296

93-
- **类型:** `string 'initial' | 'all' | 'async'`
97+
- **类型:**
98+
99+
```ts
100+
type OptimizationSplitChunksChunks =
101+
| 'initial'
102+
| 'async'
103+
| 'all'
104+
| RegExp
105+
| ((chunk: Chunk) => boolean);
106+
```
107+
94108
- **默认值:** `'async'`
95109

96-
这表明将选择哪些 chunk 进行优化。当提供一个字符串,有效值为 `all``async``initial`。设置为 `all` 可能会更强大,因为这意味着 chunk 可以在异步和非异步 chunk 之间共享
110+
此选项用于控制哪些 chunk 参与代码拆分。当传入字符串时,可选值包括 `all``async``initial`
97111

98-
或者,也可以提供一个函数来进行更多控制。返回值将显示是否包含该 Chunk。
112+
- `all`:拆分所有类型的 chunks,包括 [initial chunks](/misc/glossary#initial-chunk)[async chunks](/misc/glossary#async-chunk)
113+
- `initial`:仅拆分 initial chunks。
114+
- `async`:仅拆分 async chunks。
99115

100-
还可以传递正则表达式,它是 `(chunk) => typeof chunk.name === "string" && regex.test(chunk.name)` 的缩写
116+
通常来说,设置为 `all` 会有助于减少被重复打包的模块,因为这意味着 chunks 可以被 initial chunks 和 async chunks 共享
101117

102118
```js title="rspack.config.mjs"
103119
export default {
104-
//...
105120
optimization: {
106121
splitChunks: {
107122
// include all types of chunks
@@ -111,6 +126,64 @@ export default {
111126
};
112127
```
113128

129+
:::tip
130+
在使用 [模块联邦](/guide/features/module-federation) 时,如果应用使用了 `exposes` 来暴露出远程模块,则无法使用 `chunks: 'all'`,因为这会破坏远程模块的拆分。
131+
:::
132+
133+
`chunks` 选项可以设置为正则表达式,它是 `(chunk) => typeof chunk.name === "string" && regex.test(chunk.name)` 的缩写。
134+
135+
```js title="rspack.config.mjs"
136+
export default {
137+
optimization: {
138+
splitChunks: {
139+
// 等价于 `chunks: (chunk) => typeof chunk.name === "string" && /foo/.test(chunk.name)`
140+
chunks: /foo/,
141+
},
142+
},
143+
};
144+
```
145+
146+
`chunks` 选项可以设置为函数来进行更细粒度的控制。该函数接收一个 `chunk` 参数,返回值为 `true` 表示该 chunk 参与拆分(其中的模块可能被提取到新的 chunk 中),返回值为 `false` 表示该 chunk 不参与拆分(保持原样)。
147+
148+
```js title="rspack.config.mjs"
149+
export default {
150+
optimization: {
151+
splitChunks: {
152+
chunks(chunk) {
153+
// exclude `foo` chunk
154+
return chunk.name !== 'foo';
155+
},
156+
},
157+
},
158+
};
159+
```
160+
161+
:::warning
162+
使用函数形式的 `chunks` 会显著降低构建性能,因为该函数需要对每个模块进行调用,从而产生大量的 Rust 与 JavaScript 之间的跨语言通信开销。因此我们不推荐使用函数形式。
163+
:::
164+
165+
你可以为每个 cacheGroup 单独配置 `chunks`,例如:
166+
167+
```js title="rspack.config.mjs"
168+
export default {
169+
optimization: {
170+
splitChunks: {
171+
cacheGroups: {
172+
groupA: {
173+
chunks: 'all',
174+
},
175+
groupB: {
176+
chunks: 'initial',
177+
},
178+
groupC: {
179+
chunks: 'async',
180+
},
181+
},
182+
},
183+
},
184+
};
185+
```
186+
114187
### splitChunks.maxAsyncRequests
115188

116189
- **类型:** `number`
@@ -399,7 +472,7 @@ export default {
399472
控制此缓存组选择的模块。省略它会选择所有模块。它可以匹配绝对模块资源路径或 chunk 名称。匹配 chunk 名称时,将选择 chunk 中的所有模块。
400473

401474
:::warning
402-
使用函数形式的 `test` 会显著降低构建性能,因为该函数需要对每个 module 进行调用,从而产生大量的 Rust 与 JavaScript 之间的跨语言通信开销。因此我们不推荐使用函数形式。
475+
使用函数形式的 `test` 会显著降低构建性能,因为该函数需要对每个模块进行调用,从而产生大量的 Rust 与 JavaScript 之间的跨语言通信开销。因此我们不推荐使用函数形式。
403476
:::
404477

405478
#### splitChunks.cacheGroups.\{cacheGroup\}.enforce

0 commit comments

Comments
 (0)