Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions website/docs/en/guide/advanced/env-vars.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,28 @@ We recommend using `--env-mode` to set the env mode instead of modifying `proces

:::

#### Accessing in client code

By default, Rsbuild does not inject the env mode into client code. You can manually define a global identifier using [source.define](#using-define) to make it available in the client code:

```js title="rsbuild.config.js"
export default ({ envMode }) => ({
source: {
define: {
ENV_MODE: JSON.stringify(envMode),
},
},
});
```

Then you can access it in client code:

```js title="src/index.js"
if (ENV_MODE === 'my-mode') {
// ...
}
```

### Env directory

By default, the `.env` file is located in the root directory of the project. You can specify the env directory by using the `--env-dir <dir>` option in the CLI.
Expand Down
22 changes: 22 additions & 0 deletions website/docs/zh/guide/advanced/env-vars.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,28 @@ Rsbuild 会按照以下顺序读取这些文件,并合并它们的内容。如

:::

#### 在 client 代码中访问

Rsbuild 默认不会将 Env 模式注入到 client 代码中,但你可以通过 [source.define](#使用-define) 手动定义一个全局标识符,使其在 client 代码中可用:

```js title="rsbuild.config.js"
export default ({ envMode }) => ({
source: {
define: {
ENV_MODE: JSON.stringify(envMode),
},
},
});
```

在 client 代码中即可访问该值:

```js title="src/index.js"
if (ENV_MODE === 'my-mode') {
// ...
}
```

### Env 目录

默认情况下,`.env` 文件位于项目的根目录。你可以通过 CLI 的 `--env-dir <dir>` 选项来指定 env 目录。
Expand Down
Loading