Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RemoveComments in html-minifier by command line

Tags:

html

npm

minify

I want to use html-minifier to minify my html files.

I have install it by npm install -g html-minifier

However, sudo html-minifier rb.html --removeComments did NOT remove the comments in rb.html.

Does anyone know how to use it?

Additionally, I just want to minify the size of the html files by keeping exactly the same layout, what are conventional parameters that we put to html-minifier?

like image 390
Tie Avatar asked Jun 08 '17 14:06

Tie


People also ask

How do you minify an HTML file?

To minify HTML, try HTMLMinifier. To minify CSS, try CSSNano and csso. To minify JavaScript, try UglifyJS. The Closure Compiler is also very effective.

Should you minify your HTML?

If you want faster page load times and better performance scores, you will want to minify you HTML, CSS and Javascript files. With all the online tools available, you can easily minify your code for any website.

What is HTML Minification?

Minification is the process of minimizing code and markup in your web pages and script files. It's one of the main methods used to reduce load times and bandwidth usage on websites. Minification dramatically improves site speed and accessibility, directly translating into a better user experience.


1 Answers

You can target all html files in a specified directory with the following command:

html-minifier --input-dir dist --output-dir dist

With this example script I am compressing all html files in dist and outputting them to the same directory — essentially replacing the uncompressed html files with the compressed ones.

The command above actually doesn't do anything to the files because no options were defined. Some useful options are:

  • --collapse-whitespace: Collapse whitespace that contributes to text nodes in a document tree.
  • --remove-comments: Remove HTML comments
  • --minify-css: Minifies embedded and inline CSS (<style></style> and style="")
  • --minify-js: Minifies embedded and inline JS (<script></script> and e.g onload="")

Here's a command that you may end up with:

html-minifier --input-dir dist --output-dir dist --remove-comments --collapse-whitespace --minify-js --minify-css

To see the complete list of available options, run html-minifier -h.

like image 172
Adam Avatar answered Sep 29 '22 03:09

Adam