包详细信息

esbuild-plugins-node-modules-polyfill

imranbarbhuiya1.5mMIT1.7.1

Polyfills nodejs builtin modules and globals for the browser.

esbuild-plugins, esbuild-plugins-node-modules-polyfill, node-modules-polyfill, esbuild

自述文件

# esbuild-plugins-node-modules-polyfill Polyfills nodejs builtin modules for the browser. GitHub npm

Description

Polyfills nodejs builtin modules and globals for the browser.

Features

  • Written In Typescript
  • Offers CJS and ESM builds
  • Full TypeScript & JavaScript support
  • Supports node: protocol
  • Supports browser field in package.json
  • Optionally injects globals
  • Optionally provides empty fallbacks

Install

npm install --save-dev esbuild-plugins-node-modules-polyfill

Usage

import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill';
import { build } from 'esbuild';
build({
    plugins: [nodeModulesPolyfillPlugin()],
});

Inject globals when detected:

import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill';
import { build } from 'esbuild';
build({
    plugins: [
        nodeModulesPolyfillPlugin({
            globals: {
                process: true,
                Buffer: true,
            },
        }),
    ],
});

[!Note] If you are utilizing the modules option, ensure that you include polyfills for the global modules you are using.

Configure which modules to polyfill:

import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill';
import { build } from 'esbuild';
build({
    plugins: [
        nodeModulesPolyfillPlugin({
            modules: ['crypto'],
        }),
    ],
});
import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill';
import { build } from 'esbuild';
build({
    plugins: [
        nodeModulesPolyfillPlugin({
            modules: {
                crypto: true,
                fs: false,
            },
        }),
    ],
});

Provide empty polyfills:

Provide empty polyfills for specific modules:

import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill';
import { build } from 'esbuild';
build({
    plugins: [
        nodeModulesPolyfillPlugin({
            modules: {
                fs: 'empty',
                crypto: true,
            },
        }),
    ],
});

Provide empty fallbacks for any unpolyfilled modules:

import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill';
import { build } from 'esbuild';
build({
    plugins: [
        nodeModulesPolyfillPlugin({
            fallback: 'empty',
        }),
    ],
});

Provide empty fallbacks for any unconfigured modules:

import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill';
import { build } from 'esbuild';
build({
    plugins: [
        nodeModulesPolyfillPlugin({
            fallback: 'empty',
            modules: {
                crypto: true,
            },
        }),
    ],
});

Fail the build when certain modules are used:

[!Important] The write option in esbuild must be false to support this.

import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill';
import { build } from 'esbuild';
const buildResult = await build({
    write: false,
    plugins: [
        nodeModulesPolyfillPlugin({
            modules: {
                crypto: 'error',
                path: true,
            },
        }),
    ],
});

Fail the build when a module is not polyfilled or configured:

[!Important] The write option in esbuild must be false to support this.

import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill';
import { build } from 'esbuild';
const buildResult = await build({
    write: false,
    plugins: [
        nodeModulesPolyfillPlugin({
            fallback: 'error',
            modules: {
                path: true,
            },
        }),
    ],
});

Provide a custom error formatter when a module is not polyfilled or configured:

Return an esbuild PartialMessage object from the formatError function to override any properties of the default error message.

[!Important] The write option in esbuild must be false to support this.

import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill';
import { build } from 'esbuild';
const buildResult = await build({
    write: false,
    plugins: [
        nodeModulesPolyfillPlugin({
            fallback: 'error',
            modules: {
                path: true,
            },
            formatError({ moduleName, importer, polyfillExists }) {
                return {
                    text: polyfillExists
                        ? `Polyfill has not been configured for "${moduleName}", imported by "${importer}"`
                        : `Polyfill does not exist for "${moduleName}", imported by "${importer}"`,
                };
            },
        }),
    ],
});

Buy me some doughnuts

If you want to support me by donating, you can do so by using any of the following methods. Thank you very much in advance!

Buy Me A Coffee Buy Me a Coffee at ko-fi.com

Contributors ✨

Thanks goes to these wonderful people:

更新日志

Changelog

All notable changes to this project will be documented in this file.

1.7.1 - (2025-06-11)

🐛 Bug Fixes

  • deps: Update all non-major dependencies (#322) (227c2b6)

🧪 Testing

1.7.0 - (2025-02-24)

🐛 Bug Fixes

  • Type changes (0bf239f)
  • deps: Update dependency local-pkg to v1 (#314) (74012cd)

🚀 Features

🧪 Testing

1.6.8 - (2024-11-17)

🐛 Bug Fixes

  • Accept node prefix in modules obj (4b8b611)

1.6.6 - (2024-08-29)

🐛 Bug Fixes

1.6.2 - (2024-01-28)

🐛 Bug Fixes

  • Esbuild version and add node types (49827c1)
  • Bump local-pkg (448e8cd)

1.6.1 - (2023-08-29)

🐛 Bug Fixes

  • Dont bundle the content (5ad8f06)

📝 Documentation

  • Add modules to global polyfills usage (#157) (54e8d46)

🧪 Testing

  • Use consistent folder structure for fixtures (#158) (776d755)
  • Ensure tree-shaken modules avoid fallback (#156) (f6da65b)

1.6.0 - (2023-08-25)

🚀 Features

  • Support custom error formatting (#153) (769e642)

1.5.0 - (2023-08-24)

🐛 Bug Fixes

  • Use empty polyfill if browser field is false (#151) (3946db8)

📝 Documentation

🚀 Features

  • Allow modules to be marked as errors (#152) (c02060d)

1.4.1 - (2023-08-23)

🐛 Bug Fixes

  • Support package.json browser field (#150) (6e84c9b)

1.4.0 - (2023-08-19)

🚀 Features

  • Support empty fallbacks (#146) (5b26071)

1.3.2 - (2023-08-16)

🐛 Bug Fixes

  • Support esbuild 0.19 (#145) (d5d0412)

1.3.1 - (2023-08-14)

🐛 Bug Fixes

  • Mark polyfills as side-effect free (#141) (30eda0a)

1.3.0 - (2023-07-17)

🚀 Features

  • Optionally provide empty polyfills (#132) (6642f2f)

1.2.0 - (2023-07-07)

🚀 Features

  • Add modules option (#124) (932db4b)

1.1.3 - (2023-06-30)

🐛 Bug Fixes

  • Regex to not match relative files (c795019)

1.1.2 - (2023-06-30)

🐛 Bug Fixes

  • Remove module from package.json (0f1a60c)

1.1.1 - (2023-06-30)

🐛 Bug Fixes

  • Remove esm formats as it breaks the plugin on resolve (17aed11)
  • Add node 14-20 test workflow (515bd0f)

1.1.0 - (2023-06-26)

🐛 Bug Fixes

  • Port modern polyfill code to add caching in internal call (#122) (ed22079)
  • Handle frozen default exports (#120) (d7e6226)
  • Repo url (8d764b1)

🚀 Features

  • Support process and Buffer globals (#121) (c345cf6)

1.0.16 - (2023-06-20)

🐛 Bug Fixes

  • Cache usage of modern-node-polyfills API (#115) (c71714f)

1.0.14 - (2023-05-30)

🐛 Bug Fixes

  • Replace direct eval with indirect eval fixes: #31 (db9b7e5)

1.0.13 - (2023-04-24)

🐛 Bug Fixes

1.0.12 - (2023-04-23)

🐛 Bug Fixes

  • Add @rollup/pluginutils deps (75433a7)

1.0.9 - (2022-12-18)

📝 Documentation

  • Add favna as a contributor for code (#56) (2e5d2e7)

1.0.8 - (2022-12-18)

🐛 Bug Fixes

  • Fixed node14 support (#51) (ced7aca)

1.0.6 - (2022-10-08)

📝 Documentation

1.0.3 - (2022-08-05)

🪞 Styling

1.0.9 - (2022-12-18)

📝 Documentation

  • Add favna as a contributor for code (#56) (2e5d2e7)

1.0.8 - (2022-12-18)

🐛 Bug Fixes

  • Fixed node14 support (#51) (ced7aca)

1.0.6 - (2022-10-08)

📝 Documentation

1.0.3 - (2022-08-05)

🪞 Styling

1.0.2 - (2022-07-24)

🐛 Bug Fixes

  • Change polyfill package (6db0963)

1.0.1 - (2022-07-08)

🐛 Bug Fixes

1.0.0 - (2022-07-08)

🚀 Features