Registry / devops / html-minifier-with-remove-custom-attrs

html-minifier-with-remove-custom-attrs

JSON →
library4.2.0jsnpmunverified

HTMLMinifier is a highly configurable, well-tested JavaScript-based HTML minifier for Node.js (version 4.2.0). It compresses HTML by removing unnecessary whitespace, comments, and redundant attributes, with options for custom attribute removal. Compared to alternatives like htmlcompressor and minimize, it consistently achieves superior size reduction (e.g., 42 KB vs 46 KB on Google homepage). The package supports CLI and programmatic usage, and is actively maintained with regular releases.

npm install html-minifier-with-remove-custom-attrs
INSTALL
IMPORT
SIG · HTML-MINIFIER-WITH
H
html-minifier-with-remove-custom-attrs
devopsjavascriptv4.2.0
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.

minify
import { minify } from 'html-minifier'
const minify = require('html-minifier').minify
Both ESM import and CJS require work; this is the main function.
default
import htmlMinifier from 'html-minifier'
const htmlMinifier = require('html-minifier').default
Default export is an object with minify function; also available as named export.
minify
const { minify } = require('html-minifier')
const minify = require('html-minifier/minify')
Subpath imports are not supported; always use the main package.

Minifies an HTML file with whitespace collapse, comment removal, CSS/JS minification, and custom attribute removal using HTMLMinifier.

import { minify } from 'html-minifier'; import * as fs from 'fs'; const html = fs.readFileSync('input.html', 'utf8'); const result = minify(html, { removeAttributeQuotes: true, removeComments: true, collapseWhitespace: true, minifyCSS: true, // requires clean-css minifyJS: true, // requires uglify-js removeCustomAttrs: true, // custom attr removal customAttrRemoveRegexes: [/^data-/] // remove data-* attributes }); console.log(result);
Debug
Known issues
deprecatedOption 'removeCustomAttrs' is deprecated in favor of 'customAttrRemove' and 'customAttrRemoveRegexes'.
fix
Use 'customAttrRemove' (boolean) or 'customAttrRemoveRegexes' (array) instead.
affects: >=4.0
gotchaUnicode characters may be lost when using 'decodeEntities' option; use with caution.
fix
Avoid 'decodeEntities' if you need to preserve non-ASCII characters.
affects: >=3.0
gotchaCSS/JS minification options require optional dependencies (clean-css, uglify-js) to be installed; missing them silently skips minification.
fix
Install optional dependencies: npm install clean-css uglify-js
affects: >=4.0
gotchaUsing 'collapseWhitespace' with 'conservativeCollapse' may still remove some whitespace incorrectly for inline elements.
fix
Set 'conservativeCollapse' to false and test output; consider 'preserveLineBreaks'.
affects: >=3.0
breakingOption 'removeEmptyAttributes' was renamed to 'removeEmpty' in v4.0.
fix
Replace 'removeEmptyAttributes' with 'removeEmpty' in options.
affects: >=4.0
Errors
Common errors & fixes
TypeError: htmlMinifier.minify is not a function
Misunderstanding of exported object structure when using require.
fix
Use const { minify } = require('html-minifier') or const minify = require('html-minifier').minify.
Error: Cannot find module 'clean-css'
Optional dependency not installed when minifyCSS option is true.
fix
Run npm install clean-css (and uglify-js if minifyJS is true).
Attribute attributes from directive not minified
Angular or similar custom directives with attributes that are not recognized.
fix
Use customAttrRemoveRegexes: [/^\[.*\]$/] to match Angular attribute syntax.
Unicode characters lost during minification
decodeEntities option set to true converts non-ASCII to HTML entities, altering content.
fix
Set decodeEntities: false.
Upgrade
Version history
4.2.0latest on npm
Audit
Dependencies
clean-cssoptionalUsed for inline CSS minification when minifyCSS option is enabled.
uglify-jsoptionalUsed for inline JavaScript minification when minifyJS option is enabled.
heoptionalUsed for HTML entity encoding/decoding (e.g., decodeEntities, encodeAttributes).
Agent activity
8 hits · last 30 days
node
8
Resources
html-minifier-with-remove-custom-attrs — npm install html-minifier-with-remove-custom-attrs · libregistry