Code Format and Minify: Lexer-Aware HTML, CSS and JS

Open Tool Code Formatter and Minifier

Why spaces cannot simply be deleted

Most JavaScript whitespace is semantically inert, but spaces inside string and regex literals must survive; CSS colour values, content strings and URLs may contain spaces; HTML text nodes collapse repeated spaces except inside preformatted elements. Proper formatting therefore tokenises first, identifying strings, comments and template literals, then adjusts whitespace only where it is safe.

Steps

  1. Choose auto-detect, JavaScript, CSS or HTML — mixed bundles are better handled separately.
  2. Pick beautify or minify into one line.
  3. Toggle keeping or removing comments, plus blank-line merging and trailing-space trimming.
  4. Set blank lines between CSS rules and enable line numbers.
  5. Process, copy, download, or clear.

Real risks in minification

Automatic semicolon insertion can change meaning when two lines merge — a returned object becomes undefined plus a block. Newlines inside template literals are significant and vanish under compression. Comments may hold licence notices whose removal is a legal breach. Inline HTML elements lose spacing when gaps disappear. And calc requires literal spaces around operators, so stripping them is a syntax error.

Readable beautification

Readable is not the same as airy. Two-space indents suffice for most projects; four feel roomier until deep nesting runs off-screen. One declaration per line beats a single long line for diffs. Long call chains keep their breaks. Beautifying shifts line numbers, so debug production using source maps rather than reformatting the minified artifact. And if reformatting changes behaviour, the original depended on line breaks.

Common problems

Regex-based formatting collapses on nesting and escaped quotes. Compressing without regression tests misses tool bugs around templates and regex literals. Misidentifying the language yields nonsense output. Mixing CRLF and LF makes version control show whole-file rewrites. Re-minifying already minified code can break semicolons and brackets. And shipping minified code without source maps leaves production undebuggable.

When not to format:When the team already has a formatter, use the project's own tooling and hooks; a web tool diverges from house style and turns diffs into noise. Files with syntax errors may produce garbage, so fix syntax first. Binary or huge files will hang the browser. And internal code or secrets never belong in an online tool.

⏫︎