diff options
author | Richard van Velzen <rvanvelzen@experty.com> | 2016-09-30 12:27:45 +0200 |
---|---|---|
committer | Richard van Velzen <rvanvelzen@experty.com> | 2016-10-06 14:11:32 +0200 |
commit | e05510f3bce78943f76649a4102c2f10bfccdaef (patch) | |
tree | 9e18dff4a3af126d4c85633e6561af5a6bc15b94 | |
parent | fc9804b90955d3b38ac6e6c903a8c0e3f7945913 (diff) | |
download | tracifyjs-e05510f3bce78943f76649a4102c2f10bfccdaef.tar.gz tracifyjs-e05510f3bce78943f76649a4102c2f10bfccdaef.zip |
Add an option to wrap IIFEs in parenthesis
For #1307.
-rwxr-xr-x | bin/uglifyjs | 7 | ||||
-rw-r--r-- | lib/output.js | 15 | ||||
-rw-r--r-- | test/compress/wrap_iife.js | 33 |
3 files changed, 53 insertions, 2 deletions
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")})()})()();' +} |