How to show a WASM GUI in tauri (with Next.js) #5231
-
Hello, I am trying to show a wasm gui object in tauri. I've no idea how to make this embedding. I have already complied the gui part targeting at Recently, I failed to use the wasm in tauri. I know it is not the problem with tauri, but next.js or typescrite. However, I don't understant next.js well and don't plan to learn it deeply, since I think Rust can solve most cases. Would someone give an short example of combining wasm and tauri? Thanks. // this approach fails to work, Module not found: Can't resolve 'wbg' in *pkg*
const WasmComponent = dynamic({
loader: async () => {
const wasmModule = await import('../public/a.wasm')
wasmModule .start();
return () => <div></div>
},
}) // this approach fails to work, too. TypeError: Cannot read properties of undefined (reading 'instantiate')
import { useEffect, useState } from 'react';
import { AsBind } from 'as-bind';
export const useWasm = () => {
const [state, setState] = useState(null);
useEffect(() => {
const fetchWasm = async () => {
const wasm = await fetch('a.wasm');
const instance = await AsBind.instantiate(wasm, {});
setState(instance);
};
fetchWasm();
}, []);
return state;
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
The second example looks almost correct i think. I did basically the same but without as-bind (which is for assemblyscript?). // The file's location is `/public/whatever.wasm`. The public folder mechanism may differ depending on the frontend stack.
const response = await fetch('/whatever.wasm');
const bytes = await response.arrayBuffer();
const { module, instance } = await WebAssembly.instantiate(bytes);
const add = instance.exports.add as CallableFunction;
console.log(add(12, 34)); |
Beta Was this translation helpful? Give feedback.
-
I'd like to share the follow-up investigation on this problem. Since I don't know much on rendering or frontend, I'm only 80% sure. Loading wasmFabianLars has provided the correct way, as the Answer shown above. Another way is using the JS file, that is generated together with the .wasm file. Suppose you have already put the JS file (
Version of wasm-bindgenOlder version of wasm-bindgen has bugs in generating JS files, so remember to use bug-free versions. |
Beta Was this translation helpful? Give feedback.
The second example looks almost correct i think. I did basically the same but without as-bind (which is for assemblyscript?).
iirc it was something like this