How to use the minify function from html-minifier

Find comprehensive JavaScript html-minifier.minify code examples handpicked from public code repositorys.

html-minifier.minify is a method in the html-minifier library that minifies HTML code by removing unnecessary whitespace and other characters.

98
99
100
101
102
103
104
105
106
107
});

// Minify HTML output
eleventyConfig.addTransform("htmlmin", function(content, outputPath) {
    if (outputPath && outputPath.indexOf(".html") > -1) {
        let minified = htmlmin.minify(content, {
            useShortDoctype: true,
            removeComments: true,
            collapseWhitespace: true
        });
fork icon2
star icon2
watch icon3

+ 3 other calls in file

1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
    if (err) {
        return res.status(500).json({
            error: "Failed..",
        });
    }
    const result = minify(content, {
        minifyJS: true,
    });
    return res.set("Content-Type", "text/html").send(result);
});
fork icon0
star icon2
watch icon3

How does html-minifier.minify work?

html-minifier.minify is a method in the html-minifier library that minifies HTML code by removing unnecessary whitespace and other characters. Here's how it works:

  1. html-minifier.minify takes an HTML string as its input and returns a minified version of the HTML string.

  2. By default, html-minifier.minify removes all comments, spaces between tags, and extra whitespace inside tags.

  3. It also removes unnecessary attributes, such as type="text/javascript" and lang="en", and converts attribute values that can be expressed in a shorter form, such as class="foo bar" to class="foobar".

  4. html-minifier.minify provides a number of options that can be used to customize the minification process, such as collapseWhitespace, removeComments, and removeAttributeQuotes.

  5. Some of the options may change the output of the HTML code, so it is important to review the documentation and test the output thoroughly.

Here is an example of using html-minifier.minify to minify an HTML string:

javascript
import { minify } from 'html-minifier'; const html = ` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>Hello, world!</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> </body> </html> `; const minifiedHtml = minify(html, { collapseWhitespace: true }); console.log(minifiedHtml);

In this example, we import the minify method from the html-minifier library.

We create an HTML string with some whitespace and comments.

We use minify to minify the HTML string and remove unnecessary whitespace and comments.

Finally, we log the minified HTML string to the console.

208
209
210
211
212
213
214
215
216
217
processBody(moduleName) {
    this.files[moduleName].forEach(file => {
        const tpl = {};
        tpl.source = fs.readFileSync(file);
        // tpl.source = htmlmin(tpl.source);
        tpl.source = htmlMinifier.minify(
            tpl.source.toString(),
            {
            collapseBooleanAttributes: true,
            collapseInlineTagWhitespace: false,
fork icon0
star icon0
watch icon1

+ 4 other calls in file

122
123
124
125
126
127
128
129
130
131
		if(typeof minimizeOptions[name] === "undefined") {
			minimizeOptions[name] = true;
		}
	});

	content = htmlMinifier.minify(content, minimizeOptions);
}

if(config.interpolate && config.interpolate !== 'require') {
	// Double escape quotes so that they are not unescaped completely in the template string
fork icon0
star icon0
watch icon1

+ 4 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { minify } from "html-minifier";

const html = `

Document

Hello, world!
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

`;

const minifiedHtml = minify(html, { collapseWhitespace: true });

console.log(minifiedHtml);

In this example, we import the minify method from the html-minifier library. We create an HTML string with some whitespace and comments. We use minify to minify the HTML string and remove unnecessary whitespace and comments. Finally, we log the minified HTML string to the console. The output will be a minified version of the HTML code, with all the unnecessary whitespace and comments removed.

397
398
399
400
401
402
403
404
405
406
})

// Minify HTML
eleventyConfig.addTransform('htmlmin', (content, outputPath) => {
    if (outputPath && outputPath.endsWith('.html')) {
        const minified = htmlmin.minify(content, {
            useShortDoctype: true,
            removeComments: true,
            collapseWhitespace: true,
        })
fork icon0
star icon0
watch icon1

+ 3 other calls in file

32
33
34
35
36
37
38
39
40
41
eleventyConfig.setTemplateFormats(['md', 'njk']);
eleventyConfig.addPlugin(ErrorOverlay);
eleventyConfig.addPlugin(eleventyNavigationPlugin);
eleventyConfig.addTransform('htmlmin', (content, outputPath) => {
  if (outputPath.endsWith('.html')) {
    let minified = HtmlMin.minify(content, {
      useShortDoctype: true,
      removeComments: true,
      collapseWhitespace: true,
    });
fork icon0
star icon0
watch icon1

+ 3 other calls in file

68
69
70
71
72
73
74
75
76
77
module.exports = function(eleventyConfig) {
    eleventyConfig.addPassthroughCopy("src/assets");
    eleventyConfig.addShortcode('image', imageShortcode);
    eleventyConfig.addTransform('htmlmin', function(content, outputPath) {
        if (outputPath.endsWith('.html')) {
            return htmlmin.minify(content, {
                useShortDoctype: true,
                removeComments: true,
                collapseWhitespace: true
            });
fork icon0
star icon0
watch icon1