diff options
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/compress.js b/lib/compress.js index 8dfbc72b..8d936bac 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -66,6 +66,7 @@ function Compressor(options, false_by_default) { join_vars : !false_by_default, cascade : !false_by_default, side_effects : !false_by_default, + negate_iife : !false_by_default, screw_ie8 : false, warnings : true, @@ -214,6 +215,11 @@ merge(Compressor.prototype, { statements = join_consecutive_vars(statements, compressor); } } while (CHANGED); + + if (compressor.option("negate_iife")) { + negate_iifes(statements, compressor); + } + return statements; function eliminate_spurious_blocks(statements) { @@ -497,6 +503,40 @@ merge(Compressor.prototype, { }, []); }; + function negate_iifes(statements, compressor) { + statements.forEach(function(stat){ + if (stat instanceof AST_SimpleStatement) { + stat.body = (function transform(thing) { + return thing.transform(new TreeTransformer(function(node){ + if (node instanceof AST_Call && node.expression instanceof AST_Function) { + return make_node(AST_UnaryPrefix, node, { + operator: "!", + expression: node + }); + } + else if (node instanceof AST_Call) { + node.expression = transform(node.expression); + } + else if (node instanceof AST_Seq) { + node.car = transform(node.car); + } + else if (node instanceof AST_Conditional) { + var expr = transform(node.condition); + if (expr !== node.condition) { + // it has been negated, reverse + node.condition = expr; + var tmp = node.consequent; + node.consequent = node.alternative; + node.alternative = tmp; + } + } + return node; + })); + })(stat.body); + } + }); + }; + }; function extract_declarations_from_unreachable_code(compressor, stat, target) { |