esbuild-plugin-package-json

raw JSON →
2.0.0 verified Fri May 01 auth: no javascript

An esbuild plugin that prepares package.json for publishing by removing unnecessary fields (e.g., scripts, devDependencies) and copying it to the output folder. Version 2.0.0 is the latest stable release, with active development. It is designed for developers who want to publish clean packages directly from esbuild builds. Differentiators include zero configuration defaults and support for custom output paths.

error Error: Cannot find module 'esbuild-plugin-package-json'
cause The package is not installed or is missing from node_modules.
fix
Run npm install -D esbuild-plugin-package-json to install it as a dev dependency.
error TypeError: packageJsonPlugin is not a function
cause Using a default import when the package exports a named function.
fix
Use import { packageJsonPlugin } from 'esbuild-plugin-package-json' instead of import packageJsonPlugin from ....
breaking Version 2.0.0 changed the export from default to named export. Old code using `import packageJsonPlugin from 'esbuild-plugin-package-json'` will break.
fix Use `import { packageJsonPlugin } from 'esbuild-plugin-package-json'` instead.
gotcha If `overrideOut` is not set, the plugin copies package.json to esbuild's default output directory, which may not be desired if multiple output paths are configured.
fix Explicitly set `overrideOut` to your intended output directory.
gotcha The plugin only removes specific fields (scripts, devDependencies) by default. Other fields like 'private' are not automatically removed.
fix Manually specify additional fields to remove in your own build script.
gotcha Using `lifecycle: 'onStart'` may cause the package.json to be copied before the build produces the final output, potentially leading to outdated files.
fix Use the default `onEnd` lifecycle to ensure the build is complete.
npm install esbuild-plugin-package-json
yarn add esbuild-plugin-package-json
pnpm add esbuild-plugin-package-json

Demonstrates basic usage of the plugin with esbuild, configuring the lifecycle and output directory.

import { packageJsonPlugin } from 'esbuild-plugin-package-json';
import esbuild from 'esbuild';

await esbuild.build({
  entryPoints: ['src/index.ts'],
  outdir: 'dist',
  bundle: true,
  plugins: [
    packageJsonPlugin({
      lifecycle: 'onEnd',
      overrideOut: 'dist',
      // overridePackageJson: './src', // optional custom path
    }),
  ],
});