From 0f95faad247247c06f37b165bab45da1178f31e4 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Mon, 5 Aug 2024 13:14:56 +0100 Subject: [PATCH] avoid tweaking _ENTRIES if an _ENTRIES declaration is present --- .../dedupeEdgeFunctions.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/next-on-pages/src/buildApplication/processVercelFunctions/dedupeEdgeFunctions.ts b/packages/next-on-pages/src/buildApplication/processVercelFunctions/dedupeEdgeFunctions.ts index 5a9721c6c..541bdfc12 100644 --- a/packages/next-on-pages/src/buildApplication/processVercelFunctions/dedupeEdgeFunctions.ts +++ b/packages/next-on-pages/src/buildApplication/processVercelFunctions/dedupeEdgeFunctions.ts @@ -292,15 +292,22 @@ function iifefyFunctionFile( functionInfo: FunctionInfo, chunksExportsMap: Map>, ): string { + const fileContentsContainEntriesDeclaration = + /(let|var|const)\s+_ENTRIES\b/g.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(/(?