/esbuild-plugin-globals

Provides Webpack's externals functionality for esbuild

Primary LanguageJavaScriptMIT LicenseMIT

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);
      },
    }),
  ],
});