@rsbuild/plugin-node-polyfill 1.1.0 → 1.2.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
@@ -28,11 +28,11 @@ Add plugin to your `rsbuild.config.ts`:
28
28
 
29
29
  ```ts
30
30
  // rsbuild.config.ts
31
- import { pluginNodePolyfill } from "@rsbuild/plugin-node-polyfill";
31
+ import { pluginNodePolyfill } from '@rsbuild/plugin-node-polyfill'
32
32
 
33
33
  export default {
34
34
  plugins: [pluginNodePolyfill()],
35
- };
35
+ }
36
36
  ```
37
37
 
38
38
  ## Node Polyfills
@@ -47,7 +47,7 @@ When you use the above global variables in your code, the corresponding polyfill
47
47
  For instance, the following code would inject the `Buffer` polyfill:
48
48
 
49
49
  ```ts
50
- const bufferData = Buffer.from("abc");
50
+ const bufferData = Buffer.from('abc')
51
51
  ```
52
52
 
53
53
  You can disable this behavior through the `globals` option of the plugin:
@@ -58,7 +58,7 @@ pluginNodePolyfill({
58
58
  Buffer: false,
59
59
  process: false,
60
60
  },
61
- });
61
+ })
62
62
  ```
63
63
 
64
64
  ### Modules
@@ -95,9 +95,9 @@ pluginNodePolyfill({
95
95
  When the above module is referenced in code via import / require syntax, the corresponding polyfill will be injected.
96
96
 
97
97
  ```ts
98
- import { Buffer } from "buffer";
98
+ import { Buffer } from 'buffer'
99
99
 
100
- const bufferData = Buffer.from("abc");
100
+ const bufferData = Buffer.from('abc')
101
101
  ```
102
102
 
103
103
  ### Fallbacks
@@ -116,9 +116,9 @@ const bufferData = Buffer.from("abc");
116
116
  Currently there is no polyfill for the above modules on the browser side, so when you import the above modules, it will automatically fallback to an empty object.
117
117
 
118
118
  ```ts
119
- import fs from "fs";
119
+ import fs from 'fs'
120
120
 
121
- console.log(fs); // -> {}
121
+ console.log(fs) // -> {}
122
122
  ```
123
123
 
124
124
  ## Options
@@ -131,9 +131,9 @@ Used to specify whether to inject polyfills for global variables.
131
131
 
132
132
  ```ts
133
133
  type Globals = {
134
- process?: boolean;
135
- Buffer?: boolean;
136
- };
134
+ process?: boolean
135
+ Buffer?: boolean
136
+ }
137
137
  ```
138
138
 
139
139
  - **Default:**
@@ -142,7 +142,7 @@ type Globals = {
142
142
  const defaultGlobals = {
143
143
  Buffer: true,
144
144
  process: true,
145
- };
145
+ }
146
146
  ```
147
147
 
148
148
  ### protocolImports
@@ -157,7 +157,7 @@ For example, if you disable `protocolImports`, modules such as `node:path`, `nod
157
157
  ```ts
158
158
  pluginNodePolyfill({
159
159
  protocolImports: false,
160
- });
160
+ })
161
161
  ```
162
162
 
163
163
  ### include
@@ -169,8 +169,8 @@ Specify an array of modules for which polyfills should be injected. If this opti
169
169
 
170
170
  ```ts
171
171
  pluginNodePolyfill({
172
- include: ["buffer", "crypto"], // Only "buffer" and "crypto" modules will be polyfilled.
173
- });
172
+ include: ['buffer', 'crypto'], // Only "buffer" and "crypto" modules will be polyfilled.
173
+ })
174
174
  ```
175
175
 
176
176
  ### exclude
@@ -182,10 +182,30 @@ Specify an array of modules for which polyfills should not be injected from the
182
182
 
183
183
  ```ts
184
184
  pluginNodePolyfill({
185
- exclude: ["http", "https"], // All modules except "http" and "https" will be polyfilled.
186
- });
185
+ exclude: ['http', 'https'], // All modules except "http" and "https" will be polyfilled.
186
+ })
187
187
  ```
188
188
 
189
+ ### overrides
190
+
191
+ Override the default polyfills for specific modules.
192
+
193
+ - **Type:** `Record<string, string>`
194
+ - **Default:** `{}`
195
+
196
+ ```ts
197
+ pluginNodePolyfill({
198
+ overrides: {
199
+ fs: 'memfs',
200
+ },
201
+ })
202
+ ```
203
+
204
+ ## Exported variables
205
+
206
+ - `builtinMappingResolved`: A map of Node.js builtin modules to their resolved corresponding polyfills modules.
207
+ - `resolvedPolyfillToModules`: A map of resolved polyfill modules to the polyfill modules before resolving.
208
+
189
209
  ## License
190
210
 
191
211
  [MIT](./LICENSE).
package/dist/index.cjs CHANGED
@@ -21,9 +21,12 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
23
  PLUGIN_NODE_POLYFILL_NAME: () => PLUGIN_NODE_POLYFILL_NAME,
24
+ builtinMappingResolved: () => builtinMappingResolved,
24
25
  getProvideGlobals: () => getProvideGlobals,
25
26
  getResolveFallback: () => getResolveFallback,
26
- pluginNodePolyfill: () => pluginNodePolyfill
27
+ pluginNodePolyfill: () => pluginNodePolyfill,
28
+ resolvePolyfill: () => resolvePolyfill,
29
+ resolvedPolyfillToModules: () => resolvedPolyfillToModules
27
30
  });
28
31
  module.exports = __toCommonJS(src_exports);
29
32
 
@@ -51,113 +54,78 @@ var ProtocolImportsPlugin = class {
51
54
  };
52
55
 
53
56
  // src/libs.ts
54
- var libs_exports = {};
55
- __export(libs_exports, {
56
- _stream_duplex: () => _stream_duplex,
57
- _stream_passthrough: () => _stream_passthrough,
58
- _stream_readable: () => _stream_readable,
59
- _stream_transform: () => _stream_transform,
60
- _stream_writable: () => _stream_writable,
61
- assert: () => assert,
62
- buffer: () => buffer,
63
- child_process: () => child_process,
64
- cluster: () => cluster,
65
- console: () => console,
66
- constants: () => constants,
67
- crypto: () => crypto,
68
- dgram: () => dgram,
69
- dns: () => dns,
70
- domain: () => domain,
71
- events: () => events,
72
- fs: () => fs,
73
- http: () => http,
74
- https: () => https,
75
- module: () => module2,
76
- net: () => net,
77
- os: () => os,
78
- path: () => path,
79
- process: () => process,
80
- punycode: () => punycode,
81
- querystring: () => querystring,
82
- readline: () => readline,
83
- repl: () => repl,
84
- stream: () => stream,
85
- string_decoder: () => string_decoder,
86
- sys: () => sys,
87
- timers: () => timers,
88
- tls: () => tls,
89
- tty: () => tty,
90
- url: () => url,
91
- util: () => util,
92
- vm: () => vm,
93
- zlib: () => zlib
94
- });
95
57
  var import_node_module = require("module");
96
58
  var require2 = (0, import_node_module.createRequire)(importMetaUrl);
97
- var assert = require2.resolve("assert/");
98
- var buffer = require2.resolve("buffer/");
99
- var child_process = null;
100
- var cluster = null;
101
- var console = require2.resolve("console-browserify");
102
- var constants = require2.resolve("constants-browserify");
103
- var crypto = require2.resolve("crypto-browserify");
104
- var dgram = null;
105
- var dns = null;
106
- var domain = require2.resolve("domain-browser");
107
- var events = require2.resolve("events/");
108
- var fs = null;
109
- var http = require2.resolve("stream-http");
110
- var https = require2.resolve("https-browserify");
111
- var module2 = null;
112
- var net = null;
113
- var os = require2.resolve("os-browserify/browser.js");
114
- var path = require2.resolve("path-browserify");
115
- var punycode = require2.resolve("punycode/");
116
- var process = require2.resolve("process/browser.js");
117
- var querystring = require2.resolve("querystring-es3/");
118
- var readline = null;
119
- var repl = null;
120
- var stream = require2.resolve("stream-browserify");
121
- var _stream_duplex = require2.resolve(
122
- "readable-stream/lib/_stream_duplex.js"
123
- );
124
- var _stream_passthrough = require2.resolve(
125
- "readable-stream/lib/_stream_passthrough.js"
126
- );
127
- var _stream_readable = require2.resolve(
128
- "readable-stream/lib/_stream_readable.js"
129
- );
130
- var _stream_transform = require2.resolve(
131
- "readable-stream/lib/_stream_transform.js"
132
- );
133
- var _stream_writable = require2.resolve(
134
- "readable-stream/lib/_stream_writable.js"
59
+ var builtinMappingResolved = {
60
+ assert: require2.resolve("assert/"),
61
+ buffer: require2.resolve("buffer/"),
62
+ child_process: null,
63
+ cluster: null,
64
+ console: require2.resolve("console-browserify"),
65
+ constants: require2.resolve("constants-browserify"),
66
+ crypto: require2.resolve("crypto-browserify"),
67
+ dgram: null,
68
+ dns: null,
69
+ domain: require2.resolve("domain-browser"),
70
+ events: require2.resolve("events/"),
71
+ fs: null,
72
+ http: require2.resolve("stream-http"),
73
+ https: require2.resolve("https-browserify"),
74
+ module: null,
75
+ net: null,
76
+ os: require2.resolve("os-browserify/browser.js"),
77
+ path: require2.resolve("path-browserify"),
78
+ punycode: require2.resolve("punycode/"),
79
+ process: require2.resolve("process/browser.js"),
80
+ querystring: require2.resolve("querystring-es3/"),
81
+ readline: null,
82
+ repl: null,
83
+ stream: require2.resolve("stream-browserify"),
84
+ _stream_duplex: require2.resolve("readable-stream/lib/_stream_duplex.js"),
85
+ _stream_passthrough: require2.resolve(
86
+ "readable-stream/lib/_stream_passthrough.js"
87
+ ),
88
+ _stream_readable: require2.resolve("readable-stream/lib/_stream_readable.js"),
89
+ _stream_transform: require2.resolve(
90
+ "readable-stream/lib/_stream_transform.js"
91
+ ),
92
+ _stream_writable: require2.resolve("readable-stream/lib/_stream_writable.js"),
93
+ string_decoder: require2.resolve("string_decoder/"),
94
+ sys: require2.resolve("util/util.js"),
95
+ timers: require2.resolve("timers-browserify"),
96
+ tls: null,
97
+ tty: require2.resolve("tty-browserify"),
98
+ url: require2.resolve("url/"),
99
+ util: require2.resolve("util/util.js"),
100
+ vm: require2.resolve("vm-browserify"),
101
+ zlib: require2.resolve("browserify-zlib")
102
+ };
103
+ var resolvedPolyfillToModules = Object.fromEntries(
104
+ Object.entries(builtinMappingResolved).filter(([key]) => key !== null).map(([key, value]) => [value, key])
135
105
  );
136
- var string_decoder = require2.resolve("string_decoder/");
137
- var sys = require2.resolve("util/util.js");
138
- var timers = require2.resolve("timers-browserify");
139
- var tls = null;
140
- var tty = require2.resolve("tty-browserify");
141
- var url = require2.resolve("url/");
142
- var util = require2.resolve("util/util.js");
143
- var vm = require2.resolve("vm-browserify");
144
- var zlib = require2.resolve("browserify-zlib");
145
106
 
146
107
  // src/index.ts
108
+ var resolvePolyfill = (libPath, overrides) => {
109
+ if (overrides?.[libPath] !== void 0) {
110
+ return overrides[libPath];
111
+ }
112
+ return builtinMappingResolved[libPath];
113
+ };
147
114
  var getResolveFallback = ({
148
115
  protocolImports,
149
116
  exclude,
150
- include
117
+ include,
118
+ overrides
151
119
  }) => {
152
120
  if (exclude && include) {
153
121
  throw new Error("`include` is mutually exclusive with `exclude`.");
154
122
  }
155
- const resolvedNodeLibs = include ? include : Object.keys(libs_exports).filter((name) => {
123
+ const resolvedNodeLibs = include ? include : Object.keys(builtinMappingResolved).filter((name) => {
156
124
  return !(exclude || []).includes(name);
157
125
  });
158
126
  const fallback = {};
159
127
  for (const name of resolvedNodeLibs) {
160
- const libPath = libs_exports[name];
128
+ const libPath = resolvePolyfill(name, overrides);
161
129
  fallback[name] = libPath ?? false;
162
130
  if (protocolImports) {
163
131
  fallback[`node:${name}`] = fallback[name];
@@ -165,19 +133,19 @@ var getResolveFallback = ({
165
133
  }
166
134
  return fallback;
167
135
  };
168
- var getProvideGlobals = async (globals) => {
136
+ var getProvideGlobals = async (globals, overrides) => {
169
137
  const result = {};
170
138
  if (globals?.Buffer !== false) {
171
- result.Buffer = [buffer, "Buffer"];
139
+ result.Buffer = [resolvePolyfill("buffer", overrides), "Buffer"];
172
140
  }
173
141
  if (globals?.process !== false) {
174
- result.process = [process];
142
+ result.process = [resolvePolyfill("process", overrides)];
175
143
  }
176
144
  return result;
177
145
  };
178
146
  var PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
179
147
  function pluginNodePolyfill(options = {}) {
180
- const { protocolImports = true, include, exclude } = options;
148
+ const { protocolImports = true, include, exclude, overrides } = options;
181
149
  return {
182
150
  name: PLUGIN_NODE_POLYFILL_NAME,
183
151
  setup(api) {
@@ -186,9 +154,17 @@ function pluginNodePolyfill(options = {}) {
186
154
  return;
187
155
  }
188
156
  chain.resolve.fallback.merge(
189
- getResolveFallback({ protocolImports, include, exclude })
157
+ getResolveFallback({
158
+ protocolImports,
159
+ include,
160
+ exclude,
161
+ overrides
162
+ })
163
+ );
164
+ const provideGlobals = await getProvideGlobals(
165
+ options.globals,
166
+ overrides
190
167
  );
191
- const provideGlobals = await getProvideGlobals(options.globals);
192
168
  if (Object.keys(provideGlobals).length) {
193
169
  chain.plugin("node-polyfill-provide").use(bundler.ProvidePlugin, [provideGlobals]);
194
170
  }
@@ -202,7 +178,10 @@ function pluginNodePolyfill(options = {}) {
202
178
  // Annotate the CommonJS export names for ESM import in node:
203
179
  0 && (module.exports = {
204
180
  PLUGIN_NODE_POLYFILL_NAME,
181
+ builtinMappingResolved,
205
182
  getProvideGlobals,
206
183
  getResolveFallback,
207
- pluginNodePolyfill
184
+ pluginNodePolyfill,
185
+ resolvePolyfill,
186
+ resolvedPolyfillToModules
208
187
  });
package/dist/index.d.cts CHANGED
@@ -1,5 +1,47 @@
1
1
  import { RsbuildPlugin } from '@rsbuild/core';
2
2
 
3
+ declare const builtinMappingResolved: {
4
+ readonly assert: string;
5
+ readonly buffer: string;
6
+ readonly child_process: null;
7
+ readonly cluster: null;
8
+ readonly console: string;
9
+ readonly constants: string;
10
+ readonly crypto: string;
11
+ readonly dgram: null;
12
+ readonly dns: null;
13
+ readonly domain: string;
14
+ readonly events: string;
15
+ readonly fs: null;
16
+ readonly http: string;
17
+ readonly https: string;
18
+ readonly module: null;
19
+ readonly net: null;
20
+ readonly os: string;
21
+ readonly path: string;
22
+ readonly punycode: string;
23
+ readonly process: string;
24
+ readonly querystring: string;
25
+ readonly readline: null;
26
+ readonly repl: null;
27
+ readonly stream: string;
28
+ readonly _stream_duplex: string;
29
+ readonly _stream_passthrough: string;
30
+ readonly _stream_readable: string;
31
+ readonly _stream_transform: string;
32
+ readonly _stream_writable: string;
33
+ readonly string_decoder: string;
34
+ readonly sys: string;
35
+ readonly timers: string;
36
+ readonly tls: null;
37
+ readonly tty: string;
38
+ readonly url: string;
39
+ readonly util: string;
40
+ readonly vm: string;
41
+ readonly zlib: string;
42
+ };
43
+ declare const resolvedPolyfillToModules: any;
44
+
3
45
  type Globals = {
4
46
  process?: boolean;
5
47
  Buffer?: boolean;
@@ -32,10 +74,16 @@ type PluginNodePolyfillOptions = {
32
74
  * @default undefined
33
75
  */
34
76
  include?: string[];
77
+ /**
78
+ * Override the default polyfills for specific modules.
79
+ * @default undefined
80
+ */
81
+ overrides?: Record<string, string | false>;
35
82
  };
36
- declare const getResolveFallback: ({ protocolImports, exclude, include, }: Pick<PluginNodePolyfillOptions, "protocolImports" | "exclude" | "include">) => Record<string, string | false>;
37
- declare const getProvideGlobals: (globals?: Globals) => Promise<Record<string, string | string[]>>;
83
+ declare const resolvePolyfill: (libPath: string, overrides?: PluginNodePolyfillOptions["overrides"]) => string | false | null;
84
+ declare const getResolveFallback: ({ protocolImports, exclude, include, overrides, }: Pick<PluginNodePolyfillOptions, "protocolImports" | "exclude" | "include" | "overrides">) => Record<string, string | false>;
85
+ declare const getProvideGlobals: (globals?: Globals, overrides?: PluginNodePolyfillOptions["overrides"]) => Promise<Record<string, string | string[]>>;
38
86
  declare const PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
39
87
  declare function pluginNodePolyfill(options?: PluginNodePolyfillOptions): RsbuildPlugin;
40
88
 
41
- export { PLUGIN_NODE_POLYFILL_NAME, type PluginNodePolyfillOptions, getProvideGlobals, getResolveFallback, pluginNodePolyfill };
89
+ export { PLUGIN_NODE_POLYFILL_NAME, type PluginNodePolyfillOptions, builtinMappingResolved, getProvideGlobals, getResolveFallback, pluginNodePolyfill, resolvePolyfill, resolvedPolyfillToModules };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,47 @@
1
1
  import { RsbuildPlugin } from '@rsbuild/core';
2
2
 
3
+ declare const builtinMappingResolved: {
4
+ readonly assert: string;
5
+ readonly buffer: string;
6
+ readonly child_process: null;
7
+ readonly cluster: null;
8
+ readonly console: string;
9
+ readonly constants: string;
10
+ readonly crypto: string;
11
+ readonly dgram: null;
12
+ readonly dns: null;
13
+ readonly domain: string;
14
+ readonly events: string;
15
+ readonly fs: null;
16
+ readonly http: string;
17
+ readonly https: string;
18
+ readonly module: null;
19
+ readonly net: null;
20
+ readonly os: string;
21
+ readonly path: string;
22
+ readonly punycode: string;
23
+ readonly process: string;
24
+ readonly querystring: string;
25
+ readonly readline: null;
26
+ readonly repl: null;
27
+ readonly stream: string;
28
+ readonly _stream_duplex: string;
29
+ readonly _stream_passthrough: string;
30
+ readonly _stream_readable: string;
31
+ readonly _stream_transform: string;
32
+ readonly _stream_writable: string;
33
+ readonly string_decoder: string;
34
+ readonly sys: string;
35
+ readonly timers: string;
36
+ readonly tls: null;
37
+ readonly tty: string;
38
+ readonly url: string;
39
+ readonly util: string;
40
+ readonly vm: string;
41
+ readonly zlib: string;
42
+ };
43
+ declare const resolvedPolyfillToModules: any;
44
+
3
45
  type Globals = {
4
46
  process?: boolean;
5
47
  Buffer?: boolean;
@@ -32,10 +74,16 @@ type PluginNodePolyfillOptions = {
32
74
  * @default undefined
33
75
  */
34
76
  include?: string[];
77
+ /**
78
+ * Override the default polyfills for specific modules.
79
+ * @default undefined
80
+ */
81
+ overrides?: Record<string, string | false>;
35
82
  };
36
- declare const getResolveFallback: ({ protocolImports, exclude, include, }: Pick<PluginNodePolyfillOptions, "protocolImports" | "exclude" | "include">) => Record<string, string | false>;
37
- declare const getProvideGlobals: (globals?: Globals) => Promise<Record<string, string | string[]>>;
83
+ declare const resolvePolyfill: (libPath: string, overrides?: PluginNodePolyfillOptions["overrides"]) => string | false | null;
84
+ declare const getResolveFallback: ({ protocolImports, exclude, include, overrides, }: Pick<PluginNodePolyfillOptions, "protocolImports" | "exclude" | "include" | "overrides">) => Record<string, string | false>;
85
+ declare const getProvideGlobals: (globals?: Globals, overrides?: PluginNodePolyfillOptions["overrides"]) => Promise<Record<string, string | string[]>>;
38
86
  declare const PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
39
87
  declare function pluginNodePolyfill(options?: PluginNodePolyfillOptions): RsbuildPlugin;
40
88
 
41
- export { PLUGIN_NODE_POLYFILL_NAME, type PluginNodePolyfillOptions, getProvideGlobals, getResolveFallback, pluginNodePolyfill };
89
+ export { PLUGIN_NODE_POLYFILL_NAME, type PluginNodePolyfillOptions, builtinMappingResolved, getProvideGlobals, getResolveFallback, pluginNodePolyfill, resolvePolyfill, resolvedPolyfillToModules };
package/dist/index.js CHANGED
@@ -1,9 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __export = (target, all) => {
3
- for (var name in all)
4
- __defProp(target, name, { get: all[name], enumerable: true });
5
- };
6
-
7
1
  // src/ProtocolImportsPlugin.ts
8
2
  var ProtocolImportsPlugin = class {
9
3
  apply(compiler) {
@@ -24,113 +18,78 @@ var ProtocolImportsPlugin = class {
24
18
  };
25
19
 
26
20
  // src/libs.ts
27
- var libs_exports = {};
28
- __export(libs_exports, {
29
- _stream_duplex: () => _stream_duplex,
30
- _stream_passthrough: () => _stream_passthrough,
31
- _stream_readable: () => _stream_readable,
32
- _stream_transform: () => _stream_transform,
33
- _stream_writable: () => _stream_writable,
34
- assert: () => assert,
35
- buffer: () => buffer,
36
- child_process: () => child_process,
37
- cluster: () => cluster,
38
- console: () => console,
39
- constants: () => constants,
40
- crypto: () => crypto,
41
- dgram: () => dgram,
42
- dns: () => dns,
43
- domain: () => domain,
44
- events: () => events,
45
- fs: () => fs,
46
- http: () => http,
47
- https: () => https,
48
- module: () => module,
49
- net: () => net,
50
- os: () => os,
51
- path: () => path,
52
- process: () => process,
53
- punycode: () => punycode,
54
- querystring: () => querystring,
55
- readline: () => readline,
56
- repl: () => repl,
57
- stream: () => stream,
58
- string_decoder: () => string_decoder,
59
- sys: () => sys,
60
- timers: () => timers,
61
- tls: () => tls,
62
- tty: () => tty,
63
- url: () => url,
64
- util: () => util,
65
- vm: () => vm,
66
- zlib: () => zlib
67
- });
68
21
  import { createRequire } from "node:module";
69
22
  var require2 = createRequire(import.meta.url);
70
- var assert = require2.resolve("assert/");
71
- var buffer = require2.resolve("buffer/");
72
- var child_process = null;
73
- var cluster = null;
74
- var console = require2.resolve("console-browserify");
75
- var constants = require2.resolve("constants-browserify");
76
- var crypto = require2.resolve("crypto-browserify");
77
- var dgram = null;
78
- var dns = null;
79
- var domain = require2.resolve("domain-browser");
80
- var events = require2.resolve("events/");
81
- var fs = null;
82
- var http = require2.resolve("stream-http");
83
- var https = require2.resolve("https-browserify");
84
- var module = null;
85
- var net = null;
86
- var os = require2.resolve("os-browserify/browser.js");
87
- var path = require2.resolve("path-browserify");
88
- var punycode = require2.resolve("punycode/");
89
- var process = require2.resolve("process/browser.js");
90
- var querystring = require2.resolve("querystring-es3/");
91
- var readline = null;
92
- var repl = null;
93
- var stream = require2.resolve("stream-browserify");
94
- var _stream_duplex = require2.resolve(
95
- "readable-stream/lib/_stream_duplex.js"
96
- );
97
- var _stream_passthrough = require2.resolve(
98
- "readable-stream/lib/_stream_passthrough.js"
99
- );
100
- var _stream_readable = require2.resolve(
101
- "readable-stream/lib/_stream_readable.js"
102
- );
103
- var _stream_transform = require2.resolve(
104
- "readable-stream/lib/_stream_transform.js"
105
- );
106
- var _stream_writable = require2.resolve(
107
- "readable-stream/lib/_stream_writable.js"
23
+ var builtinMappingResolved = {
24
+ assert: require2.resolve("assert/"),
25
+ buffer: require2.resolve("buffer/"),
26
+ child_process: null,
27
+ cluster: null,
28
+ console: require2.resolve("console-browserify"),
29
+ constants: require2.resolve("constants-browserify"),
30
+ crypto: require2.resolve("crypto-browserify"),
31
+ dgram: null,
32
+ dns: null,
33
+ domain: require2.resolve("domain-browser"),
34
+ events: require2.resolve("events/"),
35
+ fs: null,
36
+ http: require2.resolve("stream-http"),
37
+ https: require2.resolve("https-browserify"),
38
+ module: null,
39
+ net: null,
40
+ os: require2.resolve("os-browserify/browser.js"),
41
+ path: require2.resolve("path-browserify"),
42
+ punycode: require2.resolve("punycode/"),
43
+ process: require2.resolve("process/browser.js"),
44
+ querystring: require2.resolve("querystring-es3/"),
45
+ readline: null,
46
+ repl: null,
47
+ stream: require2.resolve("stream-browserify"),
48
+ _stream_duplex: require2.resolve("readable-stream/lib/_stream_duplex.js"),
49
+ _stream_passthrough: require2.resolve(
50
+ "readable-stream/lib/_stream_passthrough.js"
51
+ ),
52
+ _stream_readable: require2.resolve("readable-stream/lib/_stream_readable.js"),
53
+ _stream_transform: require2.resolve(
54
+ "readable-stream/lib/_stream_transform.js"
55
+ ),
56
+ _stream_writable: require2.resolve("readable-stream/lib/_stream_writable.js"),
57
+ string_decoder: require2.resolve("string_decoder/"),
58
+ sys: require2.resolve("util/util.js"),
59
+ timers: require2.resolve("timers-browserify"),
60
+ tls: null,
61
+ tty: require2.resolve("tty-browserify"),
62
+ url: require2.resolve("url/"),
63
+ util: require2.resolve("util/util.js"),
64
+ vm: require2.resolve("vm-browserify"),
65
+ zlib: require2.resolve("browserify-zlib")
66
+ };
67
+ var resolvedPolyfillToModules = Object.fromEntries(
68
+ Object.entries(builtinMappingResolved).filter(([key]) => key !== null).map(([key, value]) => [value, key])
108
69
  );
109
- var string_decoder = require2.resolve("string_decoder/");
110
- var sys = require2.resolve("util/util.js");
111
- var timers = require2.resolve("timers-browserify");
112
- var tls = null;
113
- var tty = require2.resolve("tty-browserify");
114
- var url = require2.resolve("url/");
115
- var util = require2.resolve("util/util.js");
116
- var vm = require2.resolve("vm-browserify");
117
- var zlib = require2.resolve("browserify-zlib");
118
70
 
119
71
  // src/index.ts
72
+ var resolvePolyfill = (libPath, overrides) => {
73
+ if (overrides?.[libPath] !== void 0) {
74
+ return overrides[libPath];
75
+ }
76
+ return builtinMappingResolved[libPath];
77
+ };
120
78
  var getResolveFallback = ({
121
79
  protocolImports,
122
80
  exclude,
123
- include
81
+ include,
82
+ overrides
124
83
  }) => {
125
84
  if (exclude && include) {
126
85
  throw new Error("`include` is mutually exclusive with `exclude`.");
127
86
  }
128
- const resolvedNodeLibs = include ? include : Object.keys(libs_exports).filter((name) => {
87
+ const resolvedNodeLibs = include ? include : Object.keys(builtinMappingResolved).filter((name) => {
129
88
  return !(exclude || []).includes(name);
130
89
  });
131
90
  const fallback = {};
132
91
  for (const name of resolvedNodeLibs) {
133
- const libPath = libs_exports[name];
92
+ const libPath = resolvePolyfill(name, overrides);
134
93
  fallback[name] = libPath ?? false;
135
94
  if (protocolImports) {
136
95
  fallback[`node:${name}`] = fallback[name];
@@ -138,19 +97,19 @@ var getResolveFallback = ({
138
97
  }
139
98
  return fallback;
140
99
  };
141
- var getProvideGlobals = async (globals) => {
100
+ var getProvideGlobals = async (globals, overrides) => {
142
101
  const result = {};
143
102
  if (globals?.Buffer !== false) {
144
- result.Buffer = [buffer, "Buffer"];
103
+ result.Buffer = [resolvePolyfill("buffer", overrides), "Buffer"];
145
104
  }
146
105
  if (globals?.process !== false) {
147
- result.process = [process];
106
+ result.process = [resolvePolyfill("process", overrides)];
148
107
  }
149
108
  return result;
150
109
  };
151
110
  var PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
152
111
  function pluginNodePolyfill(options = {}) {
153
- const { protocolImports = true, include, exclude } = options;
112
+ const { protocolImports = true, include, exclude, overrides } = options;
154
113
  return {
155
114
  name: PLUGIN_NODE_POLYFILL_NAME,
156
115
  setup(api) {
@@ -159,9 +118,17 @@ function pluginNodePolyfill(options = {}) {
159
118
  return;
160
119
  }
161
120
  chain.resolve.fallback.merge(
162
- getResolveFallback({ protocolImports, include, exclude })
121
+ getResolveFallback({
122
+ protocolImports,
123
+ include,
124
+ exclude,
125
+ overrides
126
+ })
127
+ );
128
+ const provideGlobals = await getProvideGlobals(
129
+ options.globals,
130
+ overrides
163
131
  );
164
- const provideGlobals = await getProvideGlobals(options.globals);
165
132
  if (Object.keys(provideGlobals).length) {
166
133
  chain.plugin("node-polyfill-provide").use(bundler.ProvidePlugin, [provideGlobals]);
167
134
  }
@@ -174,7 +141,10 @@ function pluginNodePolyfill(options = {}) {
174
141
  }
175
142
  export {
176
143
  PLUGIN_NODE_POLYFILL_NAME,
144
+ builtinMappingResolved,
177
145
  getProvideGlobals,
178
146
  getResolveFallback,
179
- pluginNodePolyfill
147
+ pluginNodePolyfill,
148
+ resolvePolyfill,
149
+ resolvedPolyfillToModules
180
150
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-node-polyfill",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "repository": "https://github.com/rspack-contrib/rsbuild-plugin-node-polyfill",
5
5
  "license": "MIT",
6
6
  "type": "module",