Node.js Project to Build HTML CSS and JS Source Code Minifier and Compressor Using html-minify Library Full Project For Beginners

Node.js Project to Build HTML CSS and JS Source Code Minifier and Compressor Using html-minify Library Full Project For Beginners

 

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

See also  Build a Responsive Gallery of Images with Lightbox Popup in Browser Using HTML5 CSS3 and Javascript Full Project For Beginners

 

 

 

 

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

 

 

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);

 

 

Leave a Reply