Skip to content

Commit

Permalink
fix wasmImportsToPrepend overriding existing imports (#495)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dario-piotrowicz authored Oct 7, 2023
1 parent 5af0ca3 commit 6600d2a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 12 additions & 0 deletions .changeset/wild-olives-invent.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[]>();
const wasmImportsToPrepend = new Map<string, Set<string>>();

const newFnLocation = join('functions', `${fnInfo.relativePath}.js`);
const newFnPath = join(opts.nopDistDir, newFnLocation);
Expand Down Expand Up @@ -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));
}
}
}
Expand Down Expand Up @@ -218,7 +221,7 @@ type BuildFunctionFileOpts = {
* @param opts Options for processing the function.
*/
async function prependWasmImportsToCodeBlocks(
wasmImportsToPrepend: Map<string, string[]>,
wasmImportsToPrepend: Map<string, Set<string>>,
identifierMaps: Record<IdentifierType, IdentifiersMap>,
{ workerJsDir, nopDistDir }: ProcessVercelFunctionsOpts,
) {
Expand Down

0 comments on commit 6600d2a

Please sign in to comment.