Search code examples
javascriptminifyuglifyjs

How should I use mangle option in uglifyjs?


When running the following command:

echo "let demo = 3; console.log(demo);" | uglifyjs --compress --mangle

I would expect the following output:

let a=3;console.log(a);

Instead, I get:

let demo=3;console.log(demo);

Therefore, I don't understand how I should use --mangle option. The official documentation explains how to exclude the names which shouldn't be mangled, how to mangle properties, etc., but how do I just transform the names of ordinary variables?

Or is this option doing something completely different, and I misunderstood its purpose?

Note: I'm using uglify-es 3.2.2.


Solution

  • So I looked at The official documentation, and as you said --mangle is the option where you choose what not to mangle (how convenient), and figured out that you should use --toplevel which mangle everything in the top level scope.

    Eventually it should look like that:

    uglifyjs --compress --mangle --toplevel