esbuild HTML Minifier Plugin

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

An esbuild plugin that minifies HTML output files using html-minifier-terser. Version 1.0.0 is current. It processes HTML files after other plugins (e.g., @craftamap/esbuild-plugin-html) have generated them. Key differentiators: integrates with esbuild's build process, supports glob patterns for file selection, and allows custom minifier options. No active development observed since release.

error Error: esbuild-plugin-html-minifier-terser requires a peer of html-minifier-terser but none is installed.
cause html-minifier-terser is a peer dependency not automatically installed.
fix
Install html-minifier-terser as a devDependency: npm i -D html-minifier-terser
error TypeError: htmlMinifierPlugin is not a function (or TypeError: (0 , ...) is not a function)
cause Using require() in a CommonJS context where package is ESM-only.
fix
Use import syntax or set 'type': 'module' in package.json.
error Error: Plugin 'html-minifier-terser' must be used after HTML output plugins.
cause Order of plugins in array matters; HTML files not yet generated when plugin runs.
fix
Place htmlMinifierPlugin after plugins that generate HTML files.
gotcha minifierOptions are not merged with defaults; providing an object overwrites all defaults.
fix Include all desired options in minifierOptions, as defaults are not applied.
gotcha Plugin runs in the 'onEnd' hook, meaning HTML files must be generated by other plugins before minification.
fix Ensure plugins that generate HTML are placed before htmlMinifierPlugin in the plugins array.
deprecated Only default export is available; no named exports.
fix Use default import. Do not try to import named symbols.
gotcha Package depends on html-minifier-terser which must be installed explicitly as a devDependency.
fix Run 'npm i -D html-minifier-terser' in addition to the plugin.
npm install esbuild-plugin-html-minifier-terser
yarn add esbuild-plugin-html-minifier-terser
pnpm add esbuild-plugin-html-minifier-terser

Minifies all HTML files in the output directory during esbuild build.

import esbuild from 'esbuild';
import htmlMinifierPlugin from 'esbuild-plugin-html-minifier-terser';

await esbuild.build({
  entryPoints: ['src/index.js'],
  outdir: 'dist',
  bundle: true,
  plugins: [
    htmlMinifierPlugin({
      include: '**/*.html',
      baseDir: 'outdir',
      minifierOptions: {
        caseSensitive: true,
        collapseWhitespace: true,
        keepClosingSlash: true,
        minifyCSS: true,
        minifyJS: true,
        removeComments: true,
      },
    }),
  ],
});