瀏覽代碼

feat: commit code

mrxiaozhuox 3 年之前
父節點
當前提交
3514e34112
共有 4 個文件被更改,包括 56 次插入8 次删除
  1. 1 1
      Dioxus.toml
  2. 25 2
      src/builder.rs
  3. 1 0
      src/cli/build/mod.rs
  4. 29 5
      src/tools.rs

+ 1 - 1
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"] }
+binaryen = { wasm_opt = true }

+ 25 - 2
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 {

+ 1 - 0
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);
 

+ 29 - 5
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<Self> {
         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(())
+    }
 }