@rsbuild/plugin-node-polyfill 1.3.2 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -130,14 +130,54 @@ Used to specify whether to inject polyfills for global variables.
130
130
  - **Type:**
131
131
 
132
132
  ```ts
133
+ type GlobalsOption = {
134
+ /**
135
+ * Whether to inject the polyfill for direct identifier usage,
136
+ * e.g. `Buffer` or `process` (bare usage without any object prefix).
137
+ * @default true
138
+ */
139
+ bare?: boolean;
140
+ /**
141
+ * Whether to inject the polyfill for usage via the `global` object,
142
+ * e.g. `global.Buffer` or `global.process`.
143
+ * @default false
144
+ */
145
+ global?: boolean;
146
+ /**
147
+ * Whether to inject the polyfill for usage via the `globalThis` object,
148
+ * e.g. `globalThis.Buffer` or `globalThis.process`.
149
+ * @default false
150
+ */
151
+ globalThis?: boolean;
152
+ };
153
+
133
154
  type Globals = {
134
- process?: boolean;
135
- Buffer?: boolean;
155
+ process?: boolean | GlobalsOption;
156
+ Buffer?: boolean | GlobalsOption;
136
157
  };
137
158
  ```
138
159
 
139
160
  - **Default:**
140
161
 
162
+ ```ts
163
+ const defaultGlobals = {
164
+ Buffer: {
165
+ bare: true,
166
+ global: false,
167
+ globalThis: false,
168
+ },
169
+ process: {
170
+ bare: true,
171
+ global: false,
172
+ globalThis: false,
173
+ },
174
+ };
175
+ ```
176
+
177
+ ### protocolImports
178
+
179
+ Whether to polyfill Node.js builtin modules starting with `node:`.
180
+
141
181
  ```ts
142
182
  const defaultGlobals = {
143
183
  Buffer: true,
package/dist/index.cjs CHANGED
@@ -105,22 +105,41 @@ const getResolveFallback = ({ protocolImports, exclude, include, overrides })=>{
105
105
  }
106
106
  return fallback;
107
107
  };
108
- const getProvideGlobals = async (globals, overrides)=>{
108
+ const getProvideGlobals = async (globals = {}, overrides)=>{
109
109
  const result = {};
110
- if ((null == globals ? void 0 : globals.Buffer) !== false) {
111
- result.Buffer = [
110
+ const defaultOptions = {
111
+ bare: true,
112
+ global: false,
113
+ globalThis: false
114
+ };
115
+ if (false !== globals.Buffer) {
116
+ const options = 'object' == typeof globals.Buffer ? {
117
+ ...defaultOptions,
118
+ ...globals.Buffer
119
+ } : {
120
+ ...defaultOptions
121
+ };
122
+ const value = [
112
123
  resolvePolyfill('buffer', overrides),
113
124
  'Buffer'
114
125
  ];
115
- result['global.Buffer'] = result.Buffer;
116
- result['globalThis.Buffer'] = result.Buffer;
126
+ if (options.bare) result.Buffer = value;
127
+ if (options.global) result['global.Buffer'] = value;
128
+ if (options.globalThis) result['globalThis.Buffer'] = value;
117
129
  }
118
- if ((null == globals ? void 0 : globals.process) !== false) {
119
- result.process = [
130
+ if (false !== globals.process) {
131
+ const options = 'object' == typeof globals.process ? {
132
+ ...defaultOptions,
133
+ ...globals.process
134
+ } : {
135
+ ...defaultOptions
136
+ };
137
+ const value = [
120
138
  resolvePolyfill('process', overrides)
121
139
  ];
122
- result['global.process'] = result.process;
123
- result['globalThis.process'] = result.process;
140
+ if (options.bare) result.process = value;
141
+ if (options.global) result['global.process'] = value;
142
+ if (options.globalThis) result['globalThis.process'] = value;
124
143
  }
125
144
  return result;
126
145
  };
package/dist/index.d.ts CHANGED
@@ -1,15 +1,55 @@
1
1
  import type { RsbuildPlugin } from '@rsbuild/core';
2
+ type GlobalsOption = {
3
+ /**
4
+ * Whether to inject the polyfill for direct identifier usage,
5
+ * e.g. `Buffer` or `process` (bare usage without any object prefix).
6
+ * @default true
7
+ */
8
+ bare?: boolean;
9
+ /**
10
+ * Whether to inject the polyfill for usage via the `global` object,
11
+ * e.g. `global.Buffer` or `global.process`.
12
+ * @default false
13
+ */
14
+ global?: boolean;
15
+ /**
16
+ * Whether to inject the polyfill for usage via the `globalThis` object,
17
+ * e.g. `globalThis.Buffer` or `globalThis.process`.
18
+ * @default false
19
+ */
20
+ globalThis?: boolean;
21
+ };
2
22
  type Globals = {
3
- process?: boolean;
4
- Buffer?: boolean;
23
+ /**
24
+ * Configure polyfill injection for the `process` global variable.
25
+ * - Set to `false` to disable all polyfills for `process`.
26
+ * - Set to `true` to enable only bare identifier polyfill (e.g. `process`).
27
+ * - Or provide a {@link GlobalsOption} object for fine-grained control.
28
+ */
29
+ process?: boolean | GlobalsOption;
30
+ /**
31
+ * Configure polyfill injection for the `Buffer` global variable.
32
+ * - Set to `false` to disable all polyfills for `Buffer`.
33
+ * - Set to `true` to enable only bare identifier polyfill (e.g. `Buffer`).
34
+ * - Or provide a {@link GlobalsOption} object for fine-grained control.
35
+ */
36
+ Buffer?: boolean | GlobalsOption;
5
37
  };
6
38
  export type PluginNodePolyfillOptions = {
7
39
  /**
8
40
  * Whether to provide polyfill of globals.
9
41
  * @default
10
42
  * {
11
- * Buffer: true,
12
- * process: true,
43
+ * Buffer: {
44
+ * bare: true,
45
+ * global: false,
46
+ * globalThis: false,
47
+ * }
48
+ * process: {
49
+ * bare: true,
50
+ * global: false,
51
+ * globalThis: false,
52
+ * }
13
53
  * }
14
54
  */
15
55
  globals?: Globals;
package/dist/index.js CHANGED
@@ -68,22 +68,41 @@ const getResolveFallback = ({ protocolImports, exclude, include, overrides })=>{
68
68
  }
69
69
  return fallback;
70
70
  };
71
- const getProvideGlobals = async (globals, overrides)=>{
71
+ const getProvideGlobals = async (globals = {}, overrides)=>{
72
72
  const result = {};
73
- if ((null == globals ? void 0 : globals.Buffer) !== false) {
74
- result.Buffer = [
73
+ const defaultOptions = {
74
+ bare: true,
75
+ global: false,
76
+ globalThis: false
77
+ };
78
+ if (false !== globals.Buffer) {
79
+ const options = 'object' == typeof globals.Buffer ? {
80
+ ...defaultOptions,
81
+ ...globals.Buffer
82
+ } : {
83
+ ...defaultOptions
84
+ };
85
+ const value = [
75
86
  resolvePolyfill('buffer', overrides),
76
87
  'Buffer'
77
88
  ];
78
- result['global.Buffer'] = result.Buffer;
79
- result['globalThis.Buffer'] = result.Buffer;
89
+ if (options.bare) result.Buffer = value;
90
+ if (options.global) result['global.Buffer'] = value;
91
+ if (options.globalThis) result['globalThis.Buffer'] = value;
80
92
  }
81
- if ((null == globals ? void 0 : globals.process) !== false) {
82
- result.process = [
93
+ if (false !== globals.process) {
94
+ const options = 'object' == typeof globals.process ? {
95
+ ...defaultOptions,
96
+ ...globals.process
97
+ } : {
98
+ ...defaultOptions
99
+ };
100
+ const value = [
83
101
  resolvePolyfill('process', overrides)
84
102
  ];
85
- result['global.process'] = result.process;
86
- result['globalThis.process'] = result.process;
103
+ if (options.bare) result.process = value;
104
+ if (options.global) result['global.process'] = value;
105
+ if (options.globalThis) result['globalThis.process'] = value;
87
106
  }
88
107
  return result;
89
108
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-node-polyfill",
3
- "version": "1.3.2",
3
+ "version": "1.4.0",
4
4
  "repository": "https://github.com/rspack-contrib/rsbuild-plugin-node-polyfill",
5
5
  "license": "MIT",
6
6
  "type": "module",