Skip to content

Commit a2facf0

Browse files
committed
docs: clarify import.meta.env define behavior
1 parent 6aea470 commit a2facf0

3 files changed

Lines changed: 13 additions & 10 deletions

File tree

packages/rspack/src/builtin-plugin/DefinePlugin.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import WebpackError from '../lib/WebpackError';
66
const IMPORT_META_ENV_KEY = 'import.meta.env';
77
const IGNORED_IMPORT_META_ENV = Symbol('ignored import.meta.env');
88

9-
export type DefinePluginOptions = Record<string, CodeValue> & {
10-
'import.meta.env'?: ImportMetaEnvCodeValue;
11-
};
9+
export type DefinePluginOptions = Record<string, CodeValue>;
1210
export const DefinePlugin = create(
1311
BuiltinPluginName.DefinePlugin,
1412
function (define: DefinePluginOptions): NormalizedCodeValue {
@@ -155,7 +153,6 @@ const isImportMetaEnvObject = (
155153
!(value instanceof RegExp);
156154

157155
type CodeValue = RecursiveArrayOrRecord<CodeValuePrimitive>;
158-
type ImportMetaEnvCodeValue = string | Record<string, CodeValue>;
159156
type CodeValuePrimitive =
160157
null | undefined | RegExp | Function | string | number | boolean | bigint;
161158
type NormalizedCodeValuePrimitive = null | string | number | boolean;

website/docs/en/plugins/webpack/define-plugin.mdx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ Rspack supports defining `import.meta.env` so that environment variables are rea
170170

171171
```js title="rspack.config.mjs"
172172
export default {
173+
experiments: {
174+
env: true,
175+
},
173176
plugins: [
174177
new rspack.DefinePlugin({
175178
'import.meta.env': {
@@ -187,7 +190,7 @@ console.log(import.meta.env.MODE); // 'production'
187190
console.log(import.meta.env.NOT_EXIST); // undefined
188191
```
189192
190-
You usually do not write this `DefinePlugin` by hand. [`EnvironmentPlugin`](/plugins/webpack/environment-plugin), `NODE_ENV`, and user `DefinePlugin` `import.meta.env.*` keys are collected into one accumulated env object and exposed as `import.meta.env` automatically. User `DefinePlugin` `process.env.*` keys keep their `process.env` behavior and are not collected into `import.meta.env`.
193+
When `experiments.env` is enabled, you usually do not write this `DefinePlugin` by hand. [`EnvironmentPlugin`](/plugins/webpack/environment-plugin), `NODE_ENV`, and user `DefinePlugin` `import.meta.env.*` keys are collected into one accumulated env object and exposed as `import.meta.env` automatically. User `DefinePlugin` `process.env.*` keys keep their `process.env` behavior and are not collected into `import.meta.env`.
191194
192195
## `process.env`
193196
@@ -208,11 +211,11 @@ When the env object is accumulated (via [`EnvironmentPlugin`](/plugins/webpack/e
208211
209212
### Destructuring
210213
211-
`process.env` destructuring is supported: when every destructured key is a defined env key, the destructuring source is inlined with only those keys.
214+
`process.env` destructuring is supported for corresponding `process.env.KEY` defines. When every destructured key has a matching `process.env.KEY` define, the destructuring source is inlined with only those keys.
212215
213216
```js title="src/index.js"
214217
const { NODE_ENV, MODE } = process.env;
215218
console.log(NODE_ENV, MODE);
216219
```
217220
218-
If any destructured key is not a defined env key, the access falls back to the runtime `process.env`, since it may legitimately hold that key at runtime.
221+
If any destructured key does not have a matching `process.env.KEY` define, the access falls back to the runtime `process.env`, since it may legitimately hold that key at runtime.

website/docs/zh/plugins/webpack/define-plugin.mdx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ Rspack 支持定义 `import.meta.env`,使环境变量可以在 ESM 源码中
170170

171171
```js title="rspack.config.mjs"
172172
export default {
173+
experiments: {
174+
env: true,
175+
},
173176
plugins: [
174177
new rspack.DefinePlugin({
175178
'import.meta.env': {
@@ -187,7 +190,7 @@ console.log(import.meta.env.MODE); // 'production'
187190
console.log(import.meta.env.NOT_EXIST); // undefined
188191
```
189192
190-
通常你不需要手写这个 `DefinePlugin`。[`EnvironmentPlugin`](/plugins/webpack/environment-plugin)、`NODE_ENV` 以及用户 `DefinePlugin` 中的 `import.meta.env.*` 键,会被收集到同一个累积的环境对象中,并自动以 `import.meta.env` 暴露。用户 `DefinePlugin` 中的 `process.env.*` 键会保持 `process.env` 的行为,不会被收集到 `import.meta.env`
193+
启用 `experiments.env` 后,通常你不需要手写这个 `DefinePlugin`。[`EnvironmentPlugin`](/plugins/webpack/environment-plugin)、`NODE_ENV` 以及用户 `DefinePlugin` 中的 `import.meta.env.*` 键,会被收集到同一个累积的环境对象中,并自动以 `import.meta.env` 暴露。用户 `DefinePlugin` 中的 `process.env.*` 键会保持 `process.env` 的行为,不会被收集到 `import.meta.env`
191194
192195
## `process.env`
193196
@@ -208,11 +211,11 @@ console.log(process.env.CUSTOM); // 运行时从运行时的 process.env 解析
208211
209212
### 解构
210213
211-
`process.env` 支持解构:当解构的每个 key 都是已定义的环境变量 key 时,解构源会被内联为仅含这些 key 的对象。
214+
`process.env` 支持对对应的 `process.env.KEY` define 生效的解构:当解构的每个 key 都有匹配的 `process.env.KEY` define 时,解构源会被内联为仅含这些 key 的对象。
212215
213216
```js title="src/index.js"
214217
const { NODE_ENV, MODE } = process.env;
215218
console.log(NODE_ENV, MODE);
216219
```
217220
218-
如果解构的某个 key 不是已定义的环境变量 key,则该访问会回退到运行时的 `process.env`,因为运行时它可能确实持有该 key。
221+
如果解构的某个 key 没有匹配的 `process.env.KEY` define,则该访问会回退到运行时的 `process.env`,因为运行时它可能确实持有该 key。

0 commit comments

Comments
 (0)