Skip to content

Commit

Permalink
docs: add explanation to .env (#3993)
Browse files Browse the repository at this point in the history
Co-authored-by: neverland <[email protected]>
  • Loading branch information
hangCode2001 and chenjiahan authored Nov 16, 2024
1 parent f5016e6 commit dc9e4f7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion website/docs/en/guide/advanced/env-vars.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ During code minification, `if (false) { ... }` will be recognized as invalid cod
## `.env` File
When a `.env` file exists in the project root directory, Rsbuild CLI will automatically use [dotenv](https://npmjs.com/package/dotenv) to load these env variables and add them to the current Node.js process.
When a `.env` file exists in the project root directory, Rsbuild CLI will automatically use [dotenv](https://npmjs.com/package/dotenv) to load these env variables and add them to the current Node.js process. The [Public Variables](#public-variables) will be exposed in the client code.
You can access these env variables through `import.meta.env.[name]` or `process.env.[name]`.
Expand Down Expand Up @@ -280,6 +280,7 @@ If you are not using the Rsbuild CLI and are using the Rsbuild [JavaScript API](
```ts
import { loadEnv, mergeRsbuildConfig } from '@rsbuild/core';
// By default, `publicVars` are variables prefixed with `PUBLIC_`
const { parsed, publicVars } = loadEnv();
const mergedConfig = mergeRsbuildConfig(
Expand Down
3 changes: 2 additions & 1 deletion website/docs/zh/guide/advanced/env-vars.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ if (false) {
## `.env` 文件
当项目根目录存在 `.env` 文件时,Rsbuild CLI 会自动使用 [dotenv](https://npmjs.com/package/dotenv) 来加载这些环境变量,并添加到当前 Node.js 进程中。
当项目根目录存在 `.env` 文件时,Rsbuild CLI 会自动使用 [dotenv](https://npmjs.com/package/dotenv) 来加载这些环境变量,并添加到当前 Node.js 进程中,其中的 [public 变量](#public-变量) 会被暴露在 `client` 代码中
你可以通过 `import.meta.env.[name]``process.env.[name]` 来访问这些环境变量。
Expand Down Expand Up @@ -280,6 +280,7 @@ console.log(process.env.BAR); // '2'
```ts
import { loadEnv, mergeRsbuildConfig } from '@rsbuild/core';
// 默认情况下,`publicVars` 是以 `PUBLIC_` 为前缀的变量
const { parsed, publicVars } = loadEnv();
const mergedConfig = mergeRsbuildConfig(
Expand Down

0 comments on commit dc9e4f7

Please sign in to comment.