esbuild-plugin-lodash

raw JSON →
1.2.0 verified Mon Apr 27 auth: no javascript

An esbuild plugin (v1.2.0) that rewrites named imports from 'lodash' to direct module imports like 'lodash/get' to enable tree-shaking. Released on npm, with active maintenance. Similar to babel-plugin-lodash but for esbuild, offering a filter option and support for custom output package (e.g., lodash-es). Distinct from alternatives like manual import optimization or using lodash-es directly. Works with both CJS and ESM, handles quotes, and fixes edge cases like 'lodash/last' imports.

error Error: The plugin "esbuild-plugin-lodash" is not compatible with esbuild version X.X.X
cause The plugin has a peer dependency on esbuild; incompatible versions may cause runtime errors.
fix
Ensure esbuild version matches peer dependency range (check package.json). For esbuild >=0.17, consider updating the plugin.
error TypeError: lodashTransformer is not a function
cause In CJS, require returns the module object, not the default export.
fix
Use require('esbuild-plugin-lodash').default or import the ESM version.
error Module not found: Package path ./get is not exported from package lodash (see exports field in ./node_modules/lodash/package.json)
cause Lodash v5 changed its exports map, breaking the transformed import 'lodash/get'.
fix
Use lodash v4.x or set outLodashPackage to 'lodash-es'.
gotcha Plugin only works with direct named imports like import { get } from 'lodash'. It does not handle wildcard imports (import * as _ from 'lodash') or default imports (import _ from 'lodash').
fix Use named imports only, or manually rewrite to path-based imports.
gotcha Plugin does not rewrite calls to chained lodash methods (e.g., _.chain). Only top-level named imports are transformed.
fix Use direct imports per method, or avoid lodash chaining.
deprecated The npm package is named 'esbuild-plugin-lodash', but the most popular lodash tree-shaking plugin for esbuild is now 'esbuild-plugin-lodash-es' or manual tree-shaking. Consider using lodash-es directly instead.
fix Import from 'lodash-es' instead and use esbuild's built-in tree-shaking.
npm install esbuild-plugin-lodash
yarn add esbuild-plugin-lodash
pnpm add esbuild-plugin-lodash

Shows how to use the plugin in an esbuild build, including options for filter and output package.

import esbuild from 'esbuild';
import lodashTransformer from 'esbuild-plugin-lodash';

await esbuild.build({
  entryPoints: ['src/index.js'],
  bundle: true,
  outfile: 'dist/bundle.js',
  plugins: [
    lodashTransformer({
      filter: /.*/,
      outLodashPackage: 'lodash-es'
    }),
  ],
});