| 1 | import { fileURLToPath } from "node:url"; |
| 2 | import { defineConfig } from "vitest/config"; |
| 3 | |
| 4 | // The path is base64-encoded so the id does not look like a node_modules file, which vitest would hand to Node unprocessed. |
| 5 | const PREFIX = "virtual:wasm-module/"; |
| 6 | |
| 7 | export default defineConfig({ |
| 8 | resolve: { alias: { "cloudflare:workflows": fileURLToPath(new URL("./test/unit/stubs/cloudflare-workflows.ts", import.meta.url)) } }, |
| 9 | plugins: [ |
| 10 | { |
| 11 | // `import mod from "x.wasm?module"` gives a compiled WebAssembly.Module, like wrangler's CompiledWasm rule in the Worker. |
| 12 | name: "diffinery:wasm-module", |
| 13 | enforce: "pre", |
| 14 | async resolveId(source, importer) { |
| 15 | if (!source.endsWith(".wasm?module")) return null; |
| 16 | const file = await this.resolve(source.slice(0, -"?module".length), importer, { skipSelf: true }); |
| 17 | return file ? PREFIX + Buffer.from(file.id).toString("base64url") : null; |
| 18 | }, |
| 19 | load(id) { |
| 20 | if (!id.startsWith(PREFIX)) return null; |
| 21 | return `import { readFileSync } from "node:fs";\nexport default new WebAssembly.Module(readFileSync(${JSON.stringify(Buffer.from(id.slice(PREFIX.length), "base64url").toString())}));`; |
| 22 | }, |
| 23 | }, |
| 24 | ], |
| 25 | }); |