Skip to content

Commit

Permalink
fix(analyze): Leverage string interpolation to import Wasm files on e…
Browse files Browse the repository at this point in the history
…dge runtime (#784)

While working on #775, I found that Vite doesn't support the `?module` syntax that we use for loading Wasm files in the edge runtime. Instead, we can use the `/* @vite-ignore */` comment but it only applies to imports using string interpolation.

I checked the output of `next build` and it seems to prepare all the `.wasm` files in our `./wasm/` directory as "edge chunks" when I use this syntax, so I think we should be good in both scenarios.
  • Loading branch information
blaine-arcjet committed May 20, 2024
1 parent e613372 commit 9b85908
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions analyze/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@ async function moduleFromPath(path: string): Promise<WebAssembly.Module> {
}

if (process.env["NEXT_RUNTIME"] === "edge") {
const wasmPrefix = "arcjet_analyze_js_req.component";
if (path === "arcjet_analyze_js_req.component.core.wasm") {
const mod = await import(
"./wasm/arcjet_analyze_js_req.component.core.wasm?module"
/* @vite-ignore */
`./wasm/${wasmPrefix}.core.wasm?module`
);
wasmCache.set(path, mod.default);
return mod.default;
}
if (path === "arcjet_analyze_js_req.component.core2.wasm") {
const mod = await import(
"./wasm/arcjet_analyze_js_req.component.core2.wasm?module"
/* @vite-ignore */
`./wasm/${wasmPrefix}.core2.wasm?module`
);
wasmCache.set(path, mod.default);
return mod.default;
}
if (path === "arcjet_analyze_js_req.component.core3.wasm") {
const mod = await import(
"./wasm/arcjet_analyze_js_req.component.core3.wasm?module"
/* @vite-ignore */
`./wasm/${wasmPrefix}.core3.wasm?module`
);
wasmCache.set(path, mod.default);
return mod.default;
Expand Down

0 comments on commit 9b85908

Please sign in to comment.