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-node
polyfillNode
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 theBuffer
global. Disable it to prevent code likeif (typeof Buffer !== "undefined")
from pulling in the (quite large)buffer-es6
polyfill. Default:true
. -
globals.process
: Whether to inject theprocess
global. Disable it to preventprocess.env.NODE_ENV
from pulling in theprocess-es6
polyfill. You can use thedefine
option to replaceprocess.env.NODE_ENV
instead. 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"
.false
disables the polyfill,true
enables it, and"empty"
injects an empty polyfill.
Implemented polyfills
-
_buffer_list
as implemented inreadable-stream
-
_stream_passthrough
as implemented inreadable-stream
-
_stream_readable
as implemented inreadable-stream
-
_stream_transform
as implemented inreadable-stream
-
_stream_writable
as implemented inreadable-stream
-
assert
as implemented inassert
-
buffer
as implemented inbuffer-es6
-
console
as implemented inconsole-browserify
-
crypto
¹ as implemented incrypto-browserify
-
domain
as implemented indomain-browser
-
events
as implemented inevents
-
fs
¹ as implemented inbrowserify-fs
-
http
as implemented instream-http
-
https
as implemented instream-http
-
os
as implemented inos
-
path
as implemented inpath
-
process
as implemented inprocess-es6
-
punycode
as implemented inpunycode
-
querystring
as implemented inquerystring
-
stream
as implemented instream
-
string_decoder
as implemented instring_decoder
-
sys
as implemented inutil
-
timers
as implemented intimers-browserify
-
tty
as implemented intty-browserify
-
url
as implemented inurl
-
util
as implemented inutil
-
vm
as implemented invm-browserify
-
zlib
as implemented inbrowserify-zlib
¹ 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.
Empty stubs
The followings modules are only provided as empty stubs. It is illegal to pass true
for them.
dns
dgram
child_process
cluster
module
net
readline
repl
tls
Globals
-
global
(aliased toglobalThis
) -
process
¹ (imports theprocess
module) -
Buffer
¹ (imports thebuffer
module) -
__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. Optionally, it can use polyfills from NPM instead.
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.160.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"
.false
disables the polyfill,true
enables 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