Files
marqdo/src/wasm_cli.rs
T
cflmyandCursor 04890e0c73 feat(wasm): C5 release-wasm size profile and build reporting
Add opt-level=z/LTO strip profile, print KiB from marqdo wasm build,
optional wasm-opt, and document tree-only browser sessions.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-01 10:58:42 +08:00

118 lines
3.4 KiB
Rust

//! `marqdo wasm build` — compile `marqdo-wasm` and copy the `.wasm` artifact.
use std::path::{Path, PathBuf};
use std::process::Command;
use anyhow::{bail, Context, Result};
/// Build size-optimized wasm32 artifact and copy `marqdo_wasm.wasm` into `out_dir`.
pub fn build_wasm(out_dir: &Path) -> Result<()> {
let status = Command::new("cargo")
.args([
"build",
"-p",
"marqdo-wasm",
"--target",
"wasm32-unknown-unknown",
"--profile",
"release-wasm",
])
.status()
.context("failed to spawn cargo (is it on PATH?)")?;
if !status.success() {
bail!("cargo build -p marqdo-wasm --profile release-wasm failed with {status}");
}
let mut artifact = find_wasm_artifact()?;
std::fs::create_dir_all(out_dir)
.with_context(|| format!("create {}", out_dir.display()))?;
let dest = out_dir.join("marqdo_wasm.wasm");
if let Some(optimized) = try_wasm_opt(&artifact)? {
artifact = optimized;
}
std::fs::copy(&artifact, &dest).with_context(|| {
format!(
"copy {} → {}",
artifact.display(),
dest.display()
)
})?;
let bytes = std::fs::metadata(&dest)
.map(|m| m.len())
.unwrap_or(0);
println!(
"wrote {} ({:.1} KiB)",
dest.display(),
bytes as f64 / 1024.0
);
Ok(())
}
/// Run `wasm-opt -Oz` when Binaryen is installed; returns path to optimized file.
fn try_wasm_opt(input: &Path) -> Result<Option<PathBuf>> {
let which = Command::new("wasm-opt").arg("--version").output();
let Ok(out) = which else {
return Ok(None);
};
if !out.status.success() {
return Ok(None);
}
let tmp = input.with_extension("opt.wasm");
let status = Command::new("wasm-opt")
.args(["-Oz", "--enable-bulk-memory"])
.arg(input)
.arg("-o")
.arg(&tmp)
.status()
.context("wasm-opt spawn failed")?;
if !status.success() {
// Retry without bulk-memory flag for older binaryen.
let status2 = Command::new("wasm-opt")
.args(["-Oz"])
.arg(input)
.arg("-o")
.arg(&tmp)
.status()
.context("wasm-opt spawn failed")?;
if !status2.success() {
eprintln!("warning: wasm-opt failed; keeping cargo artifact");
return Ok(None);
}
}
println!("wasm-opt: {}", tmp.display());
Ok(Some(tmp))
}
fn find_wasm_artifact() -> Result<PathBuf> {
let mut candidates = Vec::new();
if let Ok(td) = std::env::var("CARGO_TARGET_DIR") {
candidates.push(
PathBuf::from(&td).join("wasm32-unknown-unknown/release-wasm/marqdo_wasm.wasm"),
);
candidates.push(
PathBuf::from(td).join("wasm32-unknown-unknown/release/marqdo_wasm.wasm"),
);
}
candidates.push(PathBuf::from(
"target/wasm32-unknown-unknown/release-wasm/marqdo_wasm.wasm",
));
candidates.push(PathBuf::from(
"target/wasm32-unknown-unknown/release/marqdo_wasm.wasm",
));
for c in &candidates {
if c.is_file() {
return Ok(c.clone());
}
}
bail!(
"built wasm not found; tried: {}",
candidates
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join(", ")
)
}