Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm scripts: need to minify all HTML files in folder (and subfolders)

I want to minify all .html files in a folder (and any folders within) using npm run script. Ideally, all .html files should be overwritten (if that's not possible, a new folder is acceptable). It is assumed that there will be non-HTML files in the input folder.

npm library minimize works only on per-file but not on folders.

Another npm library html-minifier does accept folder as input, but fails if there are any non-HTML files present in the input folder:

html-minifier --input-dir ./test1 --output-dir ./test2 --html-5 --collapse-whitespace

I need this to minify my static website's HTML files.

like image 525
revelt Avatar asked May 21 '16 13:05

revelt


1 Answers

Since posting original question here on SO, html-minifier added the feature to ignore the non-HTML files. Now you can set directories and html-minifier won't error-out when it encounters non-HTML files.

Usage example, taken from my working npm task:

html-minifier --input-dir ./public --output-dir ./public --collapse-whitespace --file-ext html

Let's minify all our static websites' HTML files now!

like image 146
revelt Avatar answered Sep 21 '22 23:09

revelt