leptos-rs / cargo-leptos

Build tool for Leptos (Rust)
MIT License
311 stars 85 forks source link

How to set the frontend library compile into wasm32 #273

Closed GISerliang closed 1 week ago

GISerliang commented 1 week ago

I want use leptos_dom::Mountable, but this trait is available for wasm32 arch like below picture. image

I also set toolchain in rust-toolchain.toml like targets = ["wasm32-unknown-unknown"], but I run cargo leptos watch command got an error.

I want to know how to set the frontend library compile into wasm32?

gbj commented 1 week ago

I'm confused by the question, to be honest; as far as I know it will already compile the lib crate to wasm32. Maybe you could provide some example code and what the error is?

GISerliang commented 1 week ago

I'm confused by the question, to be honest; as far as I know it will already compile the lib crate to wasm32. Maybe you could provide some example code and what the error is?

image

gbj commented 1 week ago

Right, so placing it in the app.rs that is used by both the browser/client-side (compiling from lib.rs) and from the server-side (compiling from main.rs) means that it is not available when compiling the server, hence the error.

You should consider feature-gating the import with something like #[cfg(feature = "hydrate")]

GISerliang commented 1 week ago

Right, so placing it in the app.rs that is used by both the browser/client-side (compiling from lib.rs) and from the server-side (compiling from main.rs) means that it is not available when compiling the server, hence the error.

You should consider feature-gating the import with something like #[cfg(feature = "hydrate")]

Thanks. I wiil try this method. But I also want to know could I only compile the browser/client-side?

gbj commented 1 week ago

Thanks. I wiil try this method. But I also want to know could I only compile the browser/client-side?

Do you mean if you don't want a server at all? Yes, but then the cargo-leptos setup you're using is not the best, as it's specifically designed to build the server and the browser halves at once. If you're not actually using the server, then just use Trunk or wasm-pack and build a client-side app.

GISerliang commented 1 week ago

Do you mean if you don't want a server at all? Yes, but then the cargo-leptos setup you're using is not the best, as it's specifically designed to build the server and the browser halves at once. If you're not actually using the server, then just use Trunk or wasm-pack and build a client-side app.

I understand you means. Thanks.