How to Minify CSS and JavaScript for Production
How much minification actually saves, measured on lodash, jQuery, Bootstrap and normalize.css: compression does most of the work for CSS, while JavaScript needs a real minifier (terser halved what whitespace-stripping left of lodash).
What Minification Does
A minifier rewrites code to be smaller without changing what it does. The safe part is removing whitespace and comments and shortening values (0px to 0, #ffffff to #fff). JavaScript minifiers such as terser go further: they rename local variables, drop unreachable code and fold constants, which needs a full parser of the language.
Measured on Four Real Files
We minified four widely used files from npm with our Code Minifier (whitespace and comments only, token-aware so strings and regular expressions are left alone) and with the standard tools, then compressed each result as a server would (October 14, 2026; script in our test folder):
| File | Raw | Our minifier | terser / clean-css | Raw + Brotli | Minified + Brotli (best) |
|---|---|---|---|---|---|
| lodash 4.17.21 (JS) | 544 KB | 149 KB | 71 KB (terser) | 74 KB | 22 KB |
| jQuery 3.7.1 (JS) | 285 KB | 141 KB | 87 KB (terser) | 70 KB | 28 KB |
| Bootstrap 5.3.3 (CSS) | 281 KB | 232 KB | 233 KB (clean-css) | 25 KB | 23 KB |
| normalize.css 8.0.1 | 6.1 KB | 1.8 KB | 1.8 KB (clean-css) | 1.4 KB | 0.6 KB |
Three lessons. First, compression does most of the work for CSS: Bootstrap shrank 17% by minifying but 91% by Brotli alone, and minifying on top of Brotli saved another 8%. Second, JavaScript gains far more from a real minifier: terser's renaming and compression cut lodash to half the size our whitespace-only pass reached, and after Brotli the difference was 22 KB against 31 KB. Third, comments matter: lodash is mostly documentation comments, which is why simply stripping them removed 73% of it.
Correctness check: the lodash our tool produced still ran in Node (_.chunk and _.template gave the right results) and the minified jQuery still parsed.
In Practice
- Let the build do it: Vite and esbuild minify production builds by default; webpack uses terser for JavaScript; cssnano or Lightning CSS handle CSS.
- Make sure the server or CDN sends Brotli or gzip; check the
content-encodingresponse header in DevTools. Without it, minification alone leaves most of the saving on the table. - Ship source maps to staging, and to production too if you are comfortable exposing the original code; they let DevTools show readable sources.
- Use the Code Minifier for one-off snippets, an inline script or a stylesheet without a build step; for full JavaScript bundles, use terser.