Skip to content

Commit

Permalink
feat: add rawPublicVars for loadEnv (#3732)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Oct 15, 2024
1 parent 35d5ffd commit c5ca370
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 11 deletions.
6 changes: 6 additions & 0 deletions e2e/cases/javascript-api/load-env/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ rspackOnlyTest('should load env files correctly', () => {
'process.env.REACT_VERSION': '"18"',
});

expect(env.rawPublicVars).toEqual({
REACT_EMPTY_STRING: '',
REACT_NAME: 'react',
REACT_VERSION: '18',
});

expect(env.filePaths.find((item) => item.endsWith('.env'))).toBeTruthy();
expect(
env.filePaths.find((item) => item.endsWith('.env.staging')),
Expand Down
32 changes: 29 additions & 3 deletions packages/core/src/loadEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,36 @@ export function loadEnv({
mode = getNodeEnv(),
prefixes = ['PUBLIC_'],
}: LoadEnvOptions = {}): {
/** All environment variables in the .env file */
/** All env variables in the .env file */
parsed: Record<string, string>;
/** The absolute paths to all env files */
filePaths: string[];
/** Environment variables that start with prefixes */
/**
* Env variables that start with prefixes.
*
* @example
* ```ts
* {
* PUBLIC_FOO: 'bar',
* }
* ```
**/
rawPublicVars: Record<string, string | undefined>;
/**
* Formatted env variables that start with prefixes.
* The keys contain the prefixes `process.env.*` and `import.meta.env.*`.
* The values are processed by `JSON.stringify`.
*
* @example
* ```ts
* {
* 'process.env.PUBLIC_FOO': '"bar"',
* 'import.meta.env.PUBLIC_FOO': '"bar"',
* }
* ```
**/
publicVars: Record<string, string>;
/** Clear the environment variables mounted on `process.env` */
/** Clear the env variables mounted on `process.env` */
cleanup: () => void;
} {
if (mode === 'local') {
Expand Down Expand Up @@ -69,12 +92,14 @@ export function loadEnv({
expand({ parsed });

const publicVars: Record<string, string> = {};
const rawPublicVars: Record<string, string | undefined> = {};

for (const key of Object.keys(process.env)) {
if (prefixes.some((prefix) => key.startsWith(prefix))) {
const val = process.env[key];
publicVars[`import.meta.env.${key}`] = JSON.stringify(val);
publicVars[`process.env.${key}`] = JSON.stringify(val);
rawPublicVars[key] = val;
}
}

Expand Down Expand Up @@ -104,5 +129,6 @@ export function loadEnv({
cleanup,
filePaths,
publicVars,
rawPublicVars,
};
}
33 changes: 28 additions & 5 deletions website/docs/en/api/javascript-api/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Load the `.env` file and return all environment variables starting with the spec

- **Type:**

```ts
````ts
type LoadEnvOptions = {
/**
* The root path to load the env file
Expand All @@ -103,16 +103,39 @@ type LoadEnvOptions = {
};

function loadEnv(options: LoadEnvOptions): {
/** All environment variables in the .env file */
/** All env variables in the .env file */
parsed: Record<string, string>;
/** The absolute paths to all env files */
filePaths: string[];
/** Environment variables that start with prefixes */
/**
* Env variables that start with prefixes.
*
* @example
* ```ts
* {
* PUBLIC_FOO: 'bar',
* }
* ```
**/
rawPublicVars: Record<string, string | undefined>;
/**
* Formatted env variables that start with prefixes.
* The keys contain the prefixes `process.env.*` and `import.meta.env.*`.
* The values are processed by `JSON.stringify`.
*
* @example
* ```ts
* {
* 'process.env.PUBLIC_FOO': '"bar"',
* 'import.meta.env.PUBLIC_FOO': '"bar"',
* }
* ```
**/
publicVars: Record<string, string>;
/** Clear the environment variables mounted on `process.env` */
/** Clear the env variables mounted on `process.env` */
cleanup: () => void;
};
```
````

- **Example:**

Expand Down
29 changes: 26 additions & 3 deletions website/docs/zh/api/javascript-api/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const rsbuild = await createRsbuild({

- **类型:**

```ts
````ts
type LoadEnvOptions = {
/**
* 加载 env 文件的根路径
Expand All @@ -107,12 +107,35 @@ function loadEnv(options: LoadEnvOptions): {
parsed: Record<string, string>;
/** 所有 env 文件的绝对路径 */
filePaths: string[];
/** 以 prefixes 开头的环境变量 */
/**
* 以 prefixes 开头的环境变量
*
* @example
* ```ts
* {
* PUBLIC_FOO: 'bar',
* }
* ```
**/
rawPublicVars: Record<string, string | undefined>;
/**
* 以 prefix 开头的环境变量,并经过格式化。
* key 包含前缀 `process.env.*` 和 `import.meta.env.*`。
* value 经过 `JSON.stringify` 处理。
*
* @example
* ```ts
* {
* 'process.env.PUBLIC_FOO': '"bar"',
* 'import.meta.env.PUBLIC_FOO': '"bar"',
* }
* ```
**/
publicVars: Record<string, string>;
/** 从 `process.env` 上清除挂载的环境变量 */
cleanup: () => void;
};
```
````

- **示例:**

Expand Down

1 comment on commit c5ca370

@9aoy
Copy link
Collaborator

@9aoy 9aoy commented on c5ca370 Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
plugins ✅ success
rspress ✅ success
rslib ✅ success
examples ✅ success

Please sign in to comment.