vite-plugin-html-minifier

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

A Vite plugin that minifies HTML output using html-minifier-terser. Current stable version 1.0.5, released 2024-12. Compatible with Vite >=3.0.0. Differentiators: simple configuration, supports both boolean and fine-grained MinifyOptions, allows filtering via regex/string/function. Ships TypeScript types. Requires Node >=20.10.0 and pnpm >=9.0.4 as recommended by the package.

error Cannot find module 'vite-plugin-html-minifier' or its corresponding type declarations.
cause Package is not installed or TypeScript cannot locate types.
fix
Run npm install -D vite-plugin-html-minifier and ensure tsconfig.json includes 'node_modules/@types'.
error TypeError: htmlMinifier is not a function
cause Using named import instead of default import in ESM.
fix
Change import to import htmlMinifier from 'vite-plugin-html-minifier'.
gotcha Default import export expects default, not named.
fix Use `import htmlMinifier from 'vite-plugin-html-minifier'` instead of named import.
gotcha MinifyOptions is a type, not a value; cannot be imported using runtime import.
fix Use `import type { MinifyOptions } from 'vite-plugin-html-minifier'`.
npm install vite-plugin-html-minifier
yarn add vite-plugin-html-minifier
pnpm add vite-plugin-html-minifier

Shows basic usage of the plugin with custom minify options and filter regex.

// vite.config.ts
import { defineConfig } from 'vite'
import htmlMinifier from 'vite-plugin-html-minifier'

export default defineConfig({
  plugins: [
    htmlMinifier({
      minify: {
        collapseWhitespace: true,
        removeComments: true,
        minifyCSS: true,
        minifyJS: true
      },
      filter: /.*\.html$/
    })
  ]
})