Skip to content

Commit

Permalink
Add an option to wrap IIFEs in parenthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard van Velzen committed Oct 6, 2016
1 parent fc9804b commit e05510f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
7 changes: 7 additions & 0 deletions bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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);

Expand Down
15 changes: 13 additions & 2 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions test/compress/wrap_iife.js
Original file line number Diff line number Diff line change
@@ -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")})()})()();'
}

0 comments on commit e05510f

Please sign in to comment.