Spunk Autos

Websites and web tools

The Biggest Code Minification Mistakes

Minification strips whitespace, comments and long names from JavaScript, CSS and HTML so files download faster. It is a routine build step, yet it causes a surprising number of broken pages and confusing bug reports. These are the mistakes that matter most, with a way to estimate whether the saving is worth chasing on your own site.

Estimate the saving first

Minification only helps as much as the bytes it removes, multiplied by how often the file is downloaded.

Mistake 1: no source maps

Minified code has every function on one line with short names, so an error report pointing at line 1, column 48,213 is useless. Generate source maps during the build so browser devtools and error trackers can show the original file and line. Decide whether to publish the maps or upload them privately to your error tool.

Mistake 2: treating minification as compression

Minifying and compressing are different steps. The server or CDN should also send text files with gzip or Brotli compression. The two work together: minify to remove redundancy in the source, compress to shrink what remains for transfer. Check the Content-Encoding response header to confirm compression is on.

Mistake 3: minifying already minified files

Running a minifier over third-party files that are already minified wastes build time and occasionally breaks them. Use the vendor's published minified build directly and keep it out of the minify step.

Mistake 4: aggressive settings without tests

Advanced options can rename object properties, drop code the tool thinks is unreachable or reorder statements. If your code relies on property names, for example to read JSON keys or use reflection, these options break it. Turn them on only with a test suite that runs against the minified output.

Mistake 5: shipping code nobody uses

Minifying 300 KB of unused library code still ships the unused code. Removing it saves far more than squeezing whitespace. Use your bundler's tree shaking, import only the functions you need, and check the coverage panel in browser devtools to see how much of each file actually runs on a page.

Mistake 6: breaking CSS and HTML

  • Removing whitespace inside HTML pre blocks or between inline elements can change how text looks.
  • Merging CSS rules can change order and therefore which rule wins.
  • Stripping HTML comments can remove conditional markup some tools rely on.
  • Removing quotes from attribute values is valid in many cases but can break values with spaces.

Mistake 7: editing the minified file

A quick fix made directly in app.min.js is lost on the next build. Always change the source and rebuild.

A safe setup

  1. Minify in the build, never by hand.
  2. Generate source maps for every bundle.
  3. Run the test suite against the built output.
  4. Enable gzip or Brotli on the server or CDN.
  5. Put a content hash in output filenames for long caching.
  6. Review bundle size on each release so growth is noticed early.