-
Notifications
You must be signed in to change notification settings - Fork 26
Description
Currently the wasmHelper code tries to load the wasm file through fetch. This only works in a browser context. Even if fetch were available, fetching a relative path would still not work. I believe it should be fairly easy to get it working in a server context as well. One easy fix would be to add an else if clause in the wasmHelper function:
const wasmHelper = async (opts = {}, url: string) => {
let result: WebAssembly.WebAssemblyInstantiatedSource;
if (url.startsWith("data:")) {
// Existing base64 case
else if (isServer()) {
// New server case. Wasm file should be loaded from file system.
} else {
// Existing fetch case
}
return result.instance.exports;
};The isServer check could be as simple as typeof window === "undefined". However, I could see cases where this would be problematic. Some might have other vite plugins that polyfills window for server contexts etc. Another approach might be to use import.meta.url, and see if it refers to a URL or a file, e.g. import.meta.url.startsWith("file:"). I'm not sure if this would always work, or if it might come with some gotchas.
Are you open to a PR that implements something like this?