|
1 | 1 | # Configuration |
2 | 2 |
|
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. |
4 | 4 |
|
5 | | -## `define.app()` \{#define-app} |
| 5 | +## Configuration file |
6 | 6 |
|
7 | | -TODO. |
| 7 | +Create `rstack.config.ts` in the project root and call the relevant `define.*()` APIs: |
8 | 8 |
|
9 | | -Commands: |
| 9 | +```ts title="rstack.config.ts" |
| 10 | +import { define } from 'rstack'; |
10 | 11 |
|
11 | | -- [`rs dev`](./cli/dev) |
12 | | -- [`rs build`](./cli/build) |
13 | | -- [`rs preview`](./cli/preview) |
| 12 | +define.app({ |
| 13 | + // Rsbuild configuration |
| 14 | +}); |
14 | 15 |
|
15 | | -## `define.lib()` \{#define-lib} |
| 16 | +define.test({ |
| 17 | + // Rstest configuration |
| 18 | +}); |
| 19 | +``` |
16 | 20 |
|
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. |
18 | 22 |
|
19 | | -Command: [`rs lib`](./cli/lib). |
| 23 | +By default, Rstack looks for a file with one of the following names: |
20 | 24 |
|
21 | | -## `define.doc()` \{#define-doc} |
| 25 | +- `rstack.config.ts` |
| 26 | +- `rstack.config.js` |
| 27 | +- `rstack.config.mts` |
| 28 | +- `rstack.config.mjs` |
22 | 29 |
|
23 | | -TODO. |
| 30 | +All `rs` commands accept the global `-c, --config` option for loading a file with a different name or location: |
24 | 31 |
|
25 | | -Command: [`rs doc`](./cli/doc). |
| 32 | +```bash |
| 33 | +rs build --config ./configs/rstack.config.ts |
| 34 | +``` |
26 | 35 |
|
27 | | -## `define.test()` \{#define-test} |
| 36 | +## Loading dependencies on demand |
28 | 37 |
|
29 | | -TODO. |
| 38 | +Every `rs` command loads and executes the Rstack configuration file, then resolves only the configuration functions needed by that command. |
30 | 39 |
|
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. |
32 | 41 |
|
33 | | -## `define.lint()` \{#define-lint} |
| 42 | +```ts title="rstack.config.ts" |
| 43 | +import { define } from 'rstack'; |
34 | 44 |
|
35 | | -TODO. |
| 45 | +define.app(async () => { |
| 46 | + const { pluginReact } = await import('@rsbuild/plugin-react'); |
36 | 47 |
|
37 | | -Command: [`rs lint`](./cli/lint). |
| 48 | + return { |
| 49 | + plugins: [pluginReact()], |
| 50 | + }; |
| 51 | +}); |
| 52 | +``` |
38 | 53 |
|
39 | | -## `define.staged()` \{#define-staged} |
| 54 | +## Configuration APIs |
40 | 55 |
|
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. |
42 | 57 |
|
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. |
0 commit comments