소스 검색

Add `sass` support

YuKun Liu 3 년 전
부모
커밋
768a0426d8
3개의 변경된 파일51개의 추가작업 그리고 3개의 파일을 삭제
  1. 2 0
      Cargo.toml
  2. 1 3
      src/assets/dioxus.toml
  3. 48 0
      src/builder.rs

+ 2 - 0
Cargo.toml

@@ -40,6 +40,8 @@ axum = { version = "0.5.1", features = ["ws", "headers"] }
 tower-http = { version = "0.2.2", features = ["fs", "trace"] }
 headers = "0.3.7"
 
+walkdir = "2"
+
 # tools download
 dirs = "4.0.0"
 reqwest = { version = "0.11", features = ["rustls-tls", "stream", "trust-dns"] }

+ 1 - 3
src/assets/dioxus.toml

@@ -47,8 +47,6 @@ binaryen = { wasm_opt = true }
 # use sass auto will auto check sass file and build it.
 [application.tools.sass]
 
-version = "1.51.0"
-
 # if auto = true, we will check style array, and translate all sass file to css.
 auto = true
 
@@ -56,5 +54,5 @@ auto = true
 files = [
     # some sass file path
     # this file will translate to `/css/test.css`
-    "/css/test.sass"
+    "/css/test.scss"
 ]

+ 48 - 0
src/builder.rs

@@ -1,6 +1,7 @@
 use crate::{
     config::{CrateConfig, ExecutableType},
     error::{Error, Result},
+    tools::Tool,
     DioxusConfig,
 };
 use std::{
@@ -29,6 +30,10 @@ pub fn build(config: &CrateConfig) -> Result<()> {
         ..
     } = config;
 
+
+    // start to build the assets
+    build_assets(config)?;
+
     let t_start = std::time::Instant::now();
 
     // [1] Build the .wasm module
@@ -339,6 +344,49 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String {
     html.replace("{app_title}", &title)
 }
 
+// this function will build some assets file
+// like sass tool resources
+fn build_assets(config: &CrateConfig) -> Result<()> {
+    let dioxus_config = &config.dioxus_config;
+    let dioxus_tools = dioxus_config.application.tools.clone().unwrap_or_default();
+
+    // check sass tool state
+    let sass = Tool::Sass;
+    if sass.is_installed() && dioxus_tools.contains_key("sass") {
+        let sass_conf = dioxus_tools.get("sass").unwrap();
+        if let Some(tab) = sass_conf.as_table() {
+            if tab.contains_key("auto") && tab.get("auto").unwrap().as_bool().unwrap_or(false) {
+                // if the sass open auto, we need auto-check the assets dir.
+                let asset_dir = config.asset_dir.clone();
+                if asset_dir.is_dir() {
+                    for entry in walkdir::WalkDir::new(asset_dir)
+                        .into_iter()
+                        .filter_map(|e| e.ok())
+                    {
+                        let temp = entry.path();
+                        if temp.is_file() {
+                            let suffix = temp.file_name().unwrap().to_str().unwrap();
+                            let suffix = suffix.split('.').collect::<Vec<&str>>();
+                            let suffix: &str = suffix.last().unwrap();
+                            if suffix == "scss" {
+                                // if file suffix is `scss` we need transform it.
+                                let temp_stem = temp.file_stem().unwrap().to_str().unwrap();
+                                let target_path =
+                                    temp.parent().unwrap().join(format!("{}.css", temp_stem));
+                                sass.call(
+                                    "sass",
+                                    vec![temp.to_str().unwrap(), target_path.to_str().unwrap()],
+                                )?;
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+    Ok(())
+}
+
 // use binary_install::{Cache, Download};
 
 // /// Attempts to find `wasm-opt` in `PATH` locally, or failing that downloads a