@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 +42 -2
- package/dist/index.cjs +28 -9
- package/dist/index.d.ts +44 -4
- package/dist/index.js +28 -9
- package/package.json +1 -1
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
|
-
|
|
111
|
-
|
|
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
|
|
116
|
-
result['
|
|
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 (
|
|
119
|
-
|
|
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
|
|
123
|
-
result['
|
|
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
|
-
|
|
4
|
-
|
|
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:
|
|
12
|
-
*
|
|
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
|
-
|
|
74
|
-
|
|
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
|
|
79
|
-
result['
|
|
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 (
|
|
82
|
-
|
|
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
|
|
86
|
-
result['
|
|
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
|
};
|