Highly configurable, well-tested, JavaScript-based HTML minifier with extensive optimization options
npx @tessl/cli install tessl/npm-html-minifier@4.0.0HTML Minifier is a highly configurable, well-tested JavaScript-based HTML minifier that reduces HTML file size by removing unnecessary whitespace, comments, and attributes while preserving functionality. It offers extensive configuration options and supports both programmatic use through Node.js APIs and command-line usage.
npm install html-minifierconst { minify } = require("html-minifier");For ES6 modules:
import { minify } from "html-minifier";const { minify } = require("html-minifier");
const html = `
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<style>
body { margin: 0; }
</style>
</head>
<body>
<h1>Hello World</h1>
<script>
console.log("Hello");
</script>
</body>
</html>
`;
// Basic minification
const minified = minify(html, {
collapseWhitespace: true,
removeComments: true,
minifyCSS: true,
minifyJS: true
});
console.log(minified);HTML Minifier is built around several key components:
minify()Primary HTML minification function that reduces file size through configurable optimization rules.
/**
* Minifies HTML content with configurable options
* @param {string} value - HTML string to minify
* @param {MinificationOptions} options - Configuration options (optional)
* @returns {string} Minified HTML string
*/
function minify(value, options);
interface MinificationOptions {
// Whitespace and formatting
collapseWhitespace?: boolean;
collapseInlineTagWhitespace?: boolean;
conservativeCollapse?: boolean;
preserveLineBreaks?: boolean;
trimCustomFragments?: boolean;
maxLineLength?: number;
// Comments
removeComments?: boolean;
processConditionalComments?: boolean;
ignoreCustomComments?: RegExp[];
// Attributes
removeAttributeQuotes?: boolean;
removeEmptyAttributes?: boolean;
removeRedundantAttributes?: boolean;
collapseBooleanAttributes?: boolean;
caseSensitive?: boolean;
sortAttributes?: boolean;
preventAttributesEscaping?: boolean;
// Tags and elements
removeOptionalTags?: boolean;
removeEmptyElements?: boolean;
removeTagWhitespace?: boolean;
includeAutoGeneratedTags?: boolean;
keepClosingSlash?: boolean;
// Script and style processing
minifyJS?: boolean | object | ((text: string, inline: boolean) => string);
minifyCSS?: boolean | object | ((text: string, type: string) => string);
minifyURLs?: boolean | string | object | ((text: string) => string);
removeScriptTypeAttributes?: boolean;
removeStyleLinkTypeAttributes?: boolean;
processScripts?: string[];
// HTML5 and parsing
html5?: boolean;
continueOnParseError?: boolean;
decodeEntities?: boolean;
useShortDoctype?: boolean;
// Custom patterns
customAttrAssign?: RegExp[];
customAttrSurround?: RegExp[];
customAttrCollapse?: RegExp;
customEventAttributes?: RegExp[];
ignoreCustomFragments?: RegExp[];
// CSS class handling
sortClassName?: boolean | ((value: string) => string);
// Quote handling
quoteCharacter?: string;
// Logging
log?: (message: string) => void;
}Default Values:
// Default options applied by html-minifier
{
html5: true,
ignoreCustomComments: [/^!/],
ignoreCustomFragments: [/<%[\s\S]*?%>/, /<\?[\s\S]*?\?>/],
includeAutoGeneratedTags: true,
log: function() {} // No-op logging function
}Usage Examples:
const { minify } = require("html-minifier");
// Aggressive minification
const result = minify(html, {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
removeOptionalTags: true,
minifyCSS: true,
minifyJS: true,
minifyURLs: true
});
// Conservative minification with custom options
const conservativeResult = minify(html, {
collapseWhitespace: true,
conservativeCollapse: true, // Always keep at least one space
removeComments: true,
ignoreCustomComments: [/^!/, /^\s*#/], // Keep certain comments
minifyCSS: {
level: 1 // Basic CSS optimization only
},
minifyJS: {
compress: false, // No JavaScript compression
mangle: false
}
});
// Custom validation function for empty attributes
const customResult = minify(html, {
removeEmptyAttributes: (attrName, tag) => {
// Only remove empty class and id attributes
return attrName === 'class' || attrName === 'id';
}
});Comprehensive CLI for HTML minification with file and directory processing.
# Basic usage
html-minifier input.html
# With options
html-minifier --collapse-whitespace --remove-comments input.html -o output.html
# Process directory
html-minifier --input-dir src --output-dir dist --file-ext html
# Using configuration file
html-minifier -c minify.json input.htmlCLI Options:
Core Options:
-c, --config-file <file>-o, --output <file>--input-dir <dir>--output-dir <dir>--file-ext <ext>Whitespace and Formatting:
--collapse-whitespace--collapse-inline-tag-whitespace--conservative-collapse--preserve-line-breaks--trim-custom-fragments--max-line-length <n>Comments:
--remove-comments--process-conditional-comments--ignore-custom-comments <patterns>Attributes:
--remove-attribute-quotes--remove-empty-attributes--remove-redundant-attributes--collapse-boolean-attributes--case-sensitive--sort-attributes--prevent-attributes-escapingTags and Elements:
--remove-optional-tags--remove-empty-elements--remove-tag-whitespace--include-auto-generated-tags--keep-closing-slashScript and Style Processing:
--minify-css [value]--minify-js [value]--minify-urls [value]--remove-script-type-attributes--remove-style-link-type-attributes--process-scripts <types>HTML5 and Parsing:
--html5--no-html5--continue-on-parse-error--decode-entities--use-short-doctypeCustom Patterns:
--custom-attr-assign <patterns>--custom-attr-surround <patterns>--custom-attr-collapse <pattern>--custom-event-attributes <patterns>--ignore-custom-fragments <patterns>Other Options:
--sort-class-name--quote-character <char>Advanced HTML parsing functionality for direct parser access and DOM conversion.
const { HTMLParser, HTMLtoXML, HTMLtoDOM } = require("html-minifier/src/htmlparser");
/**
* Core HTML parser class for custom parsing scenarios
* @param {string} html - HTML content to parse
* @param {object} handler - Handler object with parsing callbacks
*/
function HTMLParser(html, handler);
/**
* Convert HTML to XML format
* @param {string} html - HTML content to convert
* @returns {string} XML formatted output
*/
function HTMLtoXML(html);
/**
* Convert HTML to DOM structure
* @param {string} html - HTML content to convert
* @param {Document} doc - Document object for DOM creation
* @returns {DocumentFragment} DOM fragment containing parsed HTML
*/
function HTMLtoDOM(html, doc);Usage Examples:
const { HTMLParser, HTMLtoXML } = require("html-minifier/src/htmlparser");
// Custom parsing with handler
const handler = {
start: (tag, attrs, unary) => console.log('Start tag:', tag),
end: (tag) => console.log('End tag:', tag),
chars: (text) => console.log('Text:', text),
comment: (text) => console.log('Comment:', text)
};
HTMLParser('<div>Hello <b>world</b></div>', handler);
// Convert HTML to XML
const xml = HTMLtoXML('<div class="test">Content</div>');
console.log(xml); // Output: <div class="test">Content</div>Low-level utility functions for map creation and string processing.
const { createMap, createMapFromString } = require("html-minifier/src/utils");
/**
* Create a lookup map from array of values
* @param {string[]} values - Array of string values for map keys
* @param {boolean} ignoreCase - Whether to ignore case for lookups
* @returns {function} Lookup function that returns boolean for key existence
*/
function createMap(values, ignoreCase);
/**
* Create a lookup map from comma-separated string
* @param {string} values - Comma-separated string of values
* @param {boolean} ignoreCase - Whether to ignore case for lookups
* @returns {function} Lookup function that returns boolean for key existence
*/
function createMapFromString(values, ignoreCase);Usage Examples:
const { createMap, createMapFromString } = require("html-minifier/src/utils");
// Create map from array
const tagMap = createMap(['div', 'span', 'p'], false);
console.log(tagMap('div')); // true
console.log(tagMap('section')); // false
// Create map from string
const attrMap = createMapFromString('class,id,style', false);
console.log(attrMap('class')); // true
console.log(attrMap('data-test')); // false
// Case-insensitive map
const caseInsensitiveMap = createMapFromString('DIV,SPAN', true);
console.log(caseInsensitiveMap('div')); // true
console.log(caseInsensitiveMap('SPAN')); // trueinterface Attribute {
name: string;
value: string;
quote: string;
customAssign?: string;
customOpen?: string;
customClose?: string;
}
interface ProcessingOptions {
name: (name: string) => string;
canCollapseWhitespace: (tag: string, attrs?: Attribute[]) => boolean;
canTrimWhitespace: (tag: string, attrs?: Attribute[]) => boolean;
log: (message: string) => void;
minifyCSS: (text: string, type?: string) => string;
minifyJS: (text: string, inline?: boolean) => string;
minifyURLs: (text: string) => string;
}
interface MinificationResult {
// Minified HTML string returned directly from minify()
toString(): string;
}HTML Minifier throws descriptive errors for various failure conditions:
try {
const result = minify(invalidHtml, options);
} catch (error) {
console.error("Minification failed:", error.message);
// Common errors:
// - Parse errors for malformed HTML
// - Configuration errors for invalid options
// - Processing errors from CSS/JS minifiers
}Error Prevention:
// Handle parse errors gracefully
const result = minify(html, {
continueOnParseError: true, // Don't abort on parse errors
log: (message) => console.warn("Warning:", message) // Log warnings
});
// Validate input before processing
if (typeof html !== 'string' || html.trim().length === 0) {
throw new Error("Input must be a non-empty string");
}// Performance monitoring
const startTime = Date.now();
const result = minify(largeHtml, options);
console.log(`Processed in ${Date.now() - startTime}ms`);
// Memory-efficient processing for large files
const fs = require('fs');
const html = fs.readFileSync('large-file.html', 'utf8');
const minified = minify(html, {
maxLineLength: 1000 // Split long lines to manage memory
});SVG elements are automatically detected and processed with case-sensitivity preserved:
const svgHtml = '<svg viewBox="0 0 100 100"><circle cx="50" cy="50" r="40"/></svg>';
const result = minify(svgHtml, {
caseSensitive: false // Will be automatically enabled for SVG content
});Internet Explorer conditional comments receive special handling:
const ieHtml = '<!--[if IE]><p>You are using Internet Explorer</p><![endif]-->';
const result = minify(ieHtml, {
processConditionalComments: true // Process content inside conditional comments
});Preserve template syntax and custom markup:
const templateHtml = '<div><?php echo $content; ?></div>{{ angular.expression }}';
const result = minify(templateHtml, {
ignoreCustomFragments: [
/\<\?php[\s\S]*?\?\>/, // PHP tags
/\{\{[\s\S]*?\}\}/ // Angular expressions
]
});Use special comments to preserve specific markup sections:
<!-- htmlmin:ignore -->
<pre>
This content will be preserved exactly
including all whitespace
</pre>
<!-- htmlmin:ignore -->