Skip to content

Commit 71bfcca

Browse files
authored
docs: translate cli.mdx to Chinese (#523)
1 parent 7bf3757 commit 71bfcca

File tree

2 files changed

+265
-17
lines changed

2 files changed

+265
-17
lines changed

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

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CLI
22

3-
Rslib comes with a lightweight CLI that includes commands such as build and inspect.
3+
Rslib comes with a lightweight CLI that includes commands such as [rslib build](#rslib-build) and [rslib inspect](#rslib-inspect).
44

55
## rslib -h
66

@@ -45,15 +45,24 @@ Options:
4545
-h, --help display help for command
4646
```
4747

48+
### Watch Mode
49+
50+
You can use `rslib build --watch` or `rslib build -w` to enable watch mode for watching for changes and rebuild.
51+
52+
```bash
53+
npx rslib build -w
54+
```
55+
4856
### Environment Variables
4957

5058
Rslib supports injecting env variables or expressions into the code during build, which is helpful for distinguishing the running environment or replacing constants.
5159

52-
You can see more details in [Environment Variables](https://rsbuild.dev/guide/advanced/env-vars#environment-variables).
60+
You can see more details in [Rsbuild - Environment Variables](https://rsbuild.dev/guide/advanced/env-vars).
5361

5462
::: note
5563

56-
`process.env.NODE_ENV` will be preserved in the build output when [format](/config/lib/format) is set to `esm` or `cjs`. For `mf` or `umd` format, it will be preserved to make output directly usable.
64+
- If [format](/config/lib/format) is `esm` or `cjs`, `process.env.NODE_ENV` will be preserved in the build output.
65+
- If [format](/config/lib/format) is `mf` or `umd`, `process.env.NODE_ENV` will be replaced to ensure that the output can run in the browser.
5766

5867
:::
5968

@@ -126,14 +135,6 @@ console.log(import.meta.env.BAR); // '2'
126135
console.log(process.env.BAR); // '2'
127136
```
128137

129-
### Watch Mode
130-
131-
You can use `rslib build --watch` to turn on watch mode for watching for changes and rebuild.
132-
133-
### Filter Libraries
134-
135-
You can use the `--lib` option to build specific libraries. The `--lib` option can be repeated to build multiple libraries. Check out the [lib.id](/config/lib/id) to learn how to get or set the library ID.
136-
137138
## rslib inspect
138139

139140
The `rslib inspect` command is used to view the Rsbuild config and Rspack config of the Rslib project.
@@ -191,15 +192,11 @@ Inspect config succeed, open following files to view the content:
191192
- Rspack Config (cjs): /project/dist/.rsbuild/rspack.config.cjs.mjs
192193
```
193194

194-
### Filter Libraries
195-
196-
You can use the `--lib` option to inspect specific libraries. The `--lib` option can be repeated to inspect multiple libraries. Check out the [lib.id](/config/lib/id) to learn how to get or set the library ID.
197-
198195
## rslib mf dev
199196

200-
The `rslib mf dev` command is utilized to start Rsbuild dev server of Module Federation format.
197+
The `rslib mf dev` command is utilized to start Rsbuild dev server for the [Module Federation](/guide/advanced/module-federation) format.
201198

202-
This enables you to develop your mf format module within the host app more rapidly.
199+
This enables you to develop and debug your mf format module within the host app.
203200

204201
```text
205202
Usage: rslib mf [options] <dev>
@@ -213,3 +210,23 @@ Options:
213210
--env-dir <dir> specify the directory to load `.env` files
214211
-h, --help display help for command
215212
```
213+
214+
## Common Options
215+
216+
### Filter Libraries
217+
218+
Rslib provides the `--lib` option to run command for specified libraries.
219+
220+
```bash
221+
# Only build the library with id `esm`
222+
npx rslib build --lib esm
223+
```
224+
225+
The `--lib` option can be repeated to specify multiple libraries.
226+
227+
```bash
228+
# Build the libraries with id `esm` and `cjs`
229+
npx rslib build --lib esm --lib cjs
230+
```
231+
232+
> Check out the [lib.id](/config/lib/id) to learn how to get or set the library ID.

website/docs/zh/guide/basic/cli.mdx

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,232 @@
11
# 命令行工具
2+
3+
Rslib 提供了一个轻量级的命令行工具,包含 [rslib build](#rslib-build)[rslib inspect](#rslib-inspect) 等命令。
4+
5+
## rslib -h
6+
7+
如果你需要查看所有可用的 CLI 命令,请在项目目录中运行以下命令:
8+
9+
```bash
10+
npx rslib -h
11+
```
12+
13+
输出如下:
14+
15+
```text
16+
Usage: rslib <command> [options]
17+
18+
Options:
19+
-V, --version 显示版本号
20+
-h, --help 显示命令帮助
21+
22+
Commands:
23+
build [options] 构建用于生产环境的产物
24+
inspect [options] 检查 Rslib 项目的 Rsbuild 配置和 Rspack 配置
25+
mf [options] <dev> 为 Module Federation 格式的库启用 Rsbuild 开发服务器。
26+
help [command] 显示命令帮助
27+
```
28+
29+
## rslib build
30+
31+
`rslib build` 命令默认会在 `dist/` 目录下输出构建产物。
32+
33+
```text
34+
Usage: rslib build [options]
35+
36+
构建用于生产环境的产物
37+
38+
Options:
39+
-c --config <config> 指定配置文件路径,可以为相对路径或绝对路径
40+
-r --root <root> 指定项目根目录,可以是绝对路径或者相对于 cwd 的路径
41+
--env-mode <mode> 指定 env 模式来加载 `.env.[mode]` 文件
42+
--env-dir <dir> 指定目录来加载 `.env` 文件
43+
--lib <id> 仅构建指定的库(可以重复)
44+
-w --watch 开启 watch 模式, 监听文件变更并重新构建
45+
-h, --help 显示命令帮助
46+
```
47+
48+
### Watch 模式
49+
50+
你可以使用 `rslib build --watch``rslib build -w` 来开启 watch 模式,监听文件变更并重新构建。
51+
52+
```bash
53+
npx rslib build -w
54+
```
55+
56+
### 环境变量
57+
58+
Rslib 支持在构建过程中向代码中注入环境变量或表达式,这对于区分运行环境、替换常量值等场景很有帮助。
59+
60+
你可以在 [Rsbuild - 环境变量](https://rsbuild.dev/zh/guide/advanced/env-vars) 了解更多详细信息。
61+
62+
::: note
63+
64+
-[format](/config/lib/format) 设置为 `esm``cjs` 时,`process.env.NODE_ENV` 会在构建输出中被保留。
65+
-[format](/config/lib/format) 设置为 `mf``umd` 时,`process.env.NODE_ENV` 将被替换,以确保构建输出可以在浏览器中运行。
66+
67+
:::
68+
69+
#### Env 模式
70+
71+
Rslib 支持读取 `.env.[mode]``.env.[mode].local` 文件。你可以通过 CLI 的 `--env-mode <mode>` 选项来指定 env 模式。
72+
73+
比如,设置 env 模式为 `test`:
74+
75+
```bash
76+
npx rslib build --env-mode test
77+
```
78+
79+
Rslib 会依次读取以下文件:
80+
81+
- `.env`
82+
- `.env.local`
83+
- `.env.test`
84+
- `.env.test.local`
85+
86+
:::tip
87+
88+
`--env-mode` 选项的优先级高于 `process.env.NODE_ENV`
89+
90+
推荐使用 `--env-mode` 来指定 env 模式,不建议修改 `process.env.NODE_ENV`
91+
92+
:::
93+
94+
#### Env 目录
95+
96+
默认情况下,`.env` 文件位于项目的根目录。你可以通过 CLI 的 `--env-dir <dir>` 选项来指定 env 目录。
97+
98+
比如,指定 env 目录为 `config`
99+
100+
```bash
101+
npx rslib build --env-dir config
102+
```
103+
104+
这种情况下,Rslib 会读取 `./config/.env` 等 env 文件。
105+
106+
##### 示例
107+
108+
比如创建 `.env` 文件并添加以下内容:
109+
110+
```shell title=".env"
111+
FOO=hello
112+
BAR=1
113+
```
114+
115+
然后在 `rslib.config.ts` 文件中,你可以通过 `import.meta.env.[name]``process.env.[name]` 访问到上述环境变量:
116+
117+
```ts title="rslib.config.ts"
118+
console.log(import.meta.env.FOO); // 'hello'
119+
console.log(import.meta.env.BAR); // '1'
120+
121+
console.log(process.env.FOO); // 'hello'
122+
console.log(process.env.BAR); // '1'
123+
```
124+
125+
此时,创建一个 `.env.local` 文件,添加以下内容:
126+
127+
```shell title=".env.local"
128+
BAR=2
129+
```
130+
131+
`BAR` 的值会被覆盖为 `'2'`
132+
133+
```ts title="rslib.config.ts"
134+
console.log(import.meta.env.BAR); // '2'
135+
console.log(process.env.BAR); // '2'
136+
```
137+
138+
## rslib inspect
139+
140+
`rslib inspect` 命令用于查看 Rslib 项目的 Rsbuild 配置和 Rspack 配置。
141+
142+
```text
143+
Usage: rslib inspect [options]
144+
145+
查看 Rslib 项目的 Rsbuild 配置和 Rspack 配置
146+
147+
Options:
148+
-c --config <config> 指定配置文件路径,可以为相对路径或绝对路径
149+
-r --root <root> 指定项目根目录,可以是绝对路径或者相对于 cwd 的路径
150+
--env-mode <mode> 指定 env 模式来加载 `.env.[mode]` 文件
151+
--env-dir <dir> 指定目录来加载 `.env` 文件
152+
--lib <id> 检查指定的库(可以重复)
153+
--output <output> 指定检查内容输出路径(默认:".rsbuild")
154+
--verbose 在输出中显示完整的函数定义
155+
-h, --help 显示命令帮助
156+
```
157+
158+
当你在项目根目录下执行命令 `npx rsbuild inspect` 后,会在项目的 `dist/.rsbuild` 目录生成以下文件:
159+
160+
- `rsbuild.config.mjs`: 表示在构建时使用的 Rsbuild 配置。
161+
- `rspack.config.web.mjs`: 表示在构建时使用的 Rspack 配置。
162+
163+
```text
164+
➜ npx rslib inspect
165+
166+
Inspect config succeed, open following files to view the content:
167+
168+
- Rsbuild Config: /project/dist/.rsbuild/rsbuild.config.mjs
169+
- Rspack Config (esm): /project/dist/.rsbuild/rspack.config.esm.mjs
170+
```
171+
172+
### 完整内容
173+
174+
默认情况下,inspect 命令会省略配置对象中的函数内容,你可以添加 `--verbose` 选项来输出函数的完整内容:
175+
176+
```bash
177+
rslib inspect --verbose
178+
```
179+
180+
### 多种输出格式
181+
182+
如果当前项目同时有多种输出格式,比如 ESM 产物和 CJS 产物,那么会在 `dist/.rsbuild` 目录生成多份 Rspack 配置文件。
183+
184+
```text
185+
➜ npx rslib inspect
186+
187+
Inspect config succeed, open following files to view the content:
188+
189+
- Rsbuild Config (esm): /project/dist/.rsbuild/rsbuild.config.esm.mjs
190+
- Rsbuild Config (cjs): /project/dist/.rsbuild/rsbuild.config.cjs.mjs
191+
- Rspack Config (esm): /project/dist/.rsbuild/rspack.config.esm.mjs
192+
- Rspack Config (cjs): /project/dist/.rsbuild/rspack.config.cjs.mjs
193+
```
194+
195+
## rslib mf dev
196+
197+
`rslib mf dev` 命令用于为 [Module Federation](/guide/advanced/module-federation) 格式的库启用 Rsbuild 开发服务器。
198+
199+
这允许你在 host 应用中访问和调试 mf 格式的模块。
200+
201+
```text
202+
Usage: rslib mf [options] <dev>
203+
204+
为 Module Federation 格式的库启用 Rsbuild 开发服务器。
205+
206+
Options:
207+
-c --config <config> 指定配置文件路径,可以为相对路径或绝对路径
208+
-r --root <root> 指定项目根目录,可以是绝对路径或者相对于 cwd 的路径
209+
--env-mode <mode> 指定 env 模式来加载 `.env.[mode]` 文件
210+
--env-dir <dir> 指定目录来加载 `.env` 文件
211+
-h, --help 显示命令帮助
212+
```
213+
214+
## 公共选项
215+
216+
### 过滤器
217+
218+
Rslib 提供了 `--lib` 选项来为指定的库运行命令。
219+
220+
```bash
221+
# 仅构建 id 为 `esm` 的库
222+
npx rslib build --lib esm
223+
```
224+
225+
`--lib` 选项可以重复使用来指定多个库。
226+
227+
```bash
228+
# 构建 id 为 `esm` 和 `cjs` 的库
229+
npx rslib build --lib esm --lib cjs
230+
```
231+
232+
> 请查看 [lib.id](/config/lib/id) 了解如何获取或设置库的 ID。

0 commit comments

Comments
 (0)