0.2.0 • Published 1 year ago

esbuild-plugin-globals v0.2.0

Weekly downloads
1,190
License
MIT
Repository
github
Last release
1 year ago

esbuild-plugin-globals

Provides Webpack's externals functionality for esbuild.

Install

npm:

npm install --save-dev esbuild-plugin-globals

yarn:

yarn add --dev esbuild-plugin-globals

Usage

import esbuild from "esbuild";
import GlobalsPlugin from "esbuild-plugin-globals";

esbuild.build({
  entryPoints: ["src/index.ts"],
  bundle: true,
  plugins: [
    GlobalsPlugin({
      /**
       * Simple string pattern
       * Any module matching "react" will be replaced with
       * `module.exports = React`
       */
      react: "React",
      /**
       * Regular expression + resolver function
       * Invoked with matched module name and returns the module exports (or undefined).
       */
      "@some-scope/.*": (moduleName) => {
        /** strip the scope */
        const name = name.substring(12);
        /** generates module.exports = CamelCasedName */
        return camelCase(name);
      },
    }),
  ],
});