Closed ghost closed 11 years ago
mangle: "toplevel"
is not supported, not sure where you saw that.
Otherwise, both compress and mangle work as expected to me. In your sample compression has no effect anyway, pass some more complicated code to see the difference between compress: true
and compress: false
.
Hi mishoo,
The string is still mangled even with the option mangle:false. (see below) Is it possible to mangle the toplevel as well using .minify?
var uglify = require('uglify-js2'); strJS="var notMangled = function () {\n\tvar mangledVar='foo';\n\tconsole.log('blah' + mangledVar);\n};\n"; return uglify.minify(strJS, {fromString: true,mangle:false}).code + '\n';
Output: var notMangled=function(){var o="foo";console.log("blah"+o)};
Maybe you're using older code? It works for me (that is, it's not mangled if I pass false
).
Nope, currently there's no option to mangle toplevel variables.
Hi mishoo,
Thanks, I'm using uglify-js2@2.1.11
the github page https://github.com/mishoo/UglifyJS2#mangler-options says: "toplevel — mangle names declared in the toplevel scope (disabled by default)."
I tried to achieve toplevel mangling using this code
var uglify = require('uglify-js2'); strJS="var notMangled = function () {\n\tvar mangledVar='foo';\n\tconsole.log('blah' + mangledVar);\n};\n"; var toplevel = uglify.parse(strJS,{ filename: 'foo', toplevel: toplevel, mangle:toplevel }); toplevel.figure_out_scope(); var compressor = uglify.Compressor({}); var compressed_ast = toplevel.transform(compressor); compressed_ast.figure_out_scope(); compressed_ast.compute_char_frequency(); compressed_ast.mangle_names(); console.log(compressed_ast.print_to_string());
it outputs this: var notMangled=function(){var o="foo";console.log("blah"+o)};
Ah, you are right... I completely forgot that we have the option. :-)
Try like this:
mangle: { toplevel: true }
Also, please try with a newer version, 2.1 is rather old.
Okay I see I had the latest uglify-js2 package but it looks like you've renamed the uglify-js2 project back to uglify-js? https://github.com/mishoo/UglifyJS2/issues/290
Very nice its working now!!!! THANK YOU!!!! final code: return uglify.minify(strJS, {fromString: true,mangle: {toplevel:true}}).code;
TEST CODE var uglify = require('uglify-js2'); strJS="var notMangled = function () {\n\tvar mangledVar='foo';\n\tconsole.log('blah' + mangledVar);\n};\n"; console.log(uglify.minify(strJS, {fromString: true,compress:false,mangle:'toplevel'}).code);
Issues: