You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/en/config/node.mdx
+62-24Lines changed: 62 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,11 +15,13 @@ Controls whether Rspack should provide a polyfill for the Node.js `global` objec
15
15
16
16
See [the Node.js documentation](https://nodejs.org/api/globals.html#globals_global) for the exact behavior of this object.
17
17
18
-
Optional values:
18
+
### Available values
19
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.
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
-**Default:**`'warn-mock'`, `'node-module'` when [output.module](/config/output#outputmodule) is enabled
39
+
-**Default:**
40
+
- If `target` does not include `node`, defaults to `'warn-mock'`.
41
+
- If `target` includes `node`, uses `'node-module'` if [output.module](/config/output#outputmodule) is enabled, otherwise `'eval-only'`.
42
+
43
+
Controls how Rspack handles the Node.js [`__filename`](https://nodejs.org/api/modules.html#__filename) and [`import.meta.filename`](https://nodejs.org/api/esm.html#importmetafilename) variables when bundling for non-Node environments.
38
44
39
-
Controls how Rspack handles the Node.js [`__filename`](https://nodejs.org/api/modules.html#__filename) variable when bundling for non-Node environments.
45
+
### Available values
40
46
41
-
Optional values:
47
+
-`true`: Replace with the source file path, relative to the [`context`](/config/context) option
48
+
-`false`: Do nothing and keep the native behavior
49
+
-`'mock'`: Replace with `/index.js`
50
+
-`'mock'`: Replace with `'/index.js'`
51
+
-`'warn-mock'`: Replace with `'/index.js'`, and emit a warning to indicate a potential Node.js dependency in the code
52
+
-`'node-module'`: Only used when [output.module](/config/output#outputmodule) is enabled. Replace `__filename` in CommonJS with an equivalent implementation based on `import.meta.url`, suitable for ESM output
42
53
43
-
-`true`: The filename of the input file relative to the [`context`](/config/context) option.
44
-
-`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.
45
-
-`'mock'`: The fixed value `'/index.js'`.
46
-
-`'warn-mock'`: Use the fixed value of `'/index.js'` but show a warning.
47
-
-`'node-module'`: Replace `__filename` in CommonJS modules to `fileURLToPath(import.meta.url)` when `output.module` is enabled.
48
-
-`'eval-only'`: Equivalent to `false`.
54
+
### Examples
49
55
50
-
For example, to leave `__filename` as it is:
56
+
For example, to leave `__filename`and `import.meta.filename`as it is:
51
57
52
58
```js title="rspack.config.mjs"
53
59
exportdefault {
@@ -57,23 +63,41 @@ export default {
57
63
};
58
64
```
59
65
66
+
To replace `__filename` in ESM output with `fileURLToPath(import.meta.url)`:
-**Default:**`'warn-mock'`, `'node-module'` when `output.module` is enabled
83
+
-**Default:**
84
+
- If `target` does not include `node`, defaults to `'warn-mock'`.
85
+
- If `target` includes `node`, uses `'node-module'` if [output.module](/config/output#outputmodule) is enabled, otherwise `'eval-only'`.
64
86
65
-
Controls how Rspack handles the Node.js [`__dirname`](https://nodejs.org/api/modules.html#__dirname)variable when bundling for non-Node environments.
87
+
Controls how Rspack handles the Node.js [`__dirname`](https://nodejs.org/api/modules.html#__dirname)and [`import.meta.dirname`](https://nodejs.org/api/esm.html#importmetadirname) variables when bundling for non-Node environments.
66
88
67
-
Optional values:
89
+
### Available values
68
90
69
-
-`true`: The dirname of the **input** file relative to the [`context`](/config/context) option.
70
-
-`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.
71
-
-`'mock'`: The fixed value `'/'`.
72
-
-`'warn-mock'`: Use the fixed value of `'/'` but show a warning.
73
-
-`'node-module'`: Replace `__dirname` in CommonJS modules to `fileURLToPath(import.meta.url + "/..")` when `output.module` is enabled.
74
-
-`'eval-only'`: Equivalent to `false`.
91
+
-`true`: Replace with the directory path of the source file, relative to the [`context`](/config/context) option
92
+
-`false`: Do nothing and keep the native behavior
93
+
-`'mock'`: Replace with `'/'`
94
+
-`'eval-only'`: Equivalent to `false`
95
+
-`'warn-mock'`: Replace with `'/'`, and emit a warning to indicate a potential Node.js dependency in the code
96
+
-`'node-module'`: Only used when [output.module](/config/output#outputmodule) is enabled. Replace `__dirname` in CommonJS with an equivalent implementation based on `import.meta.url`, suitable for ESM output
75
97
76
-
For example, to leave `__dirname` as it is:
98
+
### Examples
99
+
100
+
For example, to leave `__dirname` and `import.meta.dirname` as it is:
77
101
78
102
```js title="rspack.config.mjs"
79
103
exportdefault {
@@ -82,3 +106,17 @@ export default {
82
106
},
83
107
};
84
108
```
109
+
110
+
To replace `__dirname` in ESM output with `fileURLToPath(import.meta.url + "/..")`:
0 commit comments