Skip to content

Commit 039eff8

Browse files
committed
docs: complete configuration guide
1 parent 0c51d61 commit 039eff8

2 files changed

Lines changed: 296 additions & 44 deletions

File tree

Lines changed: 148 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,169 @@
11
# Configuration
22

3-
TODO.
3+
Rstack centralizes the configuration for your project's tools in a single file. Define only the configurations your project needs with the `define.*()` APIs.
44

5-
## `define.app()` \{#define-app}
5+
## Configuration file
66

7-
TODO.
7+
Create `rstack.config.ts` in the project root and call the relevant `define.*()` APIs:
88

9-
Commands:
9+
```ts title="rstack.config.ts"
10+
import { define } from 'rstack';
1011

11-
- [`rs dev`](./cli/dev)
12-
- [`rs build`](./cli/build)
13-
- [`rs preview`](./cli/preview)
12+
define.app({
13+
// Rsbuild configuration
14+
});
1415

15-
## `define.lib()` \{#define-lib}
16+
define.test({
17+
// Rstest configuration
18+
});
19+
```
1620

17-
TODO.
21+
The configuration file does not require a default export. Each `define.*()` API can be called at most once; defining the same configuration type more than once throws an error.
1822

19-
Command: [`rs lib`](./cli/lib).
23+
By default, Rstack looks for a file with one of the following names:
2024

21-
## `define.doc()` \{#define-doc}
25+
- `rstack.config.ts`
26+
- `rstack.config.js`
27+
- `rstack.config.mts`
28+
- `rstack.config.mjs`
2229

23-
TODO.
30+
All `rs` commands accept the global `-c, --config` option for loading a file with a different name or location:
2431

25-
Command: [`rs doc`](./cli/doc).
32+
```bash
33+
rs build --config ./configs/rstack.config.ts
34+
```
2635

27-
## `define.test()` \{#define-test}
36+
## Loading dependencies on demand
2837

29-
TODO.
38+
Every `rs` command loads and executes the Rstack configuration file, then resolves only the configuration functions needed by that command.
3039

31-
Command: [`rs test`](./cli/test).
40+
When a configuration needs to import plugins or other tool-specific dependencies, use an async configuration function and load those dependencies with dynamic `import()` inside it. This ensures that they are loaded only when the configuration is resolved.
3241

33-
## `define.lint()` \{#define-lint}
42+
```ts title="rstack.config.ts"
43+
import { define } from 'rstack';
3444

35-
TODO.
45+
define.app(async () => {
46+
const { pluginReact } = await import('@rsbuild/plugin-react');
3647

37-
Command: [`rs lint`](./cli/lint).
48+
return {
49+
plugins: [pluginReact()],
50+
};
51+
});
52+
```
3853

39-
## `define.staged()` \{#define-staged}
54+
## Configuration APIs
4055

41-
TODO.
56+
Configuration options follow the formats of the underlying tools. When using APIs and helpers that Rstack re-exports, prefer the `rstack/app`, `rstack/lib`, `rstack/test`, and `rstack/lint` entry points.
4257

43-
Command: [`rs staged`](./cli/staged).
58+
| API | Tool | Commands |
59+
| ----------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
60+
| [`define.app()`](#define-app) | [Rsbuild](https://rsbuild.rs/config/) | [`rs dev`](./cli/dev), [`rs build`](./cli/build), [`rs preview`](./cli/preview) |
61+
| [`define.lib()`](#define-lib) | [Rslib](https://rslib.rs/config/) | [`rs lib`](./cli/lib) |
62+
| [`define.doc()`](#define-doc) | [Rspress](https://rspress.rs/api/config/config-basic) | [`rs doc`](./cli/doc) |
63+
| [`define.test()`](#define-test) | [Rstest](https://rstest.rs/config/) | [`rs test`](./cli/test) |
64+
| [`define.lint()`](#define-lint) | [Rslint](https://rslint.rs/config/) | [`rs lint`](./cli/lint) |
65+
| [`define.staged()`](#define-staged) | [lint-staged](https://github.com/lint-staged/lint-staged#configuration) | [`rs staged`](./cli/staged) |
66+
67+
### `define.app()` \{#define-app}
68+
69+
Defines the [Rsbuild configuration](https://rsbuild.rs/config/) for an application. It accepts a configuration object or a configuration function. The function receives the standard Rsbuild configuration parameters.
70+
71+
```ts title="rstack.config.ts"
72+
import { define } from 'rstack';
73+
74+
define.app({
75+
html: {
76+
title: 'My App',
77+
},
78+
output: {
79+
distPath: {
80+
root: 'dist',
81+
},
82+
},
83+
});
84+
```
85+
86+
### `define.lib()` \{#define-lib}
87+
88+
Defines the [Rslib configuration](https://rslib.rs/config/) for a library. It accepts a configuration object or a configuration function. The function receives the standard Rslib configuration parameters.
89+
90+
```ts title="rstack.config.ts"
91+
import { define } from 'rstack';
92+
93+
define.lib({
94+
lib: [
95+
{
96+
dts: true,
97+
format: 'esm',
98+
},
99+
],
100+
});
101+
```
102+
103+
### `define.doc()` \{#define-doc}
104+
105+
Defines the [Rspress configuration](https://rspress.rs/api/config/config-basic) for a documentation site. It accepts a configuration object or an async configuration function.
106+
107+
```ts title="rstack.config.ts"
108+
import { define } from 'rstack';
109+
110+
define.doc({
111+
root: 'docs',
112+
title: 'My Site',
113+
});
114+
```
115+
116+
`@rspress/core` is an optional dependency of Rstack. Install it in every project that uses the `rs doc` command:
117+
118+
```bash
119+
pnpm add -D @rspress/core
120+
```
121+
122+
### `define.test()` \{#define-test}
123+
124+
Defines the [Rstest configuration](https://rstest.rs/config/). It accepts a configuration object or a configuration function.
125+
126+
```ts title="rstack.config.ts"
127+
import { define } from 'rstack';
128+
129+
define.app({
130+
// Shared application configuration
131+
});
132+
133+
define.test({
134+
setupFiles: ['./tests/rstest.setup.ts'],
135+
testEnvironment: 'happy-dom',
136+
});
137+
```
138+
139+
When `extends` is omitted, Rstack automatically connects the test configuration to `define.app()` through the Rsbuild adapter. If no application configuration is defined, it falls back to `define.lib()` through the Rslib adapter. The application configuration takes precedence when both are defined. Set `extends` explicitly to opt out of this automatic inheritance.
140+
141+
If the root test configuration does not define `extends` and contains `projects`, Rstack 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.
142+
143+
### `define.lint()` \{#define-lint}
144+
145+
Defines the [Rslint configuration](https://rslint.rs/config/). Pass the configuration directly, or use an async function to load presets and plugins from `rstack/lint` on demand.
146+
147+
```ts title="rstack.config.ts"
148+
import { define } from 'rstack';
149+
150+
define.lint(async () => {
151+
const { js, ts } = await import('rstack/lint');
152+
153+
return [js.configs.recommended, ts.configs.recommended];
154+
});
155+
```
156+
157+
### `define.staged()` \{#define-staged}
158+
159+
Defines the [lint-staged configuration](https://github.com/lint-staged/lint-staged#configuration) used to run tasks on staged Git files. It accepts either an object that maps glob patterns to tasks or a task-generator function. Tasks can be commands, command arrays, or functions supported by lint-staged.
160+
161+
```ts title="rstack.config.ts"
162+
import { define } from 'rstack';
163+
164+
define.staged({
165+
'*.{js,jsx,ts,tsx}': 'rs lint',
166+
});
167+
```
168+
169+
Unlike the other commands, `rs staged` requires a `define.staged()` configuration and reports an error when it is missing.
Lines changed: 148 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,169 @@
11
# 配置
22

3-
TODO.
3+
Rstack 将项目所用工具的配置集中到一份文件中。通过 `define.*()` API 定义项目实际需要的配置即可。
44

5-
## `define.app()` \{#define-app}
5+
## 配置文件
66

7-
TODO.
7+
在项目根目录创建 `rstack.config.ts`,并调用对应的 `define.*()` API:
88

9-
对应命令:
9+
```ts title="rstack.config.ts"
10+
import { define } from 'rstack';
1011

11-
- [`rs dev`](./cli/dev)
12-
- [`rs build`](./cli/build)
13-
- [`rs preview`](./cli/preview)
12+
define.app({
13+
// Rsbuild 配置
14+
});
1415

15-
## `define.lib()` \{#define-lib}
16+
define.test({
17+
// Rstest 配置
18+
});
19+
```
1620

17-
TODO.
21+
配置文件无需默认导出。每个 `define.*()` API 最多调用一次;重复定义同一类型的配置会抛出错误。
1822

19-
对应命令:[`rs lib`](./cli/lib)。
23+
Rstack 默认会查找使用以下任一文件名的配置文件:
2024

21-
## `define.doc()` \{#define-doc}
25+
- `rstack.config.ts`
26+
- `rstack.config.js`
27+
- `rstack.config.mts`
28+
- `rstack.config.mjs`
2229

23-
TODO.
30+
所有 `rs` 命令都支持全局的 `-c, --config` 选项,用于加载其他名称或位置的配置文件:
2431

25-
对应命令:[`rs doc`](./cli/doc)。
32+
```bash
33+
rs build --config ./configs/rstack.config.ts
34+
```
2635

27-
## `define.test()` \{#define-test}
36+
## 按需加载依赖
2837

29-
TODO.
38+
每次执行 `rs` 命令时,Rstack 都会加载并执行配置文件,然后只解析当前命令需要的配置函数。
3039

31-
对应命令:[`rs test`](./cli/test)。
40+
如果配置需要导入插件或其他工具专属依赖,请使用异步配置函数,并在函数内通过动态 `import()` 加载这些依赖。这样只有解析该配置时才会加载相关依赖。
3241

33-
## `define.lint()` \{#define-lint}
42+
```ts title="rstack.config.ts"
43+
import { define } from 'rstack';
3444

35-
TODO.
45+
define.app(async () => {
46+
const { pluginReact } = await import('@rsbuild/plugin-react');
3647

37-
对应命令:[`rs lint`](./cli/lint)。
48+
return {
49+
plugins: [pluginReact()],
50+
};
51+
});
52+
```
3853

39-
## `define.staged()` \{#define-staged}
54+
## 配置 API
4055

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

43-
对应命令:[`rs staged`](./cli/staged)。
58+
| API | 底层工具 | 对应命令 |
59+
| ----------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
60+
| [`define.app()`](#define-app) | [Rsbuild](https://rsbuild.rs/zh/config/) | [`rs dev`](./cli/dev)、[`rs build`](./cli/build)、[`rs preview`](./cli/preview) |
61+
| [`define.lib()`](#define-lib) | [Rslib](https://rslib.rs/zh/config/) | [`rs lib`](./cli/lib) |
62+
| [`define.doc()`](#define-doc) | [Rspress](https://rspress.rs/zh/api/config/config-basic) | [`rs doc`](./cli/doc) |
63+
| [`define.test()`](#define-test) | [Rstest](https://rstest.rs/zh/config/) | [`rs test`](./cli/test) |
64+
| [`define.lint()`](#define-lint) | [Rslint](https://rslint.rs/config/) | [`rs lint`](./cli/lint) |
65+
| [`define.staged()`](#define-staged) | [lint-staged](https://github.com/lint-staged/lint-staged#configuration) | [`rs staged`](./cli/staged) |
66+
67+
### `define.app()` \{#define-app}
68+
69+
定义应用的 [Rsbuild 配置](https://rsbuild.rs/zh/config/),支持传入配置对象或配置函数。配置函数接收 Rsbuild 的标准配置参数。
70+
71+
```ts title="rstack.config.ts"
72+
import { define } from 'rstack';
73+
74+
define.app({
75+
html: {
76+
title: 'My App',
77+
},
78+
output: {
79+
distPath: {
80+
root: 'dist',
81+
},
82+
},
83+
});
84+
```
85+
86+
### `define.lib()` \{#define-lib}
87+
88+
定义库的 [Rslib 配置](https://rslib.rs/zh/config/),支持传入配置对象或配置函数。配置函数接收 Rslib 的标准配置参数。
89+
90+
```ts title="rstack.config.ts"
91+
import { define } from 'rstack';
92+
93+
define.lib({
94+
lib: [
95+
{
96+
dts: true,
97+
format: 'esm',
98+
},
99+
],
100+
});
101+
```
102+
103+
### `define.doc()` \{#define-doc}
104+
105+
定义文档站点的 [Rspress 配置](https://rspress.rs/zh/api/config/config-basic),支持传入配置对象或异步配置函数。
106+
107+
```ts title="rstack.config.ts"
108+
import { define } from 'rstack';
109+
110+
define.doc({
111+
root: 'docs',
112+
title: 'My Site',
113+
});
114+
```
115+
116+
`@rspress/core` 是 Rstack 的可选依赖。每个使用 `rs doc` 命令的项目都需要安装该依赖:
117+
118+
```bash
119+
pnpm add -D @rspress/core
120+
```
121+
122+
### `define.test()` \{#define-test}
123+
124+
定义 [Rstest 配置](https://rstest.rs/zh/config/),支持传入配置对象或配置函数。
125+
126+
```ts title="rstack.config.ts"
127+
import { define } from 'rstack';
128+
129+
define.app({
130+
// 共享的应用配置
131+
});
132+
133+
define.test({
134+
setupFiles: ['./tests/rstest.setup.ts'],
135+
testEnvironment: 'happy-dom',
136+
});
137+
```
138+
139+
未设置 `extends` 时,Rstack 会通过 Rsbuild 适配器让测试配置自动继承 `define.app()`;如果未定义应用配置,则通过 Rslib 适配器回退到 `define.lib()`。二者同时存在时,应用配置的优先级更高。显式设置 `extends` 可关闭自动继承。
140+
141+
如果测试根配置未定义 `extends` 且包含 `projects`,Rstack 会为每个未自行设置 `extends` 的内联项目应用自动继承。函数形式的应用或库配置只会解析一次,并由这些项目共享。字符串形式的项目会原样传给 Rstest;它们会独立加载外部配置,不继承当前应用或库的配置。
142+
143+
### `define.lint()` \{#define-lint}
144+
145+
定义 [Rslint 配置](https://rslint.rs/config/)。可以直接传入配置,也可以使用异步函数,按需从 `rstack/lint` 加载预设和插件。
146+
147+
```ts title="rstack.config.ts"
148+
import { define } from 'rstack';
149+
150+
define.lint(async () => {
151+
const { js, ts } = await import('rstack/lint');
152+
153+
return [js.configs.recommended, ts.configs.recommended];
154+
});
155+
```
156+
157+
### `define.staged()` \{#define-staged}
158+
159+
定义用于处理 Git 暂存文件的 [lint-staged 配置](https://github.com/lint-staged/lint-staged#configuration)。支持传入从 glob 匹配模式映射到任务的配置对象,也支持传入任务生成函数。任务可以是 lint-staged 支持的命令、命令数组或函数。
160+
161+
```ts title="rstack.config.ts"
162+
import { define } from 'rstack';
163+
164+
define.staged({
165+
'*.{js,jsx,ts,tsx}': 'rs lint',
166+
});
167+
```
168+
169+
与其他命令不同,`rs staged` 必须配置 `define.staged()`;缺少配置时会报错。

0 commit comments

Comments
 (0)