Skip to content

Commit

Permalink
avoid tweaking _ENTRIES if an _ENTRIES declaration is present (cl…
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz authored Aug 5, 2024
1 parent 2115b9e commit dac5be6
Showing 1 changed file with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,22 @@ function iifefyFunctionFile(
functionInfo: FunctionInfo,
chunksExportsMap: Map<string, Set<string>>,
): string {
const fileContentsContainEntriesDeclaration =
/(let|var|const)\s+_ENTRIES\s*=/.test(fileContents);

// it looks like there can be direct references to _ENTRIES (i.e. `_ENTRIES` instead of `globalThis._ENTRIES` etc...)
// we have to update all such references otherwise our proxying won't take effect on those, but only if the file doesn't
// actually declare _ENTRIES itself (as it happens in older Vercel CLI version (v31 and older))
if (!fileContentsContainEntriesDeclaration) {
fileContents = fileContents.replace(
/([^.])_ENTRIES/g,
'$1globalThis._ENTRIES',
);
}

const wrappedContent = `
export default ((self, globalThis, global) => {
${fileContents
// it looks like there can be direct references to _ENTRIES (i.e. `_ENTRIES` instead of `globalThis._ENTRIES` etc...)
// we have to update all such references otherwise our proxying won't take effect on those
// (note: older versions of the Vercel CLI (v31 and older) used to declare _ENTRIES as "let _ENTRIES = {};", so we do
// need to make sure that we don't add `globalThis.` in these cases (if we were to drop support for those older versions
// the below line to: ".replace(/([^.])_ENTRIES/g, '$1globalThis._ENTRIES')")
.replace(/(?<!(let)\s*)([^.]|^)_ENTRIES/g, '$2globalThis._ENTRIES')
// the default export needs to become the return value of the iife, which is then re-exported as default
.replace(/export\s+default\s+/g, 'return ')}
})(proxy, proxy, proxy);
Expand Down

0 comments on commit dac5be6

Please sign in to comment.