0
votes

Im new to rust and trying to have a play with web assembly.

I have created a simple app using yew which builds using trunk, this works as expected. I have then created a simple html server using actix, this also is confirmed working as expected.

The problem I have is that if the actix package is included in the cargo dependencies the wasm build fails (this seems reasonable - certainly in a browser build context).

I would prefer not to split this into mutiple crates - at least whilst im prototyping, so im hoping there is a way to set up 2 build pipelines or make a dependency conditional - looking for advice on how best to do this.

Project setup is as follows:

Cargo.toml
dist/
src/
  frontend.rs # call to frontend code from non-main function
  main.rs # fn main() here - can be empty function for the frontend
  frontend/
    mod.rs
    app.rs # no actix dependency
  shared/
    mod.rs
    shared.rs # no actix dependency
  server/
    mod.rs
    server.rs # this has the actix dependency

at the moment i have a situation where the following cargo.toml and main will run the frontend:

main.rs:

fn main() {}

Cargo.toml:

[dependencies]
seed = "0.8.0"
external_shared_stuff = "0.2.0"

and the following will run the server:

main.rs:

mod server;

fn main() -> std::io::Result<()> {
    server::server::serve()
}

Cargo.toml:

[dependencies]
seed = "0.8.0" //not required but no issue - would prefer it wasnt included
external_shared_stuff = "0.2.0"
actix-web = "3"

I currently use trunk to build/serve for the frontend and cargo run for the server. What is the best way to configure it so that both builds work and can I do it without duplicating the shared dependencies? Is it possible to make a dependency conditional on the build target?

Thanks in advance for your help

1
Even if you're prototyping, I'd strongly advise that you use different crates organized in a cargo workspace. Setting up a shared library used by multiple crates is not hard to do.kmdreko
yeh, i do kind of feel like im swimming against the stream - but if all goes to plan 90% of the project will be shared code and deps, and the versions will need to line up for it to work properly so it seems like splitting would create unecessary complications - if possible I would prefer to avoid it for the momentSwiftD

1 Answers

1
votes

I'm hoping there is a way to ... make a dependency conditional

See Specifying Dependencies in the Cargo Book for configuring conditional dependencies in Cargo.toml. In your case you would want something like:

[dependencies]
seed = "0.8.0"
external_shared_stuff = "0.2.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
actix-web = "3"