@rsbuild/plugin-node-polyfill 1.0.4 → 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
@@ -7,6 +7,7 @@ An Rsbuild plugin to automatically inject polyfills for [Node.js builtin modules
7
7
  <img src="https://img.shields.io/npm/v/@rsbuild/plugin-node-polyfill?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
8
8
  </a>
9
9
  <img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
10
+ <a href="https://npmcharts.com/compare/@rsbuild/plugin-node-polyfill?minimal=true"><img src="https://img.shields.io/npm/dm/@rsbuild/plugin-node-polyfill.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="downloads" /></a>
10
11
  </p>
11
12
 
12
13
  ## When to use
@@ -27,11 +28,11 @@ Add plugin to your `rsbuild.config.ts`:
27
28
 
28
29
  ```ts
29
30
  // rsbuild.config.ts
30
- import { pluginNodePolyfill } from "@rsbuild/plugin-node-polyfill";
31
+ import { pluginNodePolyfill } from '@rsbuild/plugin-node-polyfill'
31
32
 
32
33
  export default {
33
34
  plugins: [pluginNodePolyfill()],
34
- };
35
+ }
35
36
  ```
36
37
 
37
38
  ## Node Polyfills
@@ -46,7 +47,7 @@ When you use the above global variables in your code, the corresponding polyfill
46
47
  For instance, the following code would inject the `Buffer` polyfill:
47
48
 
48
49
  ```ts
49
- const bufferData = Buffer.from("abc");
50
+ const bufferData = Buffer.from('abc')
50
51
  ```
51
52
 
52
53
  You can disable this behavior through the `globals` option of the plugin:
@@ -57,7 +58,7 @@ pluginNodePolyfill({
57
58
  Buffer: false,
58
59
  process: false,
59
60
  },
60
- });
61
+ })
61
62
  ```
62
63
 
63
64
  ### Modules
@@ -94,9 +95,9 @@ pluginNodePolyfill({
94
95
  When the above module is referenced in code via import / require syntax, the corresponding polyfill will be injected.
95
96
 
96
97
  ```ts
97
- import { Buffer } from "buffer";
98
+ import { Buffer } from 'buffer'
98
99
 
99
- const bufferData = Buffer.from("abc");
100
+ const bufferData = Buffer.from('abc')
100
101
  ```
101
102
 
102
103
  ### Fallbacks
@@ -115,9 +116,9 @@ const bufferData = Buffer.from("abc");
115
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.
116
117
 
117
118
  ```ts
118
- import fs from "fs";
119
+ import fs from 'fs'
119
120
 
120
- console.log(fs); // -> {}
121
+ console.log(fs) // -> {}
121
122
  ```
122
123
 
123
124
  ## Options
@@ -130,9 +131,9 @@ Used to specify whether to inject polyfills for global variables.
130
131
 
131
132
  ```ts
132
133
  type Globals = {
133
- process?: boolean;
134
- Buffer?: boolean;
135
- };
134
+ process?: boolean
135
+ Buffer?: boolean
136
+ }
136
137
  ```
137
138
 
138
139
  - **Default:**
@@ -141,7 +142,7 @@ type Globals = {
141
142
  const defaultGlobals = {
142
143
  Buffer: true,
143
144
  process: true,
144
- };
145
+ }
145
146
  ```
146
147
 
147
148
  ### protocolImports
@@ -156,9 +157,55 @@ For example, if you disable `protocolImports`, modules such as `node:path`, `nod
156
157
  ```ts
157
158
  pluginNodePolyfill({
158
159
  protocolImports: false,
159
- });
160
+ })
160
161
  ```
161
162
 
163
+ ### include
164
+
165
+ Specify an array of modules for which polyfills should be injected. If this option is set, only the specified modules will be polyfilled. `include` is mutually exclusive with [`exclude`](#exclude).
166
+
167
+ - **Type:** `string[]`
168
+ - **Default:** `undefined`
169
+
170
+ ```ts
171
+ pluginNodePolyfill({
172
+ include: ['buffer', 'crypto'], // Only "buffer" and "crypto" modules will be polyfilled.
173
+ })
174
+ ```
175
+
176
+ ### exclude
177
+
178
+ Specify an array of modules for which polyfills should not be injected from the default. If this option is set, the specified modules will be excluded from polyfilled. `exclude` is mutually exclusive with [`include`](#include).
179
+
180
+ - **Type:** `string[]`
181
+ - **Default:** `undefined`
182
+
183
+ ```ts
184
+ pluginNodePolyfill({
185
+ exclude: ['http', 'https'], // All modules except "http" and "https" will be polyfilled.
186
+ })
187
+ ```
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
+
162
209
  ## License
163
210
 
164
211
  [MIT](./LICENSE).
package/dist/index.cjs CHANGED
@@ -3,9 +3,6 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __esm = (fn, res) => function __init() {
7
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
8
- };
9
6
  var __export = (target, all) => {
10
7
  for (var name in all)
11
8
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -20,154 +17,115 @@ var __copyProps = (to, from, except, desc) => {
20
17
  };
21
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
19
 
23
- // node_modules/.pnpm/tsup@8.0.2_postcss@8.4.39_typescript@5.5.2/node_modules/tsup/assets/cjs_shims.js
24
- var getImportMetaUrl, importMetaUrl;
25
- var init_cjs_shims = __esm({
26
- "node_modules/.pnpm/tsup@8.0.2_postcss@8.4.39_typescript@5.5.2/node_modules/tsup/assets/cjs_shims.js"() {
27
- "use strict";
28
- getImportMetaUrl = () => typeof document === "undefined" ? new URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
29
- importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
30
- }
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ PLUGIN_NODE_POLYFILL_NAME: () => PLUGIN_NODE_POLYFILL_NAME,
24
+ builtinMappingResolved: () => builtinMappingResolved,
25
+ getProvideGlobals: () => getProvideGlobals,
26
+ getResolveFallback: () => getResolveFallback,
27
+ pluginNodePolyfill: () => pluginNodePolyfill,
28
+ resolvePolyfill: () => resolvePolyfill,
29
+ resolvedPolyfillToModules: () => resolvedPolyfillToModules
31
30
  });
31
+ module.exports = __toCommonJS(src_exports);
32
+
33
+ // node_modules/.pnpm/tsup@8.3.0_postcss@8.4.47_typescript@5.6.3/node_modules/tsup/assets/cjs_shims.js
34
+ var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
35
+ var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
32
36
 
33
37
  // src/ProtocolImportsPlugin.ts
34
- var ProtocolImportsPlugin_exports = {};
35
- __export(ProtocolImportsPlugin_exports, {
36
- ProtocolImportsPlugin: () => ProtocolImportsPlugin
37
- });
38
- var ProtocolImportsPlugin;
39
- var init_ProtocolImportsPlugin = __esm({
40
- "src/ProtocolImportsPlugin.ts"() {
41
- "use strict";
42
- init_cjs_shims();
43
- ProtocolImportsPlugin = class {
44
- apply(compiler) {
45
- compiler.hooks.normalModuleFactory.tap(
38
+ var ProtocolImportsPlugin = class {
39
+ apply(compiler) {
40
+ compiler.hooks.normalModuleFactory.tap(
41
+ "NormalModuleReplacementPlugin",
42
+ (nmf) => {
43
+ nmf.hooks.beforeResolve.tap(
46
44
  "NormalModuleReplacementPlugin",
47
- (nmf) => {
48
- nmf.hooks.beforeResolve.tap(
49
- "NormalModuleReplacementPlugin",
50
- (resource) => {
51
- if (/^node:/.test(resource.request)) {
52
- resource.request = resource.request.replace(/^node:/, "");
53
- }
54
- }
55
- );
45
+ (resource) => {
46
+ if (/^node:/.test(resource.request)) {
47
+ resource.request = resource.request.replace(/^node:/, "");
48
+ }
56
49
  }
57
50
  );
58
51
  }
59
- };
52
+ );
60
53
  }
61
- });
62
-
63
- // src/index.ts
64
- var src_exports = {};
65
- __export(src_exports, {
66
- PLUGIN_NODE_POLYFILL_NAME: () => PLUGIN_NODE_POLYFILL_NAME,
67
- pluginNodePolyfill: () => pluginNodePolyfill
68
- });
69
- module.exports = __toCommonJS(src_exports);
70
- init_cjs_shims();
54
+ };
71
55
 
72
56
  // src/libs.ts
73
- var libs_exports = {};
74
- __export(libs_exports, {
75
- _stream_duplex: () => _stream_duplex,
76
- _stream_passthrough: () => _stream_passthrough,
77
- _stream_readable: () => _stream_readable,
78
- _stream_transform: () => _stream_transform,
79
- _stream_writable: () => _stream_writable,
80
- assert: () => assert,
81
- buffer: () => buffer,
82
- child_process: () => child_process,
83
- cluster: () => cluster,
84
- console: () => console,
85
- constants: () => constants,
86
- crypto: () => crypto,
87
- dgram: () => dgram,
88
- dns: () => dns,
89
- domain: () => domain,
90
- events: () => events,
91
- fs: () => fs,
92
- http: () => http,
93
- https: () => https,
94
- module: () => module2,
95
- net: () => net,
96
- os: () => os,
97
- path: () => path,
98
- process: () => process,
99
- punycode: () => punycode,
100
- querystring: () => querystring,
101
- readline: () => readline,
102
- repl: () => repl,
103
- stream: () => stream,
104
- string_decoder: () => string_decoder,
105
- sys: () => sys,
106
- timers: () => timers,
107
- tls: () => tls,
108
- tty: () => tty,
109
- url: () => url,
110
- util: () => util,
111
- vm: () => vm,
112
- zlib: () => zlib
113
- });
114
- init_cjs_shims();
115
57
  var import_node_module = require("module");
116
58
  var require2 = (0, import_node_module.createRequire)(importMetaUrl);
117
- var assert = require2.resolve("assert/");
118
- var buffer = require2.resolve("buffer/");
119
- var child_process = null;
120
- var cluster = null;
121
- var console = require2.resolve("console-browserify");
122
- var constants = require2.resolve("constants-browserify");
123
- var crypto = require2.resolve("crypto-browserify");
124
- var dgram = null;
125
- var dns = null;
126
- var domain = require2.resolve("domain-browser");
127
- var events = require2.resolve("events/");
128
- var fs = null;
129
- var http = require2.resolve("stream-http");
130
- var https = require2.resolve("https-browserify");
131
- var module2 = null;
132
- var net = null;
133
- var os = require2.resolve("os-browserify/browser.js");
134
- var path = require2.resolve("path-browserify");
135
- var punycode = require2.resolve("punycode/");
136
- var process = require2.resolve("process/browser.js");
137
- var querystring = require2.resolve("querystring-es3/");
138
- var readline = null;
139
- var repl = null;
140
- var stream = require2.resolve("stream-browserify");
141
- var _stream_duplex = require2.resolve(
142
- "readable-stream/lib/_stream_duplex.js"
143
- );
144
- var _stream_passthrough = require2.resolve(
145
- "readable-stream/lib/_stream_passthrough.js"
146
- );
147
- var _stream_readable = require2.resolve(
148
- "readable-stream/lib/_stream_readable.js"
149
- );
150
- var _stream_transform = require2.resolve(
151
- "readable-stream/lib/_stream_transform.js"
152
- );
153
- var _stream_writable = require2.resolve(
154
- "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])
155
105
  );
156
- var string_decoder = require2.resolve("string_decoder/");
157
- var sys = require2.resolve("util/util.js");
158
- var timers = require2.resolve("timers-browserify");
159
- var tls = null;
160
- var tty = require2.resolve("tty-browserify");
161
- var url = require2.resolve("url/");
162
- var util = require2.resolve("util/util.js");
163
- var vm = require2.resolve("vm-browserify");
164
- var zlib = require2.resolve("browserify-zlib");
165
106
 
166
107
  // src/index.ts
167
- var getResolveFallback = (protocolImports) => {
108
+ var resolvePolyfill = (libPath, overrides) => {
109
+ if (overrides?.[libPath] !== void 0) {
110
+ return overrides[libPath];
111
+ }
112
+ return builtinMappingResolved[libPath];
113
+ };
114
+ var getResolveFallback = ({
115
+ protocolImports,
116
+ exclude,
117
+ include,
118
+ overrides
119
+ }) => {
120
+ if (exclude && include) {
121
+ throw new Error("`include` is mutually exclusive with `exclude`.");
122
+ }
123
+ const resolvedNodeLibs = include ? include : Object.keys(builtinMappingResolved).filter((name) => {
124
+ return !(exclude || []).includes(name);
125
+ });
168
126
  const fallback = {};
169
- for (const name of Object.keys(libs_exports)) {
170
- const libPath = libs_exports[name];
127
+ for (const name of resolvedNodeLibs) {
128
+ const libPath = resolvePolyfill(name, overrides);
171
129
  fallback[name] = libPath ?? false;
172
130
  if (protocolImports) {
173
131
  fallback[`node:${name}`] = fallback[name];
@@ -175,19 +133,19 @@ var getResolveFallback = (protocolImports) => {
175
133
  }
176
134
  return fallback;
177
135
  };
178
- var getProvideGlobals = async (globals) => {
136
+ var getProvideGlobals = async (globals, overrides) => {
179
137
  const result = {};
180
138
  if (globals?.Buffer !== false) {
181
- result.Buffer = [buffer, "Buffer"];
139
+ result.Buffer = [resolvePolyfill("buffer", overrides), "Buffer"];
182
140
  }
183
141
  if (globals?.process !== false) {
184
- result.process = [process];
142
+ result.process = [resolvePolyfill("process", overrides)];
185
143
  }
186
144
  return result;
187
145
  };
188
146
  var PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
189
147
  function pluginNodePolyfill(options = {}) {
190
- const { protocolImports = true } = options;
148
+ const { protocolImports = true, include, exclude, overrides } = options;
191
149
  return {
192
150
  name: PLUGIN_NODE_POLYFILL_NAME,
193
151
  setup(api) {
@@ -195,14 +153,23 @@ function pluginNodePolyfill(options = {}) {
195
153
  if (isServer) {
196
154
  return;
197
155
  }
198
- chain.resolve.fallback.merge(getResolveFallback(protocolImports));
199
- const provideGlobals = await getProvideGlobals(options.globals);
156
+ chain.resolve.fallback.merge(
157
+ getResolveFallback({
158
+ protocolImports,
159
+ include,
160
+ exclude,
161
+ overrides
162
+ })
163
+ );
164
+ const provideGlobals = await getProvideGlobals(
165
+ options.globals,
166
+ overrides
167
+ );
200
168
  if (Object.keys(provideGlobals).length) {
201
169
  chain.plugin("node-polyfill-provide").use(bundler.ProvidePlugin, [provideGlobals]);
202
170
  }
203
171
  if (protocolImports) {
204
- const { ProtocolImportsPlugin: ProtocolImportsPlugin2 } = await Promise.resolve().then(() => (init_ProtocolImportsPlugin(), ProtocolImportsPlugin_exports));
205
- chain.plugin("protocol-imports").use(ProtocolImportsPlugin2);
172
+ chain.plugin("protocol-imports").use(ProtocolImportsPlugin);
206
173
  }
207
174
  });
208
175
  }
@@ -211,5 +178,10 @@ function pluginNodePolyfill(options = {}) {
211
178
  // Annotate the CommonJS export names for ESM import in node:
212
179
  0 && (module.exports = {
213
180
  PLUGIN_NODE_POLYFILL_NAME,
214
- pluginNodePolyfill
181
+ builtinMappingResolved,
182
+ getProvideGlobals,
183
+ getResolveFallback,
184
+ pluginNodePolyfill,
185
+ resolvePolyfill,
186
+ resolvedPolyfillToModules
215
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;
@@ -20,8 +62,28 @@ type PluginNodePolyfillOptions = {
20
62
  * @default true
21
63
  */
22
64
  protocolImports?: boolean;
65
+ /**
66
+ * Exclude certain modules to be polyfilled.
67
+ * This option is mutually exclusive with {@link PluginNodePolyfillOptions.include | `include`}.
68
+ * @default undefined
69
+ */
70
+ exclude?: string[];
71
+ /**
72
+ * Only include certain modules to be polyfilled.
73
+ * This option is mutually exclusive with {@link PluginNodePolyfillOptions.exclude | `exclude`}.
74
+ * @default undefined
75
+ */
76
+ include?: string[];
77
+ /**
78
+ * Override the default polyfills for specific modules.
79
+ * @default undefined
80
+ */
81
+ overrides?: Record<string, string | false>;
23
82
  };
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[]>>;
24
86
  declare const PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
25
87
  declare function pluginNodePolyfill(options?: PluginNodePolyfillOptions): RsbuildPlugin;
26
88
 
27
- export { PLUGIN_NODE_POLYFILL_NAME, type PluginNodePolyfillOptions, 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;
@@ -20,8 +62,28 @@ type PluginNodePolyfillOptions = {
20
62
  * @default true
21
63
  */
22
64
  protocolImports?: boolean;
65
+ /**
66
+ * Exclude certain modules to be polyfilled.
67
+ * This option is mutually exclusive with {@link PluginNodePolyfillOptions.include | `include`}.
68
+ * @default undefined
69
+ */
70
+ exclude?: string[];
71
+ /**
72
+ * Only include certain modules to be polyfilled.
73
+ * This option is mutually exclusive with {@link PluginNodePolyfillOptions.exclude | `exclude`}.
74
+ * @default undefined
75
+ */
76
+ include?: string[];
77
+ /**
78
+ * Override the default polyfills for specific modules.
79
+ * @default undefined
80
+ */
81
+ overrides?: Record<string, string | false>;
23
82
  };
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[]>>;
24
86
  declare const PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
25
87
  declare function pluginNodePolyfill(options?: PluginNodePolyfillOptions): RsbuildPlugin;
26
88
 
27
- export { PLUGIN_NODE_POLYFILL_NAME, type PluginNodePolyfillOptions, pluginNodePolyfill };
89
+ export { PLUGIN_NODE_POLYFILL_NAME, type PluginNodePolyfillOptions, builtinMappingResolved, getProvideGlobals, getResolveFallback, pluginNodePolyfill, resolvePolyfill, resolvedPolyfillToModules };
package/dist/index.js CHANGED
@@ -1,105 +1,95 @@
1
- import {
2
- __export
3
- } from "./chunk-6C3VEZWH.js";
1
+ // src/ProtocolImportsPlugin.ts
2
+ var ProtocolImportsPlugin = class {
3
+ apply(compiler) {
4
+ compiler.hooks.normalModuleFactory.tap(
5
+ "NormalModuleReplacementPlugin",
6
+ (nmf) => {
7
+ nmf.hooks.beforeResolve.tap(
8
+ "NormalModuleReplacementPlugin",
9
+ (resource) => {
10
+ if (/^node:/.test(resource.request)) {
11
+ resource.request = resource.request.replace(/^node:/, "");
12
+ }
13
+ }
14
+ );
15
+ }
16
+ );
17
+ }
18
+ };
4
19
 
5
20
  // src/libs.ts
6
- var libs_exports = {};
7
- __export(libs_exports, {
8
- _stream_duplex: () => _stream_duplex,
9
- _stream_passthrough: () => _stream_passthrough,
10
- _stream_readable: () => _stream_readable,
11
- _stream_transform: () => _stream_transform,
12
- _stream_writable: () => _stream_writable,
13
- assert: () => assert,
14
- buffer: () => buffer,
15
- child_process: () => child_process,
16
- cluster: () => cluster,
17
- console: () => console,
18
- constants: () => constants,
19
- crypto: () => crypto,
20
- dgram: () => dgram,
21
- dns: () => dns,
22
- domain: () => domain,
23
- events: () => events,
24
- fs: () => fs,
25
- http: () => http,
26
- https: () => https,
27
- module: () => module,
28
- net: () => net,
29
- os: () => os,
30
- path: () => path,
31
- process: () => process,
32
- punycode: () => punycode,
33
- querystring: () => querystring,
34
- readline: () => readline,
35
- repl: () => repl,
36
- stream: () => stream,
37
- string_decoder: () => string_decoder,
38
- sys: () => sys,
39
- timers: () => timers,
40
- tls: () => tls,
41
- tty: () => tty,
42
- url: () => url,
43
- util: () => util,
44
- vm: () => vm,
45
- zlib: () => zlib
46
- });
47
21
  import { createRequire } from "node:module";
48
22
  var require2 = createRequire(import.meta.url);
49
- var assert = require2.resolve("assert/");
50
- var buffer = require2.resolve("buffer/");
51
- var child_process = null;
52
- var cluster = null;
53
- var console = require2.resolve("console-browserify");
54
- var constants = require2.resolve("constants-browserify");
55
- var crypto = require2.resolve("crypto-browserify");
56
- var dgram = null;
57
- var dns = null;
58
- var domain = require2.resolve("domain-browser");
59
- var events = require2.resolve("events/");
60
- var fs = null;
61
- var http = require2.resolve("stream-http");
62
- var https = require2.resolve("https-browserify");
63
- var module = null;
64
- var net = null;
65
- var os = require2.resolve("os-browserify/browser.js");
66
- var path = require2.resolve("path-browserify");
67
- var punycode = require2.resolve("punycode/");
68
- var process = require2.resolve("process/browser.js");
69
- var querystring = require2.resolve("querystring-es3/");
70
- var readline = null;
71
- var repl = null;
72
- var stream = require2.resolve("stream-browserify");
73
- var _stream_duplex = require2.resolve(
74
- "readable-stream/lib/_stream_duplex.js"
75
- );
76
- var _stream_passthrough = require2.resolve(
77
- "readable-stream/lib/_stream_passthrough.js"
78
- );
79
- var _stream_readable = require2.resolve(
80
- "readable-stream/lib/_stream_readable.js"
81
- );
82
- var _stream_transform = require2.resolve(
83
- "readable-stream/lib/_stream_transform.js"
84
- );
85
- var _stream_writable = require2.resolve(
86
- "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])
87
69
  );
88
- var string_decoder = require2.resolve("string_decoder/");
89
- var sys = require2.resolve("util/util.js");
90
- var timers = require2.resolve("timers-browserify");
91
- var tls = null;
92
- var tty = require2.resolve("tty-browserify");
93
- var url = require2.resolve("url/");
94
- var util = require2.resolve("util/util.js");
95
- var vm = require2.resolve("vm-browserify");
96
- var zlib = require2.resolve("browserify-zlib");
97
70
 
98
71
  // src/index.ts
99
- var getResolveFallback = (protocolImports) => {
72
+ var resolvePolyfill = (libPath, overrides) => {
73
+ if (overrides?.[libPath] !== void 0) {
74
+ return overrides[libPath];
75
+ }
76
+ return builtinMappingResolved[libPath];
77
+ };
78
+ var getResolveFallback = ({
79
+ protocolImports,
80
+ exclude,
81
+ include,
82
+ overrides
83
+ }) => {
84
+ if (exclude && include) {
85
+ throw new Error("`include` is mutually exclusive with `exclude`.");
86
+ }
87
+ const resolvedNodeLibs = include ? include : Object.keys(builtinMappingResolved).filter((name) => {
88
+ return !(exclude || []).includes(name);
89
+ });
100
90
  const fallback = {};
101
- for (const name of Object.keys(libs_exports)) {
102
- const libPath = libs_exports[name];
91
+ for (const name of resolvedNodeLibs) {
92
+ const libPath = resolvePolyfill(name, overrides);
103
93
  fallback[name] = libPath ?? false;
104
94
  if (protocolImports) {
105
95
  fallback[`node:${name}`] = fallback[name];
@@ -107,19 +97,19 @@ var getResolveFallback = (protocolImports) => {
107
97
  }
108
98
  return fallback;
109
99
  };
110
- var getProvideGlobals = async (globals) => {
100
+ var getProvideGlobals = async (globals, overrides) => {
111
101
  const result = {};
112
102
  if (globals?.Buffer !== false) {
113
- result.Buffer = [buffer, "Buffer"];
103
+ result.Buffer = [resolvePolyfill("buffer", overrides), "Buffer"];
114
104
  }
115
105
  if (globals?.process !== false) {
116
- result.process = [process];
106
+ result.process = [resolvePolyfill("process", overrides)];
117
107
  }
118
108
  return result;
119
109
  };
120
110
  var PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
121
111
  function pluginNodePolyfill(options = {}) {
122
- const { protocolImports = true } = options;
112
+ const { protocolImports = true, include, exclude, overrides } = options;
123
113
  return {
124
114
  name: PLUGIN_NODE_POLYFILL_NAME,
125
115
  setup(api) {
@@ -127,13 +117,22 @@ function pluginNodePolyfill(options = {}) {
127
117
  if (isServer) {
128
118
  return;
129
119
  }
130
- chain.resolve.fallback.merge(getResolveFallback(protocolImports));
131
- const provideGlobals = await getProvideGlobals(options.globals);
120
+ chain.resolve.fallback.merge(
121
+ getResolveFallback({
122
+ protocolImports,
123
+ include,
124
+ exclude,
125
+ overrides
126
+ })
127
+ );
128
+ const provideGlobals = await getProvideGlobals(
129
+ options.globals,
130
+ overrides
131
+ );
132
132
  if (Object.keys(provideGlobals).length) {
133
133
  chain.plugin("node-polyfill-provide").use(bundler.ProvidePlugin, [provideGlobals]);
134
134
  }
135
135
  if (protocolImports) {
136
- const { ProtocolImportsPlugin } = await import("./ProtocolImportsPlugin-HUSWFIAE.js");
137
136
  chain.plugin("protocol-imports").use(ProtocolImportsPlugin);
138
137
  }
139
138
  });
@@ -142,5 +141,10 @@ function pluginNodePolyfill(options = {}) {
142
141
  }
143
142
  export {
144
143
  PLUGIN_NODE_POLYFILL_NAME,
145
- pluginNodePolyfill
144
+ builtinMappingResolved,
145
+ getProvideGlobals,
146
+ getResolveFallback,
147
+ pluginNodePolyfill,
148
+ resolvePolyfill,
149
+ resolvedPolyfillToModules
146
150
  };
package/package.json CHANGED
@@ -1,83 +1,87 @@
1
1
  {
2
- "name": "@rsbuild/plugin-node-polyfill",
3
- "version": "1.0.4",
4
- "repository": "https://github.com/rspack-contrib/rsbuild-plugin-node-polyfill",
5
- "license": "MIT",
6
- "type": "module",
7
- "exports": {
8
- ".": {
9
- "types": "./dist/index.d.ts",
10
- "import": "./dist/index.js",
11
- "require": "./dist/index.cjs"
12
- }
13
- },
14
- "main": "./dist/index.js",
15
- "module": "./dist/index.mjs",
16
- "types": "./dist/index.d.ts",
17
- "files": [
18
- "dist"
19
- ],
20
- "simple-git-hooks": {
21
- "pre-commit": "npx nano-staged"
22
- },
23
- "nano-staged": {
24
- "*.{js,jsx,ts,tsx,mjs,cjs}": [
25
- "biome check --write --no-errors-on-unmatched"
26
- ]
27
- },
28
- "dependencies": {
29
- "assert": "^2.1.0",
30
- "browserify-zlib": "^0.2.0",
31
- "buffer": "^5.7.1",
32
- "console-browserify": "^1.2.0",
33
- "constants-browserify": "^1.0.0",
34
- "crypto-browserify": "^3.12.0",
35
- "domain-browser": "^5.7.0",
36
- "events": "^3.3.0",
37
- "https-browserify": "^1.0.0",
38
- "os-browserify": "^0.3.0",
39
- "path-browserify": "^1.0.1",
40
- "process": "^0.11.10",
41
- "punycode": "^2.3.1",
42
- "querystring-es3": "^0.2.1",
43
- "readable-stream": "^4.5.2",
44
- "stream-browserify": "^3.0.0",
45
- "stream-http": "^3.2.0",
46
- "string_decoder": "^1.3.0",
47
- "timers-browserify": "^2.0.12",
48
- "tty-browserify": "^0.0.1",
49
- "url": "^0.11.3",
50
- "util": "^0.12.5",
51
- "vm-browserify": "^1.1.2"
52
- },
53
- "devDependencies": {
54
- "@biomejs/biome": "^1.8.3",
55
- "@playwright/test": "^1.44.1",
56
- "@rsbuild/core": "^1.0.1-beta.10",
57
- "@types/node": "^20.14.1",
58
- "nano-staged": "^0.8.0",
59
- "playwright": "^1.44.1",
60
- "simple-git-hooks": "^2.11.1",
61
- "tsup": "^8.0.2",
62
- "typescript": "^5.5.2"
63
- },
64
- "peerDependencies": {
65
- "@rsbuild/core": "1.x || ^1.0.1-beta.0"
66
- },
67
- "peerDependenciesMeta": {
68
- "@rsbuild/core": {
69
- "optional": true
70
- }
71
- },
72
- "publishConfig": {
73
- "access": "public",
74
- "registry": "https://registry.npmjs.org/"
75
- },
76
- "scripts": {
77
- "build": "tsup",
78
- "dev": "tsup --watch",
79
- "lint": "biome check .",
80
- "lint:write": "biome check . --write",
81
- "test": "playwright test"
82
- }
83
- }
2
+ "name": "@rsbuild/plugin-node-polyfill",
3
+ "version": "1.2.0",
4
+ "repository": "https://github.com/rspack-contrib/rsbuild-plugin-node-polyfill",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "require": "./dist/index.cjs"
12
+ }
13
+ },
14
+ "main": "./dist/index.js",
15
+ "module": "./dist/index.mjs",
16
+ "types": "./dist/index.d.ts",
17
+ "files": ["dist"],
18
+ "scripts": {
19
+ "build": "tsup",
20
+ "dev": "tsup --watch",
21
+ "lint": "biome check .",
22
+ "lint:write": "biome check . --write",
23
+ "prepare": "simple-git-hooks && npm run build",
24
+ "test": "pnpm run /^test:/",
25
+ "test:e2e": "playwright test",
26
+ "test:unit": "vitest run",
27
+ "bump": "npx bumpp"
28
+ },
29
+ "simple-git-hooks": {
30
+ "pre-commit": "npx nano-staged"
31
+ },
32
+ "nano-staged": {
33
+ "*.{js,jsx,ts,tsx,mjs,cjs}": [
34
+ "biome check --write --no-errors-on-unmatched"
35
+ ]
36
+ },
37
+ "dependencies": {
38
+ "assert": "^2.1.0",
39
+ "browserify-zlib": "^0.2.0",
40
+ "buffer": "^5.7.1",
41
+ "console-browserify": "^1.2.0",
42
+ "constants-browserify": "^1.0.0",
43
+ "crypto-browserify": "^3.12.0",
44
+ "domain-browser": "^5.7.0",
45
+ "events": "^3.3.0",
46
+ "https-browserify": "^1.0.0",
47
+ "os-browserify": "^0.3.0",
48
+ "path-browserify": "^1.0.1",
49
+ "process": "^0.11.10",
50
+ "punycode": "^2.3.1",
51
+ "querystring-es3": "^0.2.1",
52
+ "readable-stream": "^4.5.2",
53
+ "stream-browserify": "^3.0.0",
54
+ "stream-http": "^3.2.0",
55
+ "string_decoder": "^1.3.0",
56
+ "timers-browserify": "^2.0.12",
57
+ "tty-browserify": "^0.0.1",
58
+ "url": "^0.11.4",
59
+ "util": "^0.12.5",
60
+ "vm-browserify": "^1.1.2"
61
+ },
62
+ "devDependencies": {
63
+ "@biomejs/biome": "^1.9.4",
64
+ "@playwright/test": "^1.48.1",
65
+ "@rsbuild/core": "^1.0.15",
66
+ "@types/node": "^20.16.13",
67
+ "nano-staged": "^0.8.0",
68
+ "playwright": "^1.48.1",
69
+ "simple-git-hooks": "^2.11.1",
70
+ "tsup": "^8.3.0",
71
+ "typescript": "^5.6.3",
72
+ "vitest": "^2.1.3"
73
+ },
74
+ "peerDependencies": {
75
+ "@rsbuild/core": "1.x || ^1.0.1-beta.0"
76
+ },
77
+ "peerDependenciesMeta": {
78
+ "@rsbuild/core": {
79
+ "optional": true
80
+ }
81
+ },
82
+ "packageManager": "pnpm@9.12.2",
83
+ "publishConfig": {
84
+ "access": "public",
85
+ "registry": "https://registry.npmjs.org/"
86
+ }
87
+ }
@@ -1,23 +0,0 @@
1
- import "./chunk-6C3VEZWH.js";
2
-
3
- // src/ProtocolImportsPlugin.ts
4
- var ProtocolImportsPlugin = class {
5
- apply(compiler) {
6
- compiler.hooks.normalModuleFactory.tap(
7
- "NormalModuleReplacementPlugin",
8
- (nmf) => {
9
- nmf.hooks.beforeResolve.tap(
10
- "NormalModuleReplacementPlugin",
11
- (resource) => {
12
- if (/^node:/.test(resource.request)) {
13
- resource.request = resource.request.replace(/^node:/, "");
14
- }
15
- }
16
- );
17
- }
18
- );
19
- }
20
- };
21
- export {
22
- ProtocolImportsPlugin
23
- };
@@ -1,9 +0,0 @@
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
- export {
8
- __export
9
- };