cyco130 / esbuild-plugin-polyfill-node

ESBuild plugin to polyfill Node.js built-ins geared towards edge environments.
MIT License
55 stars 8 forks source link

esbuild-plugin-polyfill-node

LOOKING FOR MAINTAINERS: Due to time constraints, I am not able to maintain this package anymore. You can contact me via email, Twitter, or Discord. All on my profile.

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

Implemented polyfills

Polyfills are provided by the @jspm/core package.

¹ 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

You can control which globals are injected using the globals option.

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

Credits