Skip to content

Commit

Permalink
Merge directReturnFunction left and right
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Sep 18, 2021
1 parent ac37786 commit cc2cead
Show file tree
Hide file tree
Showing 28 changed files with 206 additions and 228 deletions.
89 changes: 37 additions & 52 deletions src/ast/nodes/ImportExpression.ts
Expand Up @@ -48,20 +48,18 @@ export default class ImportExpression extends NodeBase {
render(code: MagicString, options: RenderOptions): void {
if (this.inlineNamespace) {
const {
snippets: { getDirectReturnFunctionLeft, getDirectReturnFunctionRight, getPropertyAccess }
snippets: { getDirectReturnFunction, getPropertyAccess }
} = options;
const { left, right } = getDirectReturnFunction([], {
functionReturn: true,
lineBreakIndent: false,
name: null,
t: ''
});
code.overwrite(
this.start,
this.end,
`Promise.resolve().then(${getDirectReturnFunctionLeft([], {
functionReturn: true,
lineBreakIndent: false,
name: null,
t: ''
})}${this.inlineNamespace.getName(getPropertyAccess)}${getDirectReturnFunctionRight({
lineBreakIndent: false,
name: false
})})`,
`Promise.resolve().then(${left}${this.inlineNamespace.getName(getPropertyAccess)}${right})`,
{ contentOnly: true }
);
return;
Expand All @@ -83,22 +81,17 @@ export default class ImportExpression extends NodeBase {
code: MagicString,
resolution: string,
namespaceExportName: string | false | undefined,
{ getDirectReturnFunctionLeft, getDirectReturnFunctionRight }: GenerateCodeSnippets
{ getDirectReturnFunction }: GenerateCodeSnippets
): void {
code.overwrite(this.source.start, this.source.end, resolution);
if (namespaceExportName) {
code.prependLeft(
this.end,
`.then(${getDirectReturnFunctionLeft(['n'], {
functionReturn: true,
lineBreakIndent: false,
name: null,
t: ''
})}n.${namespaceExportName}${getDirectReturnFunctionRight({
lineBreakIndent: false,
name: false
})})`
);
const { left, right } = getDirectReturnFunction(['n'], {
functionReturn: true,
lineBreakIndent: false,
name: null,
t: ''
});
code.prependLeft(this.end, `.then(${left}n.${namespaceExportName}${right})`);
}
}

Expand Down Expand Up @@ -143,12 +136,7 @@ export default class ImportExpression extends NodeBase {
generatedCode: { arrowFunctions },
interop
}: NormalizedOutputOptions,
{
_,
getDirectReturnFunctionLeft,
getDirectReturnFunctionRight,
getDirectReturnIifeLeft
}: GenerateCodeSnippets,
{ _, getDirectReturnFunction, getDirectReturnIifeLeft }: GenerateCodeSnippets,
pluginDriver: PluginDriver
): { helper: string | null; mechanism: DynamicImportMechanism | null } {
const mechanism = pluginDriver.hookFirstSync('renderDynamicImport', [
Expand All @@ -173,16 +161,14 @@ export default class ImportExpression extends NodeBase {
left = `/*#__PURE__*/${helper}(${left}`;
right += ')';
}
left = `Promise.resolve().then(${getDirectReturnFunctionLeft([], {
const { left: functionLeft, right: functionRight } = getDirectReturnFunction([], {
functionReturn: true,
lineBreakIndent: false,
name: null,
t: ''
})}${left}`;
right += `${getDirectReturnFunctionRight({
lineBreakIndent: false,
name: false
})})`;
});
left = `Promise.resolve().then(${functionLeft}${left}`;
right += `${functionRight})`;
if (!arrowFunctions && hasDynamicTarget) {
left = getDirectReturnIifeLeft(['t'], `${left}t${right}`, {
needsArrowReturnParens: false,
Expand All @@ -199,27 +185,26 @@ export default class ImportExpression extends NodeBase {
const resolve = compact ? 'c' : 'resolve';
const reject = compact ? 'e' : 'reject';
const helper = getInteropHelper(resolution, exportMode, interop);
const resolveNamespace = helper
? `${getDirectReturnFunctionLeft(['m'], {
functionReturn: false,
lineBreakIndent: false,
name: null,
t: ''
})}${resolve}(/*#__PURE__*/${helper}(m))${getDirectReturnFunctionRight({
lineBreakIndent: false,
name: false
})}`
: resolve;
let left = `new Promise(${getDirectReturnFunctionLeft([resolve, reject], {
const { left: resolveLeft, right: resolveRight } = getDirectReturnFunction(['m'], {
functionReturn: false,
lineBreakIndent: false,
name: null,
t: ''
})}require([`;
let right = `],${_}${resolveNamespace},${_}${reject})${getDirectReturnFunctionRight({
lineBreakIndent: false,
name: false
})})`;
});
const resolveNamespace = helper
? `${resolveLeft}${resolve}(/*#__PURE__*/${helper}(m))${resolveRight}`
: resolve;
const { left: handlerLeft, right: handlerRight } = getDirectReturnFunction(
[resolve, reject],
{
functionReturn: false,
lineBreakIndent: false,
name: null,
t: ''
}
);
let left = `new Promise(${handlerLeft}require([`;
let right = `],${_}${resolveNamespace},${_}${reject})${handlerRight})`;
if (!arrowFunctions && hasDynamicTarget) {
left = getDirectReturnIifeLeft(['t'], `${left}t${right}`, {
needsArrowReturnParens: false,
Expand Down
84 changes: 46 additions & 38 deletions src/finalisers/shared/getExportBlock.ts
Expand Up @@ -12,20 +12,12 @@ export function getExportBlock(
dependencies: ChunkDependencies,
namedExportsMode: boolean,
interop: GetInterop,
{
_,
cnst,
getDirectReturnFunctionLeft,
getDirectReturnFunctionRight,
getFunctionIntro,
getPropertyAccess,
n,
s
}: GenerateCodeSnippets,
snippets: GenerateCodeSnippets,
t: string,
externalLiveBindings: boolean,
mechanism = 'return '
): string {
const { _, cnst, getDirectReturnFunction, getFunctionIntro, getPropertyAccess, n, s } = snippets;
if (!namedExportsMode) {
return `${n}${n}${mechanism}${getSingleDefaultExport(
exports,
Expand Down Expand Up @@ -63,20 +55,22 @@ export function getExportBlock(
getPropertyAccess
);
if (exportBlock) exportBlock += n;
exportBlock +=
specifier.imported !== '*' && specifier.needsLiveBinding
? `Object.defineProperty(exports,${_}'${specifier.reexported}',${_}{${n}` +
`${t}enumerable:${_}true,${n}` +
`${t}get:${_}${getDirectReturnFunctionLeft([], {
functionReturn: true,
lineBreakIndent: false,
name: null,
t: ''
})}${importName}${getDirectReturnFunctionRight({
lineBreakIndent: false,
name: false
})}${n}});`
: `exports${getPropertyAccess(specifier.reexported)}${_}=${_}${importName};`;
if (specifier.imported !== '*' && specifier.needsLiveBinding) {
const { left, right } = getDirectReturnFunction([], {
functionReturn: true,
lineBreakIndent: false,
name: null,
t: ''
});
exportBlock +=
`Object.defineProperty(exports,${_}'${specifier.reexported}',${_}{${n}` +
`${t}enumerable:${_}true,${n}` +
`${t}get:${_}${left}${importName}${right}${n}});`;
} else {
exportBlock += `exports${getPropertyAccess(
specifier.reexported
)}${_}=${_}${importName};`;
}
}
}
}
Expand All @@ -96,20 +90,12 @@ export function getExportBlock(
for (const specifier of reexports) {
if (specifier.reexported === '*') {
if (exportBlock) exportBlock += n;
const defineProperty = specifier.needsLiveBinding
? `Object.defineProperty(exports,${_}k,${_}{${n}` +
`${t}${t}enumerable:${_}true,${n}` +
`${t}${t}get:${_}${getDirectReturnFunctionLeft([], {
functionReturn: true,
lineBreakIndent: false,
name: null,
t: ''
})}${name}[k]${getDirectReturnFunctionRight({
lineBreakIndent: false,
name: false
})}${n}${t}})`
: `exports[k]${_}=${_}${name}[k]`;
const copyPropertyIfNecessary = `{${n}${t}if${_}(k${_}!==${_}'default'${_}&&${_}!exports.hasOwnProperty(k))${_}${defineProperty}${s}${n}}`;
const copyPropertyIfNecessary = `{${n}${t}if${_}(k${_}!==${_}'default'${_}&&${_}!exports.hasOwnProperty(k))${_}${getDefineProperty(
name,
specifier.needsLiveBinding,
t,
snippets
)}${s}${n}}`;
exportBlock +=
cnst === 'var' && specifier.needsLiveBinding
? `Object.keys(${name}).forEach(${getFunctionIntro(['k'], {
Expand Down Expand Up @@ -233,3 +219,25 @@ export function getNamespaceMarkers(
}
return namespaceMarkers;
}

const getDefineProperty = (
name: string,
needsLiveBinding: boolean,
t: string,
{ _, getDirectReturnFunction, n }: GenerateCodeSnippets
) => {
if (needsLiveBinding) {
const { left, right } = getDirectReturnFunction([], {
functionReturn: true,
lineBreakIndent: false,
name: null,
t: ''
});
return (
`Object.defineProperty(exports,${_}k,${_}{${n}` +
`${t}${t}enumerable:${_}true,${n}` +
`${t}${t}get:${_}${left}${name}[k]${right}${n}${t}})`
);
}
return `exports[k]${_}=${_}${name}[k]`;
};
53 changes: 25 additions & 28 deletions src/utils/generateCodeSnippets.ts
Expand Up @@ -6,16 +6,15 @@ export interface GenerateCodeSnippets {
cnst: string;
n: string;
s: string;
getDirectReturnFunctionLeft(
getDirectReturnFunction(
params: string[],
options: {
functionReturn: boolean;
lineBreakIndent: string | false;
name: string | null;
t: string;
}
): string;
getDirectReturnFunctionRight(options: { lineBreakIndent: string | false; name: boolean }): string;
): { left: string; right: string };
getDirectReturnIifeLeft(
params: string[],
returned: string,
Expand Down Expand Up @@ -59,11 +58,11 @@ export function getGenerateCodeSnippets({
}
: getNonArrowFunctionIntro;

const getDirectReturnFunctionLeft: GenerateCodeSnippets['getDirectReturnFunctionLeft'] = (
const getDirectReturnFunction: GenerateCodeSnippets['getDirectReturnFunction'] = (
params,
{ functionReturn, t, lineBreakIndent, name }
) =>
`${getFunctionIntro(params, {
) => ({
left: `${getFunctionIntro(params, {
isAsync: false,
name
})}${
Expand All @@ -72,15 +71,11 @@ export function getGenerateCodeSnippets({
? `${n}${lineBreakIndent}${t}`
: ''
: `{${lineBreakIndent ? `${n}${lineBreakIndent}${t}` : _}${functionReturn ? 'return ' : ''}`
}`;

const getDirectReturnFunctionRight: GenerateCodeSnippets['getDirectReturnFunctionRight'] = ({
lineBreakIndent,
name
}) =>
arrowFunctions
}`,
right: arrowFunctions
? `${name ? ';' : ''}${lineBreakIndent ? `${n}${lineBreakIndent}` : ''}`
: `${s}${lineBreakIndent ? `${n}${lineBreakIndent}` : _}}`;
: `${s}${lineBreakIndent ? `${n}${lineBreakIndent}` : _}}`
});

const isValidPropName = reservedNamesAsProps
? (name: string): boolean => validPropName.test(name)
Expand All @@ -89,21 +84,23 @@ export function getGenerateCodeSnippets({
return {
_,
cnst,
getDirectReturnFunctionLeft,
getDirectReturnFunctionRight,
getDirectReturnIifeLeft: (params, returned, { needsArrowReturnParens, needsWrappedFunction }) =>
`${wrapIfNeeded(
`${getDirectReturnFunctionLeft(params, {
functionReturn: true,
lineBreakIndent: false,
name: null,
t: ''
})}${wrapIfNeeded(
returned,
arrowFunctions && needsArrowReturnParens
)}${getDirectReturnFunctionRight({ lineBreakIndent: false, name: false })}`,
getDirectReturnFunction,
getDirectReturnIifeLeft: (
params,
returned,
{ needsArrowReturnParens, needsWrappedFunction }
) => {
const { left, right } = getDirectReturnFunction(params, {
functionReturn: true,
lineBreakIndent: false,
name: null,
t: ''
});
return `${wrapIfNeeded(
`${left}${wrapIfNeeded(returned, arrowFunctions && needsArrowReturnParens)}${right}`,
arrowFunctions || needsWrappedFunction
)}(`,
)}(`;
},
getFunctionIntro,
getNonArrowFunctionIntro,
getObject(fields, { lineBreakIndent }) {
Expand Down

0 comments on commit cc2cead

Please sign in to comment.