added -m and -c options
authorMihai Bazon <mihai@bazon.net>
Thu, 13 Sep 2012 16:45:16 +0000 (19:45 +0300)
committerMihai Bazon <mihai@bazon.net>
Thu, 13 Sep 2012 16:45:16 +0000 (19:45 +0300)
bin/uglifyjs2
tmp/todo [new file with mode: 0644]

index 60bbc0f..fd57a66 100755 (executable)
@@ -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 (file)
index 0000000..50bec92
--- /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);
+    }
+}
+
+*******
+