How do I use bevy as wasm in frontend frameworks? #6031
-
Hello, lately I'm thinking about use bevy inside a webview and hence considering the wasm of bevy. I followed the steps in this wasm example and obtained 4 files, e.g.
It seems that However, compiling my custom Rust project into wasm in the same way (i.e. I don't have much experience on next.js or typescript, since I'm using Tauri instead to save me from frontend framework. I don't know how to resolve this problem. Can anyone give some example on loading bevy wasm into Next.js? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
I haven't tried building Bevy for WASM before, but the unofficial cheat book has a page on it that you can check out. They use wasm-bindgen, so maybe you'll have more luck with that? Edit: Looking more into the actual error, it sounds like the WASM module is importing something, so you have to provide an Imports object. Check the generated JS file for the |
Beta Was this translation helpful? Give feedback.
-
Finally it appears that it is the bevy itself that causes the problem since it doesn't support Web Rendering out-of-the-box. Right? Just as the example said. The example notworking either is due to the reason that it depends on crate bevy_webgl2 which strictly restricts `wasm-bindgen = "0.2.69". However, this version of wasm-bindgen has a bug when generating JS file . EditBy switching bevy to |
Beta Was this translation helpful? Give feedback.
-
It all depends on what you are trying to do JS wise. Bevy, when built targeting With that being said, the trick part is on <script type="module">
import init from './a.js'
init()
</script> That's it and If you want to manual compile and instantiate the export { initSync }
export default init;
// Add this:
export { getImports } Then you can just use the import {init, getImports} from './wabi.js' Now just instantiate the module, like you always do, but this time, with the given const imports = getImports();
const module = new WebAssembly.Module(bytes);
const instance = new WebAssembly.Instance(module, imports); |
Beta Was this translation helpful? Give feedback.
It all depends on what you are trying to do JS wise. Bevy, when built targeting
wasm32-unknown-unknown
, is just awasm
module which needs some imports.wasm-bindgen
generate those imports in the*.js
or*.d.ts
, depending if you want to use JS or TS.With that being said, the trick part is on
wasm-bindgen
, since--target web
will generate a self contained JS module, which is intended to be used as follow:That's it and
init
will handle all bootstrap needed bywasm
module, which includes supplying the needed imports.If you want to manual compile and instantiate the
wasm
module, you can tell the*.js
file to expor…