Skip to content

Commit

Permalink
refactor solution further
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz committed Jul 24, 2024
1 parent a73fd67 commit e55428f
Showing 1 changed file with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ async function buildFunctionFile(
}, new Map<string, string>());

let chunkMapIdx = 0;
const chunksMap = new Map<string, Set<string>>();
const chunksExportsMap = new Map<string, Set<string>>();

groupedImports.forEach((keys, path) => {
const relativeImportPath = getRelativePathToAncestor({
Expand All @@ -233,7 +233,7 @@ async function buildFunctionFile(
);

const namedExportsId = `getNamedExports_${chunkMapIdx++}`;
chunksMap.set(namedExportsId, new Set([...keys.split(',')]));
chunksExportsMap.set(namedExportsId, new Set([...keys.split(',')]));
functionImports += `import { getNamedExports as ${namedExportsId} } from '${importPath}';\n`;
});

Expand All @@ -243,7 +243,7 @@ async function buildFunctionFile(
fileContents,
functionImports,
fnInfo,
chunksMap,
chunksExportsMap,
);
const buildPromise = buildFile(finalFileContents, newFnPath, {
relativeTo: nopDistDir,
Expand All @@ -269,14 +269,14 @@ type BuildFunctionFileOpts = {
* @param fileContents the function file's contents
* @param functionImports the imports that need to be added to the file
* @param fnInfo the function's information
* @param chunksMap a map containing getters and chunks being used by the function
* @param chunksExportsMap a map containing getters and chunks identifiers being used by the function
* @returns the updated/iifefied file content
*/
function iffefyFunctionFile(
fileContents: string,
functionImports: string,
fnInfo: FunctionInfo,
chunksMap: Map<string, Set<string>>,
chunksExportsMap: Map<string, Set<string>>,
): string {
const wrappedContent = `
export default ((self, globalThis, global) => {
Expand All @@ -294,12 +294,16 @@ function iffefyFunctionFile(
`const proxy = globalThis.__nextOnPagesRoutesIsolation.getProxyFor('${
fnInfo.route?.path ?? ''
}');`,
[...chunksMap].map(([getNamedExportsId, keys]) => {
return `const exportsOf${getNamedExportsId} = ${getNamedExportsId}(proxy, proxy, proxy);
${[...keys]
.map(key => `const ${key} = exportsOf${getNamedExportsId}["${key}"]`)
.join(';')}`;
}),
[
...[...chunksExportsMap].map(([getNamedExportsId, keys]) => {
return [
`const exportsOf${getNamedExportsId} = ${getNamedExportsId}(proxy, proxy, proxy)`,
[...keys].map(
key => `const ${key} = exportsOf${getNamedExportsId}["${key}"]`,
),
];
}),
],
wrappedContent,
].join(';');
}
Expand Down

0 comments on commit e55428f

Please sign in to comment.