Menci / vite-plugin-wasm

Add WebAssembly ESM integration (aka. Webpack's `asyncWebAssembly`) to Vite and support `wasm-pack` generated modules.
MIT License
281 stars 16 forks source link

[SvelteKit] Importing a WASM module created by wasm-pack from a worker doen't include path prefix #25

Closed TimoWilhelm closed 1 year ago

TimoWilhelm commented 1 year ago

I have a SvelteKit application where I would like to use some WASM modules created by wasm-pack from a WebWorker. When trying to import the module, the assets path prefix used by SvelteKit (_app/immutable/assets) is not inculded in the WASM chunk.

I created a minimal reproducible example of this isse here: https://github.com/TimoWilhelm/mre-svelte-worker-wasm-import-path In this repo, i import the wasm once from the normal app page and once from a worker module.

If you check the network tab, you can see that the WASM module imported from the worker is missing the prefix (and the Cache-Control headers).

From Svelte Page:

JS Request URL: Request URL: http://localhost:4173/_app/immutable/chunks/hello_wasm-ff822b29.js WASM Request URL: http://localhost:4173/_app/immutable/assets/hello_wasm_bg-44499581.wasm

From Worker:

JS Request URL: Request URL: http://localhost:4173/_app/immutable/workers/chunks/hello_wasm-968d1c54.js WASM Request URL: http://localhost:4173/assets/hello_wasm_bg-44499581.wasm (also missing Cache-Control headers)

To be honest, I'm not quite sure if this issue belongs here or with the Svelte plugin.

image

Menci commented 1 year ago

Thanks very much! I'll look into it when I have time.

TimoWilhelm commented 1 year ago

I've taken another look and I don't think this issue is related to this plugin at all.

This issue https://github.com/vitejs/vite/issues/7613 says that the web worker imports don't respect rollupOptions.output.assetFileNames. If I set those manually for workers in my vite config, I can overwrite the prefix and it seems to work just fine.

The problem is, that the default config for SvelteKit doesn't specify assetFileNames for workers: https://github.com/sveltejs/kit/blob/b2c464c187172c59061b198b3f6ce8254b6c5592/packages/kit/src/exports/vite/build/utils.js#L164.