Skip to content

Commit 0e9ec16

Browse files
authored
docs: add layer feature guide doc (#11985)
* docs: add layer feature guide doc * feat: add more links and layer inheritance rules * fix: change optimization strategy to compilation strategy
1 parent 7ffd353 commit 0e9ec16

File tree

8 files changed

+306
-2
lines changed

8 files changed

+306
-2
lines changed

website/docs/en/config/entry.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ The default value can be affected by different [target](/config/target):
224224

225225
Specifies the layer in which modules of this entrypoint are placed. Make the corresponding configuration take effect through layer matching in split chunks, [rules](/config/module#ruleissuerlayer), stats, and externals.
226226

227+
For more information about layers, see the [Layer guide](/guide/features/layer).
228+
227229
:::warning
228230
For version before v1.6.0, this configuration will only take effect when [experiments.layers](/config/experiments#experimentslayers) is `true`.
229231
:::

website/docs/en/config/module.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,8 @@ export default {
21212121
21222122
Matches all modules that match this resource, and will match against layer of the module that issued the current module.
21232123
2124+
For more information about layers, see the [Layer guide](/guide/features/layer).
2125+
21242126
:::warning
21252127
For version before v1.6.0, this configuration will only work if [experiments.layers = true](/config/experiments#experimentslayers).
21262128
:::
@@ -2526,6 +2528,8 @@ The meanings of all `type` options are as follows:
25262528
25272529
Used to mark the layer of the matching module. A group of modules could be united in one layer which could then be used in split chunks, stats or [entry options](/config/entry#entrydescriptionlayer).
25282530
2531+
For more information about layers, see the [Layer guide](/guide/features/layer).
2532+
25292533
:::warning
25302534
For version before v1.6.0, this configuration will only work if [experiments.layers = true](/config/experiments#experimentslayers).
25312535
:::

website/docs/en/guide/features/_meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
"lazy-compilation",
1111
"builtin-swc-loader",
1212
"builtin-lightningcss-loader",
13-
"esm"
13+
"esm",
14+
"layer"
1415
]
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Layer
2+
3+
Layer is a powerful feature provided by Rspack that allows you to categorize and manage modules. By assigning different layers to modules, you can perform differentiated processing for different types of modules, such as:
4+
5+
- Using different compilation targets based on different layers
6+
- Splitting modules from different layers into different output directories
7+
- Applying different compilation strategies for code in different environments (such as server-side and client-side)
8+
9+
## Ways to specify layers
10+
11+
When a module is assigned a layer, all its dependent modules will also be assigned to that layer. For example, if you specify the layer as `client` for the root module, all modules starting from the root module and their dependencies will be assigned to the `client` layer.
12+
13+
## Ways to specify layers
14+
15+
You can specify layers for modules in the following ways:
16+
17+
### 1. Specify via entry
18+
19+
Directly specify layers for different entry points in the [entry configuration using the `layer`](/config/entry#entrydescriptionlayer) option:
20+
21+
```js title="rspack.config.mjs"
22+
export default {
23+
entry: {
24+
server: {
25+
import: './src/server.js',
26+
layer: 'server',
27+
},
28+
client: {
29+
import: './src/client.js',
30+
layer: 'client',
31+
},
32+
},
33+
};
34+
```
35+
36+
### 2. Specify via loader rules
37+
38+
You can use the [`rule.layer`](/config/module#rulelayer) option to specify a layer for all modules that match the rule:
39+
40+
```js title="rspack.config.mjs"
41+
export default {
42+
module: {
43+
rules: [
44+
{
45+
test: /\.js$/,
46+
layer: 'yourLayer',
47+
},
48+
],
49+
},
50+
};
51+
```
52+
53+
The layer assigned by [`rule.layer`](/config/module#rulelayer) will override the module's own layer. For example, if a module has been assigned a layer through Entry configuration, the layer assigned through Rule will override the layer specified by Entry.
54+
55+
## Layer inheritance
56+
57+
To better understand how Layer works, we categorize modules into two types:
58+
59+
- **Regular modules**: Modules that are not explicitly assigned a layer
60+
- **Layer modules**: Modules that have been assigned a layer
61+
62+
### Layer inheritance rules
63+
64+
**1. Downward Inheritance**
65+
66+
All dependent modules of a Layer module will inherit the same layer. For example, when you specify the layer as `'client'` for an entry module, all modules in the entire dependency tree starting from that entry will be assigned to the `'client'` layer.
67+
68+
**2. Mid-path Override**
69+
70+
If a module in the dependency chain is reassigned to a different layer (such as `'server'`) through Loader rules, then all subsequent dependencies starting from that module will follow the new layer.
71+
72+
When a regular module is depended upon by multiple modules with different layers, that module will generate multiple copies in the build output, with each copy corresponding to a layer.
73+
74+
**Example:** If both `'client'` layer and `'server'` layer depend on the same `lib.js` module, then:
75+
76+
- The build output will contain two copies of `lib.js`: one belonging to the `'client'` layer and another to the `'server'` layer
77+
- The dependency modules of these two copies will also be duplicated and assigned according to the same rules
78+
79+
## Applying different processing rules to different layers
80+
81+
You can use [`issuerLayer`](/config/module#ruleissuerlayer) to filter modules from specific layers and apply different processing rules to them. The following example shows how to use different compilation targets for different layers:
82+
83+
```js title="rspack.config.mjs"
84+
export default {
85+
module: {
86+
rules: [
87+
{
88+
test: /\.js$/,
89+
oneOf: [
90+
{
91+
issuerLayer: 'client',
92+
use: [
93+
{
94+
loader: 'builtin:swc-loader',
95+
options: {
96+
jsc: {
97+
target: 'es5',
98+
},
99+
},
100+
},
101+
],
102+
},
103+
{
104+
issuerLayer: 'server',
105+
use: [
106+
{
107+
loader: 'builtin:swc-loader',
108+
options: {
109+
jsc: {
110+
target: 'es2020',
111+
},
112+
},
113+
},
114+
],
115+
},
116+
],
117+
},
118+
],
119+
},
120+
};
121+
```
122+
123+
## Layer-based code splitting
124+
125+
During the build optimization phase, you can assign modules to different chunks based on different layers:
126+
127+
```js title="rspack.config.mjs"
128+
export default {
129+
optimization: {
130+
splitChunks: {
131+
chunks: 'all',
132+
cacheGroups: {
133+
server: {
134+
layer: 'server',
135+
filename: 'server/[name].js',
136+
},
137+
client: {
138+
layer: 'client',
139+
filename: 'client/[name].js',
140+
},
141+
},
142+
},
143+
},
144+
};
145+
```
146+
147+
Through the above configuration, we successfully output server and client code to different directories respectively, achieving layer-based code separation.

website/docs/zh/config/entry.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ export default {
224224

225225
指定当前入口的模块所在的 layer。用于在 split chunks, [rules](/config/module#ruleissuerlayer)、stats、externals 中通过 layer 匹配使对应的配置生效。
226226

227+
更多关于 layer 的信息,请参考 [Layer 指南](/guide/features/layer)
228+
227229
:::warning
228230
对于 v1.6.0 之前的版本,只有在 [experiments.layers = true](/config/experiments#experimentslayers) 时该配置才会生效。
229231
:::

website/docs/zh/config/module.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,8 @@ export default {
21182118
21192119
匹配所有符合这个资源的模块,会与"引入当前模块"的模块的 layer 进行匹配。
21202120
2121+
更多关于 layer 的信息,请参考 [Layer 指南](/guide/features/layer)。
2122+
21212123
:::warning
21222124
对于 v1.6.0 之前的版本,只有在 [experiments.layers = true](/config/experiments#experimentslayers) 时该配置才会生效。
21232125
:::
@@ -2522,6 +2524,8 @@ export default {
25222524
25232525
用于标识匹配的模块的 layer。可以将一组模块聚合到一个 layer 中,该 layer 随后可以在 split chunks, stats 或 [entry options](/config/entry#entrydescriptionlayer) 中使用。
25242526
2527+
更多关于 layer 的信息,请参考 [Layer 指南](/guide/features/layer)。
2528+
25252529
:::warning
25262530
对于 v1.6.0 之前的版本,只有在 [experiments.layers = true](/config/experiments#experimentslayers) 时该配置才会生效。
25272531
:::

website/docs/zh/guide/features/_meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
"lazy-compilation",
1111
"builtin-swc-loader",
1212
"builtin-lightningcss-loader",
13-
"esm"
13+
"esm",
14+
"layer"
1415
]
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Layer
2+
3+
Layer 是 Rspack 提供的一个强大特性,允许你对模块进行分类管理。通过为模块分配不同的 layer,你可以针对不同类型的模块执行差异化处理,比如:
4+
5+
- 根据不同的 layer 使用不同的编译目标
6+
- 将不同 layer 的模块拆分到不同的输出目录
7+
- 为不同环境(如服务端和客户端)的代码应用不同的编译策略
8+
9+
## 指定 Layer 的方式
10+
11+
你可以通过以下几种方式为模块指定 layer:
12+
13+
### 1. 通过 Entry 指定
14+
15+
在入口配置中直接为[不同的入口点指定 `layer`](/config/entry#entrydescriptionlayer)
16+
17+
```js title="rspack.config.mjs"
18+
export default {
19+
entry: {
20+
server: {
21+
import: './src/server.js',
22+
layer: 'server',
23+
},
24+
client: {
25+
import: './src/client.js',
26+
layer: 'client',
27+
},
28+
},
29+
};
30+
```
31+
32+
### 2. 通过 Loader 规则指定
33+
34+
你可以使用 [`rule.layer`](/config/module#rulelayer) 选项为所有匹配规则的模块指定 layer:
35+
36+
```js title="rspack.config.mjs"
37+
export default {
38+
module: {
39+
rules: [
40+
{
41+
test: /\.js$/,
42+
layer: 'yourLayer',
43+
},
44+
],
45+
},
46+
};
47+
```
48+
49+
使用 [`rule.layer`](/config/module#rulelayer) 分配的 layer 将覆盖模块本身的 layer,例如某个模块被 Entry 指定了 layer,通过 Rule 分配的 layer 会覆盖 Entry 指定的 layer。
50+
51+
## Layer 的传递性
52+
53+
为了便于理解 Layer 的工作机制,我们将模块分为两类:
54+
55+
- **普通模块**:未被显式指定 layer 的模块
56+
- **Layer 模块**:已被指定 layer 的模块
57+
58+
### Layer 传递规则
59+
60+
**1. 向下传递**
61+
62+
Layer 模块的所有依赖模块都会继承相同的 layer。例如,当你为入口模块指定 layer 为 `'client'` 时,从该入口开始的整个依赖树中的所有模块都会被分配到 `'client'` layer。
63+
64+
**2. 中途覆盖**
65+
66+
如果依赖链中某个模块通过 Loader 规则被重新指定为其他 layer(如 `'server'`),那么从该模块开始的所有后续依赖都会跟随新的 layer。
67+
68+
当一个普通模块被多个不同 layer 的模块依赖时,该模块会在构建产物中生成多个副本,每个副本对应一个 layer。
69+
70+
**示例:** 如果 `'client'` layer 和 `'server'` layer 都依赖同一个 `lib.js` 模块,那么:
71+
72+
- 构建产物中会包含两份 `lib.js`:一份属于 `'client'` layer,另一份属于 `'server'` layer
73+
- 这两份副本的依赖模块也会按照相同规则进行复制和分配
74+
75+
## 给不同的 Layer 应用不同的处理规则
76+
77+
你可以使用 [`issuerLayer`](/config/module#ruleissuerlayer) 来筛选特定 layer 的模块,并为它们应用不同的处理规则。以下示例展示了如何为不同 layer 使用不同的编译目标:
78+
79+
```js title="rspack.config.mjs"
80+
export default {
81+
module: {
82+
rules: [
83+
{
84+
test: /\.js$/,
85+
oneOf: [
86+
{
87+
issuerLayer: 'client',
88+
use: [
89+
{
90+
loader: 'builtin:swc-loader',
91+
options: {
92+
jsc: {
93+
target: 'es5',
94+
},
95+
},
96+
},
97+
],
98+
},
99+
{
100+
issuerLayer: 'server',
101+
use: [
102+
{
103+
loader: 'builtin:swc-loader',
104+
options: {
105+
jsc: {
106+
target: 'es2020',
107+
},
108+
},
109+
},
110+
],
111+
},
112+
],
113+
},
114+
],
115+
},
116+
};
117+
```
118+
119+
## 基于 Layer 的代码分割
120+
121+
在构建优化阶段,你可以根据不同的 layer 将模块分配到不同的 chunk 中:
122+
123+
```js title="rspack.config.mjs"
124+
export default {
125+
optimization: {
126+
splitChunks: {
127+
chunks: 'all',
128+
cacheGroups: {
129+
server: {
130+
layer: 'server',
131+
filename: 'server/[name].js',
132+
},
133+
client: {
134+
layer: 'client',
135+
filename: 'client/[name].js',
136+
},
137+
},
138+
},
139+
},
140+
};
141+
```
142+
143+
通过上述配置,我们成功地将 server 和 client 代码分别输出到了不同的目录中,实现了基于 layer 的代码分离。

0 commit comments

Comments
 (0)