esbuild-plugin-polyfill-node v0.3.0
esbuild-plugin-polyfill-node
ESBuild plugin to polyfill Node.js built-ins and globals, geared towards edge environments and Deno (including Deno Deploy). It consists of two plugins: polyfillNode, which should work for most cases, and polyfillNodeForDeno which targets Deno and Deno Deploy specifically.
Installation
npm install esbuild-plugin-polyfill-nodepolyfillNode
Usage
import { build } from "esbuild";
import { polyfillNode } from "esbuild-plugin-polyfill-node";
build({
entryPoints: ["src/index.js"],
bundle: true,
outfile: "dist/bundle.js",
plugins: [
polyfillNode({
// Options (optional)
}),
],
});Options
globals.buffer: Whether to inject theBufferglobal. Disable it to prevent code likeif (typeof Buffer !== "undefined")from pulling in the (quite large)buffer-es6polyfill. Default:true.globals.process: Whether to inject theprocessglobal. Disable it to preventprocess.env.NODE_ENVfrom pulling in theprocess-es6polyfill. You can use thedefineoption to replaceprocess.env.NODE_ENVinstead. Default:true.polyfills: Polyfills to inject. It's an object where the keys are the names of the polyfills and the values arefalse,true, or"empty".falsedisables the polyfill,trueenables it, and"empty"injects an empty polyfill.
Implemented polyfills
Polyfills are provided by the @jspm/core package.
_stream_duplex_stream_passthrough_stream_readable_stream_transform_stream_writableassertassert/strictasync_hooksbufferchild_processclusterconsoleconstantscryptodgramdiagnostics_channeldnsdomaineventsfsfs/promiseshttphttp2httpsmodulenetospathperf_hooksprocesspunycodequerystringreadlinereplstreamstring_decodersystimerstimers/promisestlsttyurlutilv8vmwasiworker_threadszlib
¹ All except crypto and fs polyfills are on by default. crypto and fs have to be explicitly enabled. Otherwise, they will be replaced with empty stubs.
Globals
global(aliased toglobalThis)process¹ (imports theprocessmodule)Buffer¹ (imports thebuffermodule)__dirname(always"/")__filename(always"/index.js")
¹ process and Buffer shims can be disabled by passing globals.process: false and globals.buffer: false to the plugin options.
polyfillNodeForDeno
This plugin uses Deno's std/node library to polyfill Node builtins and globals.
Note that the std/node library has been removed from Deno in version 0.178.0 because now Deno has builtin support for Node builtin modules (e.g. node:fs). This plugin is still useful for deploying to Deno Deploy, which doesn't support them yet.
Usage
import { build } from "esbuild";
import { polyfillNodeForDeno } from "esbuild-plugin-polyfill-node";
build({
entryPoints: ["src/index.js"],
bundle: true,
outfile: "dist/bundle.js",
plugins: [
polyfillNodeForDeno({
// Options (optional)
}),
],
});Options
stdVersion: Version of the Deno standard library to use. Default:"0.177.0".globals: Whether to inject global polyfills (process,Buffer,setImmediate,clearImmediate,__dirname, and__filename). Default:true.polyfills: Polyfills to inject. It's an object where the keys are the names of the polyfills and the values arefalse,true,"npm"or"empty".falsedisables the polyfill,trueenables it (default where exists),"npm"injects a polyfill from the NPM (default for"domain","punycode","vm", and"zlib"), and"empty"injects an empty polyfill (default for missing polyfills).
Credits
- Fatih Aygün, under the MIT license
- Loosely based on rollup-plugin-polyfill-node