Registry / devops / metalsmith-html-minifier

metalsmith-html-minifier

JSON →
library4.0.1jsnpmunverified

A Metalsmith plugin that minifies HTML files using kangax/html-minifier. Current stable version is 4.0.1, compatible with Node.js >=8. It offers built-in defaults that aggressively optimize HTML (e.g. collapsing whitespace, removing comments, removing attribute quotes) which may break conditional comments or whitespace-sensitive layouts. Unlike generic HTML minifiers, it integrates directly into the Metalsmith build pipeline, supporting glob patterns and custom minifier options. Release cadence is low; no major updates since 2019.

npm install metalsmith-html-minifier
INSTALL
IMPORT
SIG · METALSMITH-HTML-MI
M
metalsmith-html-minifier
devopsjavascriptv4.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

htmlMinifier
const htmlMinifier = require('metalsmith-html-minifier');
import htmlMinifier from 'metalsmith-html-minifier';
This package uses CommonJS, not ESM. The default export is the plugin function.
plugin (default export)
Metalsmith(__dirname).use(htmlMinifier());
Metalsmith(__dirname).use(htmlMinifier({})());
htmlMinifier is a function that returns a Metalsmith plugin. Do not call .use() with the function itself without invoking it.
Options (pattern, minifierOptions)
htmlMinifier({ pattern: '**/*.html', minifierOptions: { removeComments: false } })
htmlMinifier({ pattern: '**/*.html', removeComments: false })
Minifier-specific options must be nested inside the `minifierOptions` object, not at the top level.

Shows Metalsmith setup with html-minifier plugin, custom glob pattern, and explicit minifier options. Demonstrates disabling certain defaults to avoid breaking HTML.

const Metalsmith = require('metalsmith'); const htmlMinifier = require('metalsmith-html-minifier'); Metalsmith(__dirname) .use(htmlMinifier({ pattern: '**/*.html', minifierOptions: { collapseWhitespace: true, removeComments: true, removeAttributeQuotes: true, collapseBooleanAttributes: true, removeEmptyAttributes: false, removeRedundantAttributes: false } })) .build((err) => { if (err) throw err; console.log('Build complete!'); });
Debug
Known issues
breakingDefault options remove comments and collapse whitespace, which can break conditional comments (e.g., IE-specific) or pre-formatted text.
fix
Set minifierOptions.removeComments: false and minifierOptions.collapseWhitespace: false if you need to preserve them.
affects: >=1.0.0
gotchaThe `pattern` option uses minimatch globs; the default pattern is `**/*.html`, but if you specify a single string, it will only match that exact string (for non-glob patterns).
fix
Always use a glob pattern like `'**/*.html'` or an array of globs.
affects: >=4.0.0
deprecatedThe html-minifier library used internally has been largely unmaintained since 2019. No security updates are expected.
fix
Consider switching to an alternative html-minifier that is actively maintained (e.g., html-minifier-terser or using a different build tool).
affects: *
gotchaThe plugin minifies all HTML files by default unless you provide a `pattern` option. This may minify files you didn't intend (e.g., partials or includes).
fix
Always specify a restrictive pattern like `['**/*.html', '!**/_*/**']` to exclude partials.
affects: *
Errors
Common errors & fixes
TypeError: htmlMinifier is not a function
Using ES module import syntax (import) on a CommonJS module that does not have a default export.
fix
Use `const htmlMinifier = require('metalsmith-html-minifier');` instead of `import htmlMinifier from 'metalsmith-html-minifier';`.
TypeError: metalsmith(...).use(...) is not a function
Calling .use() with the result of htmlMinifier() incorrectly, e.g., passing extra parentheses.
fix
Use `Metalsmith(__dirname).use(htmlMinifier())` not `Metalsmith(__dirname).use(htmlMinifier({})())`.
Error: Cannot find module 'html-minifier'
The dependency 'html-minifier' is missing from node_modules (e.g., npm install failed or using npm with no-shrinkwrap and peer dependencies).
fix
Run `npm install metalsmith-html-minifier --save` which will install the required dependency automatically.
Upgrade
Version history
4.0.1latest on npm
Audit
Dependencies
html-minifierrequiredCore minification library used to process HTML files.
minimatchrequiredFile pattern matching for selecting files to minify.
Agent activity
2 hits · last 30 days
node
2
Resources
metalsmith-html-minifier — npm install metalsmith-html-minifier · libregistry