Simple HTML Minifier for ESP

A simple HTML minifier for ESP/microcontrollers

GitHub/LuckyMallari

For embedded systems where memory is crucial, you’d want to save as much as possible. Even unnecessary spaces should be removed because every bit counts! What this code does is take your HTML code and minimizes it using regular expression. It DOES not minify the code like others do, it will simply remove unnecessary characters.

I use this primarily for ESP when a simple html code will be served using a lightweight webserver.

Minifying saved me 31% !

The magic happens here:

/* Remove comments (both html and js comments) */
.replace(/((<!--[\s\S]*?(?:-->)?<!---+>?|<!(?![dD][oO][cC][tT][yY][pP][eE]|\[CDATA\[)[^>]*>?|<[?][^>]*>?)|(\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$))/g, "")
.replace(/"/g, '\"') /* Escape quote */
.replace(/\s\s+/g, " ") /* Replace double spaces with a single space */
.replace("\r", "") /* Remove carriage returns */
.replace("\n", "") /* Remove new lines */
.replace(/\s*\(\s*/g, "(") /* Remove space before and after ( */
.replace(/\s*\)\s*/g, ")") /* Remove space before and after ) */
.replace(/\s*{\s*/g, "{") /* Remove space before and after { */
.replace(/\s*}\s*/g, "}") /* Remove space before and after { */
.replace(/\s*=\s*/g, "=") /* Remove space before and after = */
.replace(/\s*:\s*/g, ":") /* Remove space before and after : */
.replace(/\s*;\s*/g, ";") /* Remove space before and after ; */
.replace(/\s*,\s*/g, ",") /* Remove space before and after , */
.replace(/\s*>\s*/g, ">") /* Remove space before and after > */
.replace(/\s*<\s*/g, "<") /* Remove space before and after < */