@rsbuild/plugin-node-polyfill 1.1.0 → 1.3.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
@@ -186,6 +186,39 @@ pluginNodePolyfill({
186
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
+ ### force
205
+
206
+ By default, the plugin only polyfills the browser-side code. If you want to polyfill the server-side code as well (when `output.target` is `node`), you can set the `force` option to `true`.
207
+
208
+ - **Type:** `boolean`
209
+ - **Default:** `false`
210
+
211
+ ```ts
212
+ pluginNodePolyfill({
213
+ force: true,
214
+ });
215
+ ```
216
+
217
+ ## Exported variables
218
+
219
+ - `builtinMappingResolved`: A map of Node.js builtin modules to their resolved corresponding polyfills modules.
220
+ - `resolvedPolyfillToModules`: A map of resolved polyfill modules to the polyfill modules before resolving.
221
+
189
222
  ## License
190
223
 
191
224
  [MIT](./LICENSE).
@@ -0,0 +1,4 @@
1
+ import type { Rspack } from '@rsbuild/core';
2
+ export declare class ProtocolImportsPlugin {
3
+ apply(compiler: Rspack.Compiler): void;
4
+ }
package/dist/index.cjs CHANGED
@@ -1,208 +1,159 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- PLUGIN_NODE_POLYFILL_NAME: () => PLUGIN_NODE_POLYFILL_NAME,
24
- getProvideGlobals: () => getProvideGlobals,
25
- getResolveFallback: () => getResolveFallback,
26
- pluginNodePolyfill: () => pluginNodePolyfill
2
+ const __rslib_import_meta_url__ = /*#__PURE__*/ function() {
3
+ return 'undefined' == typeof document ? new (require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
4
+ }();
5
+ // The require scope
6
+ var __webpack_require__ = {};
7
+ /************************************************************************/ // webpack/runtime/define_property_getters
8
+ (()=>{
9
+ __webpack_require__.d = function(exports1, definition) {
10
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
11
+ enumerable: true,
12
+ get: definition[key]
13
+ });
14
+ };
15
+ })();
16
+ // webpack/runtime/has_own_property
17
+ (()=>{
18
+ __webpack_require__.o = function(obj, prop) {
19
+ return Object.prototype.hasOwnProperty.call(obj, prop);
20
+ };
21
+ })();
22
+ // webpack/runtime/make_namespace_object
23
+ (()=>{
24
+ // define __esModule on exports
25
+ __webpack_require__.r = function(exports1) {
26
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
27
+ value: 'Module'
28
+ });
29
+ Object.defineProperty(exports1, '__esModule', {
30
+ value: true
31
+ });
32
+ };
33
+ })();
34
+ /************************************************************************/ var __webpack_exports__ = {};
35
+ // ESM COMPAT FLAG
36
+ __webpack_require__.r(__webpack_exports__);
37
+ // EXPORTS
38
+ __webpack_require__.d(__webpack_exports__, {
39
+ PLUGIN_NODE_POLYFILL_NAME: ()=>/* binding */ PLUGIN_NODE_POLYFILL_NAME,
40
+ resolvedPolyfillToModules: ()=>/* reexport */ resolvedPolyfillToModules,
41
+ builtinMappingResolved: ()=>/* reexport */ builtinMappingResolved,
42
+ getResolveFallback: ()=>/* binding */ getResolveFallback,
43
+ pluginNodePolyfill: ()=>/* binding */ pluginNodePolyfill,
44
+ resolvePolyfill: ()=>/* binding */ resolvePolyfill,
45
+ getProvideGlobals: ()=>/* binding */ getProvideGlobals
27
46
  });
28
- module.exports = __toCommonJS(src_exports);
29
-
30
- // node_modules/.pnpm/tsup@8.3.0_postcss@8.4.47_typescript@5.6.3/node_modules/tsup/assets/cjs_shims.js
31
- var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
32
- var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
33
-
34
- // src/ProtocolImportsPlugin.ts
35
- var ProtocolImportsPlugin = class {
36
- apply(compiler) {
37
- compiler.hooks.normalModuleFactory.tap(
38
- "NormalModuleReplacementPlugin",
39
- (nmf) => {
40
- nmf.hooks.beforeResolve.tap(
41
- "NormalModuleReplacementPlugin",
42
- (resource) => {
43
- if (/^node:/.test(resource.request)) {
44
- resource.request = resource.request.replace(/^node:/, "");
45
- }
46
- }
47
- );
48
- }
49
- );
50
- }
47
+ class ProtocolImportsPlugin {
48
+ apply(compiler) {
49
+ compiler.hooks.normalModuleFactory.tap('NormalModuleReplacementPlugin', (nmf)=>{
50
+ nmf.hooks.beforeResolve.tap('NormalModuleReplacementPlugin', (resource)=>{
51
+ // Remove the `node:` prefix
52
+ // see: https://github.com/webpack/webpack/issues/14166
53
+ if (/^node:/.test(resource.request)) resource.request = resource.request.replace(/^node:/, '');
54
+ });
55
+ });
56
+ }
57
+ }
58
+ const external_node_module_namespaceObject = require("node:module");
59
+ const libs_require = (0, external_node_module_namespaceObject.createRequire)(__rslib_import_meta_url__);
60
+ const builtinMappingResolved = {
61
+ assert: libs_require.resolve('assert/'),
62
+ buffer: libs_require.resolve('buffer/'),
63
+ child_process: null,
64
+ cluster: null,
65
+ console: libs_require.resolve('console-browserify'),
66
+ constants: libs_require.resolve('constants-browserify'),
67
+ crypto: libs_require.resolve('crypto-browserify'),
68
+ dgram: null,
69
+ dns: null,
70
+ domain: libs_require.resolve('domain-browser'),
71
+ events: libs_require.resolve('events/'),
72
+ fs: null,
73
+ http: libs_require.resolve('stream-http'),
74
+ https: libs_require.resolve('https-browserify'),
75
+ module: null,
76
+ net: null,
77
+ os: libs_require.resolve('os-browserify/browser.js'),
78
+ path: libs_require.resolve('path-browserify'),
79
+ punycode: libs_require.resolve('punycode/'),
80
+ process: libs_require.resolve('process/browser.js'),
81
+ querystring: libs_require.resolve('querystring-es3/'),
82
+ readline: null,
83
+ repl: null,
84
+ stream: libs_require.resolve('stream-browserify'),
85
+ _stream_duplex: libs_require.resolve('readable-stream/lib/_stream_duplex.js'),
86
+ _stream_passthrough: libs_require.resolve('readable-stream/lib/_stream_passthrough.js'),
87
+ _stream_readable: libs_require.resolve('readable-stream/lib/_stream_readable.js'),
88
+ _stream_transform: libs_require.resolve('readable-stream/lib/_stream_transform.js'),
89
+ _stream_writable: libs_require.resolve('readable-stream/lib/_stream_writable.js'),
90
+ string_decoder: libs_require.resolve('string_decoder/'),
91
+ sys: libs_require.resolve('util/util.js'),
92
+ timers: libs_require.resolve('timers-browserify'),
93
+ tls: null,
94
+ tty: libs_require.resolve('tty-browserify'),
95
+ url: libs_require.resolve('url/'),
96
+ util: libs_require.resolve('util/util.js'),
97
+ vm: libs_require.resolve('vm-browserify'),
98
+ zlib: libs_require.resolve('browserify-zlib')
51
99
  };
52
-
53
- // 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
- var import_node_module = require("module");
96
- 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"
135
- );
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
-
146
- // src/index.ts
147
- var getResolveFallback = ({
148
- protocolImports,
149
- exclude,
150
- include
151
- }) => {
152
- if (exclude && include) {
153
- throw new Error("`include` is mutually exclusive with `exclude`.");
154
- }
155
- const resolvedNodeLibs = include ? include : Object.keys(libs_exports).filter((name) => {
156
- return !(exclude || []).includes(name);
157
- });
158
- const fallback = {};
159
- for (const name of resolvedNodeLibs) {
160
- const libPath = libs_exports[name];
161
- fallback[name] = libPath ?? false;
162
- if (protocolImports) {
163
- fallback[`node:${name}`] = fallback[name];
100
+ const resolvedPolyfillToModules = Object.fromEntries(Object.entries(builtinMappingResolved).filter(([key])=>null !== key).map(([key, value])=>[
101
+ value,
102
+ key
103
+ ]));
104
+ const resolvePolyfill = (libPath, overrides)=>{
105
+ if ((null == overrides ? void 0 : overrides[libPath]) !== void 0) return overrides[libPath];
106
+ return builtinMappingResolved[libPath];
107
+ };
108
+ const getResolveFallback = ({ protocolImports, exclude, include, overrides })=>{
109
+ if (exclude && include) throw new Error('`include` is mutually exclusive with `exclude`.');
110
+ const resolvedNodeLibs = include ? include : Object.keys(builtinMappingResolved).filter((name)=>!(exclude || []).includes(name));
111
+ const fallback = {};
112
+ for (const name of resolvedNodeLibs){
113
+ const libPath = resolvePolyfill(name, overrides);
114
+ fallback[name] = libPath ?? false;
115
+ if (protocolImports) fallback[`node:${name}`] = fallback[name];
164
116
  }
165
- }
166
- return fallback;
117
+ return fallback;
167
118
  };
168
- var getProvideGlobals = async (globals) => {
169
- const result = {};
170
- if (globals?.Buffer !== false) {
171
- result.Buffer = [buffer, "Buffer"];
172
- }
173
- if (globals?.process !== false) {
174
- result.process = [process];
175
- }
176
- return result;
119
+ const getProvideGlobals = async (globals, overrides)=>{
120
+ const result = {};
121
+ if ((null == globals ? void 0 : globals.Buffer) !== false) result.Buffer = [
122
+ resolvePolyfill('buffer', overrides),
123
+ 'Buffer'
124
+ ];
125
+ if ((null == globals ? void 0 : globals.process) !== false) result.process = [
126
+ resolvePolyfill('process', overrides)
127
+ ];
128
+ return result;
177
129
  };
178
- var PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
130
+ const PLUGIN_NODE_POLYFILL_NAME = 'rsbuild:node-polyfill';
179
131
  function pluginNodePolyfill(options = {}) {
180
- const { protocolImports = true, include, exclude } = options;
181
- return {
182
- name: PLUGIN_NODE_POLYFILL_NAME,
183
- setup(api) {
184
- api.modifyBundlerChain(async (chain, { isServer, bundler }) => {
185
- if (isServer) {
186
- return;
187
- }
188
- chain.resolve.fallback.merge(
189
- getResolveFallback({ protocolImports, include, exclude })
190
- );
191
- const provideGlobals = await getProvideGlobals(options.globals);
192
- if (Object.keys(provideGlobals).length) {
193
- chain.plugin("node-polyfill-provide").use(bundler.ProvidePlugin, [provideGlobals]);
132
+ const { protocolImports = true, include, exclude, overrides, force = false } = options;
133
+ return {
134
+ name: PLUGIN_NODE_POLYFILL_NAME,
135
+ setup (api) {
136
+ api.modifyBundlerChain(async (chain, { isServer, bundler })=>{
137
+ // The server bundle does not require node polyfill
138
+ if (isServer && !force) return;
139
+ // module polyfill
140
+ chain.resolve.fallback.merge(getResolveFallback({
141
+ protocolImports,
142
+ include: include,
143
+ exclude,
144
+ overrides
145
+ }));
146
+ const provideGlobals = await getProvideGlobals(options.globals, overrides);
147
+ if (Object.keys(provideGlobals).length) chain.plugin('node-polyfill-provide').use(bundler.ProvidePlugin, [
148
+ provideGlobals
149
+ ]);
150
+ if (protocolImports) chain.plugin('protocol-imports').use(ProtocolImportsPlugin);
151
+ });
194
152
  }
195
- if (protocolImports) {
196
- chain.plugin("protocol-imports").use(ProtocolImportsPlugin);
197
- }
198
- });
199
- }
200
- };
153
+ };
201
154
  }
202
- // Annotate the CommonJS export names for ESM import in node:
203
- 0 && (module.exports = {
204
- PLUGIN_NODE_POLYFILL_NAME,
205
- getProvideGlobals,
206
- getResolveFallback,
207
- pluginNodePolyfill
155
+ var __webpack_export_target__ = exports;
156
+ for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
157
+ if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
158
+ value: true
208
159
  });
package/dist/index.d.ts CHANGED
@@ -1,10 +1,9 @@
1
- import { RsbuildPlugin } from '@rsbuild/core';
2
-
1
+ import type { RsbuildPlugin } from '@rsbuild/core';
3
2
  type Globals = {
4
3
  process?: boolean;
5
4
  Buffer?: boolean;
6
5
  };
7
- type PluginNodePolyfillOptions = {
6
+ export type PluginNodePolyfillOptions = {
8
7
  /**
9
8
  * Whether to provide polyfill of globals.
10
9
  * @default
@@ -32,10 +31,22 @@ type PluginNodePolyfillOptions = {
32
31
  * @default undefined
33
32
  */
34
33
  include?: string[];
34
+ /**
35
+ * Override the default polyfills for specific modules.
36
+ * @default undefined
37
+ */
38
+ overrides?: Record<string, string | false>;
39
+ /**
40
+ * By default, the plugin only polyfills the browser-side code.
41
+ * If you want to polyfill the server-side code as well (when `output.target` is `node`),
42
+ * you can set the `force` option to `true`.
43
+ * @default false
44
+ */
45
+ force?: boolean;
35
46
  };
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[]>>;
38
- declare const PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
39
- declare function pluginNodePolyfill(options?: PluginNodePolyfillOptions): RsbuildPlugin;
40
-
41
- export { PLUGIN_NODE_POLYFILL_NAME, type PluginNodePolyfillOptions, getProvideGlobals, getResolveFallback, pluginNodePolyfill };
47
+ export declare const resolvePolyfill: (libPath: string, overrides?: PluginNodePolyfillOptions["overrides"]) => string | false | null;
48
+ export declare const getResolveFallback: ({ protocolImports, exclude, include, overrides, }: Pick<PluginNodePolyfillOptions, "protocolImports" | "exclude" | "include" | "overrides">) => Record<string, string | false>;
49
+ export declare const getProvideGlobals: (globals?: Globals, overrides?: PluginNodePolyfillOptions["overrides"]) => Promise<Record<string, string | string[]>>;
50
+ export declare const PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
51
+ export declare function pluginNodePolyfill(options?: PluginNodePolyfillOptions): RsbuildPlugin;
52
+ export { builtinMappingResolved, resolvedPolyfillToModules, } from './libs.js';
package/dist/index.js CHANGED
@@ -1,180 +1,109 @@
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 });
1
+ import * as __WEBPACK_EXTERNAL_MODULE_node_module__ from "node:module";
2
+ class ProtocolImportsPlugin {
3
+ apply(compiler) {
4
+ compiler.hooks.normalModuleFactory.tap('NormalModuleReplacementPlugin', (nmf)=>{
5
+ nmf.hooks.beforeResolve.tap('NormalModuleReplacementPlugin', (resource)=>{
6
+ // Remove the `node:` prefix
7
+ // see: https://github.com/webpack/webpack/issues/14166
8
+ if (/^node:/.test(resource.request)) resource.request = resource.request.replace(/^node:/, '');
9
+ });
10
+ });
11
+ }
12
+ }
13
+ const libs_require = (0, __WEBPACK_EXTERNAL_MODULE_node_module__.createRequire)(import.meta.url);
14
+ const builtinMappingResolved = {
15
+ assert: libs_require.resolve('assert/'),
16
+ buffer: libs_require.resolve('buffer/'),
17
+ child_process: null,
18
+ cluster: null,
19
+ console: libs_require.resolve('console-browserify'),
20
+ constants: libs_require.resolve('constants-browserify'),
21
+ crypto: libs_require.resolve('crypto-browserify'),
22
+ dgram: null,
23
+ dns: null,
24
+ domain: libs_require.resolve('domain-browser'),
25
+ events: libs_require.resolve('events/'),
26
+ fs: null,
27
+ http: libs_require.resolve('stream-http'),
28
+ https: libs_require.resolve('https-browserify'),
29
+ module: null,
30
+ net: null,
31
+ os: libs_require.resolve('os-browserify/browser.js'),
32
+ path: libs_require.resolve('path-browserify'),
33
+ punycode: libs_require.resolve('punycode/'),
34
+ process: libs_require.resolve('process/browser.js'),
35
+ querystring: libs_require.resolve('querystring-es3/'),
36
+ readline: null,
37
+ repl: null,
38
+ stream: libs_require.resolve('stream-browserify'),
39
+ _stream_duplex: libs_require.resolve('readable-stream/lib/_stream_duplex.js'),
40
+ _stream_passthrough: libs_require.resolve('readable-stream/lib/_stream_passthrough.js'),
41
+ _stream_readable: libs_require.resolve('readable-stream/lib/_stream_readable.js'),
42
+ _stream_transform: libs_require.resolve('readable-stream/lib/_stream_transform.js'),
43
+ _stream_writable: libs_require.resolve('readable-stream/lib/_stream_writable.js'),
44
+ string_decoder: libs_require.resolve('string_decoder/'),
45
+ sys: libs_require.resolve('util/util.js'),
46
+ timers: libs_require.resolve('timers-browserify'),
47
+ tls: null,
48
+ tty: libs_require.resolve('tty-browserify'),
49
+ url: libs_require.resolve('url/'),
50
+ util: libs_require.resolve('util/util.js'),
51
+ vm: libs_require.resolve('vm-browserify'),
52
+ zlib: libs_require.resolve('browserify-zlib')
5
53
  };
6
-
7
- // src/ProtocolImportsPlugin.ts
8
- var ProtocolImportsPlugin = class {
9
- apply(compiler) {
10
- compiler.hooks.normalModuleFactory.tap(
11
- "NormalModuleReplacementPlugin",
12
- (nmf) => {
13
- nmf.hooks.beforeResolve.tap(
14
- "NormalModuleReplacementPlugin",
15
- (resource) => {
16
- if (/^node:/.test(resource.request)) {
17
- resource.request = resource.request.replace(/^node:/, "");
18
- }
19
- }
20
- );
21
- }
22
- );
23
- }
54
+ const resolvedPolyfillToModules = Object.fromEntries(Object.entries(builtinMappingResolved).filter(([key])=>null !== key).map(([key, value])=>[
55
+ value,
56
+ key
57
+ ]));
58
+ const resolvePolyfill = (libPath, overrides)=>{
59
+ if ((null == overrides ? void 0 : overrides[libPath]) !== void 0) return overrides[libPath];
60
+ return builtinMappingResolved[libPath];
24
61
  };
25
-
26
- // 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
- import { createRequire } from "node:module";
69
- 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"
108
- );
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
-
119
- // src/index.ts
120
- var getResolveFallback = ({
121
- protocolImports,
122
- exclude,
123
- include
124
- }) => {
125
- if (exclude && include) {
126
- throw new Error("`include` is mutually exclusive with `exclude`.");
127
- }
128
- const resolvedNodeLibs = include ? include : Object.keys(libs_exports).filter((name) => {
129
- return !(exclude || []).includes(name);
130
- });
131
- const fallback = {};
132
- for (const name of resolvedNodeLibs) {
133
- const libPath = libs_exports[name];
134
- fallback[name] = libPath ?? false;
135
- if (protocolImports) {
136
- fallback[`node:${name}`] = fallback[name];
62
+ const getResolveFallback = ({ protocolImports, exclude, include, overrides })=>{
63
+ if (exclude && include) throw new Error('`include` is mutually exclusive with `exclude`.');
64
+ const resolvedNodeLibs = include ? include : Object.keys(builtinMappingResolved).filter((name)=>!(exclude || []).includes(name));
65
+ const fallback = {};
66
+ for (const name of resolvedNodeLibs){
67
+ const libPath = resolvePolyfill(name, overrides);
68
+ fallback[name] = libPath ?? false;
69
+ if (protocolImports) fallback[`node:${name}`] = fallback[name];
137
70
  }
138
- }
139
- return fallback;
71
+ return fallback;
140
72
  };
141
- var getProvideGlobals = async (globals) => {
142
- const result = {};
143
- if (globals?.Buffer !== false) {
144
- result.Buffer = [buffer, "Buffer"];
145
- }
146
- if (globals?.process !== false) {
147
- result.process = [process];
148
- }
149
- return result;
73
+ const getProvideGlobals = async (globals, overrides)=>{
74
+ const result = {};
75
+ if ((null == globals ? void 0 : globals.Buffer) !== false) result.Buffer = [
76
+ resolvePolyfill('buffer', overrides),
77
+ 'Buffer'
78
+ ];
79
+ if ((null == globals ? void 0 : globals.process) !== false) result.process = [
80
+ resolvePolyfill('process', overrides)
81
+ ];
82
+ return result;
150
83
  };
151
- var PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
84
+ const PLUGIN_NODE_POLYFILL_NAME = 'rsbuild:node-polyfill';
152
85
  function pluginNodePolyfill(options = {}) {
153
- const { protocolImports = true, include, exclude } = options;
154
- return {
155
- name: PLUGIN_NODE_POLYFILL_NAME,
156
- setup(api) {
157
- api.modifyBundlerChain(async (chain, { isServer, bundler }) => {
158
- if (isServer) {
159
- return;
160
- }
161
- chain.resolve.fallback.merge(
162
- getResolveFallback({ protocolImports, include, exclude })
163
- );
164
- const provideGlobals = await getProvideGlobals(options.globals);
165
- if (Object.keys(provideGlobals).length) {
166
- chain.plugin("node-polyfill-provide").use(bundler.ProvidePlugin, [provideGlobals]);
86
+ const { protocolImports = true, include, exclude, overrides, force = false } = options;
87
+ return {
88
+ name: PLUGIN_NODE_POLYFILL_NAME,
89
+ setup (api) {
90
+ api.modifyBundlerChain(async (chain, { isServer, bundler })=>{
91
+ // The server bundle does not require node polyfill
92
+ if (isServer && !force) return;
93
+ // module polyfill
94
+ chain.resolve.fallback.merge(getResolveFallback({
95
+ protocolImports,
96
+ include: include,
97
+ exclude,
98
+ overrides
99
+ }));
100
+ const provideGlobals = await getProvideGlobals(options.globals, overrides);
101
+ if (Object.keys(provideGlobals).length) chain.plugin('node-polyfill-provide').use(bundler.ProvidePlugin, [
102
+ provideGlobals
103
+ ]);
104
+ if (protocolImports) chain.plugin('protocol-imports').use(ProtocolImportsPlugin);
105
+ });
167
106
  }
168
- if (protocolImports) {
169
- chain.plugin("protocol-imports").use(ProtocolImportsPlugin);
170
- }
171
- });
172
- }
173
- };
107
+ };
174
108
  }
175
- export {
176
- PLUGIN_NODE_POLYFILL_NAME,
177
- getProvideGlobals,
178
- getResolveFallback,
179
- pluginNodePolyfill
180
- };
109
+ export { PLUGIN_NODE_POLYFILL_NAME, builtinMappingResolved, getProvideGlobals, getResolveFallback, pluginNodePolyfill, resolvePolyfill, resolvedPolyfillToModules };
package/dist/libs.d.ts ADDED
@@ -0,0 +1,41 @@
1
+ export declare const builtinMappingResolved: {
2
+ readonly assert: string;
3
+ readonly buffer: string;
4
+ readonly child_process: null;
5
+ readonly cluster: null;
6
+ readonly console: string;
7
+ readonly constants: string;
8
+ readonly crypto: string;
9
+ readonly dgram: null;
10
+ readonly dns: null;
11
+ readonly domain: string;
12
+ readonly events: string;
13
+ readonly fs: null;
14
+ readonly http: string;
15
+ readonly https: string;
16
+ readonly module: null;
17
+ readonly net: null;
18
+ readonly os: string;
19
+ readonly path: string;
20
+ readonly punycode: string;
21
+ readonly process: string;
22
+ readonly querystring: string;
23
+ readonly readline: null;
24
+ readonly repl: null;
25
+ readonly stream: string;
26
+ readonly _stream_duplex: string;
27
+ readonly _stream_passthrough: string;
28
+ readonly _stream_readable: string;
29
+ readonly _stream_transform: string;
30
+ readonly _stream_writable: string;
31
+ readonly string_decoder: string;
32
+ readonly sys: string;
33
+ readonly timers: string;
34
+ readonly tls: null;
35
+ readonly tty: string;
36
+ readonly url: string;
37
+ readonly util: string;
38
+ readonly vm: string;
39
+ readonly zlib: string;
40
+ };
41
+ export declare const resolvedPolyfillToModules: any;
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.3.0",
4
4
  "repository": "https://github.com/rspack-contrib/rsbuild-plugin-node-polyfill",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -16,8 +16,8 @@
16
16
  "types": "./dist/index.d.ts",
17
17
  "files": ["dist"],
18
18
  "scripts": {
19
- "build": "tsup",
20
- "dev": "tsup --watch",
19
+ "build": "rslib build",
20
+ "dev": "rslib build --watch",
21
21
  "lint": "biome check .",
22
22
  "lint:write": "biome check . --write",
23
23
  "prepare": "simple-git-hooks && npm run build",
@@ -40,7 +40,7 @@
40
40
  "buffer": "^5.7.1",
41
41
  "console-browserify": "^1.2.0",
42
42
  "constants-browserify": "^1.0.0",
43
- "crypto-browserify": "^3.12.0",
43
+ "crypto-browserify": "^3.12.1",
44
44
  "domain-browser": "^5.7.0",
45
45
  "events": "^3.3.0",
46
46
  "https-browserify": "^1.0.0",
@@ -61,25 +61,25 @@
61
61
  },
62
62
  "devDependencies": {
63
63
  "@biomejs/biome": "^1.9.4",
64
- "@playwright/test": "^1.48.1",
65
- "@rsbuild/core": "^1.0.15",
66
- "@types/node": "^20.16.13",
64
+ "@playwright/test": "^1.49.0",
65
+ "@rsbuild/core": "^1.1.6",
66
+ "@rslib/core": "^0.1.1",
67
+ "@types/node": "^22.10.1",
67
68
  "nano-staged": "^0.8.0",
68
- "playwright": "^1.48.1",
69
+ "playwright": "^1.49.0",
69
70
  "simple-git-hooks": "^2.11.1",
70
- "tsup": "^8.3.0",
71
- "typescript": "^5.6.3",
72
- "vitest": "^2.1.3"
71
+ "typescript": "^5.7.2",
72
+ "vitest": "^2.1.6"
73
73
  },
74
74
  "peerDependencies": {
75
- "@rsbuild/core": "1.x || ^1.0.1-beta.0"
75
+ "@rsbuild/core": "1.x"
76
76
  },
77
77
  "peerDependenciesMeta": {
78
78
  "@rsbuild/core": {
79
79
  "optional": true
80
80
  }
81
81
  },
82
- "packageManager": "pnpm@9.12.2",
82
+ "packageManager": "pnpm@9.14.4",
83
83
  "publishConfig": {
84
84
  "access": "public",
85
85
  "registry": "https://registry.npmjs.org/"
package/dist/index.d.cts DELETED
@@ -1,41 +0,0 @@
1
- import { RsbuildPlugin } from '@rsbuild/core';
2
-
3
- type Globals = {
4
- process?: boolean;
5
- Buffer?: boolean;
6
- };
7
- type PluginNodePolyfillOptions = {
8
- /**
9
- * Whether to provide polyfill of globals.
10
- * @default
11
- * {
12
- * Buffer: true,
13
- * process: true,
14
- * }
15
- */
16
- globals?: Globals;
17
- /**
18
- * Whether to polyfill Node.js builtin modules starting with `node:`.
19
- * @see https://nodejs.org/api/esm.html#node-imports
20
- * @default true
21
- */
22
- protocolImports?: boolean;
23
- /**
24
- * Exclude certain modules to be polyfilled.
25
- * This option is mutually exclusive with {@link PluginNodePolyfillOptions.include | `include`}.
26
- * @default undefined
27
- */
28
- exclude?: string[];
29
- /**
30
- * Only include certain modules to be polyfilled.
31
- * This option is mutually exclusive with {@link PluginNodePolyfillOptions.exclude | `exclude`}.
32
- * @default undefined
33
- */
34
- include?: string[];
35
- };
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[]>>;
38
- declare const PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
39
- declare function pluginNodePolyfill(options?: PluginNodePolyfillOptions): RsbuildPlugin;
40
-
41
- export { PLUGIN_NODE_POLYFILL_NAME, type PluginNodePolyfillOptions, getProvideGlobals, getResolveFallback, pluginNodePolyfill };