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

Memory import from wasm produces an error #28

Closed BLaZeKiLL closed 1 year ago

BLaZeKiLL commented 1 year ago

wasm-pack creates a <crate>_bg.wasm.t.ds file which contains the typedef for the web assembly linear memory and other low-level stuff, to access this memory I am using the following import statement

import { memory } from 'game-of-life/game_of_life_bg.wasm';

This works fine in prod mode, but when I start the dev server I get the following error.

As a workaround, I remove the '.wasm' in the end which allows the server to start and then I add it back performing a hot reload which makes the app work normally.

In the error log the ENOENT is trying to look for the file at the wrong location, as it should be under node_modules.

LOG: ➜ Local: http://localhost:4200/ ✘ [ERROR] [plugin vite-plugin-wasm] ENOENT: no such file or directory, open '/Users/codeblaze/src/rust/game-of-life/www/apps/app-game-of-life/src/app/game-of-life/game_of_life_bg.wasm'

apps/app-game-of-life/src/app/app.element.ts:3:23:
  3 │ import { memory } from 'game-of-life/game_of_life_bg.wasm';
    ╵                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This error came from the "onLoad" callback registered here:

node_modules/vite-plugin-wasm/dist/esbuild-plugin.js:40:18:
  40 │             build.onLoad({ filter: /.*/, namespace: NAMESPACE }, async (args) => {
     ╵                   ~~~~~~

at setup (/Users/codeblaze/src/rust/game-of-life/www/node_modules/vite-plugin-wasm/dist/esbuild-plugin.js:40:19)
at handlePlugins (/Users/codeblaze/src/rust/game-of-life/www/node_modules/esbuild/lib/main.js:1287:21)
at buildOrServeImpl (/Users/codeblaze/src/rust/game-of-life/www/node_modules/esbuild/lib/main.js:974:5)
at Object.buildOrServe (/Users/codeblaze/src/rust/game-of-life/www/node_modules/esbuild/lib/main.js:780:5)
at /Users/codeblaze/src/rust/game-of-life/www/node_modules/esbuild/lib/main.js:2132:17
at new Promise (<anonymous>)
at Object.build (/Users/codeblaze/src/rust/game-of-life/www/node_modules/esbuild/lib/main.js:2131:14)
at build (/Users/codeblaze/src/rust/game-of-life/www/node_modules/esbuild/lib/main.js:1978:51)
at scanImports (file:///Users/codeblaze/src/rust/game-of-life/www/node_modules/vite/dist/node/chunks/dep-ca21228b.js:41785:11)

Build failed with 1 error: apps/app-game-of-life/src/app/app.element.ts:3:23: ERROR: [plugin: vite-plugin-wasm] ENOENT: no such file or directory, open '/Users/codeblaze/src/rust/game-of-life/www/apps/app-game-of-life/src/app/game-of-life/game_of_life_bg.wasm'

Menci commented 1 year ago

This is a known bug. I'll fix it when I have more time.

Menci commented 1 year ago

To workaround it, try disable optimizeDeps

Menci commented 1 year ago

Try v3.2.2

lafkpages commented 1 year ago

Not sure if I should make this a separate issue, but if I do:

import { greet } from 'wasm'

This gives an error on first page load/full reload. If I remove it and re-add it, HMR seems to fix it, and everything works fine. I'm on v3.2.2 and using wasm-pack. I've also excluded the wasm module from optimizeDeps.

The error is:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".wasm"