Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: CLI wasm-bindgen-cli Installer #3335

Merged
merged 14 commits into from
Dec 11, 2024
403 changes: 158 additions & 245 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ futures-channel = "0.3.21"
futures-util = { version = "0.3", default-features = false }
rustc-hash = "1.1.0"
wasm-bindgen = "0.2.99"
wasm-bindgen-cli-support = "0.2.99"
wasm-bindgen-shared = "0.2.99"
wasm-bindgen-futures = "0.4.42"
js-sys = "0.3.76"
web-sys = { version = "0.3.76", default-features = false }
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ dioxus-dx-wire-format = { workspace = true }
clap = { workspace = true, features = ["derive", "cargo"] }
convert_case = { workspace = true }
thiserror = { workspace = true }
wasm-bindgen-cli-support = { workspace = true }
wasm-bindgen-shared = { workspace = true }
uuid = { version = "1.3.0", features = ["v4"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
Expand Down Expand Up @@ -120,6 +118,8 @@ strum = { version = "0.26.3", features = ["derive"] }
tauri-utils = { workspace = true }
tauri-bundler = { workspace = true }
include_dir = "0.7.4"
flate2 = "1.0.35"
tar = "0.4.43"

[build-dependencies]
built = { version = "=0.7.4", features = ["git2"] }
Expand Down
42 changes: 23 additions & 19 deletions packages/cli/src/build/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::templates::InfoPlistData;
use crate::wasm_bindgen::WasmBindgenBuilder;
use crate::{BuildRequest, Platform};
use crate::{Result, TraceSrc};
use anyhow::Context;
Expand All @@ -10,9 +12,6 @@ use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::{sync::atomic::AtomicUsize, time::Duration};
use tokio::process::Command;
use wasm_bindgen_cli_support::Bindgen;

use super::templates::InfoPlistData;

/// The end result of a build.
///
Expand Down Expand Up @@ -610,22 +609,27 @@ impl AppBundle {
&& !self.build.build.release;

let start = std::time::Instant::now();
tokio::task::spawn_blocking(move || {
Bindgen::new()
.input_path(&input_path)
.web(true)
.unwrap()
.debug(keep_debug)
.demangle(keep_debug)
.keep_debug(keep_debug)
.remove_name_section(!keep_debug)
.remove_producers_section(!keep_debug)
.out_name(&name)
.generate(&bindgen_outdir)
})
.await
.context("Wasm-bindgen crashed while optimizing the wasm binary")?
.context("Failed to generate wasm-bindgen bindings")?;

let bindgen_version = self
.build
.krate
.wasm_bindgen_version()
.expect("this should have been checked by tool verification");

WasmBindgenBuilder::new(bindgen_version)
.input_path(&input_path)
.target("web")
.debug(keep_debug)
.demangle(keep_debug)
.keep_debug(keep_debug)
.remove_name_section(!keep_debug)
.remove_producers_section(!keep_debug)
.out_name(&name)
.out_dir(&bindgen_outdir)
.build()
.run()
.await
.context("Failed to generate wasm-bindgen bindings")?;

tracing::debug!(dx_src = ?TraceSrc::Bundle, "wasm-bindgen complete in {:?}", start.elapsed());

Expand Down
47 changes: 12 additions & 35 deletions packages/cli/src/build/verify.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::process::Stdio;

use crate::{BuildRequest, Platform, Result, RustupShow};
use anyhow::Context;
use crate::{wasm_bindgen::WasmBindgen, BuildRequest, Platform, Result, RustupShow};
use anyhow::{anyhow, Context};
use tokio::process::Command;

impl BuildRequest {
Expand Down Expand Up @@ -40,6 +38,7 @@ impl BuildRequest {
}

pub(crate) async fn verify_web_tooling(&self, rustup: RustupShow) -> Result<()> {
// Rust wasm32 target
if !rustup.has_wasm32_unknown_unknown() {
tracing::info!(
"Web platform requires wasm32-unknown-unknown to be installed. Installing..."
Expand All @@ -50,38 +49,16 @@ impl BuildRequest {
.await?;
}

let our_wasm_bindgen_version = wasm_bindgen_shared::version();
match self.krate.wasm_bindgen_version() {
Some(version) if version == our_wasm_bindgen_version => {
tracing::debug!("wasm-bindgen version {version} is compatible with dioxus-cli ✅");
},
Some(version) => {
tracing::warn!(
"wasm-bindgen version {version} is not compatible with the cli crate ({}). Attempting to upgrade the target wasm-bindgen crate manually...",
our_wasm_bindgen_version
);

let output = Command::new("cargo")
.args([
"update",
"-p",
"wasm-bindgen",
"--precise",
&our_wasm_bindgen_version,
])
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.output()
.await;

match output {
Ok(output) if output.status.success() => tracing::info!("✅ wasm-bindgen updated successfully"),
Ok(output) => tracing::error!("Failed to update wasm-bindgen: {:?}", output),
Err(err) => tracing::error!("Failed to update wasm-bindgen: {err}"),
}
// Wasm bindgen
let krate_bindgen_version = self.krate.wasm_bindgen_version().ok_or(anyhow!(
"failed to detect wasm-bindgen version, unable to proceed"
))?;

}
None => tracing::debug!("User is attempting a web build without wasm-bindgen detected. This is probably a bug in the dioxus-cli."),
let is_installed = WasmBindgen::verify_install(&krate_bindgen_version).await?;
if !is_installed {
WasmBindgen::install(&krate_bindgen_version)
.await
.context("failed to install wasm-bindgen-cli")?;
}

Ok(())
Expand Down
4 changes: 1 addition & 3 deletions packages/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ mod filemap;
mod logging;
mod metadata;
mod platform;
mod profiles;
mod rustup;
mod serve;
mod settings;
mod slog;
mod tooling;
mod wasm_bindgen;

pub(crate) use build::*;
pub(crate) use cli::*;
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/profiles.rs

This file was deleted.

1 change: 0 additions & 1 deletion packages/cli/src/slog.rs

This file was deleted.

1 change: 0 additions & 1 deletion packages/cli/src/tooling.rs

This file was deleted.

Loading
Loading