diff options
-rw-r--r-- | README.md | 11 | ||||
-rwxr-xr-x | bin/uglifyjs | 10 | ||||
-rw-r--r-- | lib/compress.js | 3 | ||||
-rw-r--r-- | lib/sourcemap.js | 15 | ||||
-rw-r--r-- | test/compress/collapse_vars.js | 50 | ||||
-rw-r--r-- | tools/exports.js | 1 | ||||
-rw-r--r-- | tools/node.js | 6 |
7 files changed, 79 insertions, 17 deletions
@@ -125,7 +125,9 @@ The available options are: --noerr Don't throw an error for unknown options in -c, -b or -m. --bare-returns Allow return outside of functions. Useful when - minifying CommonJS modules. + minifying CommonJS modules and Userscripts that + may be anonymous function wrapped (IIFE) by the + .user.js engine `caller`. --keep-fnames Do not mangle/drop function names. Useful for code relying on Function.prototype.name. --reserved-file File containing reserved names @@ -639,6 +641,9 @@ Other options: - `compress` (default `{}`) — pass `false` to skip compressing entirely. Pass an object to specify custom [compressor options][compressor]. +- `parse` (default {}) — pass an object if you wish to specify some + additional [parser options][parser]. (not all options available... see below) + ##### mangleProperties options - `regex` — Pass a RegExp to only mangle certain names (maps to the `--mange-regex` CLI arguments option) @@ -661,6 +666,9 @@ properties are available: - `strict` — disable automatic semicolon insertion and support for trailing comma in arrays and objects +- `bare_returns` — Allow return outside of functions. (maps to the + `--bare-returns` CLI arguments option and available to `minify` `parse` + other options object) - `filename` — the name of the file where this code is coming from - `toplevel` — a `toplevel` node (as returned by a previous invocation of `parse`) @@ -800,3 +808,4 @@ The `source_map_options` (optional) can contain the following properties: [sm-spec]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit [codegen]: http://lisperator.net/uglifyjs/codegen [compressor]: http://lisperator.net/uglifyjs/compress + [parser]: http://lisperator.net/uglifyjs/parser diff --git a/bin/uglifyjs b/bin/uglifyjs index f7f22215..1f449aa9 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -499,17 +499,19 @@ function normalize(o) { } } -function getOptions(x, constants) { - x = ARGS[x]; +function getOptions(flag, constants) { + var x = ARGS[flag]; if (x == null) return null; var ret = {}; if (x !== "") { + if (Array.isArray(x)) x = x.map(function (v) { return "(" + v + ")"; }).join(", "); + var ast; try { ast = UglifyJS.parse(x, { expression: true }); } catch(ex) { if (ex instanceof UglifyJS.JS_Parse_Error) { - print_error("Error parsing arguments in: " + x); + print_error("Error parsing arguments for flag `" + flag + "': " + x); process.exit(1); } } @@ -529,7 +531,7 @@ function getOptions(x, constants) { return true; // no descend } print_error(node.TYPE) - print_error("Error parsing arguments in: " + x); + print_error("Error parsing arguments for flag `" + flag + "': " + x); process.exit(1); })); } diff --git a/lib/compress.js b/lib/compress.js index 6cd5571e..6fdf8f2d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -320,7 +320,10 @@ merge(Compressor.prototype, { if (node instanceof AST_Lambda || node instanceof AST_Try || node instanceof AST_With + || node instanceof AST_Case || node instanceof AST_IterationStatement + || (parent instanceof AST_If && node !== parent.condition) + || (parent instanceof AST_Conditional && node !== parent.condition) || (parent instanceof AST_Binary && (parent.operator == "&&" || parent.operator == "||") && node === parent.right) diff --git a/lib/sourcemap.js b/lib/sourcemap.js index a67011f0..e5d7df60 100644 --- a/lib/sourcemap.js +++ b/lib/sourcemap.js @@ -53,16 +53,11 @@ function SourceMap(options) { orig_line_diff : 0, dest_line_diff : 0, }); + var generator = new MOZ_SourceMap.SourceMapGenerator({ + file : options.file, + sourceRoot : options.root + }); var orig_map = options.orig && new MOZ_SourceMap.SourceMapConsumer(options.orig); - var generator; - if (orig_map) { - generator = MOZ_SourceMap.SourceMapGenerator.fromSourceMap(orig_map); - } else { - generator = new MOZ_SourceMap.SourceMapGenerator({ - file : options.file, - sourceRoot : options.root - }); - } function add(source, gen_line, gen_col, orig_line, orig_col, name) { if (orig_map) { var info = orig_map.originalPositionFor({ @@ -83,7 +78,7 @@ function SourceMap(options) { source : source, name : name }); - } + }; return { add : add, get : function() { return generator }, diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 39fee597..934a5c73 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -1103,3 +1103,53 @@ collapse_vars_short_circuit: { } } +collapse_vars_short_circuited_conditions: { + options = { + collapse_vars: true, + sequences: false, + dead_code: true, + conditionals: false, + comparisons: false, + evaluate: true, + booleans: true, + loops: true, + unused: true, + hoist_funs: true, + keep_fargs: true, + if_return: false, + join_vars: true, + cascade: true, + side_effects: true, + } + input: { + function c1(x) { var a = foo(), b = bar(), c = baz(); return a ? b : c; } + function c2(x) { var a = foo(), b = bar(), c = baz(); return a ? c : b; } + function c3(x) { var a = foo(), b = bar(), c = baz(); return b ? a : c; } + function c4(x) { var a = foo(), b = bar(), c = baz(); return b ? c : a; } + function c5(x) { var a = foo(), b = bar(), c = baz(); return c ? a : b; } + function c6(x) { var a = foo(), b = bar(), c = baz(); return c ? b : a; } + + function i1(x) { var a = foo(), b = bar(), c = baz(); if (a) return b; else return c; } + function i2(x) { var a = foo(), b = bar(), c = baz(); if (a) return c; else return b; } + function i3(x) { var a = foo(), b = bar(), c = baz(); if (b) return a; else return c; } + function i4(x) { var a = foo(), b = bar(), c = baz(); if (b) return c; else return a; } + function i5(x) { var a = foo(), b = bar(), c = baz(); if (c) return a; else return b; } + function i6(x) { var a = foo(), b = bar(), c = baz(); if (c) return b; else return a; } + } + expect: { + function c1(x) { var a = foo(), b = bar(), c = baz(); return a ? b : c; } + function c2(x) { var a = foo(), b = bar(), c = baz(); return a ? c : b; } + function c3(x) { var a = foo(), b = bar(), c = baz(); return b ? a : c; } + function c4(x) { var a = foo(), b = bar(), c = baz(); return b ? c : a; } + function c5(x) { var a = foo(), b = bar(); return baz() ? a : b; } + function c6(x) { var a = foo(), b = bar(); return baz() ? b : a; } + + function i1(x) { var a = foo(), b = bar(), c = baz(); if (a) return b; else return c; } + function i2(x) { var a = foo(), b = bar(), c = baz(); if (a) return c; else return b; } + function i3(x) { var a = foo(), b = bar(), c = baz(); if (b) return a; else return c; } + function i4(x) { var a = foo(), b = bar(), c = baz(); if (b) return c; else return a; } + function i5(x) { var a = foo(), b = bar(); if (baz()) return a; else return b; } + function i6(x) { var a = foo(), b = bar(); if (baz()) return b; else return a; } + } +} + diff --git a/tools/exports.js b/tools/exports.js index 5007e03b..110b5c4e 100644 --- a/tools/exports.js +++ b/tools/exports.js @@ -15,3 +15,4 @@ exports["parse"] = parse; exports["push_uniq"] = push_uniq; exports["string_template"] = string_template; exports["is_identifier"] = is_identifier; +exports["SymbolDef"] = SymbolDef; diff --git a/tools/node.js b/tools/node.js index 5764286e..fa8c19dc 100644 --- a/tools/node.js +++ b/tools/node.js @@ -42,7 +42,8 @@ exports.minify = function(files, options) { mangleProperties : false, nameCache : null, output : null, - compress : {} + compress : {}, + parse : {} }); UglifyJS.base54.reset(); @@ -62,7 +63,8 @@ exports.minify = function(files, options) { sourcesContent[file] = code; toplevel = UglifyJS.parse(code, { filename: options.fromString ? i : file, - toplevel: toplevel + toplevel: toplevel, + bare_returns: options.parse ? options.parse.bare_returns : undefined }); }); } |