Socket
Socket
Sign inDemoInstall

@rsbuild/plugin-node-polyfill

Package Overview
Dependencies
Maintainers
0
Versions
181
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rsbuild/plugin-node-polyfill - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

dist/chunk-6C3VEZWH.js

26

dist/index.d.ts
import { RsbuildPlugin } from '@rsbuild/core';
declare function pluginNodePolyfill(): RsbuildPlugin;
type Globals = {
process?: boolean;
Buffer?: boolean;
};
type PluginNodePolyfillOptions = {
/**
* Whether to provide polyfill of globals.
* @default
* {
* Buffer: true,
* process: true,
* }
*/
globals?: Globals;
/**
* Whether to polyfill Node.js builtin modules starting with `node:`.
* @see https://nodejs.org/api/esm.html#node-imports
* @default true
*/
protocolImports?: boolean;
};
declare const PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
declare function pluginNodePolyfill(options?: PluginNodePolyfillOptions): RsbuildPlugin;
export { pluginNodePolyfill };
export { PLUGIN_NODE_POLYFILL_NAME, type PluginNodePolyfillOptions, pluginNodePolyfill };

194

dist/index.js

@@ -1,60 +0,125 @@

"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
import {
__export
} from "./chunk-6C3VEZWH.js";
// src/libs.ts
var libs_exports = {};
__export(libs_exports, {
_stream_duplex: () => _stream_duplex,
_stream_passthrough: () => _stream_passthrough,
_stream_readable: () => _stream_readable,
_stream_transform: () => _stream_transform,
_stream_writable: () => _stream_writable,
assert: () => assert,
buffer: () => buffer,
child_process: () => child_process,
cluster: () => cluster,
console: () => console,
constants: () => constants,
crypto: () => crypto,
dgram: () => dgram,
dns: () => dns,
domain: () => domain,
events: () => events,
fs: () => fs,
http: () => http,
https: () => https,
module: () => module,
net: () => net,
os: () => os,
path: () => path,
process: () => process,
punycode: () => punycode,
querystring: () => querystring,
readline: () => readline,
repl: () => repl,
stream: () => stream,
string_decoder: () => string_decoder,
sys: () => sys,
timers: () => timers,
tls: () => tls,
tty: () => tty,
url: () => url,
util: () => util,
vm: () => vm,
zlib: () => zlib
});
import { createRequire } from "node:module";
var require2 = createRequire(import.meta.url);
var assert = require2.resolve("assert/");
var buffer = require2.resolve("buffer/");
var child_process = null;
var cluster = null;
var console = require2.resolve("console-browserify");
var constants = require2.resolve("constants-browserify");
var crypto = require2.resolve("crypto-browserify");
var dgram = null;
var dns = null;
var domain = require2.resolve("domain-browser");
var events = require2.resolve("events/");
var fs = null;
var http = require2.resolve("stream-http");
var https = require2.resolve("https-browserify");
var module = null;
var net = null;
var os = require2.resolve("os-browserify/browser.js");
var path = require2.resolve("path-browserify");
var punycode = require2.resolve("punycode/");
var process = require2.resolve("process/browser.js");
var querystring = require2.resolve("querystring-es3/");
var readline = null;
var repl = null;
var stream = require2.resolve("stream-browserify");
var _stream_duplex = require2.resolve(
"readable-stream/lib/_stream_duplex.js"
);
var _stream_passthrough = require2.resolve(
"readable-stream/lib/_stream_passthrough.js"
);
var _stream_readable = require2.resolve(
"readable-stream/lib/_stream_readable.js"
);
var _stream_transform = require2.resolve(
"readable-stream/lib/_stream_transform.js"
);
var _stream_writable = require2.resolve(
"readable-stream/lib/_stream_writable.js"
);
var string_decoder = require2.resolve("string_decoder/");
var sys = require2.resolve("util/util.js");
var timers = require2.resolve("timers-browserify");
var tls = null;
var tty = require2.resolve("tty-browserify");
var url = require2.resolve("url/");
var util = require2.resolve("util/util.js");
var vm = require2.resolve("vm-browserify");
var zlib = require2.resolve("browserify-zlib");
// src/index.ts
var src_exports = {};
__export(src_exports, {
pluginNodePolyfill: () => pluginNodePolyfill
});
module.exports = __toCommonJS(src_exports);
var getResolveFallback = (nodeLibs) => Object.keys(nodeLibs).reduce(
(previous, name) => {
if (nodeLibs[name]) {
previous[name] = nodeLibs[name];
} else {
previous[name] = false;
var getResolveFallback = (protocolImports) => {
const fallback = {};
for (const name of Object.keys(libs_exports)) {
const libPath = libs_exports[name];
fallback[name] = libPath ?? false;
if (protocolImports) {
fallback[`node:${name}`] = fallback[name];
}
return previous;
},
{}
);
var getProvideLibs = async () => {
const { default: nodeLibs } = await import(
// @ts-expect-error
"node-libs-browser"
);
return {
Buffer: [nodeLibs.buffer, "Buffer"],
process: [nodeLibs.process]
};
}
return fallback;
};
function pluginNodePolyfill() {
var getProvideGlobals = async (globals) => {
const result = {};
if (globals?.Buffer !== false) {
result.Buffer = [buffer, "Buffer"];
}
if (globals?.process !== false) {
result.process = [process];
}
return result;
};
var PLUGIN_NODE_POLYFILL_NAME = "rsbuild:node-polyfill";
function pluginNodePolyfill(options = {}) {
const { protocolImports = true } = options;
return {
name: "rsbuild:node-polyfill",
name: PLUGIN_NODE_POLYFILL_NAME,
setup(api) {

@@ -65,8 +130,11 @@ api.modifyBundlerChain(async (chain, { CHAIN_ID, isServer, bundler }) => {

}
const { default: nodeLibs } = await import(
// @ts-expect-error
"node-libs-browser"
);
chain.resolve.fallback.merge(getResolveFallback(nodeLibs));
chain.plugin(CHAIN_ID.PLUGIN.NODE_POLYFILL_PROVIDE).use(bundler.ProvidePlugin, [await getProvideLibs()]);
chain.resolve.fallback.merge(getResolveFallback(protocolImports));
const provideGlobals = await getProvideGlobals(options.globals);
if (Object.keys(provideGlobals).length) {
chain.plugin(CHAIN_ID.PLUGIN.NODE_POLYFILL_PROVIDE).use(bundler.ProvidePlugin, [provideGlobals]);
}
if (protocolImports) {
const { ProtocolImportsPlugin } = await import("./ProtocolImportsPlugin-HUSWFIAE.js");
chain.plugin("protocol-imports").use(ProtocolImportsPlugin);
}
});

@@ -76,5 +144,5 @@ }

}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
export {
PLUGIN_NODE_POLYFILL_NAME,
pluginNodePolyfill
});
};
{
"name": "@rsbuild/plugin-node-polyfill",
"version": "1.0.0",
"description": "Node polyfill plugin for Rsbuild",
"homepage": "https://rsbuild.dev",
"repository": {
"type": "git",
"url": "https://github.com/web-infra-dev/rsbuild",
"directory": "packages/plugin-node-polyfill"
},
"version": "1.0.1",
"repository": "https://github.com/rspack-contrib/rsbuild-plugin-template",
"license": "MIT",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"default": "./dist/index.js"
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",

@@ -24,20 +20,65 @@ "files": [

],
"simple-git-hooks": {
"pre-commit": "npx nano-staged"
},
"nano-staged": {
"*.{js,jsx,ts,tsx,mjs,cjs}": [
"biome check --write --no-errors-on-unmatched"
]
},
"dependencies": {
"node-libs-browser": "2.2.1",
"@rsbuild/shared": "1.0.0"
"assert": "^2.1.0",
"browserify-zlib": "^0.2.0",
"buffer": "^5.7.1",
"console-browserify": "^1.2.0",
"constants-browserify": "^1.0.0",
"crypto-browserify": "^3.12.0",
"domain-browser": "^5.7.0",
"events": "^3.3.0",
"https-browserify": "^1.0.0",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"process": "^0.11.10",
"punycode": "^2.3.1",
"querystring-es3": "^0.2.1",
"readable-stream": "^4.5.2",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"string_decoder": "^1.3.0",
"timers-browserify": "^2.0.12",
"tty-browserify": "^0.0.1",
"url": "^0.11.3",
"util": "^0.12.5",
"vm-browserify": "^1.1.2"
},
"devDependencies": {
"typescript": "^5.3.0",
"@rsbuild/core": "1.0.0",
"@rsbuild/webpack": "1.0.0"
"@biomejs/biome": "^1.8.3",
"@playwright/test": "^1.44.1",
"@rsbuild/core": "^0.7.10",
"@types/node": "^20.14.1",
"nano-staged": "^0.8.0",
"playwright": "^1.44.1",
"simple-git-hooks": "^2.11.1",
"tsup": "^8.0.2",
"typescript": "^5.5.2"
},
"peerDependencies": {
"@rsbuild/core": "0.x || 1.x"
},
"peerDependenciesMeta": {
"@rsbuild/core": {
"optional": true
}
},
"publishConfig": {
"access": "public",
"provenance": true,
"registry": "https://registry.npmjs.org/"
},
"scripts": {
"build": "modern build",
"dev": "modern build --watch"
"build": "tsup",
"dev": "tsup --watch",
"lint": "biome check .",
"lint:write": "biome check . --write",
"test": "playwright test"
}
}

@@ -1,19 +0,158 @@

<p align="center">
<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>
# @rsbuild/plugin-node-polyfill
@rsbuild/plugin-node-polyfill is a Rsbuild plugin to do something.
<p>
<a href="https://npmjs.com/package/@rsbuild/plugin-node-polyfill">
<img src="https://img.shields.io/npm/v/@rsbuild/plugin-node-polyfill?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
</a>
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
</p>
# Rsbuild
## Usage
Unleash the power of Rspack with the out-of-the-box build tool.
Install:
## Documentation
```bash
npm add @rsbuild/plugin-node-polyfill -D
```
https://rsbuild.dev/
Add plugin to your `rsbuild.config.ts`:
## Contributing
```ts
// rsbuild.config.ts
import { pluginNodePolyfill } from "@rsbuild/plugin-node-polyfill";
Please read the [Contributing Guide](https://github.com/web-infra-dev/rsbuild/blob/main/CONTRIBUTING.md).
export default {
plugins: [pluginNodePolyfill()],
};
```
## Node Polyfills
### Globals
- `Buffer`
- `process`
When you use the above global variables in your code, the corresponding polyfill will be automatically injected.
For instance, the following code would inject the `Buffer` polyfill:
```ts
const bufferData = Buffer.from("abc");
```
You can disable this behavior through the `globals` option of the plugin:
```ts
pluginNodePolyfill({
globals: {
Buffer: false,
process: false,
},
});
```
### Modules
- `assert`
- `buffer`
- `console`
- `constants`
- `crypto`
- `domain`
- `events`
- `http`
- `https`
- `os`
- `path`
- `punycode`
- `process`
- `querystring`
- `stream`
- `_stream_duplex`
- `_stream_passthrough`
- `_stream_readable`
- `_stream_transform`
- `_stream_writable`
- `string_decoder`
- `sys`
- `timers`
- `tty`
- `url`
- `util`
- `vm`
- `zlib`
When the above module is referenced in code via import / require syntax, the corresponding polyfill will be injected.
```ts
import { Buffer } from "buffer";
const bufferData = Buffer.from("abc");
```
### Fallbacks
- `child_process`
- `cluster`
- `dgram`
- `dns`
- `fs`
- `module`
- `net`
- `readline`
- `repl`
- `tls`
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.
```ts
import fs from "fs";
console.log(fs); // -> {}
```
## Options
### globals
Used to specify whether to inject polyfills for global variables.
- **Type:**
```ts
type Globals = {
process?: boolean;
Buffer?: boolean;
};
```
- **Default:**
```ts
const defaultGlobals = {
Buffer: true,
process: true,
};
```
### protocolImports
Whether to polyfill Node.js builtin modules starting with `node:`.
- **Type:** `boolean`
- **Default:** `true`
For example, if you disable `protocolImports`, modules such as `node:path`, `node:http`, etc. will not be polyfilled.
```ts
pluginNodePolyfill({
protocolImports: false,
});
```
## License
Rsbuild is [MIT licensed](https://github.com/web-infra-dev/rsbuild/blob/main/LICENSE).
[MIT](./LICENSE).

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc