简体   繁体   English

如何使用Uglify2 CLI处理顶级变量名称

[英]How to mangle toplevel variable names using Uglify2 cli

For reasons which escape me, uglifyjs does not appear to be mangling top-level names . 出于某种原因,uglifyjs似乎没有在破坏顶级名称 What am I doing wrong? 我究竟做错了什么?

A simple testugly.js looks like this: 一个简单的testugly.js看起来像这样:

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: 如果我通过uglify运行它而不尝试破坏顶级变量,那么事情将会按预期进行:

$ 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 . 现在,我也想处理顶级变量,所以我添加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. 也许我弄错了,让我们也尝试旧的-mt

$ 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? testThing是'a'还是什么?

Try enclosing your code in a function (-e): 尝试将您的代码包含在函数(-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) (此处为2.4.3版)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM