From 5d013ecf824b1e62b8741e7e4565af8bf3262f97 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Sat, 7 May 2022 11:54:00 +0800 Subject: [PATCH 01/19] Try to add `Sass` support --- src/assets/dioxus.toml | 5 ++++- src/tools.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index b43d0d6df..0c57542b8 100644 --- a/src/assets/dioxus.toml +++ b/src/assets/dioxus.toml @@ -42,4 +42,7 @@ script = [] # use binaryen.wasm-opt for output Wasm file # binaryen just will trigger in `web` platform -binaryen = { wasm_opt = true } \ No newline at end of file +binaryen = { wasm_opt = true } + +# use sass auto will auto check sass file and build it. +sass = { auto = true } \ No newline at end of file diff --git a/src/tools.rs b/src/tools.rs index 937063be0..db4d82268 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -13,10 +13,11 @@ use tokio::io::AsyncWriteExt; #[derive(Debug, PartialEq, Eq)] pub enum Tool { Binaryen, + Sass, } pub fn tool_list() -> Vec<&'static str> { - vec!["binaryen"] + vec!["binaryen", "sass"] } pub fn app_path() -> PathBuf { @@ -60,6 +61,7 @@ impl Tool { pub fn name(&self) -> &str { match self { Self::Binaryen => "binaryen", + Self::Sass => "sass", } } @@ -67,6 +69,7 @@ impl Tool { pub fn bin_path(&self) -> &str { match self { Self::Binaryen => "bin", + Self::Sass => ".", } } @@ -84,6 +87,17 @@ impl Tool { panic!("unsupported platformm"); } } + Self::Sass => { + if cfg!(target_os = "windows") { + "windows" + } else if cfg!(target_os = "macos") { + "macos" + } else if cfg!(target_os = "linux") { + "linux" + } else { + panic!("unsupported platformm"); + } + } } } @@ -96,6 +110,12 @@ impl Tool { target = self.target_platform() ) } + Self::Sass => { + format!( + "https://github.com/sass/dart-sass/releases/download/1.51.0/dart-sass-1.51.0-{target}-x64.tar.gz", + target = self.target_platform() + ) + } } } @@ -103,6 +123,13 @@ impl Tool { pub fn extension(&self) -> &str { match self { Self::Binaryen => "tar.gz", + Self::Sass => { + if cfg!(target_os = "windows") { + "zip" + } else { + "tar.ge" + } + }, } } @@ -169,6 +196,9 @@ impl Tool { command.to_string() } } + Tool::Sass => { + command.to_string() + } }; if !bin_path.join(&command_file).is_file() { From c48345b86e556c0535d4ead6134921c68d4c121c Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Sat, 7 May 2022 16:25:23 +0800 Subject: [PATCH 02/19] Add `zip` support --- Cargo.toml | 2 ++ src/tools.rs | 46 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 21e2dad7e..d2bda881f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,11 +39,13 @@ hyper = "0.14.17" axum = { version = "0.5.1", features = ["ws", "headers"] } tower-http = { version = "0.2.2", features = ["fs", "trace"] } headers = "0.3.7" + # tools download dirs = "4.0.0" reqwest = { version = "0.11", features = ["rustls-tls", "stream", "trust-dns"] } flate2 = "1.0.22" tar = "0.4.38" +zip = "0.6.2" tower = "0.4.12" [[bin]] diff --git a/src/tools.rs b/src/tools.rs index db4d82268..b37d10cd2 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -1,6 +1,6 @@ use std::{ fs::{create_dir_all, File}, - path::PathBuf, + path::{Path, PathBuf}, process::Command, }; @@ -112,8 +112,9 @@ impl Tool { } Self::Sass => { format!( - "https://github.com/sass/dart-sass/releases/download/1.51.0/dart-sass-1.51.0-{target}-x64.tar.gz", - target = self.target_platform() + "https://github.com/sass/dart-sass/releases/download/1.51.0/dart-sass-1.51.0-{target}-x64.{extension}", + target = self.target_platform(), + extension = self.extension() ) } } @@ -129,7 +130,7 @@ impl Tool { } else { "tar.ge" } - }, + } } } @@ -169,6 +170,8 @@ impl Tool { let dir_name = if self == &Tool::Binaryen { "binaryen-version_105" + } else if self == &Tool::Sass { + "sass-version-1_51" } else { "" }; @@ -180,6 +183,8 @@ impl Tool { 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()))?; + } else if self.extension() == "zip" { + // decompress the `zip` file } Ok(()) @@ -196,9 +201,7 @@ impl Tool { command.to_string() } } - Tool::Sass => { - command.to_string() - } + Tool::Sass => command.to_string(), }; if !bin_path.join(&command_file).is_file() { @@ -215,3 +218,32 @@ impl Tool { Ok(output.stdout) } } + +fn extract_zip(file: &Path, target: &Path) -> anyhow::Result<()> { + let zip_file = std::fs::File::open(&file)?; + let mut zip = zip::ZipArchive::new(zip_file)?; + + if !target.exists() { + let _ = std::fs::create_dir_all(target)?; + } + + for i in 0..zip.len() { + let mut file = zip.by_index(i)?; + if file.is_dir() { + // dir + let target = target.join(Path::new(&file.name().replace('\\', ""))); + let _ = std::fs::create_dir_all(target)?; + } else { + // file + let file_path = target.join(Path::new(file.name())); + let mut target_file = if !file_path.exists() { + std::fs::File::create(file_path)? + } else { + std::fs::File::open(file_path)? + }; + let _num = std::io::copy(&mut file, &mut target_file)?; + } + } + + Ok(()) +} From ea723f64cd362d4139d0766fc5a74b95166e2ca0 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Sat, 7 May 2022 19:11:12 +0800 Subject: [PATCH 03/19] Support `sass` install --- src/cli/tool/mod.rs | 4 ++-- src/tools.rs | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/cli/tool/mod.rs b/src/cli/tool/mod.rs index d5c4581aa..f13c1620e 100644 --- a/src/cli/tool/mod.rs +++ b/src/cli/tool/mod.rs @@ -20,9 +20,9 @@ impl Tool { Tool::List {} => { for item in tools::tool_list() { if tools::Tool::from_str(item).unwrap().is_installed() { - println!("{item} [installed]"); + println!("- {item} [installed]"); } else { - println!("{item}"); + println!("- {item}"); } } } diff --git a/src/tools.rs b/src/tools.rs index b37d10cd2..9f96ce0b9 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -53,6 +53,7 @@ impl Tool { pub fn from_str(name: &str) -> Option { match name { "binaryen" => Some(Self::Binaryen), + "sass" => Some(Self::Sass), _ => None, } } @@ -128,7 +129,7 @@ impl Tool { if cfg!(target_os = "windows") { "zip" } else { - "tar.ge" + "tar.gz" } } } @@ -159,7 +160,7 @@ impl Tool { let chunk = chunk_res.context("error reading chunk from download")?; let _ = file.write(chunk.as_ref()).await; } - + // log::info!("temp file path: {:?}", temp_out); Ok(temp_out) } @@ -170,10 +171,8 @@ impl Tool { let dir_name = if self == &Tool::Binaryen { "binaryen-version_105" - } else if self == &Tool::Sass { - "sass-version-1_51" } else { - "" + "dart-sass" }; if self.extension() == "tar.gz" { @@ -181,10 +180,11 @@ impl Tool { let tar = GzDecoder::new(tar_gz); 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()))?; } else if self.extension() == "zip" { // decompress the `zip` file + extract_zip(&temp_path, &tool_path)?; + std::fs::rename(tool_path.join(dir_name), tool_path.join(self.name()))?; } Ok(()) From f7f1d90520bf9712172175ad6ccbec41803172f2 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Sun, 8 May 2022 00:56:22 +0800 Subject: [PATCH 04/19] Add `sass` support --- src/assets/dioxus.toml | 14 +++++++++++++- src/cli/tool/mod.rs | 7 +++++-- src/tools.rs | 8 +++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index 0c57542b8..8f160b293 100644 --- a/src/assets/dioxus.toml +++ b/src/assets/dioxus.toml @@ -45,4 +45,16 @@ script = [] binaryen = { wasm_opt = true } # use sass auto will auto check sass file and build it. -sass = { auto = true } \ No newline at end of file +[application.tools.sass] + +version = "1.51.0" + +# if auto = true, we will check style array, and translate all sass file to css. +auto = true + +# or you can specify some sass file -> css file +files = [ + # some sass file path + # this file will translate to `/css/test.css` + "/css/test.sass" +] \ No newline at end of file diff --git a/src/cli/tool/mod.rs b/src/cli/tool/mod.rs index f13c1620e..45c866c40 100644 --- a/src/cli/tool/mod.rs +++ b/src/cli/tool/mod.rs @@ -27,7 +27,11 @@ impl Tool { } } Tool::AppPath {} => { - println!("{}", tools::tools_path().to_str().unwrap()); + if let Some(v) = tools::tools_path().to_str() { + println!("{}", v); + } else { + log::error!("Tools path get failed."); + } } Tool::Add { name } => { let tool_list = tools::tool_list(); @@ -58,7 +62,6 @@ impl Tool { log::info!("Tool {name} install successfully!"); } } - Ok(()) } } diff --git a/src/tools.rs b/src/tools.rs index 9f96ce0b9..ae4e58b79 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -201,7 +201,13 @@ impl Tool { command.to_string() } } - Tool::Sass => command.to_string(), + Tool::Sass => { + if cfg!(target_os = "windows") { + format!("{}.bat", command) + } else { + command.to_string() + } + }, }; if !bin_path.join(&command_file).is_file() { From 768a0426d8519073259f9e642bc0b44f75110f52 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Tue, 10 May 2022 10:31:02 +0800 Subject: [PATCH 05/19] Add `sass` support --- Cargo.toml | 2 ++ src/assets/dioxus.toml | 4 +--- src/builder.rs | 48 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d2bda881f..c869267b1 100644 --- a/Cargo.toml +++ b/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"] } diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index 8f160b293..3e82e0c93 100644 --- a/src/assets/dioxus.toml +++ b/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" ] \ No newline at end of file diff --git a/src/builder.rs b/src/builder.rs index f7f68e2bc..7c3a7e9fb 100644 --- a/src/builder.rs +++ b/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::>(); + 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 From 8b06fe60596371118e47ef71a72627044c17672a Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Tue, 10 May 2022 13:46:53 +0800 Subject: [PATCH 06/19] Fixed sass tool --- src/assets/dioxus.toml | 4 ++-- src/builder.rs | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index 3e82e0c93..105251aa1 100644 --- a/src/assets/dioxus.toml +++ b/src/assets/dioxus.toml @@ -47,10 +47,10 @@ binaryen = { wasm_opt = true } # use sass auto will auto check sass file and build it. [application.tools.sass] -# if auto = true, we will check style array, and translate all sass file to css. +# auto will check the assets dirs, and auto to transform all scss file to css file. auto = true -# or you can specify some sass file -> css file +# you can specify some scss file -> css file files = [ # some sass file path # this file will translate to `/css/test.css` diff --git a/src/builder.rs b/src/builder.rs index 7c3a7e9fb..0e0cf7e9a 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -30,7 +30,6 @@ pub fn build(config: &CrateConfig) -> Result<()> { .. } = config; - // start to build the assets build_assets(config)?; @@ -359,20 +358,21 @@ fn build_assets(config: &CrateConfig) -> Result<()> { // 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) + 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::>(); - let suffix: &str = suffix.last().unwrap(); + let suffix = temp.extension().unwrap().to_str().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)); + let out_file = + format!("{}.css", temp.file_stem().unwrap().to_str().unwrap()); + let target_path = config + .out_dir + .join(temp.strip_prefix(&asset_dir).unwrap().parent().unwrap()) + .join(out_file); sass.call( "sass", vec![temp.to_str().unwrap(), target_path.to_str().unwrap()], From b5ecd09185062724415e9bf7b6b10d7e2c6860e8 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Tue, 10 May 2022 19:22:19 +0800 Subject: [PATCH 07/19] Add `files` list option --- src/assets/dioxus.toml | 2 +- src/builder.rs | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index 105251aa1..5f5032e2a 100644 --- a/src/assets/dioxus.toml +++ b/src/assets/dioxus.toml @@ -50,7 +50,7 @@ binaryen = { wasm_opt = true } # auto will check the assets dirs, and auto to transform all scss file to css file. auto = true -# you can specify some scss file -> css file +# or you can specify some scss file -> css file files = [ # some sass file path # this file will translate to `/css/test.css` diff --git a/src/builder.rs b/src/builder.rs index 0e0cf7e9a..fb0c8088d 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -381,6 +381,28 @@ fn build_assets(config: &CrateConfig) -> Result<()> { } } } + } else if tab.contains_key("files") && tab.get("files").unwrap().is_array() { + // check files list. + let list = tab.get("files").unwrap().as_array().unwrap(); + for i in list { + if i.is_str() { + let path = i.as_str().unwrap(); + let path = PathBuf::from(path); + let path = config.asset_dir.join(path); + if path.is_file() { + sass.call( + "sass", + vec![ + path.to_str().unwrap(), + path.parent().unwrap().join(format!( + "{}.css", + path.file_stem().unwrap().to_str().unwrap() + )).to_str().unwrap(), + ], + )?; + } + } + } } } } From 551d2477ffcd84d13cfc4c5d15e1c8033a23a160 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Tue, 10 May 2022 22:32:47 +0800 Subject: [PATCH 08/19] Add `source_map` option --- src/assets/dioxus.toml | 4 +++- src/builder.rs | 30 +++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index 5f5032e2a..b481193a8 100644 --- a/src/assets/dioxus.toml +++ b/src/assets/dioxus.toml @@ -55,4 +55,6 @@ files = [ # some sass file path # this file will translate to `/css/test.css` "/css/test.scss" -] \ No newline at end of file +] + +source_map = true \ No newline at end of file diff --git a/src/builder.rs b/src/builder.rs index fb0c8088d..825a9eebf 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -354,6 +354,17 @@ fn build_assets(config: &CrateConfig) -> Result<()> { 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() { + let source_map = tab.contains_key("source_map"); + let source_map = if source_map && tab.get("source_map").unwrap().is_bool() { + if tab.get("source_map").unwrap().as_bool().unwrap_or_default() { + "--source-map" + } else { + "--no-source-map" + } + } else { + "--no-source-map" + }; + 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(); @@ -375,7 +386,11 @@ fn build_assets(config: &CrateConfig) -> Result<()> { .join(out_file); sass.call( "sass", - vec![temp.to_str().unwrap(), target_path.to_str().unwrap()], + vec![ + temp.to_str().unwrap(), + target_path.to_str().unwrap(), + source_map, + ], )?; } } @@ -394,10 +409,15 @@ fn build_assets(config: &CrateConfig) -> Result<()> { "sass", vec![ path.to_str().unwrap(), - path.parent().unwrap().join(format!( - "{}.css", - path.file_stem().unwrap().to_str().unwrap() - )).to_str().unwrap(), + path.parent() + .unwrap() + .join(format!( + "{}.css", + path.file_stem().unwrap().to_str().unwrap() + )) + .to_str() + .unwrap(), + source_map, ], )?; } From 9ddaa773d9ffa8d6274a2731dfdac00223f6b74d Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Tue, 10 May 2022 22:58:48 +0800 Subject: [PATCH 09/19] Remove `auto` option --- src/builder.rs | 111 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 38 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 825a9eebf..75c83990d 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -362,48 +362,54 @@ fn build_assets(config: &CrateConfig) -> Result<()> { "--no-source-map" } } else { - "--no-source-map" + "--source-map" }; - 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.extension().unwrap().to_str().unwrap(); - if suffix == "scss" { - // if file suffix is `scss` we need transform it. - let out_file = - format!("{}.css", temp.file_stem().unwrap().to_str().unwrap()); - let target_path = config - .out_dir - .join(temp.strip_prefix(&asset_dir).unwrap().parent().unwrap()) - .join(out_file); - sass.call( - "sass", - vec![ - temp.to_str().unwrap(), - target_path.to_str().unwrap(), - source_map, - ], - )?; + if tab.contains_key("files") { + if tab.get("files").unwrap().is_str() { + let file = tab.get("files").unwrap().as_str().unwrap().trim(); + + if file == "*" { + // 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.extension().unwrap().to_str().unwrap(); + if suffix == "scss" { + // if file suffix is `scss` we need transform it. + let out_file = format!( + "{}.css", + temp.file_stem().unwrap().to_str().unwrap() + ); + let target_path = config + .out_dir + .join( + temp.strip_prefix(&asset_dir) + .unwrap() + .parent() + .unwrap(), + ) + .join(out_file); + sass.call( + "sass", + vec![ + temp.to_str().unwrap(), + target_path.to_str().unwrap(), + source_map, + ], + )?; + } + } } } - } - } - } else if tab.contains_key("files") && tab.get("files").unwrap().is_array() { - // check files list. - let list = tab.get("files").unwrap().as_array().unwrap(); - for i in list { - if i.is_str() { - let path = i.as_str().unwrap(); - let path = PathBuf::from(path); - let path = config.asset_dir.join(path); + } else { + // just transform one file. + let path = config.asset_dir.join(file); if path.is_file() { sass.call( "sass", @@ -422,10 +428,39 @@ fn build_assets(config: &CrateConfig) -> Result<()> { )?; } } + } else if tab.get("files").unwrap().is_array() { + // check files list. + let list = tab.get("files").unwrap().as_array().unwrap(); + for i in list { + if i.is_str() { + let path = i.as_str().unwrap(); + let path = PathBuf::from(path); + let path = config.asset_dir.join(path); + if path.is_file() { + sass.call( + "sass", + vec![ + path.to_str().unwrap(), + path.parent() + .unwrap() + .join(format!( + "{}.css", + path.file_stem().unwrap().to_str().unwrap() + )) + .to_str() + .unwrap(), + source_map, + ], + )?; + } + } + } } } } } + // SASS END + Ok(()) } From ecb846dd24d34c0827bfa3503048bf97a8389fda Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Tue, 10 May 2022 23:00:39 +0800 Subject: [PATCH 10/19] Rename `files` to `input` --- src/assets/dioxus.toml | 12 ++++++------ src/builder.rs | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index b481193a8..a5e849bfb 100644 --- a/src/assets/dioxus.toml +++ b/src/assets/dioxus.toml @@ -48,13 +48,13 @@ binaryen = { wasm_opt = true } [application.tools.sass] # auto will check the assets dirs, and auto to transform all scss file to css file. -auto = true +input = "*" # or you can specify some scss file -> css file -files = [ - # some sass file path - # this file will translate to `/css/test.css` - "/css/test.scss" -] +# input = [ +# # some sass file path +# # this file will translate to `/css/test.css` +# "/css/test.scss" +# ] source_map = true \ No newline at end of file diff --git a/src/builder.rs b/src/builder.rs index 75c83990d..f9f0bd9bd 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -365,9 +365,9 @@ fn build_assets(config: &CrateConfig) -> Result<()> { "--source-map" }; - if tab.contains_key("files") { - if tab.get("files").unwrap().is_str() { - let file = tab.get("files").unwrap().as_str().unwrap().trim(); + if tab.contains_key("input") { + if tab.get("input").unwrap().is_str() { + let file = tab.get("input").unwrap().as_str().unwrap().trim(); if file == "*" { // if the sass open auto, we need auto-check the assets dir. @@ -428,9 +428,9 @@ fn build_assets(config: &CrateConfig) -> Result<()> { )?; } } - } else if tab.get("files").unwrap().is_array() { + } else if tab.get("input").unwrap().is_array() { // check files list. - let list = tab.get("files").unwrap().as_array().unwrap(); + let list = tab.get("input").unwrap().as_array().unwrap(); for i in list { if i.is_str() { let path = i.as_str().unwrap(); From bf49d526d773cc87449ac343659609c52890f0ac Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 11 May 2022 00:27:37 +0800 Subject: [PATCH 11/19] Fixed some problem --- src/builder.rs | 13 ++++++++++++- src/tools.rs | 6 +++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index f9f0bd9bd..77f0838f0 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -409,7 +409,13 @@ fn build_assets(config: &CrateConfig) -> Result<()> { } } else { // just transform one file. - let path = config.asset_dir.join(file); + let path = if &file[0..] == "/" { + &file[1..file.len() - 1] + } else { + file + }; + let path = PathBuf::from(path); + let path = config.asset_dir.join(path); if path.is_file() { sass.call( "sass", @@ -434,6 +440,11 @@ fn build_assets(config: &CrateConfig) -> Result<()> { for i in list { if i.is_str() { let path = i.as_str().unwrap(); + let path = if &path[0..] == "/" { + &path[1..path.len() - 1] + } else { + path + }; let path = PathBuf::from(path); let path = config.asset_dir.join(path); if path.is_file() { diff --git a/src/tools.rs b/src/tools.rs index ae4e58b79..2952a9614 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -207,7 +207,7 @@ impl Tool { } else { command.to_string() } - }, + } }; if !bin_path.join(&command_file).is_file() { @@ -232,7 +232,7 @@ fn extract_zip(file: &Path, target: &Path) -> anyhow::Result<()> { if !target.exists() { let _ = std::fs::create_dir_all(target)?; } - + for i in 0..zip.len() { let mut file = zip.by_index(i)?; if file.is_dir() { @@ -252,4 +252,4 @@ fn extract_zip(file: &Path, target: &Path) -> anyhow::Result<()> { } Ok(()) -} +} \ No newline at end of file From c6aa4e332df58c223416fafc62e729574ff8c564 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 11 May 2022 10:01:02 +0800 Subject: [PATCH 12/19] Ignore `scss` file in out_dir --- src/builder.rs | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 77f0838f0..25d087191 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -31,7 +31,7 @@ pub fn build(config: &CrateConfig) -> Result<()> { } = config; // start to build the assets - build_assets(config)?; + let ignore_files = build_assets(config)?; let t_start = std::time::Instant::now(); @@ -158,6 +158,13 @@ pub fn build(config: &CrateConfig) -> Result<()> { log::warn!("Error copying dir: {}", _e); } } + for ignore in &ignore_files { + let ignore = ignore.strip_prefix(&config.asset_dir).unwrap(); + let ignore = config.out_dir.join(ignore); + if ignore.is_file() { + std::fs::remove_file(ignore)?; + } + } } } } @@ -345,7 +352,11 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String { // this function will build some assets file // like sass tool resources -fn build_assets(config: &CrateConfig) -> Result<()> { +// this function will return a array which file don't need copy to out_dir. +fn build_assets(config: &CrateConfig) -> Result> { + + let mut result = vec![]; + let dioxus_config = &config.dioxus_config; let dioxus_tools = dioxus_config.application.tools.clone().unwrap_or_default(); @@ -395,14 +406,17 @@ fn build_assets(config: &CrateConfig) -> Result<()> { .unwrap(), ) .join(out_file); - sass.call( + let res = sass.call( "sass", vec![ temp.to_str().unwrap(), target_path.to_str().unwrap(), source_map, ], - )?; + ); + if res.is_ok() { + result.push(temp.to_path_buf()); + } } } } @@ -417,7 +431,7 @@ fn build_assets(config: &CrateConfig) -> Result<()> { let path = PathBuf::from(path); let path = config.asset_dir.join(path); if path.is_file() { - sass.call( + let res = sass.call( "sass", vec![ path.to_str().unwrap(), @@ -431,7 +445,10 @@ fn build_assets(config: &CrateConfig) -> Result<()> { .unwrap(), source_map, ], - )?; + ); + if res.is_ok() { + result.push(path); + } } } } else if tab.get("input").unwrap().is_array() { @@ -448,7 +465,7 @@ fn build_assets(config: &CrateConfig) -> Result<()> { let path = PathBuf::from(path); let path = config.asset_dir.join(path); if path.is_file() { - sass.call( + let res = sass.call( "sass", vec![ path.to_str().unwrap(), @@ -462,7 +479,10 @@ fn build_assets(config: &CrateConfig) -> Result<()> { .unwrap(), source_map, ], - )?; + ); + if res.is_ok() { + result.push(path); + } } } } @@ -472,7 +492,7 @@ fn build_assets(config: &CrateConfig) -> Result<()> { } // SASS END - Ok(()) + Ok(result) } // use binary_install::{Cache, Download}; From dccd82091a0b499527cd435fccf1a088262235e1 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 11 May 2022 11:12:23 +0800 Subject: [PATCH 13/19] Fixed `input` option error --- src/builder.rs | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 25d087191..e1ed0a8f1 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -354,7 +354,6 @@ pub fn gen_page(config: &DioxusConfig, serve: bool) -> String { // like sass tool resources // this function will return a array which file don't need copy to out_dir. fn build_assets(config: &CrateConfig) -> Result> { - let mut result = vec![]; let dioxus_config = &config.dioxus_config; @@ -430,19 +429,23 @@ fn build_assets(config: &CrateConfig) -> Result> { }; let path = PathBuf::from(path); let path = config.asset_dir.join(path); + let out_file = + format!("{}.css", path.file_stem().unwrap().to_str().unwrap()); + let target_path = config + .out_dir + .join( + path.strip_prefix(&config.asset_dir) + .unwrap() + .parent() + .unwrap(), + ) + .join(out_file); if path.is_file() { let res = sass.call( "sass", vec![ path.to_str().unwrap(), - path.parent() - .unwrap() - .join(format!( - "{}.css", - path.file_stem().unwrap().to_str().unwrap() - )) - .to_str() - .unwrap(), + target_path.to_str().unwrap(), source_map, ], ); @@ -464,19 +467,23 @@ fn build_assets(config: &CrateConfig) -> Result> { }; let path = PathBuf::from(path); let path = config.asset_dir.join(path); + let out_file = + format!("{}.css", path.file_stem().unwrap().to_str().unwrap()); + let target_path = config + .out_dir + .join( + path.strip_prefix(&config.asset_dir) + .unwrap() + .parent() + .unwrap(), + ) + .join(out_file); if path.is_file() { let res = sass.call( "sass", vec![ path.to_str().unwrap(), - path.parent() - .unwrap() - .join(format!( - "{}.css", - path.file_stem().unwrap().to_str().unwrap() - )) - .to_str() - .unwrap(), + target_path.to_str().unwrap(), source_map, ], ); From 065da8db6589a1c745392b3973bae127ebecb183 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 11 May 2022 11:14:49 +0800 Subject: [PATCH 14/19] Add `sass` desktop build support --- src/builder.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/builder.rs b/src/builder.rs index e1ed0a8f1..ae6e83605 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -177,6 +177,8 @@ pub fn build(config: &CrateConfig) -> Result<()> { pub fn build_desktop(config: &CrateConfig, is_serve: bool) -> Result<()> { log::info!("🚅 Running build [Desktop] command..."); + let ignore_files = build_assets(config)?; + let mut cmd = Command::new("cargo"); cmd.current_dir(&config.crate_dir) .arg("build") @@ -261,6 +263,13 @@ pub fn build_desktop(config: &CrateConfig, is_serve: bool) -> Result<()> { log::warn!("Error copying dir: {}", e); } } + for ignore in &ignore_files { + let ignore = ignore.strip_prefix(&config.asset_dir).unwrap(); + let ignore = config.out_dir.join(ignore); + if ignore.is_file() { + std::fs::remove_file(ignore)?; + } + } } } } From 60bb0d9eb9f6a6e10267df85840914afa04d460b Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 11 May 2022 11:15:33 +0800 Subject: [PATCH 15/19] Format code --- src/cli/tool/mod.rs | 2 +- src/tools.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cli/tool/mod.rs b/src/cli/tool/mod.rs index 45c866c40..fde94b29a 100644 --- a/src/cli/tool/mod.rs +++ b/src/cli/tool/mod.rs @@ -27,7 +27,7 @@ impl Tool { } } Tool::AppPath {} => { - if let Some(v) = tools::tools_path().to_str() { + if let Some(v) = tools::tools_path().to_str() { println!("{}", v); } else { log::error!("Tools path get failed."); diff --git a/src/tools.rs b/src/tools.rs index 2952a9614..2ab358d6b 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -252,4 +252,4 @@ fn extract_zip(file: &Path, target: &Path) -> anyhow::Result<()> { } Ok(()) -} \ No newline at end of file +} From cb650bad8a726c50eacdf8d29669532a5fb75c42 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 11 May 2022 11:47:49 +0800 Subject: [PATCH 16/19] Fixed some problem --- src/builder.rs | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index ae6e83605..8f3823a65 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -431,24 +431,19 @@ fn build_assets(config: &CrateConfig) -> Result> { } } else { // just transform one file. - let path = if &file[0..] == "/" { - &file[1..file.len() - 1] + let relative_path = if &file[0..1] == "/" { + &file[1..file.len()] } else { file }; - let path = PathBuf::from(path); - let path = config.asset_dir.join(path); + let path = config.asset_dir.join(relative_path); let out_file = format!("{}.css", path.file_stem().unwrap().to_str().unwrap()); let target_path = config .out_dir - .join( - path.strip_prefix(&config.asset_dir) - .unwrap() - .parent() - .unwrap(), - ) + .join(PathBuf::from(relative_path).parent().unwrap()) .join(out_file); + println!("{:?} -- {:?}", target_path, path); if path.is_file() { let res = sass.call( "sass", @@ -460,6 +455,8 @@ fn build_assets(config: &CrateConfig) -> Result> { ); if res.is_ok() { result.push(path); + } else { + log::error!("{:?}", res); } } } @@ -469,23 +466,17 @@ fn build_assets(config: &CrateConfig) -> Result> { for i in list { if i.is_str() { let path = i.as_str().unwrap(); - let path = if &path[0..] == "/" { - &path[1..path.len() - 1] + let relative_path = if &path[0..1] == "/" { + &path[1..path.len()] } else { path }; - let path = PathBuf::from(path); - let path = config.asset_dir.join(path); + let path = config.asset_dir.join(relative_path); let out_file = format!("{}.css", path.file_stem().unwrap().to_str().unwrap()); let target_path = config .out_dir - .join( - path.strip_prefix(&config.asset_dir) - .unwrap() - .parent() - .unwrap(), - ) + .join(PathBuf::from(relative_path).parent().unwrap()) .join(out_file); if path.is_file() { let res = sass.call( From 7f829c4553eaf45eda2e9c7964ba28cac63723cb Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 11 May 2022 11:50:01 +0800 Subject: [PATCH 17/19] Format code --- src/builder.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/builder.rs b/src/builder.rs index 8f3823a65..e0d3ac285 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -443,7 +443,6 @@ fn build_assets(config: &CrateConfig) -> Result> { .out_dir .join(PathBuf::from(relative_path).parent().unwrap()) .join(out_file); - println!("{:?} -- {:?}", target_path, path); if path.is_file() { let res = sass.call( "sass", From cc56dd19ddbdaf9578ee6f56df7a354bbd2d3bc6 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Thu, 12 May 2022 00:11:51 +0800 Subject: [PATCH 18/19] Add `sass` file extension --- src/builder.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index e0d3ac285..cdb431933 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -399,8 +399,8 @@ fn build_assets(config: &CrateConfig) -> Result> { let temp = entry.path(); if temp.is_file() { let suffix = temp.extension().unwrap().to_str().unwrap(); - if suffix == "scss" { - // if file suffix is `scss` we need transform it. + if suffix == "scss" || suffix == "sass" { + // if file suffix is `scss` / `sass` we need transform it. let out_file = format!( "{}.css", temp.file_stem().unwrap().to_str().unwrap() From 87e6ed0a3d13dfb023c185a249ceee3a7a22d9e8 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Thu, 12 May 2022 22:49:27 +0800 Subject: [PATCH 19/19] Fixed builder problem --- src/builder.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/builder.rs b/src/builder.rs index cdb431933..4f6824780 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -398,7 +398,9 @@ fn build_assets(config: &CrateConfig) -> Result> { { let temp = entry.path(); if temp.is_file() { - let suffix = temp.extension().unwrap().to_str().unwrap(); + let suffix = temp.extension(); + if suffix.is_none() { continue; } + let suffix = suffix.unwrap().to_str().unwrap(); if suffix == "scss" || suffix == "sass" { // if file suffix is `scss` / `sass` we need transform it. let out_file = format!(