A JavaScript minifier built on Terser
This JavaScript minifier uses Terser, the minifier inside webpack, Rollup, Vite and many other build tools. It parses your code into a syntax tree, applies optimizations, and prints the smallest equivalent code it can. The result downloads faster and parses sooner in the browser. Smaller scripts matter most on mobile networks, and every kilobyte you save is also saved for each visitor on every uncached visit.
Example
/*! MyLib v1.2 | MIT */
// helper
function calculateTotal(items, taxRate) {
const subtotal = items.reduce((sum, item) => sum + item.price * item.qty, 0);
if (taxRate === undefined) {
return subtotal;
}
return subtotal * (1 + taxRate);
}
export { calculateTotal };
function e(e,r){const t=e.reduce((e,r)=>e+r.price*r.qty,0);return void 0===r?t:t*(1+r)}export{e as calculateTotal};
The status bar reports Minified: 275 → 115 bytes (58% smaller). Terser removed the comments and whitespace, renamed parameters and locals to single letters, turned the if statement into a conditional expression, replaced undefined with the shorter void 0, and kept the exported name calculateTotal intact through export { e as calculateTotal } so importers still work.
Options explained
- Shorten variable names (mangle) renames local variables, parameters and functions declared inside other functions. Global names are left unchanged, because other scripts may use them. This option usually gives the biggest savings.
- Compress code runs Terser's optimizer twice: it removes unreachable code, inlines constants, joins consecutive statements, simplifies conditionals and drops unused local functions and variables.
- Keep license comments preserves comments that must stay for legal reasons: those starting with
/*!or containing@licenseor@preserve. With it on, the example above keeps its/*! MyLib v1.2 | MIT */banner.
With only Compress enabled, names stay readable (function calculateTotal(items,taxRate){...}), which is handy when you want a smaller file that you can still debug.
ES modules and modern syntax
Files containing import or export are detected automatically and minified as ES modules, which lets Terser treat top-level declarations as private to the module. Modern syntax such as classes, arrow functions, async/await, optional chaining and template literals is kept as is; Terser does not transpile to older JavaScript, so the output runs wherever the input ran.
Minification tips
- Keep the original. Minified code is for delivery, not editing. Store the readable source in version control.
- Combine with compression. Servers usually also compress files with gzip or Brotli. Minifying first still helps, because it removes content that compression cannot drop entirely, like comments and long names.
- Use source maps in production builds so stack traces point to your original code. Build tools like Vite and webpack generate them alongside the minified bundle.
- Watch for code that depends on names. Libraries that read
function.nameor useevalcan behave differently after mangling. Turn off Shorten variable names if something breaks.
Errors
Terser needs valid JavaScript. A syntax error such as const a = {b: 1,,}; is reported with its position, for example Unexpected token: punc (,) (line 1, column 17). TypeScript and JSX must be compiled to JavaScript first. You can check syntax with the JavaScript Validator.
To read a minified file again, use the JavaScript Formatter. If you want the code to be hard to read rather than just small, see the JavaScript Obfuscator. Everything runs in your browser; your code is not uploaded.