Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parentheses to factory function of UMD bundles to avoid lazy parsing #3183

Merged
merged 1 commit into from Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/finalisers/umd.ts
Expand Up @@ -138,14 +138,15 @@ export default function umd(
`${_}${cjsExport}${factoryVar}(${cjsDeps.join(`,${_}`)})${_}:${n}`
: '';

// factory function should be wrapped by parentheses to avoid lazy parsing
const wrapperIntro =
`(function${_}(${globalParam}${factoryVar})${_}{${n}` +
cjsIntro +
`${t}typeof ${define}${_}===${_}'function'${_}&&${_}${define}.amd${_}?${_}${define}(${amdParams}${factoryVar})${_}:${n}` +
`${t}${iifeStart}${iifeExport}${iifeEnd};${n}` +
`}(${globalArg}function${_}(${factoryArgs.join(', ')})${_}{${useStrict}${n}`;
`}(${globalArg}(function${_}(${factoryArgs.join(', ')})${_}{${useStrict}${n}`;

const wrapperOutro = n + n + '}));';
const wrapperOutro = n + n + '})));';

// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock(dependencies, options, varOrConst);
Expand Down
4 changes: 2 additions & 2 deletions test/cli/samples/indent-none/_expected.js
@@ -1,8 +1,8 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';
}((function () { 'use strict';

assert.equal( 1 + 1, 2 );

}));
})));
4 changes: 2 additions & 2 deletions test/cli/samples/module-name/_expected.js
Expand Up @@ -2,10 +2,10 @@
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.myBundle = factory());
}(this, function () { 'use strict';
}(this, (function () { 'use strict';

var main = 42;

return main;

}));
})));
4 changes: 2 additions & 2 deletions test/cli/samples/no-conflict/_expected.js
Expand Up @@ -6,10 +6,10 @@
var exports = global.conflictyName = factory();
exports.noConflict = function () { global.conflictyName = current; return exports; };
}()));
}(this, function () { 'use strict';
}(this, (function () { 'use strict';

var main = {};

return main;

}));
})));
Expand Up @@ -2,7 +2,7 @@
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.bundle = {}));
}(this, function (exports) { 'use strict';
}(this, (function (exports) { 'use strict';

var buffer = new ArrayBuffer( 8 );

Expand All @@ -15,4 +15,4 @@

Object.defineProperty(exports, '__esModule', { value: true });

}));
})));
Expand Up @@ -2,11 +2,11 @@
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.myModule = {}));
}(this, function (exports) { 'use strict';
}(this, (function (exports) { 'use strict';

exports.Foo = class Foo {};
exports.Foo = lol( exports.Foo );

Object.defineProperty(exports, '__esModule', { value: true });

}));
})));
4 changes: 2 additions & 2 deletions test/form/samples/assignment-to-exports/_expected/umd.js
Expand Up @@ -2,7 +2,7 @@
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.bundle = {}));
}(this, function (exports) { 'use strict';
}(this, (function (exports) { 'use strict';

// Unassigned export
var foo1;
Expand Down Expand Up @@ -32,4 +32,4 @@

Object.defineProperty(exports, '__esModule', { value: true });

}));
})));
4 changes: 2 additions & 2 deletions test/form/samples/banner-and-footer/_expected/umd.js
Expand Up @@ -5,11 +5,11 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';
}((function () { 'use strict';

console.log( 'hello world' );

}));
})));
/* this is a footer */
/* first footer */
/* second footer */
Expand Down
4 changes: 2 additions & 2 deletions test/form/samples/body-less-for-loops/_expected/umd.js
@@ -1,7 +1,7 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';
}((function () { 'use strict';

for ( let i = 0; i < 10; i += 1 ) console.log( i );
for ( const letter of array ) console.log( letter );
Expand All @@ -16,4 +16,4 @@
let index;
for ( index in array ) console.log( index );

}));
})));
4 changes: 2 additions & 2 deletions test/form/samples/circular-member-expression/_expected/umd.js
@@ -1,12 +1,12 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';
}((function () { 'use strict';

var foo = function() {
foo.toString = null;
}.toString();

console.log(foo);

}));
})));
4 changes: 2 additions & 2 deletions test/form/samples/comment-before-import/_expected/umd.js
@@ -1,7 +1,7 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';
}((function () { 'use strict';

// bar.js
var bar = 21;
Expand All @@ -14,4 +14,4 @@

console.log( foo );

}));
})));
@@ -1,11 +1,11 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';
}((function () { 'use strict';

var foo = () => 'foo';

// /*
console.log(foo());

}));
})));
2 changes: 1 addition & 1 deletion test/form/samples/compact-empty-external/_expected/umd.js
@@ -1 +1 @@
(function(f){typeof define==='function'&&define.amd?define(['external'],f):f();}(function(){'use strict';}));
(function(f){typeof define==='function'&&define.amd?define(['external'],f):f();}((function(){'use strict';})));
4 changes: 2 additions & 2 deletions test/form/samples/compact-multiple-imports/_expected/umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/form/samples/compact/_expected/umd.js
@@ -1,6 +1,6 @@
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f(require('external')):typeof define==='function'&&define.amd?define(['external'],f):(g=g||self,g.foo=f(g.x));}(this,function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',__proto__:null,get default(){return foo}});console.log(self);
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f(require('external')):typeof define==='function'&&define.amd?define(['external'],f):(g=g||self,g.foo=f(g.x));}(this,(function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',__proto__:null,get default(){return foo}});console.log(self);
function foo () {
console.log( x );
}
// trailing comment
return foo;}));
return foo;})));
4 changes: 2 additions & 2 deletions test/form/samples/computed-properties/_expected/umd.js
Expand Up @@ -2,7 +2,7 @@
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.computedProperties = {}));
}(this, function (exports) { 'use strict';
}(this, (function (exports) { 'use strict';

var foo = 'foo';
var bar = 'bar';
Expand All @@ -22,4 +22,4 @@

Object.defineProperty(exports, '__esModule', { value: true });

}));
})));
4 changes: 2 additions & 2 deletions test/form/samples/conditional-expression/_expected/umd.js
@@ -1,7 +1,7 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';
}((function () { 'use strict';

// side-effect in condition
var a = foo() ? 1 : 2;
Expand All @@ -21,4 +21,4 @@
var i2 = ( function () {this.x = 1;})();
var i3 = ( () => () => console.log( 'effect' ))()();

}));
})));
4 changes: 2 additions & 2 deletions test/form/samples/configure-file-url/_expected/umd.js
@@ -1,7 +1,7 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';
}((function () { 'use strict';

var asset1 = 'chunkId=umd.js:moduleId=solved:fileName=assets/asset-solved-28a7ac89.txt:format=umd:relativePath=assets/asset-solved-28a7ac89.txt:referenceId=6296c678';

Expand All @@ -11,4 +11,4 @@

console.log(asset1, asset2, asset3);

}));
})));
4 changes: 2 additions & 2 deletions test/form/samples/conflicting-imports/_expected/umd.js
Expand Up @@ -2,10 +2,10 @@
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('foo'), require('bar')) :
typeof define === 'function' && define.amd ? define(['foo', 'bar'], factory) :
(global = global || self, factory(global.foo, global.bar));
}(this, function (foo, bar) { 'use strict';
}(this, (function (foo, bar) { 'use strict';

console.log( bar.a );

console.log( foo.a );

}));
})));
4 changes: 2 additions & 2 deletions test/form/samples/curried-function/_expected/umd.js
@@ -1,7 +1,7 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';
}((function () { 'use strict';

function curry1 ( fn ) {
return function f1 ( a ) {
Expand All @@ -17,4 +17,4 @@

var T = always(true);

}));
})));
4 changes: 2 additions & 2 deletions test/form/samples/custom-context/_expected/umd.js
@@ -1,8 +1,8 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';
}((function () { 'use strict';

lolwut.prop = '???';

}));
})));
@@ -1,10 +1,10 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';
}((function () { 'use strict';

undefined.prop = 'nope';

lolwut.prop = '???';

}));
})));
4 changes: 2 additions & 2 deletions test/form/samples/custom-module-context/_expected/umd.js
@@ -1,10 +1,10 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';
}((function () { 'use strict';

undefined.prop = 'nope';

lolwut.prop = '???';

}));
})));
Expand Up @@ -2,7 +2,7 @@
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.bundle = {}));
}(this, function (exports) { 'use strict';
}(this, (function (exports) { 'use strict';

const exports$1 = {
x: 42
Expand Down Expand Up @@ -30,4 +30,4 @@

Object.defineProperty(exports, '__esModule', { value: true });

}));
})));
Expand Up @@ -2,7 +2,7 @@
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('external')) :
typeof define === 'function' && define.amd ? define(['external'], factory) :
(global = global || self, global.bundle = factory(global.external));
}(this, function (external) { 'use strict';
}(this, (function (external) { 'use strict';

external = external && external.hasOwnProperty('default') ? external['default'] : external;

Expand Down Expand Up @@ -53,4 +53,4 @@

return exports.default;

}));
})));
4 changes: 2 additions & 2 deletions test/form/samples/dedupes-external-imports/_expected/umd.js
Expand Up @@ -2,7 +2,7 @@
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('external')) :
typeof define === 'function' && define.amd ? define(['exports', 'external'], factory) :
(global = global || self, factory(global.myBundle = {}, global.external));
}(this, function (exports, external) { 'use strict';
}(this, (function (exports, external) { 'use strict';

class Foo extends external.Component {
constructor () {
Expand Down Expand Up @@ -35,4 +35,4 @@

Object.defineProperty(exports, '__esModule', { value: true });

}));
})));
@@ -1,7 +1,7 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';
}((function () { 'use strict';

function a() {
a = someGlobal;
Expand All @@ -10,4 +10,4 @@

a();

}));
})));
4 changes: 2 additions & 2 deletions test/form/samples/define-es-modules-false/_expected/umd.js
Expand Up @@ -2,7 +2,7 @@
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.foo = {}));
}(this, function (exports) { 'use strict';
}(this, (function (exports) { 'use strict';

const make1 = () => {};

Expand All @@ -11,4 +11,4 @@
exports.make1 = make1;
exports.make2 = make2;

}));
})));
4 changes: 2 additions & 2 deletions test/form/samples/define-replacement/_expected/umd.js
@@ -1,7 +1,7 @@
(function (factory) {
typeof enifed === 'function' && enifed.amd ? enifed(factory) :
factory();
}(function () { 'use strict';
}((function () { 'use strict';

var a = () => {
console.log('props');
Expand All @@ -10,4 +10,4 @@
a();
a();

}));
})));