minify js

This commit is contained in:
blek 2023-10-14 13:19:09 +10:00
parent 5fff047b92
commit b1292a2f47
Signed by: blek
GPG Key ID: 14546221E3595D0C
4 changed files with 109 additions and 13 deletions

View File

@ -5,4 +5,8 @@ RUN cargo install cargo-watch && \
touch /opt/code/dev-entry.sh && \
chmod +x /opt/code/dev-entry.sh
RUN apt update && \
apt install nodejs npm -y --no-install-recommends && \
npm i -g uglify-js
CMD [ "/opt/code/dev-entry.sh" ]

62
filed/Cargo.lock generated
View File

@ -17,6 +17,26 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "ahash"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f"
dependencies = [
"cfg-if",
"once_cell",
"version_check",
]
[[package]]
name = "aho-corasick"
version = "0.7.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
dependencies = [
"memchr",
]
[[package]]
name = "android-tzdata"
version = "0.1.1"
@ -315,6 +335,7 @@ dependencies = [
"futures-util",
"hex",
"log",
"minify-js",
"num",
"redis",
"serde",
@ -447,6 +468,16 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hashbrown"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e"
dependencies = [
"ahash",
"bumpalo",
]
[[package]]
name = "headers"
version = "0.3.9"
@ -613,7 +644,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
dependencies = [
"autocfg",
"hashbrown",
"hashbrown 0.12.3",
]
[[package]]
@ -631,6 +662,12 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.148"
@ -675,6 +712,16 @@ dependencies = [
"unicase",
]
[[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",
]
[[package]]
name = "minimal-lexical"
version = "0.2.1"
@ -833,6 +880,19 @@ version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "parse-js"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ec3b11d443640ec35165ee8f6f0559f1c6f41878d70330fe9187012b5935f02"
dependencies = [
"aho-corasick",
"bumpalo",
"hashbrown 0.13.2",
"lazy_static",
"memchr",
]
[[package]]
name = "percent-encoding"
version = "2.3.0"

View File

@ -30,3 +30,4 @@ lto = true
[build-dependencies]
css-minify = "0.3.1"
minify-js = "0.5.6"

View File

@ -1,28 +1,59 @@
use std::{fs, path::PathBuf};
use std::{fs, path::PathBuf, ffi::OsStr, process::Command};
use css_minify::optimizations::{Minifier, Level};
fn asset_path(asset: &PathBuf) -> PathBuf {
let mut path = asset.components().take(1).collect::<PathBuf>();
path.push(asset.components().last().unwrap());
path
}
fn extfilter(valid: String, x: Option<&OsStr>) -> bool {
if x.is_none() {
return false
}
let ext = x.clone().unwrap().to_str().unwrap().to_string();
ext == valid
}
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 styles = assets
.iter()
.filter(|x| {
extfilter("css".into(), x.extension())
})
.collect::<Vec<&PathBuf>>();
let scripts = assets
.iter()
.filter(|x| {
extfilter("js".into(), x.extension())
})
.collect::<Vec<&PathBuf>>();
styles.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();
fs::write(asset_path(asset), bundled).unwrap();
});
scripts.iter().for_each(|asset| {
Command::new("uglifyjs")
.arg("-c")
.arg(asset)
.arg("-o")
.arg(asset_path(asset))
.spawn()
.unwrap();
})
}