Skip to content

Commit fca50c3

Browse files
committed
docs: add formatting guide
1 parent d7cea9b commit fca50c3

8 files changed

Lines changed: 388 additions & 148 deletions

File tree

website/docs/en/guide/_meta.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@
2424
},
2525
{
2626
"type": "file",
27-
"name": "monorepo",
28-
"label": "Monorepo"
27+
"name": "testing",
28+
"label": "Testing"
2929
},
3030
{
3131
"type": "file",
32-
"name": "testing",
33-
"label": "Testing"
32+
"name": "formatting",
33+
"label": "Formatting"
34+
},
35+
{
36+
"type": "file",
37+
"name": "monorepo",
38+
"label": "Monorepo"
3439
},
3540
{
3641
"type": "dir-section-header",

website/docs/en/guide/cli/fmt.mdx

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,42 @@
11
# fmt
22

3-
The `rs fmt` command is built on [Prettier](https://prettier.io/). It processes files in parallel by default and uses the high-performance [Yuku](https://yuku.fyi/) parser for JavaScript and TypeScript, providing better performance than using Prettier directly.
3+
The `rs fmt` command formats files or checks whether they are formatted. For detailed usage, see [Formatting](../formatting).
44

55
## Usage
66

77
```bash
88
rs fmt [options] [files/globs...]
99
```
1010

11-
Pass files, directories, or glob patterns to select what to format. When no inputs are provided, `rs fmt` formats the current directory. `rs format` is an alias for `rs fmt`.
12-
13-
## Options
14-
15-
| Option | Description |
16-
| ---------------------------- | ----------------------------------------------------------------------------------- |
17-
| `--write` | Write formatted files in place. This is the default mode. |
18-
| `--check` | Check formatting without writing files. Exits with code 1 when files are different. |
19-
| `--list-different` | Print only unformatted paths. Exits with code 1 when files are different. |
20-
| `--parallel-workers <count>` | Set the maximum number of formatting workers. |
21-
| `-h, --help` | Display usage and option information. |
22-
23-
`--write`, `--check`, and `--list-different` are mutually exclusive.
11+
Pass files, directories, or glob patterns to choose what to format. When no paths are provided, `rs fmt` formats the current directory.
2412

2513
Examples:
2614

2715
```bash
28-
# Format the current directory in place
16+
# Format files in the current directory
2917
rs fmt
3018

3119
# Format specific files and directories
3220
rs fmt src package.json
3321

34-
# Include and exclude files with quoted globs
35-
rs fmt "src/**/*.{js,ts}" "!src/generated/**"
36-
3722
# Check formatting in CI
3823
rs fmt --check
3924
```
4025

41-
## File discovery
42-
43-
Directory and glob discovery follows `.gitignore` files, skips binary files, and does not traverse version-control directories or `node_modules`. Files for which Prettier cannot infer a parser are skipped.
44-
45-
Use [`ignorePatterns`](../configuration#define-fmt) for additional project-specific exclusions. Both positive and negative input globs are resolved from the current working directory.
46-
47-
## Configuration
26+
`rs format` is an alias for `rs fmt`:
4827

49-
Configure formatting through [`define.fmt()`](../configuration#define-fmt) in the [Rstack configuration file](/guide/configuration#configuration-file):
50-
51-
```ts title="rstack.config.ts"
52-
import { define } from 'rstack';
53-
54-
define.fmt({
55-
printWidth: 100,
56-
singleQuote: true,
57-
sortPackageJson: true,
58-
ignorePatterns: ['dist/**'],
59-
overrides: [
60-
{
61-
files: '*.md',
62-
options: {
63-
proseWrap: 'always',
64-
},
65-
},
66-
],
67-
});
28+
```bash
29+
rs format
6830
```
6931

70-
`define.fmt()` accepts standard [Prettier options](https://prettier.io/docs/options) and the `overrides` field, plus these Rstack options:
71-
72-
- `ignorePatterns`: Gitignore-compatible patterns relative to the directory containing `rstack.config.ts`.
73-
- `sortPackageJson`: Sort `package.json` fields before formatting. The default value is `false`.
74-
75-
:::warning Configuration sources
76-
77-
`rs fmt` does not load Prettier configuration files, `.prettierignore`, or `.editorconfig`. Move those settings and ignore rules into `define.fmt()`.
32+
## Options
7833

79-
:::
34+
| Option | Description |
35+
| ---------------------------- | ----------------------------------------------------------------------------------- |
36+
| `--write` | Write formatted files in place. This is the default mode. |
37+
| `--check` | Check formatting without writing files. Exits with code 1 when files are different. |
38+
| `--list-different` | Print only unformatted paths. Exits with code 1 when files are different. |
39+
| `--parallel-workers <count>` | Set the maximum number of formatting workers. |
40+
| `-h, --help` | Display usage and option information. |
8041

81-
When using [Prettier plugins](https://prettier.io/docs/plugins), pass each plugin as a package name, file path, or URL. Imported plugin objects are not supported because formatting runs in workers. Package names and relative paths are resolved from the Rstack configuration directory.
42+
> `--write`, `--check`, and `--list-different` are mutually exclusive.

website/docs/en/guide/configuration.mdx

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,32 +168,18 @@ define.lint(async () => {
168168

169169
### `define.fmt()` \{#define-fmt}
170170

171-
Defines the formatting configuration used by [`rs fmt`](./cli/fmt). Pass the configuration directly, or return it from a synchronous or asynchronous function.
172-
173-
`define.fmt()` accepts standard [Prettier options](https://prettier.io/docs/options) and the `overrides` field. It also supports `ignorePatterns` for Gitignore-compatible exclusions and `sortPackageJson` for sorting `package.json` fields before formatting.
171+
Defines formatting settings for [`rs fmt`](./cli/fmt). Pass a configuration object directly, or use a synchronous or asynchronous function that returns one.
174172

175173
```ts title="rstack.config.ts"
176174
import { define } from 'rstack';
177175

178176
define.fmt({
179177
printWidth: 100,
180178
singleQuote: true,
181-
sortPackageJson: true,
182-
ignorePatterns: ['dist/**'],
183-
overrides: [
184-
{
185-
files: '*.md',
186-
options: {
187-
proseWrap: 'always',
188-
},
189-
},
190-
],
191179
});
192180
```
193181

194-
Ignore patterns, override patterns, and relative plugin paths are resolved from the directory containing the Rstack configuration file. Specify Prettier plugins as package names, paths, or URLs rather than imported plugin objects.
195-
196-
`rs fmt` does not load Prettier configuration files, `.prettierignore`, or `.editorconfig`; keep all formatting options and ignore rules in `define.fmt()`.
182+
For detailed usage, see [Formatting](./formatting).
197183

198184
### `define.staged()` \{#define-staged}
199185

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# Formatting
2+
3+
import { PackageManagerTabs } from '@rspress/core/theme';
4+
5+
Rstack CLI includes a formatter built on [Prettier](https://prettier.io/). Compared with running Prettier directly, `rs fmt` offers better performance in two ways:
6+
7+
- **Parallel formatting**: `rs fmt` formats files concurrently in a worker pool.
8+
- **Yuku parser**: `rs fmt` uses the high-performance [Yuku](https://yuku.fyi/) parser by default for JavaScript, JSX, and TypeScript files.
9+
10+
`rs fmt` supports Prettier options and plugins and adds built-in capabilities such as [sorting package.json fields](#sort-package-json).
11+
12+
## Basic usage
13+
14+
Run `rs fmt` without file arguments to format files in the current directory and save the changes:
15+
16+
```bash
17+
rs fmt
18+
```
19+
20+
Use `--check` to verify formatting without changing files:
21+
22+
```bash
23+
rs fmt --check
24+
```
25+
26+
See the [`rs fmt` CLI reference](./cli/fmt) for more command-line options.
27+
28+
## Configuration
29+
30+
Use [`define.fmt()`](./configuration#define-fmt) in `rstack.config.ts` to set formatting rules. It supports all [Prettier options](https://prettier.io/docs/options):
31+
32+
```ts title="rstack.config.ts"
33+
import { define } from 'rstack';
34+
35+
define.fmt({
36+
printWidth: 100,
37+
singleQuote: true,
38+
});
39+
```
40+
41+
In addition to Prettier options and `overrides`, Rstack provides two options:
42+
43+
- [`ignorePatterns`](#ignore-files): exclude files with Gitignore-compatible patterns.
44+
- [`sortPackageJson`](#sort-package-json): sort fields in `package.json` files. The default value is `false`.
45+
46+
:::warning Prettier configuration files
47+
48+
`rs fmt` does not read Prettier configuration files, `.prettierignore`, or `.editorconfig`. Keep formatting options and additional ignore rules in `define.fmt()`.
49+
50+
:::
51+
52+
## Formatting scope
53+
54+
`rs fmt` determines the formatting scope from the paths passed on the command line. You can combine the following inputs:
55+
56+
- **Files**: format only the specified files.
57+
- **Directories**: scan directories recursively and format supported files.
58+
- **Glob patterns**: match multiple paths, and prefix a pattern with `!` to exclude matches.
59+
60+
When no paths are provided, `rs fmt` formats the current directory. All glob patterns are resolved from the current working directory. Quote them so that `rs fmt`, rather than the shell, expands them:
61+
62+
```bash
63+
# Format a directory and a file
64+
rs fmt src package.json
65+
66+
# Format JavaScript and TypeScript files, excluding generated files
67+
rs fmt "src/**/*.{js,ts}" "!src/generated/**"
68+
```
69+
70+
When scanning directories or globs, `rs fmt` follows `.gitignore` rules, skips binary files, and does not traverse version-control directories or `node_modules`. It also skips files for which Prettier cannot infer a parser.
71+
72+
`.gitignore` applies only when scanning directories and globs. It does not exclude files passed explicitly on the command line. To always exclude a file, use [`ignorePatterns`](#ignore-files).
73+
74+
## Ignore files
75+
76+
Use `ignorePatterns` to exclude files from formatting:
77+
78+
```ts title="rstack.config.ts"
79+
import { define } from 'rstack';
80+
81+
define.fmt({
82+
ignorePatterns: ['dist/**', 'coverage/**', '**/generated/**'],
83+
});
84+
```
85+
86+
Patterns follow Gitignore syntax and are resolved relative to the directory containing the Rstack configuration file. Because they are applied after the files are selected, they also exclude files passed explicitly on the command line.
87+
88+
## Sort package.json fields \{#sort-package-json}
89+
90+
Enable `sortPackageJson` to sort fields in each selected `package.json` with [`sort-package-json`](https://github.com/keithamus/sort-package-json):
91+
92+
```ts title="rstack.config.ts"
93+
import { define } from 'rstack';
94+
95+
define.fmt({
96+
sortPackageJson: true,
97+
});
98+
```
99+
100+
## Overrides
101+
102+
Use the `overrides` field to set options for specific files. Each override supports these fields:
103+
104+
- `files`: files or glob patterns to match.
105+
- `options`: formatting options applied to matching files.
106+
- `excludeFiles`: optional files or glob patterns to exclude.
107+
108+
```ts title="rstack.config.ts"
109+
import { define } from 'rstack';
110+
111+
define.fmt({
112+
overrides: [
113+
{
114+
files: 'docs/**/*.md',
115+
excludeFiles: 'docs/generated/**',
116+
options: {
117+
proseWrap: 'always',
118+
},
119+
},
120+
],
121+
});
122+
```
123+
124+
### Pattern matching
125+
126+
The `files` and `excludeFiles` patterns are resolved relative to the directory containing `rstack.config.ts`.
127+
128+
In `files`, a pattern without `/` matches file names at any depth, while a pattern containing `/` matches relative paths. In this example, `*.md` matches Markdown files in any directory, while `scripts/**/*.js` matches paths relative to the configuration directory:
129+
130+
```ts
131+
define.fmt({
132+
overrides: [
133+
{ files: '*.md', options: { proseWrap: 'always' } },
134+
{ files: 'scripts/**/*.js', options: { singleQuote: true } },
135+
],
136+
});
137+
```
138+
139+
### Merge order
140+
141+
When multiple overrides match, they are applied in declaration order, so later values take precedence. Here, `README.md` matches both overrides, so the final `printWidth` is `80`:
142+
143+
```ts
144+
define.fmt({
145+
overrides: [
146+
{ files: '*.md', options: { printWidth: 100 } },
147+
{ files: 'README.md', options: { printWidth: 80 } },
148+
],
149+
});
150+
```
151+
152+
## Prettier plugins
153+
154+
To add formatting capabilities that are not built into Rstack, install the corresponding [Prettier plugin](https://prettier.io/docs/plugins) and add it to `plugins`. Plugins can be referenced by package name, file path, or URL. Package names and relative paths are resolved from the directory containing the Rstack configuration file.
155+
156+
Because `rs fmt` loads plugins in workers, plugin objects cannot be passed directly. Reference each plugin by package name, path, or URL instead. For example, install and enable [`prettier-plugin-tailwindcss`](https://github.com/tailwindlabs/prettier-plugin-tailwindcss):
157+
158+
<PackageManagerTabs command="install -D prettier-plugin-tailwindcss" />
159+
160+
```ts title="rstack.config.ts"
161+
import { define } from 'rstack';
162+
163+
define.fmt({
164+
plugins: ['prettier-plugin-tailwindcss'],
165+
});
166+
```
167+
168+
To enable a plugin only for specific files, add `plugins` to the `options` of an [`overrides`](#overrides) entry.

website/docs/zh/guide/_meta.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@
2424
},
2525
{
2626
"type": "file",
27-
"name": "monorepo",
28-
"label": "Monorepo"
27+
"name": "testing",
28+
"label": "测试"
2929
},
3030
{
3131
"type": "file",
32-
"name": "testing",
33-
"label": "测试"
32+
"name": "formatting",
33+
"label": "格式化"
34+
},
35+
{
36+
"type": "file",
37+
"name": "monorepo",
38+
"label": "Monorepo"
3439
},
3540
{
3641
"type": "dir-section-header",

0 commit comments

Comments
 (0)