Ignore chunks of markup
gh-pagesTo prevent specific sections of your HTML from being minified, wrap them in the following comment tags:
<!-- htmlmin:ignore -->
<div class="preserved">This content will not be minified</div>
<!-- htmlmin:ignore -->repository·gh-pages·Indexed 26 days ago
https://github.com/kangax/html-minifierA highly configurable, well-tested, JavaScript-based HTML minifier (version 4.0.0). It provides extensive options to control the processing of whitespace, comments, attributes, and embedded CSS/JS. It can be used as a global CLI app, programmatically in Node.js, or integrated into Gulp pipelines. Note: This version is no longer maintained; users are encouraged to migrate to HTML Minifier Next (HMN).
To prevent specific sections of your HTML from being minified, wrap them in the following comment tags:
<!-- htmlmin:ignore -->
<div class="preserved">This content will not be minified</div>
<!-- htmlmin:ignore -->html-minifier. During minification, both case-sensitivity and closing slashes in SVG tags are preserved, regardless of the other minification settings applied to the file.To run the built-in benchmarks for minified HTML, execute the following command in the repository root:
node benchmark.jsYou can install html-minifier via NPM for either CLI or programmatic use, or directly from Git.
As a global CLI app:
npm install html-minifier -gFor programmatic use in projects:
npm install html-minifierFrom Git:
git clone git://github.com/kangax/html-minifier.git
cd html-minifier
npm link .sortAttributes and sortClassName does not reduce the plain-text size of the output. However, these options create long repetitive chains of characters that improve the compression ratio of gzip used in HTTP compression.html-minifier CLI allows you to minify HTML files, directories, or input from STDIN via the terminal. You can specify output files, directories, or use a configuration file to control minification behavior.You can provide a configuration file using the --config-file <file> flag. The CLI supports two formats for the config file:
.js file that exports a configuration object.When using JSON for options that require arrays of regexes (like customAttrSurround), you must provide the regex as a string. The CLI will attempt to parse these strings into RegExp objects.
You can pass configuration objects to minifyCSS and minifyJS to use specialized minifiers:
Pass an object to minifyCSS to configure clean-css. The library wraps CSS in a dummy selector to allow minification of inline styles.
Pass an object to minifyJS to configure uglify-js.
value.parse.bare_returns: Set to true if you want to allow bare returns in inline scripts.Pass an object to minifyURLs to configure relateurl. If you pass a string, it is treated as the site base URL for relative path conversion.
html-minifier is no longer maintained. For an up-to-date version with new features and critical security fixes, use HTML Minifier Next (HMN) (@j9t).Integrate html-minifier into a Gulp pipeline by calling htmlMinify.minify() on the file contents within a data event listener.
const { src, dest, series } = require('gulp');
const htmlMinify = require('html-minifier');
const options = {
includeAutoGeneratedTags: true,
removeAttributeQuotes: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
sortClassName: true,
useShortDoctype: true,
collapseWhitespace: true
};
function html() {
return src('app/**/*.html')
.on('data', function(file) {
const buferFile = Buffer.from(htmlMinify.minify(file.contents.toString(), options))
return file.contents = buferFile
})
.pipe(dest('build'))
}
exports.html = series(html)Import the minify function from html-minifier to process HTML strings programmatically.
var minify = require('html-minifier').minify;
var result = minify('<p title="blah" id="moo">foo</p>', {
removeAttributeQuotes: true
});
result; // '<p title=blah id=moo>foo</p>'The minify function uses an options object to customize behavior. If you provide an object instead of a function for certain keys, the library provides default implementations:
name: Function to normalize tag/attribute names (defaults to toLowerCase). Set to identity if caseSensitive: true is used.collapseWhitespace: Boolean to enable/disable whitespace collapsing.canCollapseWhitespace: Function to determine if a tag allows whitespace collapsing.canTrimWhitespace: Function to determine if a tag allows whitespace trimming.html5: Boolean (defaults to true).ignoreCustomComments: Array of RegExps to ignore specific comments.ignoreCustomFragments: Array of RegExps to ignore custom markup fragments (e.g., template engines).includeAutoGeneratedTags: Boolean to control whether auto-generated tags are kept.log: Function for logging errors or warnings.minifyCSS: Function or object (using clean-css) to minify inline CSS.minifyJS: Function or object (using uglify-js) to minify inline JavaScript.minifyURLs: Function or object (using relateurl) to minify URLs.