Module: lib/bundler/minify

Removes what a browser does not need, and nothing else.

What this does, stated precisely

Comments are deleted. Leading and trailing whitespace is trimmed from every line whose margins are code rather than the inside of a string or a template literal. That is the whole transformation.

It does not rename identifiers, fold constants, remove dead branches or join statements. Those need a real ECMAScript parser to do safely, and a minifier that guesses produces a bundle that is smaller and wrong — which is strictly worse than one that is larger and right. The size this leaves on the table is reported honestly rather than closed by guessing.

One property worth the constraint

Line count is preserved exactly. A deleted block comment leaves its newlines behind; trimming touches only the margins. So the source map emitted for a development build is equally valid for the minified production build, and a production stack trace still names a file and a line the developer wrote. Most minifiers buy their last few per cent by giving that up.

Comments and indentation are also what gzip compresses best, so the gap between this and an identifier-mangling minifier is much narrower on the wire than it is on disk. The build reports both numbers.

Source:

Methods

(static) minify(code) → {string}

Strips comments and indentation from emitted JavaScript.

Parameters:
Name Type Description
code string

The bundle source.

Source:
Returns:

The same program, with comments and margins removed.

Type
string