JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 61429
  • Score
    100M100P100Q163906F
  • License MIT

ESBuild plugin for polyfilling Node.js builtins

Package Exports

  • esbuild-plugin-polyfill-node

Readme

esbuild-plugin-polyfill-node

ESBuild plugin to polyfill Node.js built-ins geared towards edge environments.

Installation

npm install esbuild-plugin-polyfill-node

Usage

import { build } from "esbuild";
import { nodePolyfills } from "esbuild-plugin-polyfill-node";

build({
    entryPoints: ["src/index.js"],
    bundle: true,
    outfile: "dist/bundle.js",
    plugins: [
        nodePolyfills({
            // Options (optional)
        }),
    ],
});

Options

  • buffer: Whether to inject the Buffer global. Disable it to prevent code like if (typeof Buffer !== "undefined") from pulling in the (quite large) buffer-es6 polyfill. Default: true.
  • process: Whether to inject the process global. Disable it to prevent process.env.NODE_ENV from pulling in the process-es6 polyfill. You can use the define option to replace process.env.NODE_ENV instead. Default: true.
  • crypto: Whether to polyfill the crypto module. This is disabled by default because the crypto-browserify polyfill is quite large and you may want to think again before pulling it in. Using the Web Crypto API is usually a better idea. Default: false.
  • fs: Whether to polyfill the fs module. This is disabled by default because the broserify-fs polyfill is quite large and you may want to think again before pulling it in. Default: false.

Provided polyfills

¹ crypto and fs polyfills have to be explicitly enabled by passing crypto: true and fs: true to the plugin options. Otherwise, they will be replaced with empty stubs.

Shimmed globals

  • global (aliased to globalThis)
  • process¹ (imports the process module)
  • Buffer¹ (imports the buffer module)
  • __dirname (always "/")
  • __filename (always "/index.js")

¹ process and Buffer shims can be disabled by passing process: false and buffer: false to the plugin options.

Empty polyfills

  • dns
  • dgram
  • child_process
  • cluster
  • module
  • net
  • readline
  • repl
  • tls

crypto and fs will also produce empty stubs unless explicitly enabled by passing crypto: true and fs: true to the plugin options.

Credits