From: Mihai Bazon Date: Thu, 13 Sep 2012 16:45:16 +0000 (+0300) Subject: added -m and -c options X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=93b973c99dd25aeff66ff31f2881e9ce252eaac6;p=UglifyJS.git added -m and -c options --- diff --git a/bin/uglifyjs2 b/bin/uglifyjs2 index 60bbc0fb..fd57a662 100755 --- a/bin/uglifyjs2 +++ b/bin/uglifyjs2 @@ -17,28 +17,44 @@ Use a single dash to read input from the standard input.\ .describe("stats", "Display operations run time on STDERR") .describe("v", "Verbose") .describe("b", "Beautify output") + .describe("m", "Don't mangle names") + .describe("c", "Compressor options") .alias("p", "prefix") .alias("o", "output") .alias("v", "verbose") .alias("b", "beautify") + .alias("c", "options") .boolean("b") .boolean("v") .boolean("stats") + .boolean("m") .argv ; -for (var i in ARGS) if (ARGS.hasOwnProperty(i) && /-/.test(i)) { - ARGS[i.replace(/-/g, "_")] = ARGS[i]; +function normalize(o) { + for (var i in o) if (o.hasOwnProperty(i) && /-/.test(i)) { + o[i.replace(/-/g, "_")] = o[i]; + } } +normalize(ARGS); + if (ARGS.h || ARGS.help) { sys.puts(optimist.help()); process.exit(0); } +var COMPRESSOR_OPTIONS = {}; +if (ARGS.c) { + ARGS.c.replace(/^\s+|\s+$/g).split(/\s*,+\s*/).forEach(function(opt){ + var a = opt.split(/\s*=\s*/); + COMPRESSOR_OPTIONS[a[0]] = new Function("return(" + a[1] + ")")(); + }); +} + var files = ARGS._.slice(); if (files.length == 0) @@ -117,7 +133,7 @@ function do_file_1(file) { ast.figure_out_scope(); }); time_it("squeeze", function(){ - var compressor = UglifyJS.Compressor({}); + var compressor = UglifyJS.Compressor(COMPRESSOR_OPTIONS); ast = ast.squeeze(compressor); }); ast.filename = file; @@ -127,7 +143,9 @@ function do_file_1(file) { function do_file_2(ast) { time_it("scope", function(){ ast.figure_out_scope(); - ast.compute_char_frequency(); + if (!ARGS.m) { + ast.compute_char_frequency(); + } }); return ast; } @@ -137,7 +155,7 @@ function do_file_3(ast) { // if (ARGS.v) { // sys.error("Mangling/generating " + file); // } - time_it("mangle", function(){ + if (!ARGS.m) time_it("mangle", function(){ ast.mangle_names(); }); time_it("generate", function(){ diff --git a/tmp/todo b/tmp/todo new file mode 100644 index 00000000..50bec924 --- /dev/null +++ b/tmp/todo @@ -0,0 +1,75 @@ +a = a + x ==> a+=x + +******* + +join consecutive var statements + +******* + +x == false ==> x == 0 +x == true ==> x == 1 + +should warn too; +JS is so sloppy that this could be an indication of a bug. + +******* + +x, x ==> x +x = foo, x ==> x + +other similar cases? + +******* + +Try to concatenate all scripts somehow before starting minification; +the issue will be keeping track of the current source file for +generating source maps. perhaps store that in the AST? Have a single +AST_Toplevel for all files. + +XXX? Not sure if this is worth the trouble. + +******* + +discard spurious break statements + +******* + +for (...) { + if (foo) continue; + ... +} + +==> + +for (...) { + if (!foo) { ... } +} + +******* + +The following seems to compress suboptimally. Should probably run more +passes somewhere. + +function setOpacity(el, o) { + if (o != null) { + if (o == "" && o != 0) { + is_ie + ? el.style.filter = "" + : el.style.opacity = ""; + } else { + is_ie + ? el.style.filter = "alpha(opacity=" + Math.round(o * 100) + ")" + : el.style.opacity = o; + } + return o; + } else { + if (!is_ie) + return parseFloat(el.style.opacity); + else + if (/alpha\(opacity=([0-9.])+\)/.test(el.style.opacity)) + return parseFloat(RegExp.$1); + } +} + +******* +