Skip to content

Commit 18fa891

Browse files
authored
Add Wasm helper (#8587)
* Added wasm-helper plugin * Added changeset * Fix plugin name
1 parent 36ae02c commit 18fa891

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

.changeset/eighty-glasses-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
Add support for `.wasm?init` extension to load WebAssembly as documented by Vite (https://vite.dev/guide/features.html#webassembly).

packages/vite-plugin-cloudflare/playground/additional-modules/__tests__/additional-modules.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ test("supports CompiledWasm modules with a '.wasm' extension", async () => {
2828
});
2929

3030
test("supports CompiledWasm modules with a '.wasm?module' extension", async () => {
31-
const result = await getJsonResponse("/wasm-with-param");
31+
const result = await getJsonResponse("/wasm-with-module-param");
3232
expect(result).toEqual({ result: 11 });
3333
});
34+
35+
test("supports CompiledWasm modules with a '.wasm?init' extension", async () => {
36+
const result = await getJsonResponse("/wasm-with-init-param");
37+
expect(result).toEqual({ result: 15 });
38+
});

packages/vite-plugin-cloudflare/playground/additional-modules/src/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import bin from "./modules/bin-example.bin";
22
import html from "./modules/html-example.html";
33
import text from "./modules/text-example.txt";
44
import wasm from "./modules/wasm-example.wasm";
5-
import wasmWithParam from "./modules/wasm-example.wasm?module";
5+
import init from "./modules/wasm-example.wasm?init";
6+
import wasmWithModuleParam from "./modules/wasm-example.wasm?module";
67

78
export default {
89
async fetch(request) {
@@ -26,12 +27,18 @@ export default {
2627

2728
return Response.json({ result });
2829
}
29-
case "/wasm-with-param": {
30-
const instance = await WebAssembly.instantiate(wasmWithParam);
30+
case "/wasm-with-module-param": {
31+
const instance = await WebAssembly.instantiate(wasmWithModuleParam);
3132
const result = instance.exports.add(5, 6);
3233

3334
return Response.json({ result });
3435
}
36+
case "/wasm-with-init-param": {
37+
const instance = await init();
38+
const result = instance.exports.add(7, 8);
39+
40+
return Response.json({ result });
41+
}
3542
default: {
3643
return new Response(null, { status: 404 });
3744
}

packages/vite-plugin-cloudflare/src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,26 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
341341
});
342342
},
343343
},
344+
// Plugin to support `.wasm?init` extension
345+
{
346+
name: "vite-plugin-cloudflare:wasm-helper",
347+
enforce: "pre",
348+
applyToEnvironment(environment) {
349+
return getWorkerConfig(environment.name) !== undefined;
350+
},
351+
load(id) {
352+
if (!id.endsWith(".wasm?init")) {
353+
return;
354+
}
355+
356+
return `
357+
import wasm from "${cleanUrl(id)}";
358+
export default function(opts = {}) {
359+
return WebAssembly.instantiate(wasm, opts);
360+
}
361+
`;
362+
},
363+
},
344364
// Plugin to support additional modules
345365
{
346366
name: "vite-plugin-cloudflare:additional-modules",

0 commit comments

Comments
 (0)