terser -c -m: Minify JavaScript
terser -c -m minifies JavaScript by compressing (-c) and mangling names (-m), the standard production minifier.
Bundlers embed Terser, but you can also run it directly to minify a build artifact. -c and -m are the two knobs that shrink code the most.
What it does
terser parses JavaScript and rewrites it smaller: -c (compress) drops dead code and simplifies expressions, -m (mangle) shortens local variable names. --source-map keeps a map back to the original.
Common usage
terser dist/bundle.js -c -m -o dist/bundle.min.js
terser dist/bundle.js -c -m --source-map "url=bundle.min.js.map" \
-o dist/bundle.min.js
# target modern syntax
terser dist/bundle.js -c -m --ecma 2020 -o dist/bundle.min.jsOptions
| Flag | What it does |
|---|---|
| -c, --compress | Enable compression (optional sub-options) |
| -m, --mangle | Shorten variable/function names |
| -o, --output <f> | Output file |
| --source-map <opts> | Emit a source map |
| --ecma <n> | Assume this ECMAScript version when minifying |
| --module | Treat input as an ES module (enables more mangling) |
In CI
If your bundle uses modern syntax, set --ecma and --module so Terser does not choke on or under-optimize it. When minifying an ES module bundle, --module unlocks safe top-level mangling.
Common errors in CI
"SyntaxError: Unexpected token" means Terser hit syntax newer than it understands; raise --ecma, add --module, or upgrade Terser. Broken behavior only in the minified build often comes from over-aggressive compress on code relying on function names; disable the offending compress option (for example -c keep_fnames=true).