Node.js Project to Minify HTML Source Code Using html-minifier Library Full Project For Beginners

Node.js Project to Minify HTML Source Code Using html-minifier Library Full Project For Beginners

 

Welcome folks today in this blog post we will be minifying html source code using html-minifier library. All the full source code of the application is given below.

 

 

 

 

Get Started

 

 

 

In order to get started we need to install html-minifier library by using the npm command

 

npm i html-minifier

 

 

After installing this library we need to create index.js file and copy paste the following code

 

 

index.js

 

 

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>'

 

 

 

Now you can see that we are including the library in the first line and then we are passing the html to the minifier function of the library and then we are printing out the minified html

 

You can pass various options to the function

 

–collapse-whitespace

–remove-comments

–remove-optional-tags

–remove-redundant-attributes

–remove-script-type-attributes

–remove-tag-whitespace

–use-short-doctype

See also  How to Export Canvas to Image Using Blob.js and Save it as PNG Image Using FileSaver.js Full Example For Beginners

–minify-css true

–minify-js

 

And you can also install this module globally inside your system by executing this command

 

 

npm i -g html-minifier

 

 

This will install the library globally inside your system so that you can compress html at the command line

 

html-minifier --input-dir views --output-dir html

 

Now in this command we are providing the input directory views and inside this we have a file called index.html and then we are providing the output directory html

 

And also you can provide additional options to this command by executing the command

 

html-minifier -h

 

-h is for help

 

This will show all the options

 

 

 

Leave a Reply