@rsbuild/plugin-node-polyfill 1.0.0 → 1.0.3

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023-present Bytedance, Inc. and its affiliates.
3
+ Copyright (c) 2024 Rspack Contrib
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,19 +1,164 @@
1
- <p align="center">
2
- <a href="https://rsbuild.dev" target="blank"><img src="https://github.com/web-infra-dev/rsbuild/assets/7237365/84abc13e-b620-468f-a90b-dbf28e7e9427" alt="Rsbuild Logo" /></a>
1
+ # @rsbuild/plugin-node-polyfill
2
+
3
+ An Rsbuild plugin to automatically inject polyfills for [Node.js builtin modules](https://nodejs.org/api/modules.html#built-in-modules) into the browser side.
4
+
5
+ <p>
6
+ <a href="https://npmjs.com/package/@rsbuild/plugin-node-polyfill">
7
+ <img src="https://img.shields.io/npm/v/@rsbuild/plugin-node-polyfill?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
8
+ </a>
9
+ <img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
3
10
  </p>
4
11
 
5
- # Rsbuild
12
+ ## When to use
13
+
14
+ Normally, we don't need to use Node builtin modules on the browser side. However, it is possible to use some Node builtin modules when the code will run on both the Node side and the browser side, and this plugin provides browser versions of polyfills for these Node builtin modules.
15
+
16
+ By using the Node Polyfill plugin, polyfills for Node builtin modules are automatically injected into the browser-side, allowing you to use these modules on the browser side with confidence.
17
+
18
+ ## Usage
19
+
20
+ Install:
21
+
22
+ ```bash
23
+ npm add @rsbuild/plugin-node-polyfill -D
24
+ ```
25
+
26
+ Add plugin to your `rsbuild.config.ts`:
27
+
28
+ ```ts
29
+ // rsbuild.config.ts
30
+ import { pluginNodePolyfill } from "@rsbuild/plugin-node-polyfill";
31
+
32
+ export default {
33
+ plugins: [pluginNodePolyfill()],
34
+ };
35
+ ```
36
+
37
+ ## Node Polyfills
38
+
39
+ ### Globals
40
+
41
+ - `Buffer`
42
+ - `process`
43
+
44
+ When you use the above global variables in your code, the corresponding polyfill will be automatically injected.
45
+
46
+ For instance, the following code would inject the `Buffer` polyfill:
47
+
48
+ ```ts
49
+ const bufferData = Buffer.from("abc");
50
+ ```
51
+
52
+ You can disable this behavior through the `globals` option of the plugin:
53
+
54
+ ```ts
55
+ pluginNodePolyfill({
56
+ globals: {
57
+ Buffer: false,
58
+ process: false,
59
+ },
60
+ });
61
+ ```
62
+
63
+ ### Modules
64
+
65
+ - `assert`
66
+ - `buffer`
67
+ - `console`
68
+ - `constants`
69
+ - `crypto`
70
+ - `domain`
71
+ - `events`
72
+ - `http`
73
+ - `https`
74
+ - `os`
75
+ - `path`
76
+ - `punycode`
77
+ - `process`
78
+ - `querystring`
79
+ - `stream`
80
+ - `_stream_duplex`
81
+ - `_stream_passthrough`
82
+ - `_stream_readable`
83
+ - `_stream_transform`
84
+ - `_stream_writable`
85
+ - `string_decoder`
86
+ - `sys`
87
+ - `timers`
88
+ - `tty`
89
+ - `url`
90
+ - `util`
91
+ - `vm`
92
+ - `zlib`
93
+
94
+ When the above module is referenced in code via import / require syntax, the corresponding polyfill will be injected.
95
+
96
+ ```ts
97
+ import { Buffer } from "buffer";
98
+
99
+ const bufferData = Buffer.from("abc");
100
+ ```
101
+
102
+ ### Fallbacks
103
+
104
+ - `child_process`
105
+ - `cluster`
106
+ - `dgram`
107
+ - `dns`
108
+ - `fs`
109
+ - `module`
110
+ - `net`
111
+ - `readline`
112
+ - `repl`
113
+ - `tls`
114
+
115
+ 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
+ ```ts
118
+ import fs from "fs";
119
+
120
+ console.log(fs); // -> {}
121
+ ```
122
+
123
+ ## Options
124
+
125
+ ### globals
126
+
127
+ Used to specify whether to inject polyfills for global variables.
128
+
129
+ - **Type:**
130
+
131
+ ```ts
132
+ type Globals = {
133
+ process?: boolean;
134
+ Buffer?: boolean;
135
+ };
136
+ ```
137
+
138
+ - **Default:**
139
+
140
+ ```ts
141
+ const defaultGlobals = {
142
+ Buffer: true,
143
+ process: true,
144
+ };
145
+ ```
6
146
 
7
- Unleash the power of Rspack with the out-of-the-box build tool.
147
+ ### protocolImports
8
148
 
9
- ## Documentation
149
+ Whether to polyfill Node.js builtin modules starting with `node:`.
10
150
 
11
- https://rsbuild.dev/
151
+ - **Type:** `boolean`
152
+ - **Default:** `true`
12
153
 
13
- ## Contributing
154
+ For example, if you disable `protocolImports`, modules such as `node:path`, `node:http`, etc. will not be polyfilled.
14
155
 
15
- Please read the [Contributing Guide](https://github.com/web-infra-dev/rsbuild/blob/main/CONTRIBUTING.md).
156
+ ```ts
157
+ pluginNodePolyfill({
158
+ protocolImports: false,
159
+ });
160
+ ```
16
161
 
17
162
  ## License
18
163
 
19
- Rsbuild is [MIT licensed](https://github.com/web-infra-dev/rsbuild/blob/main/LICENSE).
164
+ [MIT](./LICENSE).
@@ -0,0 +1,23 @@
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
+ };
@@ -0,0 +1,9 @@
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
+ };
package/dist/index.cjs ADDED
@@ -0,0 +1,215 @@
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 __esm = (fn, res) => function __init() {
7
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
8
+ };
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+
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
+ }
31
+ });
32
+
33
+ // 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(
46
+ "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
+ );
56
+ }
57
+ );
58
+ }
59
+ };
60
+ }
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();
71
+
72
+ // 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
+ var import_node_module = require("module");
116
+ 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"
155
+ );
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
+
166
+ // src/index.ts
167
+ var getResolveFallback = (protocolImports) => {
168
+ const fallback = {};
169
+ for (const name of Object.keys(libs_exports)) {
170
+ const libPath = libs_exports[name];
171
+ fallback[name] = libPath ?? false;
172
+ if (protocolImports) {
173
+ fallback[`node:${name}`] = fallback[name];
174
+ }
175
+ }
176
+ return fallback;
177
+ };
178
+ var getProvideGlobals = async (globals) => {
179
+ const result = {};
180
+ if (globals?.Buffer !== false) {
181
+ result.Buffer = [buffer, "Buffer"];
182
+ }
183
+ if (globals?.process !== false) {
184
+ result.process = [process];
185
+ }
186
+ return result;
187
+ };
188
+ var PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
189
+ function pluginNodePolyfill(options = {}) {
190
+ const { protocolImports = true } = options;
191
+ return {
192
+ name: PLUGIN_NODE_POLYFILL_NAME,
193
+ setup(api) {
194
+ api.modifyBundlerChain(async (chain, { isServer, bundler }) => {
195
+ if (isServer) {
196
+ return;
197
+ }
198
+ chain.resolve.fallback.merge(getResolveFallback(protocolImports));
199
+ const provideGlobals = await getProvideGlobals(options.globals);
200
+ if (Object.keys(provideGlobals).length) {
201
+ chain.plugin("node-polyfill-provide").use(bundler.ProvidePlugin, [provideGlobals]);
202
+ }
203
+ if (protocolImports) {
204
+ const { ProtocolImportsPlugin: ProtocolImportsPlugin2 } = await Promise.resolve().then(() => (init_ProtocolImportsPlugin(), ProtocolImportsPlugin_exports));
205
+ chain.plugin("protocol-imports").use(ProtocolImportsPlugin2);
206
+ }
207
+ });
208
+ }
209
+ };
210
+ }
211
+ // Annotate the CommonJS export names for ESM import in node:
212
+ 0 && (module.exports = {
213
+ PLUGIN_NODE_POLYFILL_NAME,
214
+ pluginNodePolyfill
215
+ });
@@ -0,0 +1,27 @@
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
+ declare const PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
25
+ declare function pluginNodePolyfill(options?: PluginNodePolyfillOptions): RsbuildPlugin;
26
+
27
+ export { PLUGIN_NODE_POLYFILL_NAME, type PluginNodePolyfillOptions, pluginNodePolyfill };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,27 @@
1
1
  import { RsbuildPlugin } from '@rsbuild/core';
2
2
 
3
- declare function pluginNodePolyfill(): RsbuildPlugin;
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
+ declare const PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
25
+ declare function pluginNodePolyfill(options?: PluginNodePolyfillOptions): RsbuildPlugin;
4
26
 
5
- export { pluginNodePolyfill };
27
+ export { PLUGIN_NODE_POLYFILL_NAME, type PluginNodePolyfillOptions, pluginNodePolyfill };
package/dist/index.js CHANGED
@@ -1,78 +1,146 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
+ import {
2
+ __export
3
+ } from "./chunk-6C3VEZWH.js";
29
4
 
30
- // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
33
- pluginNodePolyfill: () => pluginNodePolyfill
5
+ // 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
34
46
  });
35
- module.exports = __toCommonJS(src_exports);
36
- var getResolveFallback = (nodeLibs) => Object.keys(nodeLibs).reduce(
37
- (previous, name) => {
38
- if (nodeLibs[name]) {
39
- previous[name] = nodeLibs[name];
40
- } else {
41
- previous[name] = false;
42
- }
43
- return previous;
44
- },
45
- {}
47
+ import { createRequire } from "node:module";
48
+ 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"
46
75
  );
47
- var getProvideLibs = async () => {
48
- const { default: nodeLibs } = await import(
49
- // @ts-expect-error
50
- "node-libs-browser"
51
- );
52
- return {
53
- Buffer: [nodeLibs.buffer, "Buffer"],
54
- process: [nodeLibs.process]
55
- };
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"
87
+ );
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
+
98
+ // src/index.ts
99
+ var getResolveFallback = (protocolImports) => {
100
+ const fallback = {};
101
+ for (const name of Object.keys(libs_exports)) {
102
+ const libPath = libs_exports[name];
103
+ fallback[name] = libPath ?? false;
104
+ if (protocolImports) {
105
+ fallback[`node:${name}`] = fallback[name];
106
+ }
107
+ }
108
+ return fallback;
56
109
  };
57
- function pluginNodePolyfill() {
110
+ var getProvideGlobals = async (globals) => {
111
+ const result = {};
112
+ if (globals?.Buffer !== false) {
113
+ result.Buffer = [buffer, "Buffer"];
114
+ }
115
+ if (globals?.process !== false) {
116
+ result.process = [process];
117
+ }
118
+ return result;
119
+ };
120
+ var PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
121
+ function pluginNodePolyfill(options = {}) {
122
+ const { protocolImports = true } = options;
58
123
  return {
59
- name: "rsbuild:node-polyfill",
124
+ name: PLUGIN_NODE_POLYFILL_NAME,
60
125
  setup(api) {
61
- api.modifyBundlerChain(async (chain, { CHAIN_ID, isServer, bundler }) => {
126
+ api.modifyBundlerChain(async (chain, { isServer, bundler }) => {
62
127
  if (isServer) {
63
128
  return;
64
129
  }
65
- const { default: nodeLibs } = await import(
66
- // @ts-expect-error
67
- "node-libs-browser"
68
- );
69
- chain.resolve.fallback.merge(getResolveFallback(nodeLibs));
70
- chain.plugin(CHAIN_ID.PLUGIN.NODE_POLYFILL_PROVIDE).use(bundler.ProvidePlugin, [await getProvideLibs()]);
130
+ chain.resolve.fallback.merge(getResolveFallback(protocolImports));
131
+ const provideGlobals = await getProvideGlobals(options.globals);
132
+ if (Object.keys(provideGlobals).length) {
133
+ chain.plugin("node-polyfill-provide").use(bundler.ProvidePlugin, [provideGlobals]);
134
+ }
135
+ if (protocolImports) {
136
+ const { ProtocolImportsPlugin } = await import("./ProtocolImportsPlugin-HUSWFIAE.js");
137
+ chain.plugin("protocol-imports").use(ProtocolImportsPlugin);
138
+ }
71
139
  });
72
140
  }
73
141
  };
74
142
  }
75
- // Annotate the CommonJS export names for ESM import in node:
76
- 0 && (module.exports = {
143
+ export {
144
+ PLUGIN_NODE_POLYFILL_NAME,
77
145
  pluginNodePolyfill
78
- });
146
+ };
package/package.json CHANGED
@@ -1,42 +1,85 @@
1
1
  {
2
- "name": "@rsbuild/plugin-node-polyfill",
3
- "version": "1.0.0",
4
- "description": "Node polyfill plugin for Rsbuild",
5
- "homepage": "https://rsbuild.dev",
6
- "repository": {
7
- "type": "git",
8
- "url": "https://github.com/web-infra-dev/rsbuild",
9
- "directory": "packages/plugin-node-polyfill"
10
- },
11
- "license": "MIT",
12
- "exports": {
13
- ".": {
14
- "types": "./dist/index.d.ts",
15
- "import": "./dist/index.mjs",
16
- "default": "./dist/index.js"
17
- }
18
- },
19
- "main": "./dist/index.js",
20
- "types": "./dist/index.d.ts",
21
- "files": [
22
- "dist"
23
- ],
24
- "dependencies": {
25
- "node-libs-browser": "2.2.1",
26
- "@rsbuild/shared": "1.0.0"
27
- },
28
- "devDependencies": {
29
- "typescript": "^5.3.0",
30
- "@rsbuild/core": "1.0.0",
31
- "@rsbuild/webpack": "1.0.0"
32
- },
33
- "publishConfig": {
34
- "access": "public",
35
- "provenance": true,
36
- "registry": "https://registry.npmjs.org/"
37
- },
38
- "scripts": {
39
- "build": "modern build",
40
- "dev": "modern build --watch"
41
- }
42
- }
2
+ "name": "@rsbuild/plugin-node-polyfill",
3
+ "version": "1.0.3",
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
+ "scripts": {
21
+ "build": "tsup",
22
+ "dev": "tsup --watch",
23
+ "lint": "biome check .",
24
+ "lint:write": "biome check . --write",
25
+ "prepare": "simple-git-hooks && npm run build",
26
+ "test": "playwright test"
27
+ },
28
+ "simple-git-hooks": {
29
+ "pre-commit": "npx nano-staged"
30
+ },
31
+ "nano-staged": {
32
+ "*.{js,jsx,ts,tsx,mjs,cjs}": [
33
+ "biome check --write --no-errors-on-unmatched"
34
+ ]
35
+ },
36
+ "dependencies": {
37
+ "assert": "^2.1.0",
38
+ "browserify-zlib": "^0.2.0",
39
+ "buffer": "^5.7.1",
40
+ "console-browserify": "^1.2.0",
41
+ "constants-browserify": "^1.0.0",
42
+ "crypto-browserify": "^3.12.0",
43
+ "domain-browser": "^5.7.0",
44
+ "events": "^3.3.0",
45
+ "https-browserify": "^1.0.0",
46
+ "os-browserify": "^0.3.0",
47
+ "path-browserify": "^1.0.1",
48
+ "process": "^0.11.10",
49
+ "punycode": "^2.3.1",
50
+ "querystring-es3": "^0.2.1",
51
+ "readable-stream": "^4.5.2",
52
+ "stream-browserify": "^3.0.0",
53
+ "stream-http": "^3.2.0",
54
+ "string_decoder": "^1.3.0",
55
+ "timers-browserify": "^2.0.12",
56
+ "tty-browserify": "^0.0.1",
57
+ "url": "^0.11.3",
58
+ "util": "^0.12.5",
59
+ "vm-browserify": "^1.1.2"
60
+ },
61
+ "devDependencies": {
62
+ "@biomejs/biome": "^1.8.3",
63
+ "@playwright/test": "^1.44.1",
64
+ "@rsbuild/core": "^1.0.1-beta.0",
65
+ "@types/node": "^20.14.1",
66
+ "nano-staged": "^0.8.0",
67
+ "playwright": "^1.44.1",
68
+ "simple-git-hooks": "^2.11.1",
69
+ "tsup": "^8.0.2",
70
+ "typescript": "^5.5.2"
71
+ },
72
+ "peerDependencies": {
73
+ "@rsbuild/core": "1.x"
74
+ },
75
+ "peerDependenciesMeta": {
76
+ "@rsbuild/core": {
77
+ "optional": true
78
+ }
79
+ },
80
+ "packageManager": "pnpm@9.2.0",
81
+ "publishConfig": {
82
+ "access": "public",
83
+ "registry": "https://registry.npmjs.org/"
84
+ }
85
+ }
package/dist/index.mjs DELETED
@@ -1,51 +0,0 @@
1
- // ../../node_modules/.pnpm/@modern-js+module-tools@2.40.0_typescript@5.3.2/node_modules/@modern-js/module-tools/shims/esm.js
2
- import { fileURLToPath } from "url";
3
- import path from "path";
4
-
5
- // ../../scripts/require_shims.js
6
- import { createRequire } from "module";
7
- global.require = createRequire(import.meta.url);
8
-
9
- // src/index.ts
10
- var getResolveFallback = (nodeLibs) => Object.keys(nodeLibs).reduce(
11
- (previous, name) => {
12
- if (nodeLibs[name]) {
13
- previous[name] = nodeLibs[name];
14
- } else {
15
- previous[name] = false;
16
- }
17
- return previous;
18
- },
19
- {}
20
- );
21
- var getProvideLibs = async () => {
22
- const { default: nodeLibs } = await import(
23
- // @ts-expect-error
24
- "node-libs-browser"
25
- );
26
- return {
27
- Buffer: [nodeLibs.buffer, "Buffer"],
28
- process: [nodeLibs.process]
29
- };
30
- };
31
- function pluginNodePolyfill() {
32
- return {
33
- name: "rsbuild:node-polyfill",
34
- setup(api) {
35
- api.modifyBundlerChain(async (chain, { CHAIN_ID, isServer, bundler }) => {
36
- if (isServer) {
37
- return;
38
- }
39
- const { default: nodeLibs } = await import(
40
- // @ts-expect-error
41
- "node-libs-browser"
42
- );
43
- chain.resolve.fallback.merge(getResolveFallback(nodeLibs));
44
- chain.plugin(CHAIN_ID.PLUGIN.NODE_POLYFILL_PROVIDE).use(bundler.ProvidePlugin, [await getProvideLibs()]);
45
- });
46
- }
47
- };
48
- }
49
- export {
50
- pluginNodePolyfill
51
- };