Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add explanation to .env #3993

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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.
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved

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';

// Here publicVars is the PUBLIC_ prefixed variable read by default.
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved
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`代码中
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved

你可以通过 `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_ 前缀的变量
chenjiahan marked this conversation as resolved.
Show resolved Hide resolved
const { parsed, publicVars } = loadEnv();

const mergedConfig = mergeRsbuildConfig(
Expand Down
Loading