jakobhellermann / wasm-server-runner

cargo run for the browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use as dependency to serve WASM-only crate examples?

stnbu opened this issue · comments

I work with WASM a lot and it would be great to have a convenient, simple way to serve examples.

Specifically, I'd like to be able to

cargo run --example myexampele

And just have a URL printed that hosts my example WASM blob.

My rust is weak; am I off-base? Does a better strategy exist? It seems like a small amount of refactoring could allow for this.

Have you tried if it works? Because it should, --examples use the same runner configuration as the default cargo run. So if you did step 2 in the readme cargo run --example myexample should already work.

Have you tried if it works? Because it should

It sure does!

cargo run --target wasm32-unknown-unknown
...
    Finished dev [unoptimized + debuginfo] target(s) in 1m 27s
     Running `wasm-server-runner target/wasm32-unknown-unknown/debug/metamask-bevy.wasm`
 INFO wasm_server_runner: compressed wasm output is 14.55mb large
 INFO wasm_server_runner::server: starting webserver at http://127.0.0.1:1334

Thanks for pointing that out.

I was hoping to be able to just include it as a dependency and have my examples "just work" like above. So users don't necessarily have to have the runner registered with cargo. E.g. they could be considering wasm itself and not understand the tooling.

I was thinking more along the lines of having a re-usable module that I can wrap my examples in. Roughly speaking

I'm not how it'd be best implemented.

Maybe cargo run --example --target wasm32-unknown-unknown compiles the wasm but actually executes...

    let output = wasm_bindgen::generate(&the_example_wasm_output)?;
    let rt = tokio::runtime::Runtime::new()?;
    rt.block_on(server::run_server(options, output))?;

That's all me, I guess, but can I get to the above server? How to do that without some small shuffling of this crate evades me. Maybe it's already doable.

Or maybe have cargo install he binary package as-is and overide target.wasm32-unknown-unknown.runner somehow.

Hm, I don't know of a way to make that work without having the user manually install wasm-server-runner and configuring cargo.

What you can do is make cargo run-wasm --example your_example work, by defining a cargo alias of run-wasm to whatever you want in your project's .cargo/config.toml, like how https://github.com/rukai/cargo-run-wasm does it.

also in general if you add the

[target.wasm32-unknown-unknown]
runner = "wasm-server-runner

to your project's .cargo/config.toml, not globally, and include it in the version control, then anyone else cloning your repo won't need to configure the runner, they'll only need to cargo install wasm-server-runner.

The error they get without having wasm-server-runner installed would be

error: could not execute process `wasm-server-runner target/wasm32-unknown-unknown/debug/testingg.wasm` (never executed)
Caused by:
  No such file or directory (os error 2)

Yep. I can definitely bridge those gaps. Thanks for the guidance.