[Pkg-javascript-commits] [uglifyjs] 31/49: Add an option to wrap IIFEs in parenthesis

Jonas Smedegaard dr at jones.dk
Fri Dec 9 11:43:27 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository uglifyjs.

commit e05510f3bce78943f76649a4102c2f10bfccdaef
Author: Richard van Velzen <rvanvelzen at experty.com>
Date:   Fri Sep 30 12:27:45 2016 +0200

    Add an option to wrap IIFEs in parenthesis
    
    For #1307.
---
 bin/uglifyjs               |  7 +++++++
 lib/output.js              | 15 +++++++++++++--
 test/compress/wrap_iife.js | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/bin/uglifyjs b/bin/uglifyjs
index 8d7bd75..da9e0f1 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 a3c6b4a..c20a036 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 0000000..b1b88ac
--- /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")})()})()();'
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list