feat: minification
#17
2 changed files with 36 additions and 34 deletions
|
@ -1,3 +1,2 @@
|
||||||
[general]
|
[general]
|
||||||
# Directories to search for templates, relative to the crate root.
|
dirs = ["dist/templates"]
|
||||||
dirs = ["dist/templates", "templates"]
|
|
||||||
|
|
|
@ -89,10 +89,17 @@ 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();
|
||||||
// TODO: Check if templates dir is coherent with the whole list
|
let templates_dirs = get_askama_config().general.dirs;
|
||||||
let templates_dir = get_askama_config().general.dirs.last().unwrap().to_string();
|
if templates_dirs.len() != 1 {
|
||||||
// TODO: Check dist by askama config file
|
panic!("Too many templates directories")
|
||||||
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,
|
||||||
|
@ -101,13 +108,9 @@ 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 {
|
||||||
// The static folder is minimized only in release mode
|
|
||||||
if cfg!(debug_assertions) {
|
|
||||||
// Be sure that we not gonna use the dist folder by deleting it
|
// Be sure that we not gonna use the dist folder by deleting it
|
||||||
remove_dir_all(dist_dir).unwrap_or_default();
|
remove_dir_all(&dist_dir).unwrap_or_default();
|
||||||
|
|
||||||
".".to_string()
|
|
||||||
} else {
|
|
||||||
let cfg = Cfg::spec_compliant();
|
let cfg = Cfg::spec_compliant();
|
||||||
|
|
||||||
// Static files
|
// Static files
|
||||||
|
@ -117,7 +120,7 @@ fn init(dist_dir: String, static_dir: String, templates_dir: String) -> String {
|
||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.replace(&static_dir, &format!("{dist_dir}/{static_dir}"));
|
.replace(&static_dir, &format!("{dist_dir}/{static_dir}"));
|
||||||
|
|
||||||
minify_and_copy(&cfg, path, path_with_dist);
|
copy_to_dist(&cfg, path, path_with_dist);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Template files
|
// Template files
|
||||||
|
@ -127,14 +130,13 @@ fn init(dist_dir: String, static_dir: String, templates_dir: String) -> String {
|
||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.replace(&templates_dir, &format!("{dist_dir}/{templates_dir}"));
|
.replace(&templates_dir, &format!("{dist_dir}/{templates_dir}"));
|
||||||
|
|
||||||
minify_and_copy(&cfg, path, path_with_dist);
|
copy_to_dist(&cfg, path, path_with_dist);
|
||||||
}
|
}
|
||||||
|
|
||||||
dist_dir
|
dist_dir
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn minify_and_copy(cfg: &Cfg, path: PathBuf, path_with_dist: String) {
|
fn copy_to_dist(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();
|
||||||
|
@ -145,6 +147,7 @@ fn minify_and_copy(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;
|
||||||
|
|
Loading…
Reference in a new issue