From 6600d2a30aa5b63644ee8ff341e41b17053adc00 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Sat, 7 Oct 2023 23:33:20 +0100 Subject: [PATCH] fix wasmImportsToPrepend overriding existing imports (#495) * fix wasmImportsToPrepend overriding existing imports when dealing with wasms that have more than 1 consumer, when we collect the wasm imports to prepend for a specific edge funcion we're always re-setting the array of wasm imports instead of appending them, this causes edge functions to always only consider the last wasm import, fix such issue resolves #494 --- .changeset/wild-olives-invent.md | 12 ++++++++++++ .../processVercelFunctions/dedupeEdgeFunctions.ts | 15 +++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 .changeset/wild-olives-invent.md diff --git a/.changeset/wild-olives-invent.md b/.changeset/wild-olives-invent.md new file mode 100644 index 000000000..175e85710 --- /dev/null +++ b/.changeset/wild-olives-invent.md @@ -0,0 +1,12 @@ +--- +'@cloudflare/next-on-pages': patch +--- + +fix wasms not always getting imported when necessary + +Details: +when dealing with wasms that have more than 1 consumer, when we collect +the wasm imports to prepend for a specific edge funcion we're always +re-setting the array of wasm imports instead of appending them, this +causes edge functions to always only consider the last wasm +import, the changes here fix such behavior diff --git a/packages/next-on-pages/src/buildApplication/processVercelFunctions/dedupeEdgeFunctions.ts b/packages/next-on-pages/src/buildApplication/processVercelFunctions/dedupeEdgeFunctions.ts index 47e7b05d3..ebe4eb8a4 100644 --- a/packages/next-on-pages/src/buildApplication/processVercelFunctions/dedupeEdgeFunctions.ts +++ b/packages/next-on-pages/src/buildApplication/processVercelFunctions/dedupeEdgeFunctions.ts @@ -89,7 +89,7 @@ async function processFunctionIdentifiers( // Tracks the imports to prepend to the final code for the function and identifiers. const importsToPrepend: NewImportInfo[] = []; - const wasmImportsToPrepend = new Map(); + const wasmImportsToPrepend = new Map>(); const newFnLocation = join('functions', `${fnInfo.relativePath}.js`); const newFnPath = join(opts.nopDistDir, newFnLocation); @@ -121,10 +121,13 @@ async function processFunctionIdentifiers( if (newFilePath) identifierPathsToBuild.add(newFilePath); if (newImport) importsToPrepend.push(newImport); if (wasmImports.length) { - wasmImportsToPrepend.set( - identifierInfo.newDest as string, - wasmImports, - ); + const newDest = identifierInfo.newDest as string; + if (!wasmImportsToPrepend.get(newDest)) { + wasmImportsToPrepend.set(newDest, new Set()); + } + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const destImports = wasmImportsToPrepend.get(newDest)!; + wasmImports.forEach(wasmImport => destImports.add(wasmImport)); } } } @@ -218,7 +221,7 @@ type BuildFunctionFileOpts = { * @param opts Options for processing the function. */ async function prependWasmImportsToCodeBlocks( - wasmImportsToPrepend: Map, + wasmImportsToPrepend: Map>, identifierMaps: Record, { workerJsDir, nopDistDir }: ProcessVercelFunctionsOpts, ) {