From 43c0ab87c7ae849b359f0fc74fcdc83a27bd81c0 Mon Sep 17 00:00:00 2001 From: Miles Date: Wed, 14 Aug 2024 13:19:34 -0700 Subject: [PATCH 1/3] feat: optional optimization --- cli-support/examples/cli.rs | 2 +- cli-support/src/file.rs | 90 +++++++++++++++++++++------- cli-support/src/folder.rs | 2 +- cli-support/src/manifest.rs | 16 +++-- cli-support/tests/collects_assets.rs | 2 +- 5 files changed, 84 insertions(+), 28 deletions(-) diff --git a/cli-support/examples/cli.rs b/cli-support/examples/cli.rs index 50ca0da..d45e2c1 100644 --- a/cli-support/examples/cli.rs +++ b/cli-support/examples/cli.rs @@ -70,7 +70,7 @@ fn link() { let _ = std::fs::remove_dir_all(&assets_dir); // And copy the static assets to the public directory - assets.copy_static_assets_to(&assets_dir).unwrap(); + assets.copy_static_assets_to(&assets_dir, true).unwrap(); // Then collect the tailwind CSS let css = assets.collect_tailwind_css(true, &mut Vec::new()); diff --git a/cli-support/src/file.rs b/cli-support/src/file.rs index 0e81ad1..3e7f698 100644 --- a/cli-support/src/file.rs +++ b/cli-support/src/file.rs @@ -6,6 +6,7 @@ use manganis_common::{ JsonOptions, }; use std::{ + fs, io::{BufWriter, Write}, path::Path, sync::Arc, @@ -15,19 +16,33 @@ use swc_common::{sync::Lrc, FileName}; use swc_common::{SourceMap, GLOBALS}; pub trait Process { - fn process(&self, source: &AssetSource, output_path: &Path) -> anyhow::Result<()>; + fn process( + &self, + source: &AssetSource, + output_path: &Path, + should_opt: bool, + ) -> anyhow::Result<()>; } -/// Process a specific file asset -pub fn process_file(file: &FileAsset, output_folder: &Path) -> anyhow::Result<()> { +/// Process a specific file asset, specifying the output folder and if the file should be optimized. +pub fn process_file( + file: &FileAsset, + output_folder: &Path, + should_opt: bool, +) -> anyhow::Result<()> { let location = file.location(); let source = location.source(); let output_path = output_folder.join(location.unique_name()); - file.options().process(source, &output_path) + file.options().process(source, &output_path, should_opt) } impl Process for FileOptions { - fn process(&self, source: &AssetSource, output_path: &Path) -> anyhow::Result<()> { + fn process( + &self, + source: &AssetSource, + output_path: &Path, + should_opt: bool, + ) -> anyhow::Result<()> { if output_path.exists() { return Ok(()); } @@ -42,16 +57,16 @@ impl Process for FileOptions { })?; } Self::Css(options) => { - options.process(source, output_path)?; + options.process(source, output_path, should_opt)?; } Self::Js(options) => { - options.process(source, output_path)?; + options.process(source, output_path, should_opt)?; } Self::Json(options) => { - options.process(source, output_path)?; + options.process(source, output_path, should_opt)?; } Self::Image(options) => { - options.process(source, output_path)?; + options.process(source, output_path, should_opt)?; } _ => todo!(), } @@ -61,7 +76,18 @@ impl Process for FileOptions { } impl Process for ImageOptions { - fn process(&self, source: &AssetSource, output_path: &Path) -> anyhow::Result<()> { + fn process( + &self, + source: &AssetSource, + output_path: &Path, + should_opt: bool, + ) -> anyhow::Result<()> { + if !should_opt { + if let Some(path) = source.as_path() { + fs::copy(path, output_path)?; + return Ok(()); + } + } let mut image = image::ImageReader::new(std::io::Cursor::new(&*source.read_to_bytes()?)) .with_guessed_format()? .decode()?; @@ -161,10 +187,19 @@ fn compress_png(image: DynamicImage, output_location: &Path) { } impl Process for CssOptions { - fn process(&self, source: &AssetSource, output_path: &Path) -> anyhow::Result<()> { + fn process( + &self, + source: &AssetSource, + output_path: &Path, + should_opt: bool, + ) -> anyhow::Result<()> { let css = source.read_to_string()?; - let css = if self.minify() { minify_css(&css) } else { css }; + let css = if self.minify() && should_opt { + minify_css(&css) + } else { + css + }; std::fs::write(output_path, css).with_context(|| { format!( @@ -226,8 +261,13 @@ pub(crate) fn minify_js(source: &AssetSource) -> anyhow::Result { } impl Process for JsOptions { - fn process(&self, source: &AssetSource, output_path: &Path) -> anyhow::Result<()> { - let js = if self.minify() { + fn process( + &self, + source: &AssetSource, + output_path: &Path, + should_opt: bool, + ) -> anyhow::Result<()> { + let js = if self.minify() && should_opt { minify_js(source)? } else { source.read_to_string()? @@ -253,14 +293,22 @@ pub(crate) fn minify_json(source: &str) -> anyhow::Result { } impl Process for JsonOptions { - fn process(&self, source: &AssetSource, output_path: &Path) -> anyhow::Result<()> { + fn process( + &self, + source: &AssetSource, + output_path: &Path, + should_opt: bool, + ) -> anyhow::Result<()> { let source = source.read_to_string()?; - let json = match minify_json(&source) { - Ok(json) => json, - Err(err) => { - tracing::error!("Failed to minify json: {}", err); - source - } + let json = match should_opt { + false => source, + true => match minify_json(&source) { + Ok(json) => json, + Err(err) => { + tracing::error!("Failed to minify json: {}", err); + source + } + }, }; std::fs::write(output_path, json).with_context(|| { diff --git a/cli-support/src/folder.rs b/cli-support/src/folder.rs index a82b758..0d8ea15 100644 --- a/cli-support/src/folder.rs +++ b/cli-support/src/folder.rs @@ -54,6 +54,6 @@ fn process_file_minimal(input_path: &Path, output_path: &Path) -> anyhow::Result let options = FileOptions::default_for_extension(input_path.extension().and_then(|e| e.to_str())); let source = manganis_common::AssetSource::Local(input_path.to_path_buf()); - options.process(&source, output_path)?; + options.process(&source, output_path, true)?; Ok(()) } diff --git a/cli-support/src/manifest.rs b/cli-support/src/manifest.rs index 008b2e5..c8afc53 100644 --- a/cli-support/src/manifest.rs +++ b/cli-support/src/manifest.rs @@ -43,8 +43,12 @@ pub trait AssetManifestExt { /// /// The asset descriptions are stored inside a manifest file that is produced when the linker is intercepted. fn load_from_objects(object_paths: Vec) -> Self; - /// Optimize and copy all assets in the manifest to a folder - fn copy_static_assets_to(&self, location: impl Into) -> anyhow::Result<()>; + /// Copy all assets in the manifest to a folder with the option to optimize. + fn copy_static_assets_to( + &self, + location: impl Into, + should_opt: bool, + ) -> anyhow::Result<()>; /// Collect all tailwind classes and generate string with the output css fn collect_tailwind_css( &self, @@ -76,7 +80,11 @@ impl AssetManifestExt for AssetManifest { Self::load(json) } - fn copy_static_assets_to(&self, location: impl Into) -> anyhow::Result<()> { + fn copy_static_assets_to( + &self, + location: impl Into, + should_opt: bool, + ) -> anyhow::Result<()> { let location = location.into(); match std::fs::create_dir_all(&location) { Ok(_) => {} @@ -91,7 +99,7 @@ impl AssetManifestExt for AssetManifest { AssetType::File(file_asset) => { tracing::info!("Optimizing and bundling {}", file_asset); tracing::trace!("Copying asset from {:?} to {:?}", file_asset, location); - match process_file(file_asset, &location) { + match process_file(file_asset, &location, should_opt) { Ok(_) => {} Err(err) => { tracing::error!("Failed to copy static asset: {}", err); diff --git a/cli-support/tests/collects_assets.rs b/cli-support/tests/collects_assets.rs index c179967..f912c82 100644 --- a/cli-support/tests/collects_assets.rs +++ b/cli-support/tests/collects_assets.rs @@ -89,7 +89,7 @@ fn link() { // Then copy the assets to a temporary directory and run the application let assets_dir = PathBuf::from("./assets"); - assets.copy_static_assets_to(assets_dir).unwrap(); + assets.copy_static_assets_to(assets_dir, true).unwrap(); // Then run the application let status = Command::new("cargo") From f1409a66c2225aef17316f4d6363914b6cd106b1 Mon Sep 17 00:00:00 2001 From: Miles Date: Wed, 14 Aug 2024 13:32:22 -0700 Subject: [PATCH 2/3] fix: missing opt --- macro/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/macro/src/lib.rs b/macro/src/lib.rs index 2f6136e..da45c62 100644 --- a/macro/src/lib.rs +++ b/macro/src/lib.rs @@ -366,7 +366,7 @@ pub(crate) fn url_encoded_asset( format!("Failed to create output folder: {}", e), ) })?; - manganis_cli_support::process_file(file_asset, &output_folder).map_err(|e| { + manganis_cli_support::process_file(file_asset, &output_folder, true).map_err(|e| { syn::Error::new( proc_macro2::Span::call_site(), format!("Failed to process file: {}", e), From 782c0051f6fc1737ec8d5cf15fbd03c75c90b04e Mon Sep 17 00:00:00 2001 From: Miles Date: Wed, 14 Aug 2024 13:33:29 -0700 Subject: [PATCH 3/3] fix: clippy --- test-package/test-package-nested-dependency/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test-package/test-package-nested-dependency/src/lib.rs b/test-package/test-package-nested-dependency/src/lib.rs index f22f81d..1a88b6c 100644 --- a/test-package/test-package-nested-dependency/src/lib.rs +++ b/test-package/test-package-nested-dependency/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(unused)] + mod file; pub use file::*; mod font;