rollup-plugin-html-minifier
raw JSON → 2.0.0 verified Mon Apr 27 auth: no javascript
Rollup plugin that minifies HTML files in the bundle using html-minifier. Current stable version: 2.0.0. Release cadence: low, with infrequent updates. Key differentiators: simple integration with Rollup, supports glob patterns for inclusion/exclusion, and exposes all html-minifier options. Alternative plugins like @rollup/plugin-html focus on HTML generation rather than minification. Note that most html-minifier options are disabled by default, requiring explicit configuration for actual minification.
Common errors
error Error: Could not resolve 'rollup-plugin-html-minifier' ↓
cause Package not installed or missing from node_modules.
fix
Run npm install rollup-plugin-html-minifier --save-dev
error TypeError: htmlMinifier is not a function ↓
cause Using CommonJS require() on a package that exports ESM only.
fix
Use import syntax or configure Node.js to handle ESM (e.g., type: 'module' in package.json).
error The emitted file 'index.html' is empty after build ↓
cause html-minifier options are all disabled; no minification applied, but content may also be missing if the plugin didn't process the file.
fix
Ensure the HTML file is included in Rollup's bundle and pass explicit options like { removeComments: true }.
Warnings
breaking Version 2.0.0 requires Rollup >= 2.10.0; older Rollup versions will break. ↓
fix Upgrade Rollup to version 2.10.0 or later.
gotcha Most html-minifier options are disabled by default; passing an empty options object yields no minification. ↓
fix Set desired options explicitly, e.g., options.collapseWhitespace: true.
gotcha The plugin does not process files that are not emitted or transformed by Rollup; include/exclude patterns only filter within Rollup's asset pipeline. ↓
fix Ensure HTML files are part of the Rollup bundle (e.g., through @rollup/plugin-html or a manual emit).
Install
npm install rollup-plugin-html-minifier yarn add rollup-plugin-html-minifier pnpm add rollup-plugin-html-minifier Imports
- htmlMinifier wrong
const htmlMinifier = require('rollup-plugin-html-minifier')correctimport htmlMinifier from 'rollup-plugin-html-minifier' - PluginOptions wrong
import { PluginOptions } from 'rollup-plugin-html-minifier' (if used as value)correctimport type { PluginOptions } from 'rollup-plugin-html-minifier' - htmlMinifier (with options object) wrong
htmlMinifier() without options (no minification occurs as defaults are mostly disabled)correctimport htmlMinifier from 'rollup-plugin-html-minifier'; htmlMinifier({ include: '*.html', options: { removeComments: true } })
Quickstart
// rollup.config.js
import htmlMinifier from 'rollup-plugin-html-minifier';
export default {
input: 'src/index.js',
output: {
dir: 'dist',
format: 'esm',
},
plugins: [
htmlMinifier({
include: '*.html',
options: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true,
minifyJS: true,
minifyCSS: true,
},
}),
],
};