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

JsValue gets return value undefined #15

Closed fredrikjacobson closed 1 year ago

fredrikjacobson commented 1 year ago

Hi, I'm facing issues when using this package together with wasm-bindgen and having the return type as JsValue. I've tried loading the same wasm module using webpack without any issues, suggesting that the issue is with how the wasm module is loaded.

Changing the signature to return string fixes the issue...

https://github.com/fredrikjacobson/wasm-jsvalue-undefined contains a reproduction where

yarn dev

and checking the console log yields undefined. Running npm start from ./rust-wasm however will print the correct js object.

I would guess that some part of the glue-code from wasm-bindgen somehow gets removed.

Essentially switching from

// This does not work since the value comes back as undefined...
serde_wasm_bindgen::to_value(&features).map_err(|e| JsError::new(&e.to_string()))

to

serde_json::to_string(&features).map_err(|e| JsError::new(&e.to_string()))

fixes the problem by returning string instead.

Any idea for why I'm facing issue using this plugin vs. webpack?

Thanks

Menci commented 1 year ago

Sorry for coming late. As a workaround, add optimizeDeps: { exclude: ["point-cloud-tools"] } to your Vite config.

Menci commented 1 year ago

Fixed in v3.1.0.