After cloning uglifyjs2 source from Git, go to directory:
NPM Link
If you compile and install UGLIFYJS2 successfully, you can call the UGLIFYJS command directly. However, there was a problem with global obfuscation, although the cache file Topvar.json as a global variable was specified, or it appears:
1) The confusion name conflicts with the local variables after the confusion of the global variables;
2) Confusion name conflicts of different global variables.
Check the documentation and do some testing, but no success. Finally, flip through the code and make the following changes:
1) Modify the SymbolDef.prototype.mangle function in the Scope.js file:
1 // 2 if (this . Scope instanceof Ast_toplevel) { 3 console.log (this . Name +" = + "+ this .mangled_name); 4 this . Mangled_name = "__" + this . Mangled_name + "__" ; 5 }
For the confusion of global variables, add double underscore prefixes to avoid collisions. After this modification, you need to filter out the global symbols that have been used when calculating next_mangled_name.
2) Add the following processing in the function Next_mangled_name function:
1 while(true) {2Name = base54 (+ +)scope.cname);3 //Double underline filtering4 if(in_use[' __ ' + name + ' __ ') | |!is_identifier (NAME) | | Member (name, options.reserved))Continue;5 if(!names[name]) Break;6 Holes.push (scope.cname);7}
After this, the problem of global variable confusion is completely solved.
UGLIFYJS2 Global Obfuscation