Skip to content

Commit bbfe800

Browse files
authored
docs: explain shared configuration APIs and test inheritance (#553)
1 parent 8747f93 commit bbfe800

6 files changed

Lines changed: 100 additions & 18 deletions

File tree

‎website/docs/en/guide/api-reference.mdx‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,36 @@ Rstack CLI provides a unified configuration API and re-exports the public APIs o
2525

2626
Import `define` from `rstack` to register tool configurations in `rstack.config.ts`; see [Configuration APIs](./configuration#configuration-apis) for details.
2727

28+
### `RstackConfig`
29+
30+
`RstackConfig` is the type for a [shared configuration](./configuration#shared-configurations). All fields are optional, so include only the settings you want to share.
31+
32+
Each tool field accepts the same configuration as its corresponding `define.*()` method. For example, `fmt` accepts the same input as `define.fmt()`.
33+
34+
```ts title="shared.ts"
35+
import type { RstackConfig } from 'rstack';
36+
37+
export const sharedConfig: RstackConfig = {
38+
fmt: {
39+
singleQuote: true,
40+
},
41+
};
42+
```
43+
44+
Use the `extends` field to inherit other shared configurations. Its type is `readonly RstackConfig[]`.
45+
46+
```ts title="team.ts"
47+
import type { RstackConfig } from 'rstack';
48+
import { sharedConfig } from './shared.ts';
49+
50+
export const teamConfig: RstackConfig = {
51+
extends: [sharedConfig],
52+
fmt: {
53+
printWidth: 100,
54+
},
55+
};
56+
```
57+
2858
## Re-exports
2959

3060
The tool-specific subpaths below re-export the public APIs from their corresponding core packages. Using these entry points keeps imports unified and APIs aligned with the tool versions integrated by Rstack CLI.

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,16 @@ define.fmt({
7878

7979
Configuration options follow the formats of the underlying tools. When using APIs and helpers that Rstack CLI re-exports, prefer the `rstack/app`, `rstack/lib`, `rstack/test`, and `rstack/lint` entry points.
8080

81-
| API | Tool | Commands |
82-
| ----------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
83-
| [`define.app()`](#define-app) | [Rsbuild](https://rsbuild.rs/config/) | [`rs dev`](./cli/dev), [`rs build`](./cli/build), [`rs preview`](./cli/preview) |
84-
| [`define.lib()`](#define-lib) | [Rslib](https://rslib.rs/config/) | [`rs lib`](./cli/lib) |
85-
| [`define.doc()`](#define-doc) | [Rspress](https://rspress.rs/api/config/config-basic) | [`rs doc`](./cli/doc) |
86-
| [`define.test()`](#define-test) | [Rstest](https://rstest.rs/config/) | [`rs test`](./cli/test) |
87-
| [`define.lint()`](#define-lint) | [Rslint](https://rslint.rs/config/) | [`rs lint`](./cli/lint) |
88-
| [`define.fmt()`](#define-fmt) | [Prettier](https://prettier.io/docs/options) | [`rs fmt`](./cli/fmt) |
89-
| [`define.staged()`](#define-staged) | [lint-staged](https://github.com/lint-staged/lint-staged#configuration) | [`rs staged`](./cli/staged) |
81+
| API | Tool | Commands |
82+
| ------------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
83+
| [`define.app()`](#define-app) | [Rsbuild](https://rsbuild.rs/config/) | [`rs dev`](./cli/dev), [`rs build`](./cli/build), [`rs preview`](./cli/preview) |
84+
| [`define.lib()`](#define-lib) | [Rslib](https://rslib.rs/config/) | [`rs lib`](./cli/lib) |
85+
| [`define.doc()`](#define-doc) | [Rspress](https://rspress.rs/api/config/config-basic) | [`rs doc`](./cli/doc) |
86+
| [`define.test()`](#define-test) | [Rstest](https://rstest.rs/config/) | [`rs test`](./cli/test) |
87+
| [`define.lint()`](#define-lint) | [Rslint](https://rslint.rs/config/) | [`rs lint`](./cli/lint) |
88+
| [`define.fmt()`](#define-fmt) | [Prettier](https://prettier.io/docs/options) | [`rs fmt`](./cli/fmt) |
89+
| [`define.staged()`](#define-staged) | [lint-staged](https://github.com/lint-staged/lint-staged#configuration) | [`rs staged`](./cli/staged) |
90+
| [`define.extends()`](#define-extends) | — | — |
9091

9192
### `define.app()` \{#define-app}
9293

@@ -205,6 +206,14 @@ define.staged({
205206

206207
Provide a staged configuration through `define.staged()` or a [shared configuration](#shared-configurations). `rs staged` reports an error if neither provides one.
207208

209+
### `define.extends()` \{#define-extends}
210+
211+
Accepts an array of shared configuration objects and applies their tool settings before merging the project's own `define.*()` configurations.
212+
213+
**Type:** `(configs: readonly RstackConfig[]) => void`
214+
215+
See [Shared configurations](#shared-configurations) for usage and merge rules.
216+
208217
## Shared configurations
209218

210219
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:

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import { defineInlineProject, expect, test } from 'rstack/test';
3636

3737
When `define.test()` does not set Rstest's [`extends`](https://rstest.rs/config/test/extends), Rstack CLI automatically converts the configuration registered by `define.app()` or `define.lib()` into an Rstest configuration. The inherited configuration is merged with the options passed directly to `define.test()`.
3838

39+
When using [shared configurations](./configuration#shared-configurations), Rstack first merges the shared and project test settings, then determines whether automatic inheritance is needed. If it is, Rstack uses the merged App or Lib configuration.
40+
3941
### Inherit the application configuration
4042

4143
When `define.app()` is registered, Rstack CLI converts it with [`@rstest/adapter-rsbuild`](https://rstest.rs/guide/integration/rsbuild) and uses the result as the test configuration's `extends` value:

‎website/docs/zh/guide/api-reference.mdx‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,36 @@ Rstack CLI 提供统一的配置 API,并通过专用子路径重导出 Rsbuild
2525

2626
从 `rstack` 导入 `define`,用于在 `rstack.config.ts` 中注册各项工具配置;详细用法请参阅[配置 API](./configuration#configuration-apis)。
2727

28+
### `RstackConfig`
29+
30+
`RstackConfig` 是[公共配置](./configuration#shared-configurations)的类型。所有字段均为可选,只需填写需要共享的配置。
31+
32+
各工具字段接受与对应 `define.*()` 方法相同的配置,例如 `fmt` 对应 `define.fmt()`。
33+
34+
```ts title="shared.ts"
35+
import type { RstackConfig } from 'rstack';
36+
37+
export const sharedConfig: RstackConfig = {
38+
fmt: {
39+
singleQuote: true,
40+
},
41+
};
42+
```
43+
44+
还可以通过 `extends` 字段继承其他公共配置,该字段的类型为 `readonly RstackConfig[]`。
45+
46+
```ts title="team.ts"
47+
import type { RstackConfig } from 'rstack';
48+
import { sharedConfig } from './shared.ts';
49+
50+
export const teamConfig: RstackConfig = {
51+
extends: [sharedConfig],
52+
fmt: {
53+
printWidth: 100,
54+
},
55+
};
56+
```
57+
2858
## 重导出 \{#re-exports}
2959

3060
以下工具子路径均会重导出对应 core 包的公开 API。通过这些入口导入,可以统一依赖入口,并确保 API 与 Rstack CLI 集成的工具版本匹配。

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,16 @@ define.fmt({
7878

7979
各 API 沿用底层工具的配置格式。使用 Rstack CLI 已重导出的 API 和辅助函数时,推荐从 `rstack/app`、`rstack/lib`、`rstack/test` 和 `rstack/lint` 入口导入。
8080

81-
| API | 底层工具 | 对应命令 |
82-
| ----------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
83-
| [`define.app()`](#define-app) | [Rsbuild](https://rsbuild.rs/zh/config/) | [`rs dev`](./cli/dev)、[`rs build`](./cli/build)、[`rs preview`](./cli/preview) |
84-
| [`define.lib()`](#define-lib) | [Rslib](https://rslib.rs/zh/config/) | [`rs lib`](./cli/lib) |
85-
| [`define.doc()`](#define-doc) | [Rspress](https://rspress.rs/zh/api/config/config-basic) | [`rs doc`](./cli/doc) |
86-
| [`define.test()`](#define-test) | [Rstest](https://rstest.rs/zh/config/) | [`rs test`](./cli/test) |
87-
| [`define.lint()`](#define-lint) | [Rslint](https://rslint.rs/config/) | [`rs lint`](./cli/lint) |
88-
| [`define.fmt()`](#define-fmt) | [Prettier](https://prettier.io/docs/options) | [`rs fmt`](./cli/fmt) |
89-
| [`define.staged()`](#define-staged) | [lint-staged](https://github.com/lint-staged/lint-staged#configuration) | [`rs staged`](./cli/staged) |
81+
| API | 底层工具 | 对应命令 |
82+
| ------------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
83+
| [`define.app()`](#define-app) | [Rsbuild](https://rsbuild.rs/zh/config/) | [`rs dev`](./cli/dev)、[`rs build`](./cli/build)、[`rs preview`](./cli/preview) |
84+
| [`define.lib()`](#define-lib) | [Rslib](https://rslib.rs/zh/config/) | [`rs lib`](./cli/lib) |
85+
| [`define.doc()`](#define-doc) | [Rspress](https://rspress.rs/zh/api/config/config-basic) | [`rs doc`](./cli/doc) |
86+
| [`define.test()`](#define-test) | [Rstest](https://rstest.rs/zh/config/) | [`rs test`](./cli/test) |
87+
| [`define.lint()`](#define-lint) | [Rslint](https://rslint.rs/config/) | [`rs lint`](./cli/lint) |
88+
| [`define.fmt()`](#define-fmt) | [Prettier](https://prettier.io/docs/options) | [`rs fmt`](./cli/fmt) |
89+
| [`define.staged()`](#define-staged) | [lint-staged](https://github.com/lint-staged/lint-staged#configuration) | [`rs staged`](./cli/staged) |
90+
| [`define.extends()`](#define-extends) | — | — |
9091

9192
### `define.app()` \{#define-app}
9293

@@ -205,6 +206,14 @@ define.staged({
205206

206207
请通过 `define.staged()` 或[公共配置](#shared-configurations)提供 staged 配置,否则 `rs staged` 会报错。
207208

209+
### `define.extends()` \{#define-extends}
210+
211+
接收一组公共配置对象,先应用其中各工具的配置,再合并当前项目通过 `define.*()` 定义的配置。
212+
213+
**类型:** `(configs: readonly RstackConfig[]) => void`
214+
215+
用法和合并规则请参阅[公共配置](#shared-configurations)。
216+
208217
## 公共配置 \{#shared-configurations}
209218

210219
使用 `define.extends()` 可以在多个项目之间共享构建、测试、lint 和格式化配置。公共配置是一个普通对象,各字段的用法与对应的 `define.*()` API 一致。在 TypeScript 中,可以用 `RstackConfig` 检查配置类型:

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import { defineInlineProject, expect, test } from 'rstack/test';
3636

3737
当 `define.test()` 未设置 Rstest 的 [`extends`](https://rstest.rs/zh/config/test/extends) 时,Rstack CLI 会自动将 `define.app()` 或 `define.lib()` 注册的配置转换为 Rstest 配置,再与直接传给 `define.test()` 的选项合并。
3838

39+
使用[公共配置](./configuration#shared-configurations)时,Rstack 会先合并公共配置和项目中的测试配置,再判断是否需要自动继承。需要继承时,使用合并后的 App 或 Lib 配置。
40+
3941
### 继承应用配置 \{#inherit-the-application-configuration}
4042

4143
注册 `define.app()` 后,Rstack CLI 会通过 [`@rstest/adapter-rsbuild`](https://rstest.rs/zh/guide/integration/rsbuild) 转换该配置,并将结果作为测试配置的 `extends`:

0 commit comments

Comments
 (0)