mishoo / UglifyJS

JavaScript parser / mangler / compressor / beautifier toolkit
http://lisperator.net/uglifyjs/
Other
13.11k stars 1.25k forks source link

fromString mangle & compress options not working #289

Closed ghost closed 11 years ago

ghost commented 11 years ago

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:

mishoo commented 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.

ghost commented 11 years ago

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)};

mishoo commented 11 years ago

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.

ghost commented 11 years ago

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)};

mishoo commented 11 years ago

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.

ghost commented 11 years ago

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;