Skip to content

Commit

Permalink
Refactor and simplify umd wrapper (#2594)
Browse files Browse the repository at this point in the history
* Refactor UMD output to check for and use `self` before using `this`,
remove unnecessary parentheses in UMD and IIFE

* Do not render special CJS intro if IIFE and CJS result are identical,
do not render global if not used in intro
  • Loading branch information
lukastaegert committed Dec 15, 2018
1 parent 15e0885 commit ed67b63
Show file tree
Hide file tree
Showing 310 changed files with 1,084 additions and 1,165 deletions.
2 changes: 1 addition & 1 deletion src/finalisers/iife.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default function iife(
}

if (extend) {
deps.unshift(`(${thisProp(name)}${_}=${_}${thisProp(name)}${_}||${_}{})`);
deps.unshift(`${thisProp(name)}${_}=${_}${thisProp(name)}${_}||${_}{}`);
args.unshift('exports');
} else if (namedExportsMode && hasExports) {
deps.unshift('{}');
Expand Down
66 changes: 39 additions & 27 deletions src/finalisers/umd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export default function umd(
const _ = options.compact ? '' : ' ';
const n = options.compact ? '' : '\n';

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

if (hasExports && !options.name) {
error({
code: 'INVALID_OPTION',
Expand All @@ -53,20 +51,20 @@ export default function umd(
const amdDeps = dependencies.map(m => `'${m.id}'`);
const cjsDeps = dependencies.map(m => `require('${m.id}')`);

const trimmed = trimEmptyImports(dependencies);
const globalDeps = trimmed.map(module => globalProp(module.globalName));
const args = trimmed.map(m => m.name);
const trimmedImports = trimEmptyImports(dependencies);
const globalDeps = trimmedImports.map(module => globalProp(module.globalName));
const factoryArgs = trimmedImports.map(m => m.name);

if (namedExportsMode && hasExports) {
if (namedExportsMode && (hasExports || options.noConflict === true)) {
amdDeps.unshift(`'exports'`);
cjsDeps.unshift(`exports`);
globalDeps.unshift(
`(${setupNamespace(options.name, 'global', true, options.globals, options.compact)}${_}=${_}${
`${setupNamespace(options.name, 'global', true, options.globals, options.compact)}${_}=${_}${
options.extend ? `${globalProp(options.name)}${_}||${_}` : ''
}{})`
}{}`
);

args.unshift('exports');
factoryArgs.unshift('exports');
}

const amdOptions = options.amd || {};
Expand Down Expand Up @@ -94,28 +92,42 @@ export default function umd(
factory = `var exports${_}=${_}factory(${globalDeps});`;
} else if (namedExportsMode) {
const module = globalDeps.shift();
factory = `var exports${_}=${_}${module};${n}`;
factory += `${t}${t}factory(${['exports'].concat(globalDeps)});`;
factory =
`var exports${_}=${_}${module};${n}` +
`${t}${t}factory(${['exports'].concat(globalDeps)});`;
}
globalExport = `(function()${_}{${n}`;
globalExport += `${t}${t}var current${_}=${_}${safeAccess(options.name, options.compact)};${n}`;
globalExport += `${t}${t}${factory}${n}`;
globalExport += `${t}${t}${globalProp(options.name)}${_}=${_}exports;${n}`;
globalExport += `${t}${t}exports.noConflict${_}=${_}function()${_}{${_}`;
globalExport += `${globalProp(options.name)}${_}=${_}current;${_}return exports${
options.compact ? '' : '; '
}};${n}`;
globalExport += `${t}})()`;
globalExport =
`(function()${_}{${n}` +
`${t}${t}var current${_}=${_}${safeAccess(options.name, options.compact)};${n}` +
`${t}${t}${factory}${n}` +
`${t}${t}${globalProp(options.name)}${_}=${_}exports;${n}` +
`${t}${t}exports.noConflict${_}=${_}function()${_}{${_}` +
`${globalProp(options.name)}${_}=${_}current;${_}return exports${
options.compact ? '' : '; '
}};${n}` +
`${t}})()`;
} else {
globalExport = `(${defaultExport}factory(${globalDeps}))`;
globalExport = `${defaultExport}factory(${globalDeps})`;
}

let wrapperIntro = `(function${_}(global,${_}factory)${_}{${n}`;
wrapperIntro += `${t}typeof exports${_}===${_}'object'${_}&&${_}typeof module${_}!==${_}'undefined'${_}?`;
wrapperIntro += `${_}${cjsExport}factory(${cjsDeps.join(`,${_}`)})${_}:${n}`;
wrapperIntro += `${t}typeof ${define}${_}===${_}'function'${_}&&${_}${define}.amd${_}?${_}${define}(${amdParams}factory)${_}:${n}`;
wrapperIntro += `${t}${globalExport};${n}`;
wrapperIntro += `}(this,${_}(function${_}(${args})${_}{${useStrict}${n}`;
const iifeNeedsGlobal = hasExports || (options.noConflict === true && namedExportsMode);
const globalParam = iifeNeedsGlobal ? `global,${_}` : '';
const globalArg = iifeNeedsGlobal
? `typeof self${_}!==${_}'undefined'${_}?${_}self${_}:${_}this,${_}`
: '';
const cjsIntro = iifeNeedsGlobal
? `${t}typeof exports${_}===${_}'object'${_}&&${_}typeof module${_}!==${_}'undefined'${_}?` +
`${_}${cjsExport}factory(${cjsDeps.join(`,${_}`)})${_}:${n}`
: '';

const wrapperIntro =
`(function${_}(${globalParam}factory)${_}{${n}` +
cjsIntro +
`${t}typeof ${define}${_}===${_}'function'${_}&&${_}${define}.amd${_}?${_}${define}(${amdParams}factory)${_}:${n}` +
`${t}${globalExport};${n}` +
`}(${globalArg}function${_}(${factoryArgs})${_}{${useStrict}${n}`;

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

// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock(dependencies, options, graph.varOrConst);
Expand Down
9 changes: 4 additions & 5 deletions test/cli/samples/indent-none/_expected.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
factory();
}(function () { 'use strict';

assert.equal( 1 + 1, 2 );

})));
}));
6 changes: 3 additions & 3 deletions test/cli/samples/module-name/_expected.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.myBundle = factory());
}(this, (function () { 'use strict';
global.myBundle = factory();
}(typeof self !== 'undefined' ? self : this, function () { 'use strict';

var main = 42;

return main;

})));
}));
4 changes: 2 additions & 2 deletions test/cli/samples/no-conflict/_expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
global.conflictyName = exports;
exports.noConflict = function() { global.conflictyName = current; return exports; };
})();
}(this, (function () { 'use strict';
}(typeof self !== 'undefined' ? self : this, function () { 'use strict';

var main = {};

return main;

})));
}));
2 changes: 1 addition & 1 deletion test/form/samples/absolute-path-resolver/_expected/iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
a();
a();

}());
}());
9 changes: 4 additions & 5 deletions test/form/samples/absolute-path-resolver/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
factory();
}(function () { 'use strict';

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

})));
}));
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
factory();
}(function () { 'use strict';

const callArg = arg => arg();
callArg( () => console.log( 'effect' ) );
Expand Down Expand Up @@ -34,4 +33,4 @@
const multiArgument2 = ( func, obj ) => func( obj );
multiArgument2( obj => obj.foo.bar = 1, {} );

})));
}));
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
factory();
}(function () { 'use strict';

(() => () => console.log( 'effect' ))()();
(() => () => () => console.log( 'effect' ))()()();
Expand All @@ -15,4 +14,4 @@
(() => ({ foo: () => console.log( 'effect' ) }))().foo();
(() => ({ foo: () => ({ bar: () => console.log( 'effect' ) }) }))().foo().bar();

})));
}));
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.bundle = {})));
}(this, (function (exports) { 'use strict';
factory(global.bundle = {});
}(typeof self !== 'undefined' ? self : this, function (exports) { 'use strict';

var buffer = new ArrayBuffer( 8 );

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

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

})));
}));
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.myModule = {})));
}(this, (function (exports) { 'use strict';
factory(global.myModule = {});
}(typeof self !== 'undefined' ? self : this, function (exports) { 'use strict';

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

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

})));
}));
6 changes: 3 additions & 3 deletions test/form/samples/assignment-to-exports/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.bundle = {})));
}(this, (function (exports) { 'use strict';
factory(global.bundle = {});
}(typeof self !== 'undefined' ? self : this, function (exports) { 'use strict';

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

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

})));
}));
9 changes: 4 additions & 5 deletions test/form/samples/assignment-to-global/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
factory();
}(function () { 'use strict';

globalVar = 1;

})));
}));
9 changes: 4 additions & 5 deletions test/form/samples/async-function-unused/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
factory();
}(function () { 'use strict';

async function foo () {
return 'foo';
}

foo().then( value => console.log( value ) );

})));
}));
9 changes: 4 additions & 5 deletions test/form/samples/banner-and-footer/_expected/umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
/* first banner */
/* second banner */
/* 3rd banner */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
factory();
}(function () { 'use strict';

console.log( 'hello world' );

})));
}));
/* this is a footer */
/* first footer */
/* second footer */
Expand Down
9 changes: 4 additions & 5 deletions test/form/samples/block-comments/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
factory();
}(function () { 'use strict';

function foo () {
return embiggen( 6, 7 );
Expand All @@ -20,4 +19,4 @@

alert( foo() );

})));
}));
2 changes: 1 addition & 1 deletion test/form/samples/body-less-for-loops/_expected/iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
let index;
for ( index in array ) console.log( index );

}());
}());
9 changes: 4 additions & 5 deletions test/form/samples/body-less-for-loops/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
factory();
}(function () { 'use strict';

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

})));
}));
6 changes: 3 additions & 3 deletions test/form/samples/catch-parameter-shadowing/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.bundle = {})));
}(this, (function (exports) { 'use strict';
factory(global.bundle = {});
}(typeof self !== 'undefined' ? self : this, function (exports) { 'use strict';

const e = 2.7182818284;

Expand All @@ -18,4 +18,4 @@

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

})));
}));
9 changes: 4 additions & 5 deletions test/form/samples/circular-member-expression/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
factory();
}(function () { 'use strict';

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

console.log(foo);

})));
}));

0 comments on commit ed67b63

Please sign in to comment.