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
1 2 3 4 5 |
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
–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