Minify JavaScript with Terser: names shortened, dead code and comments removed, /*! notices kept, errors by line
Make a script smaller to ship: Terser shortens local names, drops comments and code that can’t run, and keeps license notices.
How to use it
Paste JavaScript, or open a .js or .mjs file. The status bar shows the characters left and the percent saved. Copy it or download minified.js. Code with a syntax error is named with its line and column instead. Under Options: mangling, top-level names, kept names, reserved names, the output ECMAScript version, passes, console calls, comments, quotes, ASCII-only output and an inline source map.
Examples
Local names shortened, the function name kept
function greet(person) {
const message = "Hello, " + person.name;
return message;
}
console.log(greet({ name: "Ada" }));function greet(e){return"Hello, "+e.name}console.log(greet({name:"Ada"}));greet stays, since other scripts on the page may call it; person and message are only used inside.
A module with its license notice
/*! charts v4 | MIT */
// draw the chart
export function draw(canvas) {
return canvas.getContext("2d");
}/*! charts v4 | MIT */
export function draw(t){return t.getContext("2d")}A call left open
if (debug) {
console.log("x"
}Line 3, column 1Unexpected “}”, expected “,”
What Terser changes
Going further, under Options
Big files
Questions
- Can I minify JSX or TypeScript?
No: Terser reads plain JavaScript, so JSX and type annotations stop with an error at their line. Build them to JavaScript first. - Does it make a source map?
Yes, under Options: Source map adds it at the end of the minified file as a data: URL, so dev tools show your original lines. For a separate .map file, run Terser in your build. - Is my code uploaded?
No. Everything runs in your browser, so your code stays on your device. Nothing you paste is uploaded unless you share a result, which is locked with your password first. Scripts can hold API keys, and the page warns you if it spots one.