Welcome folks today in this post we will be minfying html css and js
source code using the library called html-minify
. All the source code of the application is given below.
Get Started
In order to get started we need to install html-minify
globally inside our system and then we can use this library commands inside our system. Run this npm
command as follows
npm i -g html-minify
-g
is the global flag to install this module globally inside our system
And after this we need to execute this command to compress html markup source code which contains css and js
code as well.
html-minify index.html
where index.html
is the input file name which needs to be compressed and if you run this it will output the compressed output html to the stdout
like this
So now to provide a output filename
you need to provide a -o
flag which stands for output filename as follows
html-minify index.html -o compressed.html
Now compressed.html
is the output file name which the library will generate as the output compressed html code.
Using NPM Dependency in Application
You can also use this dependency inside your node.js application. For this you just need to install this dependency without the -g
flag as follows
npm i html-minify
And then create a index.js
file and copy paste the following code
index.js
1 2 3 4 5 6 |
var puts = require('util').puts, fs = require('fs'), var htmlminify = require('./html-minify'); var text = fs.readFileSync("/Any/Random/HTML.html", encoding='utf8'); var min = htmlminify(text); puts(min); |