Skip to content

Commit ea12ee8

Browse files
authored
docs: distinguish watch mode in configuration files (#1265)
1 parent 6ee1edc commit ea12ee8

File tree

4 files changed

+58
-20
lines changed

4 files changed

+58
-20
lines changed

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,33 @@ Options:
5151

5252
### Environment variables
5353

54-
Rslib supports injecting env variables or expressions into the code during build, which is helpful for distinguishing the running environment or replacing constants.
55-
56-
You can see more details in [Rsbuild - Environment variables](https://rsbuild.rs/guide/advanced/env-vars).
54+
Rslib supports injecting environment variables or expressions into the code during the build, which is helpful for distinguishing running environments or replacing constants. You can see more details in [Rsbuild - Environment variables](https://rsbuild.rs/guide/advanced/env-vars).
55+
56+
By default, Rslib sets the `process.env.NODE_ENV` environment variable, which is always `'production'` during the build. If you need to distinguish watch mode to dynamically set different configurations, you can set as follows:
57+
58+
```ts title="rslib.config.ts"
59+
import { defineConfig } from '@rslib/core';
60+
61+
const isWatch = process.argv.includes('--watch');
62+
63+
export default defineConfig({
64+
lib: [
65+
{
66+
format: 'esm',
67+
},
68+
],
69+
source: {
70+
alias: {
71+
'@request': isWatch ? './src/request.dev.js' : './src/request.prod.js',
72+
},
73+
},
74+
});
75+
```
5776

5877
::: note
5978

60-
- If [format](/config/lib/format) is `esm` or `cjs`, `process.env.NODE_ENV` will be preserved in the build output.
61-
- 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.
79+
- If [format](/config/lib/format) is `esm` or `cjs`, `process.env.NODE_ENV` in source code will be preserved in the build output.
80+
- If [format](/config/lib/format) is `mf` or `umd`, `process.env.NODE_ENV` in source code will be replaced to ensure that the output can run in the browser.
6281

6382
:::
6483

website/docs/en/guide/basic/configure-rslib.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ When using Node.js's native loader, please note the following limitations:
168168
169169
## Using environment variables
170170

171-
In the configuration file, you can use Node.js environment variables such as `process.env.NODE_ENV` to dynamically set different configurations:
171+
In the configuration file, you can use Node.js environment variables to dynamically set different configurations:
172172

173173
```ts title="rslib.config.ts"
174174
import { defineConfig } from '@rslib/core';
@@ -181,10 +181,10 @@ export default defineConfig({
181181
],
182182
source: {
183183
alias: {
184-
'@request':
185-
process.env.NODE_ENV === 'development'
186-
? './src/request.dev.js'
187-
: './src/request.prod.js',
184+
'@language':
185+
process.env.LANGUAGE === 'en'
186+
? './src/language/en.js'
187+
: './src/language/zh.js',
188188
},
189189
},
190190
});

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,33 @@ Options:
5151

5252
### 环境变量
5353

54-
Rslib 支持在构建过程中向代码中注入环境变量或表达式,这对于区分运行环境、替换常量值等场景很有帮助。
55-
56-
你可以查看 [Rsbuild - 环境变量](https://rsbuild.rs/zh/guide/advanced/env-vars) 了解更多详细信息。
54+
Rslib 支持在构建过程中向代码中注入环境变量或表达式,这对于区分运行环境、替换常量值等场景很有帮助。你可以查看 [Rsbuild - 环境变量](https://rsbuild.rs/zh/guide/advanced/env-vars) 了解更多详细信息。
55+
56+
默认情况下,Rslib 会自动设置 `process.env.NODE_ENV` 环境变量,在 build 时始终为 `'production'`。如果你需要区分 watch 模式来动态写入不同的配置,可以如下:
57+
58+
```ts title="rslib.config.ts"
59+
import { defineConfig } from '@rslib/core';
60+
61+
const isWatch = process.argv.includes('--watch');
62+
63+
export default defineConfig({
64+
lib: [
65+
{
66+
format: 'esm',
67+
},
68+
],
69+
source: {
70+
alias: {
71+
'@request': isWatch ? './src/request.dev.js' : './src/request.prod.js',
72+
},
73+
},
74+
});
75+
```
5776

5877
::: note
5978

60-
-[format](/config/lib/format) 设置为 `esm``cjs` 时,`process.env.NODE_ENV` 会在构建产物中被保留。
61-
-[format](/config/lib/format) 设置为 `mf``umd` 时,`process.env.NODE_ENV` 将被替换,以确保构建产物可以在浏览器中运行。
79+
-[format](/config/lib/format) 设置为 `esm``cjs` 时,源代码中的 `process.env.NODE_ENV` 会在构建产物中被保留。
80+
-[format](/config/lib/format) 设置为 `mf``umd` 时,源代码中的 `process.env.NODE_ENV` 将被替换,以确保构建产物可以在浏览器中运行。
6281

6382
:::
6483

website/docs/zh/guide/basic/configure-rslib.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Rslib 提供了三种配置文件加载方式:
168168
169169
## 使用环境变量
170170

171-
在配置文件中,你可以使用 `process.env.NODE_ENV`Node.js 环境变量,来动态写入不同的配置:
171+
在配置文件中,你可以使用 Node.js 环境变量,来动态写入不同的配置:
172172

173173
```ts title="rslib.config.ts"
174174
import { defineConfig } from '@rslib/core';
@@ -181,10 +181,10 @@ export default defineConfig({
181181
],
182182
source: {
183183
alias: {
184-
'@request':
185-
process.env.NODE_ENV === 'development'
186-
? './src/request.dev.js'
187-
: './src/request.prod.js',
184+
'@language':
185+
process.env.LANGUAGE === 'en'
186+
? './src/language/en.js'
187+
: './src/language/zh.js',
188188
},
189189
},
190190
});

0 commit comments

Comments
 (0)