html-minifier

repository·gh-pages·Indexed 26 days ago

https://github.com/kangax/html-minifier

A 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).

Tokens
3.2K
Snippets
4
Records
21
Agent score
86%

What's inside html-minifier

  1. Install html-minifier

    gh-pages

    You 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 -g

    For programmatic use in projects:

    npm install html-minifier

    From Git:

    git clone git://github.com/kangax/html-minifier.git
    cd html-minifier
    npm link .
  2. Improve compression ratio with attribute and class sorting

    gh-pages
    Using 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.
  3. Use the html-minifier CLI

    gh-pages
    The 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.
  4. Configure via a Config File

    gh-pages

    You can provide a configuration file using the --config-file <file> flag. The CLI supports two formats for the config file:

    1. JSON: A standard JSON file containing the option keys.
    2. JavaScript Module: A .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.

  5. Minify inline CSS and JavaScript

    gh-pages

    You can pass configuration objects to minifyCSS and minifyJS to use specialized minifiers:

    Inline CSS

    Pass an object to minifyCSS to configure clean-css. The library wraps CSS in a dummy selector to allow minification of inline styles.

    Inline JavaScript

    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.

    URL Minification

    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.

  6. Use html-minifier with Gulp

    gh-pages

    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)
  7. Use html-minifier in Node.js

    gh-pages

    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>'
  8. Configure HTMLMinifier options

    gh-pages

    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.