Skip to content

Commit

Permalink
Always use arrow functions for large IIFEs
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Sep 2, 2021
1 parent 02e3432 commit 3388ebf
Show file tree
Hide file tree
Showing 433 changed files with 1,001 additions and 977 deletions.
4 changes: 2 additions & 2 deletions src/finalisers/amd.ts
Expand Up @@ -37,7 +37,7 @@ export default function amd(
warnOnBuiltins(warn, dependencies);
const deps = dependencies.map(m => `'${removeExtensionFromRelativeAmdId(m.id)}'`);
const args = dependencies.map(m => m.name);
const { n, getFunctionIntro, _ } = snippets;
const { n, getNonArrowFunctionIntro, _ } = snippets;

if (namedExportsMode && hasExports) {
args.unshift(`exports`);
Expand Down Expand Up @@ -99,7 +99,7 @@ export default function amd(
// factory function should be wrapped by parentheses to avoid lazy parsing,
// cf. https://v8.dev/blog/preparser#pife
.prepend(
`${amd.define}(${params}(${getFunctionIntro(args, {
`${amd.define}(${params}(${getNonArrowFunctionIntro(args, {
isAsync: false,
name: null
})}{${useStrict}${n}${n}`
Expand Down
4 changes: 2 additions & 2 deletions src/finalisers/iife.ts
Expand Up @@ -38,7 +38,7 @@ export default function iife(
strict
}: NormalizedOutputOptions
): Bundle {
const { _, getFunctionIntro, getPropertyAccess, n } = snippets;
const { _, getNonArrowFunctionIntro, getPropertyAccess, n } = snippets;
const isNamespaced = name && name.indexOf('.') !== -1;
const useVariableAssignment = !extend && !isNamespaced;

Expand Down Expand Up @@ -90,7 +90,7 @@ export default function iife(
);
magicString.prepend(`${intro}${interopBlock}`);

let wrapperIntro = `(${getFunctionIntro(args, {
let wrapperIntro = `(${getNonArrowFunctionIntro(args, {
isAsync: false,
name: null
})}{${n}${useStrict}${n}`;
Expand Down
12 changes: 6 additions & 6 deletions src/finalisers/system.ts
Expand Up @@ -21,7 +21,7 @@ export default function system(
}: FinaliserOptions,
options: NormalizedOutputOptions
): Bundle {
const { _, getFunctionIntro, n, s } = snippets;
const { _, getFunctionIntro, getNonArrowFunctionIntro, n, s } = snippets;
const { importBindings, setters, starExcludes } = analyzeDependencies(
dependencies,
exports,
Expand All @@ -41,7 +41,7 @@ export default function system(
let wrapperStart =
`System.register(${registeredName}[` +
dependencies.map(({ id }) => `'${id}'`).join(`,${_}`) +
`],${_}(${getFunctionIntro(wrapperParams, { isAsync: false, name: null })}{${n}${t}${
`],${_}(${getNonArrowFunctionIntro(wrapperParams, { isAsync: false, name: null })}{${n}${t}${
options.strict ? "'use strict';" : ''
}` +
getStarExcludesBlock(starExcludes, varOrConst, t, snippets) +
Expand All @@ -51,19 +51,19 @@ export default function system(
? `${n}${t}${t}setters:${_}[${setters
.map(setter =>
setter
? `(${getFunctionIntro(['module'], {
? `${getFunctionIntro(['module'], {
isAsync: false,
name: null
})}{${n}${t}${t}${t}${setter}${n}${t}${t}})`
})}{${n}${t}${t}${t}${setter}${n}${t}${t}}`
: options.systemNullSetters
? `null`
: `function${_}()${_}{}`
: `${getFunctionIntro([], { isAsync: false, name: null })}{}`
)
.join(`,${_}`)}],`
: ''
}${n}`;
wrapperStart +=
`${t}${t}execute:${_}(${getFunctionIntro([], {
`${t}${t}execute:${_}(${getNonArrowFunctionIntro([], {
isAsync: usesTopLevelAwait,
name: null
})}{${n}${n}` + getHoistedExportsBlock(exports, t, snippets);
Expand Down
6 changes: 3 additions & 3 deletions src/finalisers/umd.ts
Expand Up @@ -59,7 +59,7 @@ export default function umd(
strict
}: NormalizedOutputOptions
): Bundle {
const { _, getFunctionIntro, getPropertyAccess, n, s } = snippets;
const { _, getNonArrowFunctionIntro, getPropertyAccess, n, s } = snippets;
const factoryVar = compact ? 'f' : 'factory';
const globalVar = compact ? 'g' : 'global';

Expand Down Expand Up @@ -164,13 +164,13 @@ export default function umd(
: '';

const wrapperIntro =
`(${getFunctionIntro(wrapperParams, { isAsync: false, name: null })}{${n}` +
`(${getNonArrowFunctionIntro(wrapperParams, { isAsync: false, name: null })}{${n}` +
cjsIntro +
`${t}typeof ${define}${_}===${_}'function'${_}&&${_}${define}.amd${_}?${_}${define}(${amdParams}${factoryVar})${_}:${n}` +
`${t}${iifeStart}${iifeExport}${iifeEnd};${n}` +
// factory function should be wrapped by parentheses to avoid lazy parsing,
// cf. https://v8.dev/blog/preparser#pife
`})(${globalArg}(${getFunctionIntro(factoryParams, {
`})(${globalArg}(${getNonArrowFunctionIntro(factoryParams, {
isAsync: false,
name: null
})}{${useStrict}${n}`;
Expand Down
15 changes: 11 additions & 4 deletions src/utils/generateCodeSnippets.ts
Expand Up @@ -21,6 +21,10 @@ export interface GenerateCodeSnippets {
}
): string;
getFunctionIntro(params: string[], options: { isAsync: boolean; name: string | null }): string;
getNonArrowFunctionIntro(
params: string[],
options: { isAsync: boolean; name: string | null }
): string;
getObject(
fields: [key: string | null, value: string][],
options: { indent: string; lineBreaks: boolean }
Expand All @@ -36,6 +40,11 @@ export function getGenerateCodeSnippets({
}: NormalizedOutputOptions): GenerateCodeSnippets {
const { _, n, s } = compact ? { _: '', n: '', s: '' } : { _: ' ', n: '\n', s: ';' };
const cnst = blockBindings || preferConst ? 'const' : 'var';
const getNonArrowFunctionIntro: GenerateCodeSnippets['getNonArrowFunctionIntro'] = (
params,
{ isAsync, name }
) =>
`${isAsync ? `async ` : ''}function${name ? ` ${name}` : ''}${_}(${params.join(`,${_}`)})${_}`;

const getFunctionIntro: GenerateCodeSnippets['getFunctionIntro'] = arrowFunctions
? (params, { isAsync, name }) => {
Expand All @@ -45,10 +54,7 @@ export function getGenerateCodeSnippets({
singleParam ? params[0] : `(${params.join(`,${_}`)})`
}${_}=>${_}`;
}
: (params, { isAsync, name }) =>
`${isAsync ? `async ` : ''}function${name ? ` ${name}` : ''}${_}(${params.join(
`,${_}`
)})${_}`;
: getNonArrowFunctionIntro;

const getDirectReturnFunctionLeft: GenerateCodeSnippets['getDirectReturnFunctionLeft'] = (
params,
Expand Down Expand Up @@ -82,6 +88,7 @@ export function getGenerateCodeSnippets({
arrowFunctions || needsWrappedFunction
)}(`,
getFunctionIntro,
getNonArrowFunctionIntro,
getObject(fields, { indent, lineBreaks }) {
const prefix = `${lineBreaks ? n : ''}${indent}`;
return `{${fields
Expand Down
Expand Up @@ -2,9 +2,9 @@ System.register(['lib'], (function (exports) {
'use strict';
var value;
return {
setters: [(function (module) {
setters: [function (module) {
value = module["default"];
})],
}],
execute: (function () {

var dep = exports('d', 2 * value);
Expand Down
Expand Up @@ -2,9 +2,9 @@ System.register(['./generated-dep.js'], (function () {
'use strict';
var dep;
return {
setters: [(function (module) {
setters: [function (module) {
dep = module.d;
})],
}],
execute: (function () {

console.log('main1', dep);
Expand Down
Expand Up @@ -2,9 +2,9 @@ System.register(['./generated-dep.js'], (function () {
'use strict';
var dep;
return {
setters: [(function (module) {
setters: [function (module) {
dep = module.d;
})],
}],
execute: (function () {

console.log('main2', dep);
Expand Down
Expand Up @@ -2,9 +2,9 @@ System.register(['./generated-dep2.js'], (function (exports) {
'use strict';
var fn$1;
return {
setters: [(function (module) {
setters: [function (module) {
fn$1 = module.f;
})],
}],
execute: (function () {

function fn () {
Expand Down
Expand Up @@ -2,9 +2,9 @@ System.register(['./generated-dep2.js'], (function (exports) {
'use strict';
var fn$2;
return {
setters: [(function (module) {
setters: [function (module) {
fn$2 = module.f;
})],
}],
execute: (function () {

function fn$1 () {
Expand Down
Expand Up @@ -2,11 +2,11 @@ System.register(['./generated-dep1.js', './generated-dep2.js'], (function (expor
'use strict';
var x$1, x$2;
return {
setters: [(function (module) {
setters: [function (module) {
x$1 = module.x;
}), (function (module) {
}, function (module) {
x$2 = module.x;
})],
}],
execute: (function () {

var x = exports('x', x$1 + 1);
Expand Down
Expand Up @@ -2,10 +2,10 @@ System.register(['./generated-shared2.js', './generated-dep1.js', './generated-d
'use strict';
var x, y;
return {
setters: [(function (module) {
setters: [function (module) {
x = module.x;
y = module.y;
}), function () {}, function () {}],
}, function () {}, function () {}],
execute: (function () {

console.log(x + y);
Expand Down
Expand Up @@ -2,9 +2,9 @@ System.register(['./generated-dep11.js', './generated-dep112.js', './generated-d
'use strict';
var x;
return {
setters: [function () {}, (function (module) {
setters: [function () {}, function (module) {
x = module.x;
}), function () {}],
}, function () {}],
execute: (function () {

console.log('1');
Expand Down
Expand Up @@ -2,9 +2,9 @@ System.register(['./generated-dep1.js'], (function (exports) {
'use strict';
var fn;
return {
setters: [(function (module) {
setters: [function (module) {
fn = module.f;
})],
}],
execute: (function () {

class Main1 {
Expand Down
Expand Up @@ -2,9 +2,9 @@ System.register(['./generated-dep1.js'], (function (exports) {
'use strict';
var fn;
return {
setters: [(function (module) {
setters: [function (module) {
fn = module.a;
})],
}],
execute: (function () {

class Main2 {
Expand Down
@@ -1,9 +1,9 @@
System.register(['./generated-one.js'], (function (exports) {
'use strict';
return {
setters: [(function (module) {
setters: [function (module) {
exports('ItemOne', module.O);
})],
}],
execute: (function () {


Expand Down
Expand Up @@ -2,9 +2,9 @@ System.register(['./generated-one.js'], (function (exports) {
'use strict';
var ONE_CONSTANT;
return {
setters: [(function (module) {
setters: [function (module) {
ONE_CONSTANT = module.a;
})],
}],
execute: (function () {

class Two {
Expand Down
Expand Up @@ -2,9 +2,9 @@ System.register(['./generated-lib.js'], (function () {
'use strict';
var emptyFunction;
return {
setters: [(function (module) {
setters: [function (module) {
emptyFunction = module.e;
})],
}],
execute: (function () {

function fn() {
Expand Down
Expand Up @@ -2,10 +2,10 @@ System.register(['./generated-dep1.js'], (function (exports) {
'use strict';
var fn, text;
return {
setters: [(function (module) {
setters: [function (module) {
fn = module.f;
text = module.t;
})],
}],
execute: (function () {

class Main1 {
Expand Down
Expand Up @@ -2,10 +2,10 @@ System.register(['./generated-dep1.js'], (function (exports) {
'use strict';
var fn, text;
return {
setters: [(function (module) {
setters: [function (module) {
fn = module.a;
text = module.b;
})],
}],
execute: (function () {

class Main2 {
Expand Down
Expand Up @@ -2,10 +2,10 @@ System.register(['./generated-shared.js'], (function (exports) {
'use strict';
var commonjsGlobal, shared;
return {
setters: [(function (module) {
setters: [function (module) {
commonjsGlobal = module.c;
shared = module.s;
})],
}],
execute: (function () {

commonjsGlobal.fn = d => d + 1;
Expand Down
Expand Up @@ -2,9 +2,9 @@ System.register(['./generated-shared.js'], (function (exports) {
'use strict';
var shared;
return {
setters: [(function (module) {
setters: [function (module) {
shared = module.s;
})],
}],
execute: (function () {

var main2 = exports('default', shared.map(d => d + 2));
Expand Down
Expand Up @@ -2,11 +2,11 @@ System.register(['../chunks/chunk.js', '../chunks/chunk3.js'], (function () {
'use strict';
var num, num$1;
return {
setters: [(function (module) {
setters: [function (module) {
num = module.n;
}), (function (module) {
}, function (module) {
num$1 = module.n;
})],
}],
execute: (function () {

console.log(num + num$1);
Expand Down
Expand Up @@ -2,11 +2,11 @@ System.register(['./chunks/chunk.js', './chunks/chunk2.js'], (function () {
'use strict';
var num, num$1;
return {
setters: [(function (module) {
setters: [function (module) {
num = module.n;
}), (function (module) {
}, function (module) {
num$1 = module.n;
})],
}],
execute: (function () {

console.log(num + num$1);
Expand Down

0 comments on commit 3388ebf

Please sign in to comment.