Skip to content

Commit 29d7220

Browse files
authored
docs(config): improve node options docs (#12236)
* docs(config): improve `node.global` docs * docs: update * docs
1 parent 510e054 commit 29d7220

File tree

4 files changed

+95
-21
lines changed

4 files changed

+95
-21
lines changed

website/docs/en/config/mode.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import WebpackLicense from '@components/WebpackLicense';
22
import PropertyType from '@components/PropertyType';
33

4-
<WebpackLicense from="https://webpack.js.org/configuration/node/" />
4+
<WebpackLicense from="https://webpack.js.org/configuration/mode/" />
55

66
# Mode
77

website/docs/en/config/node.mdx

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,37 @@ The following Node.js options configure whether to polyfill or mock certain [Nod
88

99
## node.global
1010

11-
- **Type:** `boolean` `'warn'`
11+
- **Type:** `boolean | 'warn'`
1212
- **Default:** `'warn'`
1313

14+
Controls whether Rspack should provide a polyfill for the Node.js `global` object when bundling for non-Node environments.
15+
1416
See [the Node.js documentation](https://nodejs.org/api/globals.html#globals_global) for the exact behavior of this object.
1517

16-
Options:
18+
Optional values:
19+
20+
- `true`: Rspack injects a polyfill so that `global` is available in the bundle. This ensures compatibility with code that relies on Node.js globals in non-Node runtimes.
21+
- `false`: No polyfill is provided. References to `global` remain untouched. If your target environment does not define `global`, accessing it will throw a `ReferenceError` at runtime.
22+
- `'warn'`: Inject a polyfill like `true`, but also emit a warning when `global` is used.
23+
24+
For example, to disable `global` polyfill:
1725

18-
- `true`: Provide a polyfill.
19-
- `false`: Provide nothing. Code that expects this object may crash with a `ReferenceError`.
20-
- `'warn'`: Show a warning when using `global`.
26+
```js title="rspack.config.mjs"
27+
export default {
28+
node: {
29+
global: false,
30+
},
31+
};
32+
```
2133

2234
## node.\_\_filename
2335

24-
- **Type:** `boolean` `'mock' | 'warn-mock' | 'eval-only'`
25-
- **Default:** `'warn-mock'`, `'node-module'` when `output.module` is enabled
36+
- **Type:** `boolean | 'mock' | 'warn-mock' | 'eval-only'`
37+
- **Default:** `'warn-mock'`, `'node-module'` when [output.module](/config/output#outputmodule) is enabled
38+
39+
Controls how Rspack handles the Node.js [`__filename`](https://nodejs.org/api/modules.html#__filename) variable when bundling for non-Node environments.
2640

27-
Options:
41+
Optional values:
2842

2943
- `true`: The filename of the input file relative to the [`context`](/config/context) option.
3044
- `false`: Rspack won't touch your `__filename` code, which means you have the regular Node.js `__filename` behavior. The filename of the **output** file when run in a Node.js environment.
@@ -33,16 +47,38 @@ Options:
3347
- `'node-module'`: Replace `__filename` in CommonJS modules to `fileURLToPath(import.meta.url)` when `output.module` is enabled.
3448
- `'eval-only'`: Equivalent to `false`.
3549

50+
For example, to leave `__filename` as it is:
51+
52+
```js title="rspack.config.mjs"
53+
export default {
54+
node: {
55+
__filename: false,
56+
},
57+
};
58+
```
59+
3660
## node.\_\_dirname
3761

38-
- **Type:** `boolean` `'mock' | 'warn-mock' | 'eval-only'`
62+
- **Type:** `boolean | 'mock' | 'warn-mock' | 'eval-only'`
3963
- **Default:** `'warn-mock'`, `'node-module'` when `output.module` is enabled
4064

41-
Options:
65+
Controls how Rspack handles the Node.js [`__dirname`](https://nodejs.org/api/modules.html#__dirname) variable when bundling for non-Node environments.
66+
67+
Optional values:
4268

4369
- `true`: The dirname of the **input** file relative to the [`context`](/config/context) option.
4470
- `false`: Rspack won't touch your `__dirname` code, which means you have the regular Node.js `__dirname` behavior. The dirname of the **output** file when run in a Node.js environment.
4571
- `'mock'`: The fixed value `'/'`.
4672
- `'warn-mock'`: Use the fixed value of `'/'` but show a warning.
4773
- `'node-module'`: Replace `__dirname` in CommonJS modules to `fileURLToPath(import.meta.url + "/..")` when `output.module` is enabled.
4874
- `'eval-only'`: Equivalent to `false`.
75+
76+
For example, to leave `__dirname` as it is:
77+
78+
```js title="rspack.config.mjs"
79+
export default {
80+
node: {
81+
__dirname: false,
82+
},
83+
};
84+
```

website/docs/zh/config/mode.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import WebpackLicense from '@components/WebpackLicense';
22
import PropertyType from '@components/PropertyType';
33

4-
<WebpackLicense from="https://webpack.js.org/configuration/node/" />
4+
<WebpackLicense from="https://webpack.js.org/configuration/mode/" />
55

66
# Mode
77

website/docs/zh/config/node.mdx

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,34 @@ import WebpackLicense from '@components/WebpackLicense';
1111
- **类型:** `boolean | 'warn'`
1212
- **默认值:** `'warn'`
1313

14-
选项:
14+
控制 Rspack 在为非 Node 环境打包时,是否需要为 Node.js 的 `global` 对象提供 polyfill。
1515

16-
- `true`:提供 polyfill.
17-
- `false`:不提供任何 polyfill。代码可能会出现 `ReferenceError` 的崩溃。
18-
- `'warn'`:当使用 `global` 时展示一个警告。
16+
关于该对象的具体行为,请参考 [Node.js 文档](https://nodejs.org/api/globals.html#globals_global)
17+
18+
可选值有:
19+
20+
- `true`:Rspack 会注入 polyfill,使打包产物中能够使用 `global`。适用于依赖 Node.js 全局变量、但运行在非 Node 环境的代码。
21+
- `false`:不注入 polyfill。对 `global` 的引用将保持原样。如果目标环境不支持 `global`,运行时会抛出 `ReferenceError`
22+
- `'warn'`:行为与 `true` 相同,会注入 polyfill,同时在使用 `global` 时额外打印警告。
23+
24+
例如,禁用 `global` polyfill:
25+
26+
```js title="rspack.config.mjs"
27+
export default {
28+
node: {
29+
global: false,
30+
},
31+
};
32+
```
1933

2034
## node.\_\_filename
2135

2236
- **类型:** `boolean | 'mock' | 'warn-mock' | 'eval-only'`
23-
- **默认值:** `'warn-mock'`,当启用 `output.module` 时为 `'node-module'`
37+
- **默认值:** `'warn-mock'`,当启用 [output.module](/config/output#outputmodule) 时为 `'node-module'`
38+
39+
控制 Rspack 在为非 Node 环境打包时,如何处理 Node.js 的 [`__filename`](https://nodejs.org/api/modules.html#__filename) 变量。
2440

25-
选项
41+
可选值有
2642

2743
- `true`:输入文件的文件名,是相对于 [`context`](/config/context) 选项。
2844
- `false`:Rspack 不会更改 `__filename` 的代码。在 Node.js 环境中运行时输出文件的文件名。
@@ -31,16 +47,38 @@ import WebpackLicense from '@components/WebpackLicense';
3147
- `'node-module'`:当 `output.module` 启用时,将 CommonJS 模块中的 `__filename` 替换为 `fileURLToPath(import.meta.url)`
3248
- `'eval-only'`:等同于 `false`
3349

50+
例如,不对 `__filename` 进行任何处理:
51+
52+
```js title="rspack.config.mjs"
53+
export default {
54+
node: {
55+
__filename: false,
56+
},
57+
};
58+
```
59+
3460
## node.\_\_dirname
3561

3662
- **类型:** `boolean | 'mock' | 'warn-mock' | 'eval-only'`
37-
- **默认值:** `'warn-mock'`,当启用 `output.module` 时为 `'node-module'`
63+
- **默认值:** `'warn-mock'`,当启用 [output.module](/config/output#outputmodule) 时为 `'node-module'`
64+
65+
控制 Rspack 在为非 Node 环境打包时,如何处理 Node.js 的 [`__dirname`](https://nodejs.org/api/modules.html#__dirname) 变量。
3866

39-
选项
67+
可选值有
4068

4169
- `true`**输入** 文件的目录名,是相对于 [`context`](/config/context) 选项。
42-
- `false`webpack 不会更改 `__dirname` 的代码,这意味着你有常规 Node.js 中的 `__dirname` 的行为。在 Node.js 环境中运行时,**输出** 文件的目录名。
70+
- `false`Rspack 不会更改 `__dirname` 的代码,这意味着你有常规 Node.js 中的 `__dirname` 的行为。在 Node.js 环境中运行时,**输出** 文件的目录名。
4371
- `'mock'`:value 填充为 `'/'`
4472
- `'warn-mock'`:使用 `'/'` 但是会显示一个警告。
4573
- `'node-module'`: 当启用 `output.module` 时,将 CommonJS 模块中的 `__dirname` 替换为 `fileURLToPath(import.meta.url + "/..")`
4674
- `'eval-only'`:等同于 `false`
75+
76+
例如,不对 `__dirname` 进行任何处理:
77+
78+
```js title="rspack.config.mjs"
79+
export default {
80+
node: {
81+
__dirname: false,
82+
},
83+
};
84+
```

0 commit comments

Comments
 (0)