@rsbuild/plugin-node-polyfill 1.2.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
@@ -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,8 +182,8 @@ 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
189
  ### overrides
@@ -196,9 +196,22 @@ Override the default polyfills for specific modules.
196
196
  ```ts
197
197
  pluginNodePolyfill({
198
198
  overrides: {
199
- fs: 'memfs',
199
+ fs: "memfs",
200
200
  },
201
- })
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
+ });
202
215
  ```
203
216
 
204
217
  ## Exported variables
@@ -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,187 +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
- builtinMappingResolved: () => builtinMappingResolved,
25
- getProvideGlobals: () => getProvideGlobals,
26
- getResolveFallback: () => getResolveFallback,
27
- pluginNodePolyfill: () => pluginNodePolyfill,
28
- resolvePolyfill: () => resolvePolyfill,
29
- resolvedPolyfillToModules: () => resolvedPolyfillToModules
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
30
46
  });
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();
36
-
37
- // src/ProtocolImportsPlugin.ts
38
- var ProtocolImportsPlugin = class {
39
- apply(compiler) {
40
- compiler.hooks.normalModuleFactory.tap(
41
- "NormalModuleReplacementPlugin",
42
- (nmf) => {
43
- nmf.hooks.beforeResolve.tap(
44
- "NormalModuleReplacementPlugin",
45
- (resource) => {
46
- if (/^node:/.test(resource.request)) {
47
- resource.request = resource.request.replace(/^node:/, "");
48
- }
49
- }
50
- );
51
- }
52
- );
53
- }
54
- };
55
-
56
- // src/libs.ts
57
- var import_node_module = require("module");
58
- var require2 = (0, import_node_module.createRequire)(importMetaUrl);
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")
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')
102
99
  };
103
- var resolvedPolyfillToModules = Object.fromEntries(
104
- Object.entries(builtinMappingResolved).filter(([key]) => key !== null).map(([key, value]) => [value, key])
105
- );
106
-
107
- // src/index.ts
108
- var resolvePolyfill = (libPath, overrides) => {
109
- if (overrides?.[libPath] !== void 0) {
110
- return overrides[libPath];
111
- }
112
- return builtinMappingResolved[libPath];
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];
113
107
  };
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
- });
126
- const fallback = {};
127
- for (const name of resolvedNodeLibs) {
128
- const libPath = resolvePolyfill(name, overrides);
129
- fallback[name] = libPath ?? false;
130
- if (protocolImports) {
131
- fallback[`node:${name}`] = fallback[name];
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];
132
116
  }
133
- }
134
- return fallback;
117
+ return fallback;
135
118
  };
136
- var getProvideGlobals = async (globals, overrides) => {
137
- const result = {};
138
- if (globals?.Buffer !== false) {
139
- result.Buffer = [resolvePolyfill("buffer", overrides), "Buffer"];
140
- }
141
- if (globals?.process !== false) {
142
- result.process = [resolvePolyfill("process", overrides)];
143
- }
144
- 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;
145
129
  };
146
- var PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
130
+ const PLUGIN_NODE_POLYFILL_NAME = 'rsbuild:node-polyfill';
147
131
  function pluginNodePolyfill(options = {}) {
148
- const { protocolImports = true, include, exclude, overrides } = options;
149
- return {
150
- name: PLUGIN_NODE_POLYFILL_NAME,
151
- setup(api) {
152
- api.modifyBundlerChain(async (chain, { isServer, bundler }) => {
153
- if (isServer) {
154
- return;
155
- }
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
- );
168
- if (Object.keys(provideGlobals).length) {
169
- 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
+ });
170
152
  }
171
- if (protocolImports) {
172
- chain.plugin("protocol-imports").use(ProtocolImportsPlugin);
173
- }
174
- });
175
- }
176
- };
153
+ };
177
154
  }
178
- // Annotate the CommonJS export names for ESM import in node:
179
- 0 && (module.exports = {
180
- PLUGIN_NODE_POLYFILL_NAME,
181
- builtinMappingResolved,
182
- getProvideGlobals,
183
- getResolveFallback,
184
- pluginNodePolyfill,
185
- resolvePolyfill,
186
- resolvedPolyfillToModules
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
187
159
  });
package/dist/index.d.ts CHANGED
@@ -1,52 +1,9 @@
1
- import { RsbuildPlugin } from '@rsbuild/core';
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
-
1
+ import type { RsbuildPlugin } from '@rsbuild/core';
45
2
  type Globals = {
46
3
  process?: boolean;
47
4
  Buffer?: boolean;
48
5
  };
49
- type PluginNodePolyfillOptions = {
6
+ export type PluginNodePolyfillOptions = {
50
7
  /**
51
8
  * Whether to provide polyfill of globals.
52
9
  * @default
@@ -79,11 +36,17 @@ type PluginNodePolyfillOptions = {
79
36
  * @default undefined
80
37
  */
81
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;
82
46
  };
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[]>>;
86
- declare const PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
87
- declare function pluginNodePolyfill(options?: PluginNodePolyfillOptions): RsbuildPlugin;
88
-
89
- export { PLUGIN_NODE_POLYFILL_NAME, type PluginNodePolyfillOptions, builtinMappingResolved, getProvideGlobals, getResolveFallback, pluginNodePolyfill, resolvePolyfill, resolvedPolyfillToModules };
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,150 +1,109 @@
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
- };
19
-
20
- // src/libs.ts
21
- import { createRequire } from "node:module";
22
- var require2 = createRequire(import.meta.url);
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")
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')
66
53
  };
67
- var resolvedPolyfillToModules = Object.fromEntries(
68
- Object.entries(builtinMappingResolved).filter(([key]) => key !== null).map(([key, value]) => [value, key])
69
- );
70
-
71
- // src/index.ts
72
- var resolvePolyfill = (libPath, overrides) => {
73
- if (overrides?.[libPath] !== void 0) {
74
- return overrides[libPath];
75
- }
76
- return builtinMappingResolved[libPath];
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];
77
61
  };
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
- });
90
- const fallback = {};
91
- for (const name of resolvedNodeLibs) {
92
- const libPath = resolvePolyfill(name, overrides);
93
- fallback[name] = libPath ?? false;
94
- if (protocolImports) {
95
- 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];
96
70
  }
97
- }
98
- return fallback;
71
+ return fallback;
99
72
  };
100
- var getProvideGlobals = async (globals, overrides) => {
101
- const result = {};
102
- if (globals?.Buffer !== false) {
103
- result.Buffer = [resolvePolyfill("buffer", overrides), "Buffer"];
104
- }
105
- if (globals?.process !== false) {
106
- result.process = [resolvePolyfill("process", overrides)];
107
- }
108
- 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;
109
83
  };
110
- var PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
84
+ const PLUGIN_NODE_POLYFILL_NAME = 'rsbuild:node-polyfill';
111
85
  function pluginNodePolyfill(options = {}) {
112
- const { protocolImports = true, include, exclude, overrides } = options;
113
- return {
114
- name: PLUGIN_NODE_POLYFILL_NAME,
115
- setup(api) {
116
- api.modifyBundlerChain(async (chain, { isServer, bundler }) => {
117
- if (isServer) {
118
- return;
119
- }
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
- if (Object.keys(provideGlobals).length) {
133
- 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
+ });
134
106
  }
135
- if (protocolImports) {
136
- chain.plugin("protocol-imports").use(ProtocolImportsPlugin);
137
- }
138
- });
139
- }
140
- };
107
+ };
141
108
  }
142
- export {
143
- PLUGIN_NODE_POLYFILL_NAME,
144
- builtinMappingResolved,
145
- getProvideGlobals,
146
- getResolveFallback,
147
- pluginNodePolyfill,
148
- resolvePolyfill,
149
- resolvedPolyfillToModules
150
- };
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.2.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,89 +0,0 @@
1
- import { RsbuildPlugin } from '@rsbuild/core';
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
-
45
- type Globals = {
46
- process?: boolean;
47
- Buffer?: boolean;
48
- };
49
- type PluginNodePolyfillOptions = {
50
- /**
51
- * Whether to provide polyfill of globals.
52
- * @default
53
- * {
54
- * Buffer: true,
55
- * process: true,
56
- * }
57
- */
58
- globals?: Globals;
59
- /**
60
- * Whether to polyfill Node.js builtin modules starting with `node:`.
61
- * @see https://nodejs.org/api/esm.html#node-imports
62
- * @default true
63
- */
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>;
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[]>>;
86
- declare const PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
87
- declare function pluginNodePolyfill(options?: PluginNodePolyfillOptions): RsbuildPlugin;
88
-
89
- export { PLUGIN_NODE_POLYFILL_NAME, type PluginNodePolyfillOptions, builtinMappingResolved, getProvideGlobals, getResolveFallback, pluginNodePolyfill, resolvePolyfill, resolvedPolyfillToModules };