Trunk build output as a cargo dependency
I am working on a rust application that has to serve up a web UI which is also written in rust (egui) and compiled to wasm. Ideally, the wasm code and associated assets would be embedded in the executable with something like rust-embed, and then the executable would serve up both the embedded files as well as a websocket. Does anyone know if there is a nice way to set up cargo to automatically run trunk and build the wasm portion as part of building the server executable?
Not sure it's "nice", but it did do its job: https://github.com/seedwing-io/seedwing-policy/blob/main/server/embedded-frontend/build.rs
I mean, it would be great if trunk had some actual integration with cargo, but short of that I guess effectively running trunk "manually" from build.rs is probably going to be the best option. That file actually doesn't look too bad, I guess I can just delete the NPM stuff.
I have found a couple of references to similar stuff:
https://stackoverflow.com/questions/75824771/how-to-embed-a-wasm-artifact-generated-by-one-crate-in-a-cargo-workspace-in-a-ru : runs wasm-pack from build.rs
https://github.com/inanna-malick/embed-wasm : looks rather nice but also appears dead
Unfortunately that doesn't seem to completely work - trunk build hangs when called from build.rs. I think I am possibly running in to some sort of locking issue with cargo. The complicating factor is that I actually need to build a large portion of the exact same code for BOTH wasm and the server executable (as the executable contains both the server as well as a native client).
Ah, I guess I missed that I had to exclude the package in Cargo.toml. Seems to build just fine now, thanks!
I guess the only remaining problem is that it doesn't detect that any of the dependencies have changed. Without either deeper integration into cargo or some kind of support from cargo (e.g. bindep on the wasm binary or something along those lines) there probably isn't any easy way to fix that.