HTML Loader provides built-in HTML minification capabilities using html-minifier-terser, with automatic production mode detection and extensive configuration options.
Control HTML minification behavior through boolean or detailed options.
/**
* Configure HTML minification
* @param minimize - Boolean to enable/disable with defaults, or detailed configuration object
*/
interface MinimizeOptions {
/** Treat attributes in case sensitive manner */
caseSensitive?: boolean;
/** Collapse white space that contributes to text nodes in a document tree */
collapseWhitespace?: boolean;
/** Always collapse to 1 space (never remove it entirely) */
conservativeCollapse?: boolean;
/** Keep the trailing slash on singleton elements */
keepClosingSlash?: boolean;
/** Minify CSS in style elements and style attributes */
minifyCSS?: boolean;
/** Minify JavaScript in script elements and event attributes */
minifyJS?: boolean;
/** Strip HTML comments */
removeComments?: boolean;
/** Remove attributes when value matches default */
removeRedundantAttributes?: boolean;
/** Remove type="text/javascript" from script tags */
removeScriptTypeAttributes?: boolean;
/** Remove type="text/css" from style and link tags */
removeStyleLinkTypeAttributes?: boolean;
}Access the default minification configuration used by html-loader.
/**
* Default options object for HTML minification
* These are the options applied when minimize: true
*/
const defaultMinimizerOptions: MinimizeOptions;
// Actual default values:
const defaultMinimizerOptions = {
caseSensitive: true,
collapseWhitespace: true,
conservativeCollapse: true,
keepClosingSlash: true,
minifyCSS: true,
minifyJS: true,
removeComments: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
};Usage Examples:
// Import default options
const { defaultMinimizerOptions } = require("html-loader");
// Use defaults in webpack config
{
test: /\.html$/i,
loader: "html-loader",
options: {
minimize: true // Uses defaultMinimizerOptions
}
}
// Extend defaults with custom options
{
test: /\.html$/i,
loader: "html-loader",
options: {
minimize: {
...defaultMinimizerOptions,
removeComments: false,
collapseWhitespace: false
}
}
}Minification is automatically enabled in webpack production mode.
/**
* Automatic minification behavior based on webpack mode
* - Production mode: minimize defaults to defaultMinimizerOptions
* - Development mode: minimize defaults to false
* - No mode specified: treated as production, minimize defaults to defaultMinimizerOptions
*/Configuration Examples:
// Minification automatically enabled in production
module.exports = {
mode: 'production', // or no mode specified
module: {
rules: [
{
test: /\.html$/i,
loader: "html-loader"
// minimize option not needed - automatically enabled
}
]
}
};
// Explicitly disable in production
module.exports = {
mode: 'production',
module: {
rules: [
{
test: /\.html$/i,
loader: "html-loader",
options: {
minimize: false
}
}
]
}
};
// Explicitly enable in development
module.exports = {
mode: 'development',
module: {
rules: [
{
test: /\.html$/i,
loader: "html-loader",
options: {
minimize: true
}
}
]
}
};Override specific minification behaviors while maintaining others.
/**
* Custom minification configuration examples
*/Common Customizations:
// Preserve comments
{
test: /\.html$/i,
loader: "html-loader",
options: {
minimize: {
removeComments: false
}
}
}
// Preserve whitespace
{
test: /\.html$/i,
loader: "html-loader",
options: {
minimize: {
collapseWhitespace: false,
conservativeCollapse: false
}
}
}
// Disable CSS/JS minification (handle with separate loaders)
{
test: /\.html$/i,
loader: "html-loader",
options: {
minimize: {
minifyCSS: false,
minifyJS: false
}
}
}
// XHTML-compatible settings
{
test: /\.html$/i,
loader: "html-loader",
options: {
minimize: {
keepClosingSlash: true,
caseSensitive: true,
removeRedundantAttributes: false
}
}
}
// Conservative minification (minimal changes)
{
test: /\.html$/i,
loader: "html-loader",
options: {
minimize: {
collapseWhitespace: true,
conservativeCollapse: true,
keepClosingSlash: true,
removeComments: true,
// Disable potentially breaking optimizations
removeRedundantAttributes: false,
removeScriptTypeAttributes: false,
removeStyleLinkTypeAttributes: false,
minifyCSS: false,
minifyJS: false
}
}
}Understanding which options are safe to use in different contexts.
/**
* Safety guidelines for minification options
*/Safe Options (Generally recommended):
removeComments: truecollapseWhitespace: trueconservativeCollapse: truekeepClosingSlash: trueUse with Caution:
removeRedundantAttributes: falseremoveEmptyAttributes: falsecaseSensitive: trueAdvanced Options:
minifyCSS: trueminifyJS: trueminifyURLs: falseExample Safe Production Configuration:
{
test: /\.html$/i,
loader: "html-loader",
options: {
minimize: {
caseSensitive: true,
collapseWhitespace: true,
conservativeCollapse: true,
keepClosingSlash: true,
minifyCSS: true,
minifyJS: true,
removeComments: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
// Explicitly avoid potentially breaking options
removeRedundantAttributes: false,
removeEmptyAttributes: false,
minifyURLs: false
}
}
}HTML Loader uses html-minifier-terser internally for minification.
/**
* All html-minifier-terser options are supported
* See: https://github.com/DanielRuf/html-minifier-terser#options-quick-reference
*/Complete Option Reference:
All options from html-minifier-terser are supported, including:
collapseBooleanAttributescollapseInlineTagWhitespacecollapseWhitespaceconservativeCollapsecontinueOnParseErrorcustomAttrAssigncustomAttrCollapsecustomAttrSurroundcustomEventAttributesdecodeEntitieshtml5ignoreCustomCommentsignoreCustomFragmentsincludeAutoGeneratedTagskeepClosingSlashmaxLineLengthminifyCSSminifyJSminifyURLspreserveLineBreakspreventAttributesEscapingprocessConditionalCommentsprocessScriptsquoteCharacterremoveAttributeQuotesremoveCommentsremoveEmptyAttributesremoveEmptyElementsremoveOptionalTagsremoveRedundantAttributesremoveScriptTypeAttributesremoveStyleLinkTypeAttributesremoveTagWhitespacesortAttributessortClassNametrimCustomFragmentsuseShortDoctypeRefer to the html-minifier-terser documentation for detailed descriptions of each option.