diff --git a/src/compile/tests.rs b/src/compile/tests.rs index 14914422..31d6e750 100644 --- a/src/compile/tests.rs +++ b/src/compile/tests.rs @@ -10,6 +10,7 @@ use super::server::build_cargo_server_cmd; fn release_opts() -> Opts { Opts { release: true, + precompress: false, // if set to true, testing could take quite a while longer hot_reload: false, project: None, verbose: 0, @@ -21,6 +22,7 @@ fn release_opts() -> Opts { fn dev_opts() -> Opts { Opts { release: false, + precompress: false, hot_reload: false, project: None, verbose: 0, diff --git a/src/config/snapshots/cargo_leptos__config__tests__project.snap b/src/config/snapshots/cargo_leptos__config__tests__project.snap index 0a758db1..94b9dbb3 100644 --- a/src/config/snapshots/cargo_leptos__config__tests__project.snap +++ b/src/config/snapshots/cargo_leptos__config__tests__project.snap @@ -62,6 +62,7 @@ Config { }, watch: true, release: false, + precompress: false, hot_reload: false, site: Site { addr: 127.0.0.1:3000, @@ -87,6 +88,7 @@ Config { ], cli: Opts { release: false, + precompress: false, hot_reload: false, project: None, features: [], diff --git a/src/config/snapshots/cargo_leptos__config__tests__workspace.snap b/src/config/snapshots/cargo_leptos__config__tests__workspace.snap index be0ed803..890c16d5 100644 --- a/src/config/snapshots/cargo_leptos__config__tests__workspace.snap +++ b/src/config/snapshots/cargo_leptos__config__tests__workspace.snap @@ -53,6 +53,7 @@ Config { }, watch: true, release: false, + precompress: false, hot_reload: false, site: Site { addr: 127.0.0.1:3000, @@ -123,6 +124,7 @@ Config { }, watch: true, release: false, + precompress: false, hot_reload: false, site: Site { addr: 127.0.0.1:3000, @@ -143,6 +145,7 @@ Config { ], cli: Opts { release: false, + precompress: false, hot_reload: false, project: None, features: [], diff --git a/src/config/snapshots/cargo_leptos__config__tests__workspace_in_subdir_project2.snap b/src/config/snapshots/cargo_leptos__config__tests__workspace_in_subdir_project2.snap index 4074e813..33c6a300 100644 --- a/src/config/snapshots/cargo_leptos__config__tests__workspace_in_subdir_project2.snap +++ b/src/config/snapshots/cargo_leptos__config__tests__workspace_in_subdir_project2.snap @@ -57,6 +57,7 @@ Config { }, watch: true, release: false, + precompress: false, hot_reload: false, site: Site { addr: 127.0.0.1:3000, @@ -77,6 +78,7 @@ Config { ], cli: Opts { release: false, + precompress: false, hot_reload: false, project: None, features: [], diff --git a/src/config/snapshots/cargo_leptos__config__tests__workspace_project1.snap b/src/config/snapshots/cargo_leptos__config__tests__workspace_project1.snap index 8e496f9a..a7b51d42 100644 --- a/src/config/snapshots/cargo_leptos__config__tests__workspace_project1.snap +++ b/src/config/snapshots/cargo_leptos__config__tests__workspace_project1.snap @@ -53,6 +53,7 @@ Config { }, watch: true, release: false, + precompress: false, hot_reload: false, site: Site { addr: 127.0.0.1:3000, @@ -73,6 +74,7 @@ Config { ], cli: Opts { release: false, + precompress: false, hot_reload: false, project: Some( "project1", diff --git a/src/config/snapshots/cargo_leptos__config__tests__workspace_project2.snap b/src/config/snapshots/cargo_leptos__config__tests__workspace_project2.snap index 46026336..ebc6ef3f 100644 --- a/src/config/snapshots/cargo_leptos__config__tests__workspace_project2.snap +++ b/src/config/snapshots/cargo_leptos__config__tests__workspace_project2.snap @@ -57,6 +57,7 @@ Config { }, watch: true, release: false, + precompress: false, hot_reload: false, site: Site { addr: 127.0.0.1:3000, @@ -77,6 +78,7 @@ Config { ], cli: Opts { release: false, + precompress: false, hot_reload: false, project: Some( "project2", diff --git a/src/config/tests.rs b/src/config/tests.rs index e077e5f5..45361b53 100644 --- a/src/config/tests.rs +++ b/src/config/tests.rs @@ -3,6 +3,7 @@ use super::Config; fn opts(project: Option<&str>) -> crate::config::Opts { crate::config::Opts { release: false, + precompress: false, hot_reload: false, project: project.map(|s| s.to_string()), verbose: 0, diff --git a/src/ext/compress.rs b/src/ext/compress.rs index ac800167..536c2290 100644 --- a/src/ext/compress.rs +++ b/src/ext/compress.rs @@ -9,12 +9,13 @@ use tokio::time::Instant; pub async fn compress_static_files(path: PathBuf) -> Result<()> { let start = Instant::now(); + tokio::task::spawn_blocking(move || compress_dir_all(path)).await??; + log::info!( "Precompression of static files finished after {} ms", start.elapsed().as_millis() ); - Ok(()) } @@ -31,29 +32,28 @@ fn compress_dir_all(path: PathBuf) -> Result<()> { let metadata = fs::metadata(&path)?; if metadata.is_dir() { - compress_dir_all(path)? + compress_dir_all(path)?; } else { - let pstr = path.as_os_str().to_str().unwrap(); + let pstr = path.to_str().unwrap_or_default(); if pstr.ends_with(".gz") || pstr.ends_with(".br") { // skip all files that are already compressed continue; } let file = fs::read(&path)?; - let pstr = path.to_str(); // gzip let mut encoder = gzip::Encoder::new(Vec::new())?; encoder.write_all(file.as_ref())?; let encoded_data = encoder.finish().into_result()?; - let path_gz = format!("{}.gz", pstr.unwrap()); + let path_gz = format!("{}.gz", pstr); fs::write(path_gz, encoded_data)?; // brotli - let path_br = format!("{}.br", pstr.unwrap()); + let path_br = format!("{}.br", pstr); let mut output = File::create(path_br)?; let mut reader = BufReader::new(file.as_slice()); - brotli::BrotliCompress(&mut reader, &mut output, &brotli_params).unwrap(); + brotli::BrotliCompress(&mut reader, &mut output, &brotli_params)?; } }