feat: minification #17

Merged
Anri merged 14 commits from minification into main 2023-04-09 19:30:04 +02:00
2 changed files with 34 additions and 36 deletions
Showing only changes of commit d1f1d5d478 - Show all commits

View file

@ -1,2 +1,3 @@
[general] [general]
dirs = ["dist/templates"] # Directories to search for templates, relative to the crate root.
dirs = ["dist/templates", "templates"]

View file

@ -89,17 +89,10 @@ pub fn get_config(file_path: &str) -> Config {
let internal_config = get_file_config(file_path); let internal_config = get_file_config(file_path);
let static_dir = "static".to_string(); let static_dir = "static".to_string();
let templates_dirs = get_askama_config().general.dirs; // TODO: Check if templates dir is coherent with the whole list
if templates_dirs.len() != 1 { let templates_dir = get_askama_config().general.dirs.last().unwrap().to_string();
panic!("Too many templates directories") // TODO: Check dist by askama config file
} let files_root = init("dist".to_string(), static_dir.clone(), templates_dir);
let mut template_data = templates_dirs[0].split('/');
let files_root = init(
template_data.next().unwrap().to_string(),
static_dir.clone(),
template_data.next().unwrap().to_string(),
);
Config { Config {
fc: internal_config, fc: internal_config,
@ -108,35 +101,40 @@ pub fn get_config(file_path: &str) -> Config {
} }
fn init(dist_dir: String, static_dir: String, templates_dir: String) -> String { fn init(dist_dir: String, static_dir: String, templates_dir: String) -> String {
// Be sure that we not gonna use the dist folder by deleting it // The static folder is minimized only in release mode
remove_dir_all(&dist_dir).unwrap_or_default(); if cfg!(debug_assertions) {
// Be sure that we not gonna use the dist folder by deleting it
remove_dir_all(dist_dir).unwrap_or_default();
let cfg = Cfg::spec_compliant(); ".".to_string()
} else {
let cfg = Cfg::spec_compliant();
// Static files // Static files
for entry in glob(&format!("{static_dir}/**/*.*")).unwrap() { for entry in glob(&format!("{static_dir}/**/*.*")).unwrap() {
let path = entry.unwrap(); let path = entry.unwrap();
let path_with_dist = path let path_with_dist = path
.to_string_lossy() .to_string_lossy()
.replace(&static_dir, &format!("{dist_dir}/{static_dir}")); .replace(&static_dir, &format!("{dist_dir}/{static_dir}"));
copy_to_dist(&cfg, path, path_with_dist); minify_and_copy(&cfg, path, path_with_dist);
}
// Template files
for entry in glob(&format!("{templates_dir}/**/*.*")).unwrap() {
let path = entry.unwrap();
let path_with_dist = path
.to_string_lossy()
.replace(&templates_dir, &format!("{dist_dir}/{templates_dir}"));
minify_and_copy(&cfg, path, path_with_dist);
}
dist_dir
} }
// Template files
for entry in glob(&format!("{templates_dir}/**/*.*")).unwrap() {
let path = entry.unwrap();
let path_with_dist = path
.to_string_lossy()
.replace(&templates_dir, &format!("{dist_dir}/{templates_dir}"));
copy_to_dist(&cfg, path, path_with_dist);
}
dist_dir
} }
fn copy_to_dist(cfg: &Cfg, path: PathBuf, path_with_dist: String) { fn minify_and_copy(cfg: &Cfg, path: PathBuf, path_with_dist: String) {
// Create folders // Create folders
let new_path = Path::new(&path_with_dist); let new_path = Path::new(&path_with_dist);
fs::create_dir_all(new_path.parent().unwrap()).unwrap(); fs::create_dir_all(new_path.parent().unwrap()).unwrap();
@ -147,7 +145,6 @@ fn copy_to_dist(cfg: &Cfg, path: PathBuf, path_with_dist: String) {
if ["html", "css", "js", "svg", "webmanifest", "xml"] if ["html", "css", "js", "svg", "webmanifest", "xml"]
.iter() .iter()
.any(|item| ext.to_string_lossy().to_lowercase().contains(item)) .any(|item| ext.to_string_lossy().to_lowercase().contains(item))
&& !cfg!(debug_assertions)
{ {
// We won't copy, we'll minify // We won't copy, we'll minify
copy = false; copy = false;