Minifying HTML

4 January 2018

When you absolutely, positively got to kill every whitespace in the file.

Javascript

Simple method, using a javascript implementation.

apt install npm #or however your OS installs npm
npm install html-minifier -g
html-minifier --input-dir existing_html_directory --output-dir output

But it’s relatively slow and it didn’t minify standalone css files.

Golang

A Go equivalent is minify

Assuming that you already have go configured, and its binary directory is on your $PATH:

go get github.com/tdewolff/minify/cmd/minify
minify -r -o output_dir/ existing_html_directory

It’ll minify other web files too, and no waiting around - according to time, minify was 30x faster than the Javascript version (for my files, on my computer etc). Since neither option copies other resources such as images into the output directory, better to do the replacement in-place:

minify -r -o public public
#And serve it locally to test
(cd public && python3 -m http.server 8000)

go
js
html