npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

minify-html-webpack-plugin

v1.1.7

Published

A webpack plugin to minify html file(s) after building

Downloads

7,362

Readme

Webpack plugin: minify-html-webpack-plugin

npm node-current npm Libraries.io dependency status for GitHub repo NPM

This is a webpack plugin that can minimize the HTML with HTMLMinifier for all source directory files and copy into destinations directory recursively iterating through all subfolders and files during the Webpack build.

Installation

Install the plugin with npm:

$ npm install minify-html-webpack-plugin --save-dev

Heads up!

Please do not report issues related to HTML parsing and output on this repository. Report those issues to the html-minifier issue tracker.

Basic Usage

Add the plugin to your webpack and config as follows:

    const MinifyHtmlWebpackPlugin = require('minify-html-webpack-plugin');
    const webpackConfig = {
        plugins: [
            new MinifyHtmlWebpackPlugin({
                src: './storage/framework/views',
                dest: './storage/framework/views',
                rules: {
                    collapseBooleanAttributes: true,
                    collapseWhitespace: true,
                    removeAttributeQuotes: true,
                    removeComments: true,
                    minifyJS: true,
                }
            });
        ]
    };

Optional Add Search And Replace Rules

If need to replace strings and then do minification across all source files in directoris and sub-directories.

    const MinifyHtmlWebpackPlugin = require('minify-html-webpack-plugin');
    const webpackConfig = {
        plugins: [
            new MinifyHtmlWebpackPlugin({
                src: './storage/framework/views',
                dest: './storage/framework/views',
                rules: {
                    collapseBooleanAttributes: true,
                    collapseWhitespace: true,
                    removeAttributeQuotes: true,
                    removeComments: true,
                    minifyJS: true,
                },
                searchAndReplace: [
                    {
                        /* The string, or regular expression, that will be replaced by the new value */
                        search: 'search_string',
                        /* The string to replace the search value with */
                        replace: 'replace_string' 
                    }
                ]
            });
        ]
    };

If need to replace array of string with common replace funtions and then minify it across source files and sub-directories.

    const MinifyHtmlWebpackPlugin = require('minify-html-webpack-plugin');
    const webpackConfig = {
        plugins: [
            new MinifyHtmlWebpackPlugin({
                afterBuild: true,
                src: './storage/framework/views',
                dest: './storage/framework/views',
                rules: {
                    collapseBooleanAttributes: true,
                    collapseWhitespace: true,
                    removeAttributeQuotes: true,
                    removeComments: true,
                    minifyJS: true,
                },
                searchAndReplace: [
                   {
                        search: './domain',
                        replace: 'https://original.domain.com'
                    },
                    {
                        /* The array of string value, or regular expression, that will be replaced by the new value returened from replace function */
                        search: ['./css/app.css', './js/app.js'],
                        replace: (searchString, index) => {
                            /* The custom replace logic to replace the search value with */

                            /* Example: Logic to to replace css, js file names with full domain name as prefix and version as suffix to it.

                                    './css/app.css' => 'https://original.domain.com/css/app.css?id=91352d1f26a97b89f271'
                                    './js/app.js' => 'https://original.domain.com/js/app.js?id=a1f1ae0cfce9bc2d3ce6'

                            */
                            const content = fs.readFileSync(path.resolve('/real/path/of/file', searchString), 'utf8');
                            return 'https://original.domain.com' + searchString.substring(1) + '?id=' + md5(content).substr(0, 20);
                        }
                    },
                ]
            });
        ]
    };

Laravel Mix Users

Paste below snippets into webpack.mix.js file.

    const MinifyHtmlWebpackPlugin = require('minify-html-webpack-plugin');
    const mix = require('laravel-mix');

    mix.webpackConfig({
        plugins: [
            new MinifyHtmlWebpackPlugin({
                afterBuild: true,
                src: './storage/framework/views',
                dest: './storage/framework/views',
                ignoreFileNameRegex: /\.(gitignore|php)$/,
                ignoreFileContentsRegex: /(<\?xml version)|(mail::message)/,
                rules: {
                    collapseBooleanAttributes: true,
                    collapseWhitespace: true,
                    removeAttributeQuotes: true,
                    removeComments: true,
                    minifyJS: true,
                }
            })
        ]
    });

Configuration

You can pass configuration options to MinifyHtmlWebpackPlugin. Each configuration has following items:

  • afterBuild: Optional. Add afterBuild:true, if you want to run complete minification process after webpack build, at the end.
  • dir: Optional. Base dir to find the files, if not provided, use the root of webpack context.
  • src: Required. source directory path.
  • dest: Required. destination directory path. Paste minified HTML contents from src directory files into dest directory.
  • ignoreFileNameRegex: Optional. Regex Expression to ingnore files in the src directory if it matches with the file name, if not provided, will minimize all files in src directory.`
  • ignoreFileContentsRegex: Optional. Regex Expression to ingnore files in the src directory if it matches with the file contents, if not provided, will minimize all files in src directory.`
  • rules: Required. See the html-minifer docs for all available options.
  • searchAndReplace: Optional. Array of all search and replace rules. check above examples.

License

This project is licensed under MIT.