Minification
Minification (Code Size Reduction)
The process of removing all unnecessary characters from source code (whitespace, comments, long variable names) without changing its functionality, reducing file size for faster network transfer.
ๆ่ก็่ฉณ็ดฐ
JavaScript minifiers (Terser, esbuild, SWC) perform tokenization, AST analysis, dead code elimination (tree shaking), variable name mangling (renaming to single characters), constant folding, and statement combining. CSS minifiers (cssnano, Lightning CSS) merge duplicate rules, shorten color values (#ffffff to #fff), and remove redundant properties. HTML minifiers collapse whitespace and remove optional closing tags. Source maps (.map files) enable debugging by mapping minified code positions back to original source locations.
ไพ
```javascript
// Simple CSS minifier
function minifyCSS(css) {
return css
.replace(/\/\*[\s\S]*?\*\//g, '') // remove comments
.replace(/\s+/g, ' ') // collapse whitespace
.replace(/\s*([{};:,])\s*/g, '$1') // remove around symbols
.trim();
}
// 1024 bytes โ 612 bytes (40% reduction)
```