@rsbuild/plugin-node-polyfill 1.0.0 → 1.0.1

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,158 @@
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
+ @rsbuild/plugin-node-polyfill is a Rsbuild plugin to do something.
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
+ ## Usage
13
+
14
+ Install:
15
+
16
+ ```bash
17
+ npm add @rsbuild/plugin-node-polyfill -D
18
+ ```
19
+
20
+ Add plugin to your `rsbuild.config.ts`:
21
+
22
+ ```ts
23
+ // rsbuild.config.ts
24
+ import { pluginNodePolyfill } from "@rsbuild/plugin-node-polyfill";
25
+
26
+ export default {
27
+ plugins: [pluginNodePolyfill()],
28
+ };
29
+ ```
30
+
31
+ ## Node Polyfills
32
+
33
+ ### Globals
34
+
35
+ - `Buffer`
36
+ - `process`
37
+
38
+ When you use the above global variables in your code, the corresponding polyfill will be automatically injected.
39
+
40
+ For instance, the following code would inject the `Buffer` polyfill:
41
+
42
+ ```ts
43
+ const bufferData = Buffer.from("abc");
44
+ ```
45
+
46
+ You can disable this behavior through the `globals` option of the plugin:
47
+
48
+ ```ts
49
+ pluginNodePolyfill({
50
+ globals: {
51
+ Buffer: false,
52
+ process: false,
53
+ },
54
+ });
55
+ ```
56
+
57
+ ### Modules
58
+
59
+ - `assert`
60
+ - `buffer`
61
+ - `console`
62
+ - `constants`
63
+ - `crypto`
64
+ - `domain`
65
+ - `events`
66
+ - `http`
67
+ - `https`
68
+ - `os`
69
+ - `path`
70
+ - `punycode`
71
+ - `process`
72
+ - `querystring`
73
+ - `stream`
74
+ - `_stream_duplex`
75
+ - `_stream_passthrough`
76
+ - `_stream_readable`
77
+ - `_stream_transform`
78
+ - `_stream_writable`
79
+ - `string_decoder`
80
+ - `sys`
81
+ - `timers`
82
+ - `tty`
83
+ - `url`
84
+ - `util`
85
+ - `vm`
86
+ - `zlib`
87
+
88
+ When the above module is referenced in code via import / require syntax, the corresponding polyfill will be injected.
89
+
90
+ ```ts
91
+ import { Buffer } from "buffer";
92
+
93
+ const bufferData = Buffer.from("abc");
94
+ ```
95
+
96
+ ### Fallbacks
97
+
98
+ - `child_process`
99
+ - `cluster`
100
+ - `dgram`
101
+ - `dns`
102
+ - `fs`
103
+ - `module`
104
+ - `net`
105
+ - `readline`
106
+ - `repl`
107
+ - `tls`
108
+
109
+ 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.
110
+
111
+ ```ts
112
+ import fs from "fs";
113
+
114
+ console.log(fs); // -> {}
115
+ ```
116
+
117
+ ## Options
118
+
119
+ ### globals
120
+
121
+ Used to specify whether to inject polyfills for global variables.
122
+
123
+ - **Type:**
124
+
125
+ ```ts
126
+ type Globals = {
127
+ process?: boolean;
128
+ Buffer?: boolean;
129
+ };
130
+ ```
131
+
132
+ - **Default:**
133
+
134
+ ```ts
135
+ const defaultGlobals = {
136
+ Buffer: true,
137
+ process: true,
138
+ };
139
+ ```
6
140
 
7
- Unleash the power of Rspack with the out-of-the-box build tool.
141
+ ### protocolImports
8
142
 
9
- ## Documentation
143
+ Whether to polyfill Node.js builtin modules starting with `node:`.
10
144
 
11
- https://rsbuild.dev/
145
+ - **Type:** `boolean`
146
+ - **Default:** `true`
12
147
 
13
- ## Contributing
148
+ For example, if you disable `protocolImports`, modules such as `node:path`, `node:http`, etc. will not be polyfilled.
14
149
 
15
- Please read the [Contributing Guide](https://github.com/web-infra-dev/rsbuild/blob/main/CONTRIBUTING.md).
150
+ ```ts
151
+ pluginNodePolyfill({
152
+ protocolImports: false,
153
+ });
154
+ ```
16
155
 
17
156
  ## License
18
157
 
19
- Rsbuild is [MIT licensed](https://github.com/web-infra-dev/rsbuild/blob/main/LICENSE).
158
+ [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.38_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.38_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, { CHAIN_ID, 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(CHAIN_ID.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
126
  api.modifyBundlerChain(async (chain, { CHAIN_ID, 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(CHAIN_ID.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,83 @@
1
1
  {
2
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
- },
3
+ "version": "1.0.1",
4
+ "repository": "https://github.com/rspack-contrib/rsbuild-plugin-template",
11
5
  "license": "MIT",
6
+ "type": "module",
12
7
  "exports": {
13
8
  ".": {
14
9
  "types": "./dist/index.d.ts",
15
- "import": "./dist/index.mjs",
16
- "default": "./dist/index.js"
10
+ "import": "./dist/index.js",
11
+ "require": "./dist/index.cjs"
17
12
  }
18
13
  },
19
14
  "main": "./dist/index.js",
15
+ "module": "./dist/index.mjs",
20
16
  "types": "./dist/index.d.ts",
21
17
  "files": [
22
18
  "dist"
23
19
  ],
20
+ "simple-git-hooks": {
21
+ "pre-commit": "npx nano-staged"
22
+ },
23
+ "nano-staged": {
24
+ "*.{js,jsx,ts,tsx,mjs,cjs}": [
25
+ "biome check --write --no-errors-on-unmatched"
26
+ ]
27
+ },
24
28
  "dependencies": {
25
- "node-libs-browser": "2.2.1",
26
- "@rsbuild/shared": "1.0.0"
29
+ "assert": "^2.1.0",
30
+ "browserify-zlib": "^0.2.0",
31
+ "buffer": "^5.7.1",
32
+ "console-browserify": "^1.2.0",
33
+ "constants-browserify": "^1.0.0",
34
+ "crypto-browserify": "^3.12.0",
35
+ "domain-browser": "^5.7.0",
36
+ "events": "^3.3.0",
37
+ "https-browserify": "^1.0.0",
38
+ "os-browserify": "^0.3.0",
39
+ "path-browserify": "^1.0.1",
40
+ "process": "^0.11.10",
41
+ "punycode": "^2.3.1",
42
+ "querystring-es3": "^0.2.1",
43
+ "readable-stream": "^4.5.2",
44
+ "stream-browserify": "^3.0.0",
45
+ "stream-http": "^3.2.0",
46
+ "string_decoder": "^1.3.0",
47
+ "timers-browserify": "^2.0.12",
48
+ "tty-browserify": "^0.0.1",
49
+ "url": "^0.11.3",
50
+ "util": "^0.12.5",
51
+ "vm-browserify": "^1.1.2"
27
52
  },
28
53
  "devDependencies": {
29
- "typescript": "^5.3.0",
30
- "@rsbuild/core": "1.0.0",
31
- "@rsbuild/webpack": "1.0.0"
54
+ "@biomejs/biome": "^1.8.3",
55
+ "@playwright/test": "^1.44.1",
56
+ "@rsbuild/core": "^0.7.10",
57
+ "@types/node": "^20.14.1",
58
+ "nano-staged": "^0.8.0",
59
+ "playwright": "^1.44.1",
60
+ "simple-git-hooks": "^2.11.1",
61
+ "tsup": "^8.0.2",
62
+ "typescript": "^5.5.2"
63
+ },
64
+ "peerDependencies": {
65
+ "@rsbuild/core": "0.x || 1.x"
66
+ },
67
+ "peerDependenciesMeta": {
68
+ "@rsbuild/core": {
69
+ "optional": true
70
+ }
32
71
  },
33
72
  "publishConfig": {
34
73
  "access": "public",
35
- "provenance": true,
36
74
  "registry": "https://registry.npmjs.org/"
37
75
  },
38
76
  "scripts": {
39
- "build": "modern build",
40
- "dev": "modern build --watch"
77
+ "build": "tsup",
78
+ "dev": "tsup --watch",
79
+ "lint": "biome check .",
80
+ "lint:write": "biome check . --write",
81
+ "test": "playwright test"
41
82
  }
42
83
  }
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
- };