Chuck
Chuck

Reputation: 1072

How to mangle toplevel variable names using Uglify2 cli

For reasons which escape me, uglifyjs does not appear to be mangling top-level names. What am I doing wrong?

A simple testugly.js looks like this:

var testThing={};
testThing.something = 1;
testThing.myfunction = function(alpha,beta,c) {
    var dino = 5;
    if (alpha > 2) {
        dino = 6
    }
    return dino + beta * c 
}

Simple enough. If I run it through uglify without trying to mangle top-level variables, things go as expected:

$ uglifyjs --version
uglify-js 2.4.0

$ uglifyjs testugly.js --mangle -c
var testThing={},testThing.something=1,testThing.myfunction=function(t,n,i){var e=5;return t>2&&(e=6),e+n*i};

Now, I'd like to mangle the top-level variables as well,so I add toplevel=true.

$ uglifyjs testugly.js --mangle toplevel=true -c
var testThing={},testThing.something=1,testThing.myfunction=function(t,n,i){var e=5;return t>2&&(e=6),e+n*i};

Or maybe I got that wrong, let's try the old -mt too.

$ uglifyjs testugly.js -mt -c
var testThing={},testThing.something=1,testThing.myfunction=function(t,n,i){var e=5;return t>2&&(e=6),e+n*i};

What gives? Shouldn't testThing be 'a' or something?

Upvotes: 1

Views: 1380

Answers (1)

mindrones
mindrones

Reputation: 1233

Try enclosing your code in a function (-e):

$ uglifyjs testugly.js -e -mt -c
!function(){var n={};n.something=1,n.myfunction=function(n,t,i){var o=5;return n>2&&(o=6),o+t*i}}();

(version 2.4.3 here)

Upvotes: 1

Related Questions