From: Richard van Velzen Date: Fri, 30 Sep 2016 10:27:45 +0000 (+0200) Subject: Add an option to wrap IIFEs in parenthesis X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=e05510f3bce78943f76649a4102c2f10bfccdaef;p=UglifyJS.git Add an option to wrap IIFEs in parenthesis For #1307. --- diff --git a/bin/uglifyjs b/bin/uglifyjs index 8d7bd759..da9e0f10 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -76,6 +76,7 @@ You need to pass an argument to this option to specify the name that your module .describe("name-cache", "File to hold mangled names mappings") .describe("pure-funcs", "List of functions that can be safely removed if their return value is not used") .describe("dump-spidermonkey-ast", "Dump SpiderMonkey AST to stdout.") + .describe("wrap-iife", "Wrap IIFEs in parenthesis. Note: this disables the negate_iife compression option") .alias("p", "prefix") .alias("o", "output") @@ -130,6 +131,7 @@ You need to pass an argument to this option to specify the name that your module .boolean("bare-returns") .boolean("keep-fnames") .boolean("reserve-domprops") + .boolean("wrap-iife") .wrap(80) @@ -247,6 +249,11 @@ if (ARGS.keep_fnames) { if (MANGLE) MANGLE.keep_fnames = true; } +if (ARGS.wrap_iife) { + if (COMPRESS) COMPRESS.negate_iife = false; + OUTPUT_OPTIONS.wrap_iife = true; +} + if (BEAUTIFY) UglifyJS.merge(OUTPUT_OPTIONS, BEAUTIFY); diff --git a/lib/output.js b/lib/output.js index a3c6b4ab..c20a0365 100644 --- a/lib/output.js +++ b/lib/output.js @@ -67,7 +67,8 @@ function OutputStream(options) { screw_ie8 : true, preamble : null, quote_style : 0, - keep_quoted_props: false + keep_quoted_props: false, + wrap_iife : false, }, true); // Convert comment option to RegExp if neccessary and set up comments filter @@ -552,7 +553,17 @@ function OutputStream(options) { // a function expression needs parens around it when it's provably // the first token to appear in a statement. PARENS(AST_Function, function(output){ - return first_in_statement(output); + if (first_in_statement(output)) { + return true; + } + + if (output.option('wrap_iife')) { + var p = output.parent(); + console.log() + return p instanceof AST_Call && p.expression === this; + } + + return false; }); // same goes for an object literal, because otherwise it would be diff --git a/test/compress/wrap_iife.js b/test/compress/wrap_iife.js new file mode 100644 index 00000000..b1b88ac1 --- /dev/null +++ b/test/compress/wrap_iife.js @@ -0,0 +1,33 @@ +wrap_iife: { + options = { + negate_iife: false, + } + beautify = { + wrap_iife: true, + } + input: { + (function() { + return function() { + console.log('test') + }; + })()(); + } + expect_exact: '(function(){return function(){console.log("test")}})()();' +} + +wrap_iife_in_return_call: { + options = { + negate_iife: false, + } + beautify = { + wrap_iife: true, + } + input: { + (function() { + return (function() { + console.log('test') + })(); + })()(); + } + expect_exact: '(function(){return(function(){console.log("test")})()})()();' +}