minify css on build

This commit is contained in:
blek 2023-10-14 00:10:47 +10:00
parent 6471a7b8d5
commit 2c21bdbf57
Signed by: blek
GPG Key ID: 14546221E3595D0C
11 changed files with 82 additions and 0 deletions

46
filed/Cargo.lock generated
View File

@ -191,6 +191,12 @@ dependencies = [
"tokio-util", "tokio-util",
] ]
[[package]]
name = "convert_case"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
[[package]] [[package]]
name = "core-foundation-sys" name = "core-foundation-sys"
version = "0.8.4" version = "0.8.4"
@ -216,12 +222,36 @@ dependencies = [
"typenum", "typenum",
] ]
[[package]]
name = "css-minify"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "874c6e2d19f8d4a285083b11a3241bfbe01ac3ed85f26e1e6b34888d960552bd"
dependencies = [
"derive_more",
"indexmap",
"nom",
]
[[package]] [[package]]
name = "data-encoding" name = "data-encoding"
version = "2.4.0" version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308" checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308"
[[package]]
name = "derive_more"
version = "0.99.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321"
dependencies = [
"convert_case",
"proc-macro2",
"quote",
"rustc_version",
"syn 1.0.109",
]
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.10.7" version = "0.10.7"
@ -279,6 +309,7 @@ dependencies = [
"askama", "askama",
"bytes", "bytes",
"chrono", "chrono",
"css-minify",
"dotenvy", "dotenvy",
"femme", "femme",
"futures-util", "futures-util",
@ -927,6 +958,15 @@ version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "rustc_version"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
dependencies = [
"semver",
]
[[package]] [[package]]
name = "rustls-pemfile" name = "rustls-pemfile"
version = "1.0.3" version = "1.0.3"
@ -948,6 +988,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
[[package]]
name = "semver"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.188" version = "1.0.188"

View File

@ -27,3 +27,6 @@ warp = "0.3.6"
[profile.release] [profile.release]
opt-level = 'z' opt-level = 'z'
lto = true lto = true
[build-dependencies]
css-minify = "0.3.1"

29
filed/build.rs Normal file
View File

@ -0,0 +1,29 @@
use std::{fs, path::PathBuf};
use css_minify::optimizations::{Minifier, Level};
fn main() {
let assets = fs::read_dir("static/assets").unwrap();
let assets = assets
.map(|x| x.unwrap().path())
.filter(|x| x.extension().is_some())
.filter(|x| {
let ext = x.extension().clone().unwrap().to_str().unwrap().to_string();
ext == "css"
})
.collect::<Vec<PathBuf>>();
assets.iter().for_each(|asset| {
let bundled = Minifier::default().minify(
String::from_utf8(fs::read(asset).unwrap()).unwrap().as_str(),
Level::Zero
).unwrap();
fs::write({
let mut path = asset.components().take(1).collect::<PathBuf>();
path.push(asset.components().last().unwrap());
println!("{:?}", path);
path
}, bundled).unwrap();
panic!()
})
}

4
filed/static/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.js
*.css
!assets/*.js
!assets/*.css