2026-10-07 19:09:43 +00:00
|
|
|
// Builds the Leptos UI (ui/) to wasm and generates the JS bindings, so a plain `cargo run` works.
|
|
|
|
|
// The result lands in OUT_DIR/ui-pkg and is embedded into the binary by src/main.rs.
|
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
use std::{env, fs};
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
for p in ["ui/src", "ui/js", "ui/style.css", "ui/Cargo.toml"] {
|
|
|
|
|
println!("cargo:rerun-if-changed={p}");
|
|
|
|
|
}
|
|
|
|
|
let out = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR"));
|
|
|
|
|
let pkg = out.join("ui-pkg");
|
|
|
|
|
let release = env::var("PROFILE").is_ok_and(|p| p == "release");
|
|
|
|
|
|
|
|
|
|
ensure_wasm_target();
|
|
|
|
|
|
|
|
|
|
// A separate target dir avoids deadlocking on the outer build's lock.
|
|
|
|
|
let target_dir = out.join("ui-target");
|
|
|
|
|
let mut cargo = Command::new(env::var_os("CARGO").unwrap_or_else(|| "cargo".into()));
|
|
|
|
|
cargo
|
|
|
|
|
.args([
|
|
|
|
|
"build",
|
|
|
|
|
"--manifest-path",
|
|
|
|
|
"ui/Cargo.toml",
|
|
|
|
|
"--target",
|
|
|
|
|
"wasm32-unknown-unknown",
|
|
|
|
|
])
|
|
|
|
|
.env("CARGO_TARGET_DIR", &target_dir)
|
|
|
|
|
// Host rustflags (e.g. -fuse-ld=lld) break the wasm link.
|
|
|
|
|
.env_remove("CARGO_ENCODED_RUSTFLAGS")
|
|
|
|
|
.env_remove("RUSTFLAGS");
|
|
|
|
|
if release {
|
|
|
|
|
cargo.arg("--release");
|
|
|
|
|
}
|
|
|
|
|
let status = cargo
|
|
|
|
|
.status()
|
|
|
|
|
.expect("failed to run cargo for the ui crate");
|
2026-10-11 19:23:50 +00:00
|
|
|
assert!(
|
|
|
|
|
status.success(),
|
|
|
|
|
"building ui/ failed (see the cargo output above); the wasm32-unknown-unknown target must be installed: rustup target add wasm32-unknown-unknown"
|
|
|
|
|
);
|
2026-10-07 19:09:43 +00:00
|
|
|
|
|
|
|
|
let profile = if release { "release" } else { "debug" };
|
|
|
|
|
let wasm = target_dir
|
|
|
|
|
.join("wasm32-unknown-unknown")
|
|
|
|
|
.join(profile)
|
|
|
|
|
.join("ui.wasm");
|
|
|
|
|
let _ = fs::remove_dir_all(&pkg);
|
|
|
|
|
wasm_bindgen_cli_support::Bindgen::new()
|
|
|
|
|
.input_path(&wasm)
|
|
|
|
|
.web(true)
|
|
|
|
|
.expect("bindgen target")
|
|
|
|
|
.out_name("ui")
|
|
|
|
|
.typescript(false)
|
|
|
|
|
.generate(&pkg)
|
|
|
|
|
.expect("wasm-bindgen failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ensure_wasm_target() {
|
|
|
|
|
let rustc = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
|
|
|
|
|
let sysroot = Command::new(rustc)
|
|
|
|
|
.args(["--print", "sysroot"])
|
|
|
|
|
.output()
|
|
|
|
|
.ok()
|
|
|
|
|
.and_then(|o| String::from_utf8(o.stdout).ok())
|
|
|
|
|
.map(|s| PathBuf::from(s.trim()));
|
|
|
|
|
let installed = sysroot
|
|
|
|
|
.as_deref()
|
|
|
|
|
.is_some_and(|s: &Path| s.join("lib/rustlib/wasm32-unknown-unknown").exists());
|
|
|
|
|
if !installed {
|
|
|
|
|
let _ = Command::new("rustup")
|
|
|
|
|
.args(["target", "add", "wasm32-unknown-unknown"])
|
|
|
|
|
.status();
|
|
|
|
|
}
|
|
|
|
|
}
|