From 68471b6e4fd4860b5fd712fe868776bb45194228 Mon Sep 17 00:00:00 2001 From: blek Date: Mon, 6 Nov 2023 13:46:05 +1000 Subject: [PATCH] minify templates on compile time --- filed/build.rs | 28 +++++++++++++++++++ filed/templates/.gitignore | 2 ++ filed/templates/{ => source}/authors.html | 0 .../{ => source}/bad_action_req.html | 0 filed/templates/{ => source}/base.html | 0 filed/templates/source/blek.svg | 1 + .../templates/{ => source}/curlapi_help.html | 25 ++++++++--------- filed/templates/{ => source}/error.html | 0 filed/templates/{ => source}/index.html | 0 filed/templates/{ => source}/license.html | 0 .../{ => source}/passworded-files.html | 0 filed/templates/{ => source}/seotags.html | 0 filed/templates/{ => source}/tos.html | 0 .../{ => source}/upload_success.html | 0 filed/templates/{ => source}/uploaded.html | 0 15 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 filed/templates/.gitignore rename filed/templates/{ => source}/authors.html (100%) rename filed/templates/{ => source}/bad_action_req.html (100%) rename filed/templates/{ => source}/base.html (100%) create mode 100644 filed/templates/source/blek.svg rename filed/templates/{ => source}/curlapi_help.html (89%) rename filed/templates/{ => source}/error.html (100%) rename filed/templates/{ => source}/index.html (100%) rename filed/templates/{ => source}/license.html (100%) rename filed/templates/{ => source}/passworded-files.html (100%) rename filed/templates/{ => source}/seotags.html (100%) rename filed/templates/{ => source}/tos.html (100%) rename filed/templates/{ => source}/upload_success.html (100%) rename filed/templates/{ => source}/uploaded.html (100%) diff --git a/filed/build.rs b/filed/build.rs index a3b0f11..65d912e 100644 --- a/filed/build.rs +++ b/filed/build.rs @@ -9,6 +9,12 @@ fn asset_path(asset: &PathBuf) -> PathBuf { path } +fn template_path(template: &PathBuf) -> PathBuf { + let mut path = template.components().take(template.components().count() - 2).collect::(); + path.push(template.components().last().unwrap()); + path +} + fn extfilter(valid: String, x: Option<&OsStr>) -> bool { if x.is_none() { return false @@ -33,6 +39,7 @@ fn system(cmd: &str, args: &[&str]) -> Result> { fn main() { println!("cargo:rerun-if-changed=static/assets"); + println!("cargo:rerun-if-changed=templates/source"); let assets = fs::read_dir("static/assets").unwrap(); let assets = assets @@ -73,6 +80,27 @@ fn main() { .unwrap(); }); + // precompile templates + let templates = fs::read_dir("templates/source").unwrap(); + let templates = templates + .map(|x| x.unwrap().path().canonicalize().unwrap()) + .filter(|x| extfilter("html".into(), x.extension())) + .collect::>(); + + templates.iter().for_each(|template| { + Command::new("html-minifier") + .arg(template.canonicalize().unwrap()) + .arg("--collapse-whitespace") + // .arg("--minify-js") + // .arg("--minify-css") + // .arg("--keep-closing-slash") + .arg("--output") + .arg(template_path(template)) + .spawn() + .unwrap(); + + }); + let commit = system("git", &["rev-parse", "HEAD"]).map_err(|x| x.to_string()); let branch = system("git", &["rev-parse", "--abbrev-ref", "HEAD"]).map_err(|x| x.to_string()); diff --git a/filed/templates/.gitignore b/filed/templates/.gitignore new file mode 100644 index 0000000..136d8f1 --- /dev/null +++ b/filed/templates/.gitignore @@ -0,0 +1,2 @@ +./*.html +!.gitignore \ No newline at end of file diff --git a/filed/templates/authors.html b/filed/templates/source/authors.html similarity index 100% rename from filed/templates/authors.html rename to filed/templates/source/authors.html diff --git a/filed/templates/bad_action_req.html b/filed/templates/source/bad_action_req.html similarity index 100% rename from filed/templates/bad_action_req.html rename to filed/templates/source/bad_action_req.html diff --git a/filed/templates/base.html b/filed/templates/source/base.html similarity index 100% rename from filed/templates/base.html rename to filed/templates/source/base.html diff --git a/filed/templates/source/blek.svg b/filed/templates/source/blek.svg new file mode 100644 index 0000000..55f522a --- /dev/null +++ b/filed/templates/source/blek.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/filed/templates/curlapi_help.html b/filed/templates/source/curlapi_help.html similarity index 89% rename from filed/templates/curlapi_help.html rename to filed/templates/source/curlapi_help.html index d8ef7cf..5f8a716 100644 --- a/filed/templates/curlapi_help.html +++ b/filed/templates/source/curlapi_help.html @@ -96,19 +96,18 @@ {% endblock %} \ No newline at end of file diff --git a/filed/templates/error.html b/filed/templates/source/error.html similarity index 100% rename from filed/templates/error.html rename to filed/templates/source/error.html diff --git a/filed/templates/index.html b/filed/templates/source/index.html similarity index 100% rename from filed/templates/index.html rename to filed/templates/source/index.html diff --git a/filed/templates/license.html b/filed/templates/source/license.html similarity index 100% rename from filed/templates/license.html rename to filed/templates/source/license.html diff --git a/filed/templates/passworded-files.html b/filed/templates/source/passworded-files.html similarity index 100% rename from filed/templates/passworded-files.html rename to filed/templates/source/passworded-files.html diff --git a/filed/templates/seotags.html b/filed/templates/source/seotags.html similarity index 100% rename from filed/templates/seotags.html rename to filed/templates/source/seotags.html diff --git a/filed/templates/tos.html b/filed/templates/source/tos.html similarity index 100% rename from filed/templates/tos.html rename to filed/templates/source/tos.html diff --git a/filed/templates/upload_success.html b/filed/templates/source/upload_success.html similarity index 100% rename from filed/templates/upload_success.html rename to filed/templates/source/upload_success.html diff --git a/filed/templates/uploaded.html b/filed/templates/source/uploaded.html similarity index 100% rename from filed/templates/uploaded.html rename to filed/templates/source/uploaded.html