An Rsbuild plugin to automatically inject polyfills for Node.js builtin modules into the browser side.
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.
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.
Install:
npm add @rsbuild/plugin-node-polyfill -DAdd plugin to your rsbuild.config.ts:
// rsbuild.config.ts
import { pluginNodePolyfill } from "@rsbuild/plugin-node-polyfill";
export default {
plugins: [pluginNodePolyfill()],
};Bufferprocess
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:
const bufferData = Buffer.from("abc");You can disable this behavior through the globals option of the plugin:
pluginNodePolyfill({
globals: {
Buffer: false,
process: false,
},
});assertbufferconsoleconstantscryptodomaineventshttphttpsospathpunycodeprocessquerystringstream_stream_duplex_stream_passthrough_stream_readable_stream_transform_stream_writablestring_decodersystimersttyurlutilvmzlib
When the above module is referenced in code via import / require syntax, the corresponding polyfill will be injected.
import { Buffer } from "buffer";
const bufferData = Buffer.from("abc");child_processclusterdgramdnsfsmodulenetreadlinerepltlsv8
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.
import fs from "fs";
console.log(fs); // -> {}Used to specify whether to inject polyfills for global variables.
- Type:
type GlobalsOption = {
/**
* Whether to inject the polyfill for direct identifier usage,
* e.g. `Buffer` or `process` (bare usage without any object prefix).
* @default true
*/
bare?: boolean;
/**
* Whether to inject the polyfill for usage via the `global` object,
* e.g. `global.Buffer` or `global.process`.
* @default false
*/
global?: boolean;
/**
* Whether to inject the polyfill for usage via the `globalThis` object,
* e.g. `globalThis.Buffer` or `globalThis.process`.
* @default false
*/
globalThis?: boolean;
};
type Globals = {
process?: boolean | GlobalsOption;
Buffer?: boolean | GlobalsOption;
};- Default:
const defaultGlobals = {
Buffer: {
bare: true,
global: false,
globalThis: false,
},
process: {
bare: true,
global: false,
globalThis: false,
},
};Whether to polyfill Node.js builtin modules starting with node:.
const defaultGlobals = {
Buffer: true,
process: true,
};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.
pluginNodePolyfill({
protocolImports: false,
});Specify an array of modules for which polyfills should be injected. If this option is set, only the specified modules will be polyfilled. include is mutually exclusive with exclude.
- Type:
string[] - Default:
undefined
pluginNodePolyfill({
include: ["buffer", "crypto"], // Only "buffer" and "crypto" modules will be polyfilled.
});Specify an array of modules for which polyfills should not be injected from the default. If this option is set, the specified modules will be excluded from polyfilled. exclude is mutually exclusive with include.
- Type:
string[] - Default:
undefined
pluginNodePolyfill({
exclude: ["http", "https"], // All modules except "http" and "https" will be polyfilled.
});Override the default polyfills for specific modules.
- Type:
Record<string, string> - Default:
{}
pluginNodePolyfill({
overrides: {
fs: "memfs",
},
});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.
- Type:
boolean - Default:
false
pluginNodePolyfill({
force: true,
});builtinMappingResolved: A map of Node.js builtin modules to their resolved corresponding polyfills modules.resolvedPolyfillToModules: A map of resolved polyfill modules to the polyfill modules before resolving.
MIT.