Skip to content

Commit d9d6710

Browse files
authored
docs: add shared configuration guide (#552)
1 parent 35a6554 commit d9d6710

2 files changed

Lines changed: 236 additions & 2 deletions

File tree

‎website/docs/en/guide/configuration.mdx‎

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ When `extends` is omitted, Rstack CLI automatically connects the test configurat
158158

159159
If the root test configuration does not define `extends` and contains `projects`, Rstack CLI applies automatic inheritance to each inline project that omits its own `extends`. A function-based application or library configuration is resolved once and shared by those projects. String project entries are passed to Rstest unchanged; they load their external configurations independently and do not inherit the current application or library configuration.
160160

161+
These rules also apply to [shared configurations](#shared-configurations): Rstack first merges the test settings, then determines whether to inherit the merged App or Lib configuration.
162+
161163
> For more guidance on testing, see [Testing](./testing).
162164
163165
### `define.lint()` \{#define-lint}
@@ -201,4 +203,119 @@ define.staged({
201203
});
202204
```
203205

204-
Unlike the other commands, `rs staged` requires a `define.staged()` configuration and reports an error when it is missing.
206+
Provide a staged configuration through `define.staged()` or a [shared configuration](#shared-configurations). `rs staged` reports an error if neither provides one.
207+
208+
## Shared configurations
209+
210+
Use `define.extends()` to share build, test, lint, and formatting settings across projects. A shared configuration is a plain object whose fields accept the same values as the corresponding `define.*()` APIs. In TypeScript, use `RstackConfig` to check its types:
211+
212+
```ts title="shared.ts"
213+
import type { RstackConfig } from 'rstack';
214+
215+
export const sharedConfig: RstackConfig = {
216+
test: {
217+
retry: 2,
218+
},
219+
lint: ({ js, ts }) => [js.configs.recommended, ts.configs.recommended],
220+
fmt: {
221+
singleQuote: true,
222+
printWidth: 80,
223+
},
224+
};
225+
```
226+
227+
Import the shared configuration from a local module or npm package, then add any project-specific settings:
228+
229+
```ts title="rstack.config.ts"
230+
import { define } from 'rstack';
231+
import { sharedConfig } from './shared.ts';
232+
233+
define.extends([sharedConfig]);
234+
235+
define.fmt({
236+
printWidth: 100,
237+
});
238+
```
239+
240+
The project inherits the shared settings and changes only `printWidth` to `100`.
241+
242+
Call `define.extends()` at most once per configuration load, with all shared configurations in a single array.
243+
244+
### Merge order
245+
246+
Configurations are applied in this order:
247+
248+
1. Shared configurations in the `define.extends()` array, from left to right.
249+
2. The project's own `define.*()` configurations, even if they appear before `define.extends()` in the file.
250+
251+
Each tool's [merge rules](#merge-rules) determine which values are combined and which are replaced.
252+
253+
Shared configurations can also extend other shared configurations:
254+
255+
```ts title="team.ts"
256+
import type { RstackConfig } from 'rstack';
257+
import { sharedConfig } from './shared.ts';
258+
259+
export const teamConfig: RstackConfig = {
260+
extends: [sharedConfig],
261+
test: {
262+
retry: 3,
263+
},
264+
};
265+
```
266+
267+
Inherited configurations are applied first. With `define.extends([teamConfig])`, the order is `sharedConfig → teamConfig → project configuration`.
268+
269+
Repeated references are not deduplicated. If both `a` and `b` extend `base`, `define.extends([a, b])` applies `base → a → base → b → project configuration`. Each tool handles duplicate plugins according to its own rules. Circular inheritance throws an error that identifies the circular reference.
270+
271+
### Merge rules
272+
273+
Rstack merges configurations separately for each tool:
274+
275+
| Field | Merge behavior |
276+
| -------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
277+
| `app` | Uses Rsbuild's `mergeRsbuildConfig`. |
278+
| `lib` | Uses Rslib's `mergeRslibConfig`. |
279+
| `test` | Uses Rstest's `mergeRstestConfig`. |
280+
| `lint` | Concatenates configuration arrays in order; Rslint applies their rules. |
281+
| `fmt` | Shallow merge: later values replace earlier values for the same option. Arrays and objects, including `overrides`, are replaced as a whole. |
282+
| `doc` | Uses Rspress's `mergeDocConfig`: recursively merges objects, appends arrays, and replaces ordinary functions with later functions. |
283+
| `staged` | Shallow merge by glob pattern: later tasks replace earlier tasks for the same pattern, including command arrays and task functions. |
284+
285+
The `doc` rules also apply to `builderConfig`: a later `builderConfig.tools.rspack` callback replaces an earlier one, while callback arrays are concatenated. When an array is merged with a defined non-array value, Rspress treats that value as a single array item.
286+
287+
For `staged`, if either configuration is a top-level task-generator function, the later configuration replaces the earlier one entirely. For example, `glob mappings → function → new glob mappings` leaves only the new mappings. Task functions run when lint-staged supplies the staged file list.
288+
289+
### Loading dependencies
290+
291+
Shared configurations can also [import dependencies dynamically](#loading-dependencies-on-demand) inside tool configuration functions, so each command loads only what it needs.
292+
293+
```ts title="shared.ts"
294+
import type { RstackConfig } from 'rstack';
295+
296+
export const sharedConfig: RstackConfig = {
297+
app: async () => {
298+
const { pluginReact } = await import('@rsbuild/plugin-react');
299+
return {
300+
plugins: [pluginReact()],
301+
};
302+
},
303+
};
304+
```
305+
306+
### Resolving paths
307+
308+
Relative paths follow each tool's existing rules; they are not automatically resolved from the shared module's directory. Use an absolute path for files shipped with the shared configuration:
309+
310+
```ts title="shared.ts"
311+
import { fileURLToPath } from 'node:url';
312+
import type { RstackConfig } from 'rstack';
313+
314+
export const sharedConfig: RstackConfig = {
315+
test: {
316+
setupFiles: [fileURLToPath(new URL('./setup.ts', import.meta.url))],
317+
},
318+
};
319+
```
320+
321+
Make sure the published package includes the referenced file. If you compile it, update the path to match its output location.

‎website/docs/zh/guide/configuration.mdx‎

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ define.test({
158158

159159
如果测试根配置未定义 `extends` 且包含 `projects`,Rstack CLI 会为每个未自行设置 `extends` 的内联项目应用自动继承。函数形式的应用或库配置只会解析一次,并由这些项目共享。字符串形式的项目会原样传给 Rstest;它们会独立加载外部配置,不继承当前应用或库的配置。
160160

161+
这些规则同样适用于[公共配置](#shared-configurations):Rstack 先合并测试配置,再判断是否需要继承合并后的 App 或 Lib 配置。
162+
161163
> 如需了解更多测试相关用法,请参阅[测试](./testing)。
162164
163165
### `define.lint()` \{#define-lint}
@@ -201,4 +203,119 @@ define.staged({
201203
});
202204
```
203205

204-
与其他命令不同,`rs staged` 必须配置 `define.staged()`;缺少配置时会报错。
206+
请通过 `define.staged()` 或[公共配置](#shared-configurations)提供 staged 配置,否则 `rs staged` 会报错。
207+
208+
## 公共配置 \{#shared-configurations}
209+
210+
使用 `define.extends()` 可以在多个项目之间共享构建、测试、lint 和格式化配置。公共配置是一个普通对象,各字段的用法与对应的 `define.*()` API 一致。在 TypeScript 中,可以用 `RstackConfig` 检查配置类型:
211+
212+
```ts title="shared.ts"
213+
import type { RstackConfig } from 'rstack';
214+
215+
export const sharedConfig: RstackConfig = {
216+
test: {
217+
retry: 2,
218+
},
219+
lint: ({ js, ts }) => [js.configs.recommended, ts.configs.recommended],
220+
fmt: {
221+
singleQuote: true,
222+
printWidth: 80,
223+
},
224+
};
225+
```
226+
227+
从本地模块或 npm 包导入公共配置,再添加项目自己的配置:
228+
229+
```ts title="rstack.config.ts"
230+
import { define } from 'rstack';
231+
import { sharedConfig } from './shared.ts';
232+
233+
define.extends([sharedConfig]);
234+
235+
define.fmt({
236+
printWidth: 100,
237+
});
238+
```
239+
240+
项目会继承公共配置,仅将 `printWidth` 改为 `100`。
241+
242+
每次加载配置时,`define.extends()` 最多调用一次,多个公共配置请放在同一个数组中。
243+
244+
### 合并顺序 \{#merge-order}
245+
246+
配置按以下顺序应用:
247+
248+
1. `define.extends()` 数组中的公共配置,从左到右。
249+
2. 项目自己的 `define.*()` 配置,即使这些调用写在 `define.extends()` 之前。
250+
251+
各工具的[合并规则](#merge-rules)决定同一字段的值是合并还是替换。
252+
253+
公共配置也可以继承其他公共配置:
254+
255+
```ts title="team.ts"
256+
import type { RstackConfig } from 'rstack';
257+
import { sharedConfig } from './shared.ts';
258+
259+
export const teamConfig: RstackConfig = {
260+
extends: [sharedConfig],
261+
test: {
262+
retry: 3,
263+
},
264+
};
265+
```
266+
267+
被继承的配置先应用。使用 `define.extends([teamConfig])` 时,顺序为 `sharedConfig → teamConfig → 项目配置`。
268+
269+
重复引用不会去重。例如,`a` 和 `b` 都继承 `base` 时,`define.extends([a, b])` 的顺序为 `base → a → base → b → 项目配置`。重复的插件由各工具按自身规则处理。循环继承会抛出错误,并指出循环引用的位置。
270+
271+
### 合并规则 \{#merge-rules}
272+
273+
Rstack 分别合并各个工具的配置:
274+
275+
| 字段 | 合并方式 |
276+
| -------- | ------------------------------------------------------------------------------------ |
277+
| `app` | 使用 Rsbuild 的 `mergeRsbuildConfig`。 |
278+
| `lib` | 使用 Rslib 的 `mergeRslibConfig`。 |
279+
| `test` | 使用 Rstest 的 `mergeRstestConfig`。 |
280+
| `lint` | 按顺序拼接配置数组,由 Rslint 应用其中的规则。 |
281+
| `fmt` | 浅合并,同名选项由后面的值覆盖。数组和对象(包括 `overrides`)整体替换。 |
282+
| `doc` | 使用 Rspress 的 `mergeDocConfig`:递归合并对象、追加数组,普通函数由后面的函数覆盖。 |
283+
| `staged` | 按 glob 模式浅合并,同名模式的任务整体替换,包括命令数组和任务函数。 |
284+
285+
`doc` 的合并规则同样适用于 `builderConfig`:后面的 `builderConfig.tools.rspack` 回调会覆盖前面的回调,回调数组则按顺序拼接。数组与已定义的非数组值合并时,Rspress 会将后者作为一个数组元素参与拼接。
286+
287+
合并 `staged` 时,只要任一配置是顶层任务生成函数,就由后一个配置整体替换前一个。例如,「glob 映射 → 函数 → 新的 glob 映射」最终只保留新的映射。任务函数由 lint-staged 传入暂存文件列表后执行。
288+
289+
### 依赖加载 \{#loading-dependencies}
290+
291+
公共配置也可以在工具配置函数中[动态导入依赖](#loading-dependencies-on-demand),让各命令只加载需要的依赖。
292+
293+
```ts title="shared.ts"
294+
import type { RstackConfig } from 'rstack';
295+
296+
export const sharedConfig: RstackConfig = {
297+
app: async () => {
298+
const { pluginReact } = await import('@rsbuild/plugin-react');
299+
return {
300+
plugins: [pluginReact()],
301+
};
302+
},
303+
};
304+
```
305+
306+
### 路径解析 \{#resolving-paths}
307+
308+
相对路径沿用各工具的解析规则,不会自动改为以公共模块所在目录为基准。引用公共配置自带的文件时,请使用绝对路径:
309+
310+
```ts title="shared.ts"
311+
import { fileURLToPath } from 'node:url';
312+
import type { RstackConfig } from 'rstack';
313+
314+
export const sharedConfig: RstackConfig = {
315+
test: {
316+
setupFiles: [fileURLToPath(new URL('./setup.ts', import.meta.url))],
317+
},
318+
};
319+
```
320+
321+
发布包中需要包含引用的文件。如果该文件经过编译,请将路径改为编译产物的位置。

0 commit comments

Comments
 (0)