diff --git a/Dioxus.toml b/Dioxus.toml index 2db611914..c3e0dd8a0 100644 --- a/Dioxus.toml +++ b/Dioxus.toml @@ -42,4 +42,4 @@ script = [] # use binaryen.wasm-opt for output Wasm file # binaryen just will trigger in `web` platform -binaryen = { wasm_opt = [".PROJECT_OUT"] } \ No newline at end of file +binaryen = { wasm_opt = true } \ No newline at end of file diff --git a/src/builder.rs b/src/builder.rs index 708e1ce69..870e737f0 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -82,19 +82,42 @@ pub fn build(config: &CrateConfig) -> Result<()> { bindgen_builder .input_path(input_path) - .web(true).unwrap() + .web(true) + .unwrap() .debug(true) .demangle(true) .keep_debug(true) .remove_name_section(false) .remove_producers_section(false) .out_name(&dioxus_config.application.name) - .generate(&bindgen_outdir).unwrap(); + .generate(&bindgen_outdir) + .unwrap(); }); if bindgen_result.is_err() { log::error!("Bindgen build failed! \nThis is probably due to the Bindgen version, dioxus-cli using `0.2.79` Bindgen crate."); } + // check binaryen:wasm-opt tool + let dioxus_tools = dioxus_config.application.tools.clone().unwrap_or_default(); + if dioxus_tools.contains_key("binaryen") { + let info = dioxus_tools.get("binaryen").unwrap(); + let binaryen = crate::tools::Tool::Binaryen; + if let Some(sub) = info.as_table() { + println!("sub: {sub:?}"); + if sub.contains_key("wasm_opt") + && sub.get("wasm_opt").unwrap().as_bool().unwrap_or(false) + { + let target_file = out_dir + .join("assets") + .join("dioxus") + .join(format!("{}_bg.wasm", dioxus_config.application.name)); + println!("tf: {target_file:?}"); + if target_file.is_file() { + binaryen.call("wasm-opt", vec![target_file.to_str().unwrap(), "--print"])?; + } + } + } + } // this code will copy all public file to the output dir let copy_options = fs_extra::dir::CopyOptions { diff --git a/src/cli/build/mod.rs b/src/cli/build/mod.rs index 4c043091f..3b4d343af 100644 --- a/src/cli/build/mod.rs +++ b/src/cli/build/mod.rs @@ -12,6 +12,7 @@ impl Build { pub fn build(self) -> Result<()> { let mut crate_config = crate::CrateConfig::new()?; + // change the release state. crate_config.with_release(self.build.release); diff --git a/src/tools.rs b/src/tools.rs index b5c9d658a..b84cd5579 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -1,6 +1,7 @@ use std::{ fs::{create_dir_all, File}, path::PathBuf, + process::Command, }; use anyhow::Context; @@ -47,7 +48,6 @@ pub fn tools_path() -> PathBuf { #[allow(clippy::should_implement_trait)] impl Tool { - /// from str to tool enum pub fn from_str(name: &str) -> Option { match name { @@ -152,12 +152,36 @@ impl Tool { let mut archive = Archive::new(tar); archive.unpack(&tool_path)?; // println!("{:?} -> {:?}", tool_path.join(dir_name), tool_path.join(self.name())); - std::fs::rename( - tool_path.join(dir_name), - tool_path.join(self.name()), - )?; + std::fs::rename(tool_path.join(dir_name), tool_path.join(self.name()))?; } Ok(()) } + + pub fn call(&self, command: &str, args: Vec<&str>) -> anyhow::Result<()> { + let bin_path = tools_path().join(self.name()).join(self.bin_path()); + + let command_file = match self { + Tool::Binaryen => { + if cfg!(target_os = "windows") { + format!("{}.exe", command) + } else { + command.to_string() + } + } + }; + + if !bin_path.join(command_file).is_file() { + return Err(anyhow::anyhow!("Command file not found.")); + } + + let mut command = Command::new(bin_path.to_str().unwrap()); + + command + .args(&args[..]) + .stdout(std::process::Stdio::inherit()) + .stderr(std::process::Stdio::inherit()); + + Ok(()) + } }