esbuild-plugin-compressor

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

An esbuild plugin for compressing output files with gzip or brotli after bundling. Current version 1.0.1, released in 2023 with infrequent updates. It supports filtering by file type and optional deletion of originals. Differentiators include a simple API, TypeScript types, and compatibility with esbuild >=0.17.19. Compared to alternatives like esbuild-plugin-compress or manual post-build scripts, this plugin is lightweight and straightforward.

error Error: [plugin] compressor: No files matched the given fileTypes.
cause The fileTypes option did not match any output files (e.g., extensions without dot).
fix
Use extensions with leading dot in fileTypes, e.g., ['js'] not ['js'] but note that examples show ['js', 'css'] without dot, but the plugin internally adds dot if missing? Check source; safer to include dot. Actually from README: fileTypes: ['js', 'css'] works. The error occurs if no output files have those extensions.
gotcha If fileTypes is not provided or is an empty array, no files will be compressed.
fix Always specify fileTypes with at least one extension, e.g., ['js', 'css'].
gotcha The plugin compresses files after esbuild writes them; ensure outdir or outfile outputs are in place.
fix Verify that esbuild's output matches the plugin's expectations (same directory or specified outdir).
npm install esbuild-plugin-compressor
yarn add esbuild-plugin-compressor
pnpm add esbuild-plugin-compressor

Bundles a TypeScript entry point and compresses output JS and CSS files using brotli.

import * as esbuild from 'esbuild';
import { compressor } from 'esbuild-plugin-compressor';

await esbuild.build({
  entryPoints: ['src/index.ts'],
  bundle: true,
  outfile: 'dist/index.js',
  plugins: [
    compressor({
      fileTypes: ['js', 'css'],
      compressType: 'brotli',
    }),
  ],
});