diff --git a/Cargo.lock b/Cargo.lock index cf071cb..37150f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,15 @@ dependencies = [ "version_check", ] +[[package]] +name = "aho-corasick" +version = "0.7.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4f55bd91a0978cbfd91c457a164bab8b4001c833b7f323132c0a4e1922dd44e" +dependencies = [ + "memchr", +] + [[package]] name = "anyhow" version = "1.0.65" @@ -755,6 +764,15 @@ dependencies = [ "unicase", ] +[[package]] +name = "minifier" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eb022374af2f446981254e6bf9efb6e2c9e1a53176d395fca02792fd4435729" +dependencies = [ + "regex", +] + [[package]] name = "mio" version = "0.6.23" @@ -1244,6 +1262,23 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" + [[package]] name = "remove_dir_all" version = "0.5.3" @@ -1904,6 +1939,7 @@ dependencies = [ "handlebars", "hotwatch", "lol_html", + "minifier", "pulldown-cmark", "serde", "serde_yaml", diff --git a/Cargo.toml b/Cargo.toml index 4a81b52..da2a836 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ gray_matter = "0.2" handlebars = "4.1" hotwatch = { version = "0.4", optional = true } lol_html = "0.3" +minifier = { version = "0.2", features = ["html"] } pulldown-cmark = { version = "0.9", default-features = false, features = ["simd"] } serde = { version = "1", features = ["derive"] } serde_yaml = "0.9" diff --git a/src/builder.rs b/src/builder.rs index d349d43..30c1f79 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -174,7 +174,10 @@ impl<'a> SiteBuilder<'a> { rewriter.write(out.as_bytes())?; rewriter.end()?; - let out = String::from_utf8(output)?; + let mut out = String::from_utf8(output)?; + if !self.serving { + out = minifier::html::minify(&out); + } let out_path = self.build_path.join(page_name).with_extension("html"); std::fs::create_dir_all(out_path.parent().unwrap()) @@ -205,7 +208,12 @@ impl<'a> SiteBuilder<'a> { let sheet_path = sass_path.join(sheet); if let Some(sheet_path) = sheet_path.to_str() { match grass::from_path(sheet_path, &grass::Options::default()) { - Ok(css) => { + Ok(mut css) => { + if !self.serving { + css = minifier::css::minify(&css) + .map_err(|err| anyhow::anyhow!(err))? + .to_string(); + } std::fs::write(styles_path.join(sheet).with_extension("css"), css) .with_context(|| { format!("Failed to write new CSS file for Sass: {:?}", sheet)