diff --git a/Cargo.lock b/Cargo.lock index 8a75d96..cc1ff40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -860,6 +860,7 @@ dependencies = [ "comrak", "glob", "minify-html", + "minify-js 0.5.6", "ramhorns", "reqwest", "serde", @@ -1044,6 +1045,10 @@ name = "hashbrown" version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +dependencies = [ + "ahash 0.8.3", + "bumpalo", +] [[package]] name = "heck" @@ -1404,7 +1409,7 @@ dependencies = [ "css-minify", "lazy_static", "memchr", - "minify-js", + "minify-js 0.4.3", "rustc-hash", ] @@ -1415,7 +1420,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c300f90ba1138b5c5daf5d9441dc9bdc67b808aac22cf638362a2647bc213be4" dependencies = [ "lazy_static", - "parse-js", + "parse-js 0.10.3", +] + +[[package]] +name = "minify-js" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22d6c512a82abddbbc13b70609cb2beff01be2c7afff534d6e5e1c85e438fc8b" +dependencies = [ + "lazy_static", + "parse-js 0.17.0", ] [[package]] @@ -1608,6 +1623,19 @@ dependencies = [ "memchr", ] +[[package]] +name = "parse-js" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ec3b11d443640ec35165ee8f6f0559f1c6f41878d70330fe9187012b5935f02" +dependencies = [ + "aho-corasick 0.7.20", + "bumpalo", + "hashbrown 0.13.2", + "lazy_static", + "memchr", +] + [[package]] name = "paste" version = "1.0.12" diff --git a/Cargo.toml b/Cargo.toml index 3e3d187..877c0b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ toml = "0.7.3" serde = { version = "1.0.159", features = ["derive"] } serde_yaml = "0.9" minify-html = "0.10.8" +minify-js = "0.5.6" glob = "0.3.1" comrak = "0.18" reqwest = { version = "0.11", features = ["json"] } diff --git a/src/config.rs b/src/config.rs index fd11cef..4b45c82 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,7 +2,6 @@ use serde::Deserialize; use std::{fs, path::PathBuf}; use glob::glob; -use minify_html::{minify, Cfg}; use std::{fs::File, io::Write, path::Path}; use crate::template::Template; @@ -115,11 +114,11 @@ fn init(dist_dir: String, static_dir: String, templates_dir: String) -> String { if cfg!(debug_assertions) { ".".to_owned() } else { - let cfg = Cfg { + let cfg = minify_html::Cfg { keep_closing_tags: true, minify_css: true, minify_js: true, - ..Cfg::spec_compliant() + ..minify_html::Cfg::spec_compliant() }; // Static files @@ -147,24 +146,31 @@ fn init(dist_dir: String, static_dir: String, templates_dir: String) -> String { } /// Minify some assets for production -fn minify_and_copy(cfg: &Cfg, path: PathBuf, path_with_dist: String) { +fn minify_and_copy(cfg: &minify_html::Cfg, path: PathBuf, path_with_dist: String) { // Create folders let new_path = Path::new(&path_with_dist); fs::create_dir_all(new_path.parent().unwrap()).unwrap(); + let session = minify_js::Session::new(); let mut copy = true; if let Some(ext) = path.extension() { + let js_ext = "js"; + let current_ext = ext.to_string_lossy().to_lowercase(); // List of files who should be minified - if ["html", "css", "js", "svg", "webmanifest", "xml"] - .iter() - .any(|item| ext.to_string_lossy().to_lowercase().contains(item)) - { + if ["html", "css", js_ext, "svg", "webmanifest", "xml"].contains(¤t_ext.as_str()) { // We won't copy, we'll minify copy = false; // Minify let data = fs::read(&path).unwrap(); - let minified = minify(&data, cfg); + let minified = if current_ext == js_ext { + let mut out = Vec::new(); + minify_js::minify(&session, minify_js::TopLevelMode::Global, &data, &mut out) + .unwrap(); + out + } else { + minify_html::minify(&data, cfg) + }; // Write files let file = File::create(&path_with_dist);