···11use std::{
22+ env,
23 fs::{self, remove_dir_all, File},
34 io::{self, Write},
45 path::{Path, PathBuf},
56 str::FromStr,
66- time::SystemTime,
77+ time::{SystemTime, UNIX_EPOCH},
78};
89910use crate::{
···5556 let tmp_dir = dist_dir.join("_tmp");
5657 let static_dir = PathBuf::from_str(&options.static_dir)?;
57585858- let _ = fs::remove_dir_all(&dist_dir);
5959+ let old_dist_tmp_dir = if options.clean_output_dir {
6060+ let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
6161+ let num = (duration.as_secs() + duration.subsec_nanos() as u64) % 100000;
6262+ let new_dir_for_old_dist = env::temp_dir().join(format!("maudit_old_dist_{}", num));
6363+ let _ = fs::rename(&dist_dir, &new_dir_for_old_dist);
6464+ Some(new_dir_for_old_dist)
6565+ } else {
6666+ None
6767+ };
6868+6969+ let should_clear_dist = options.clean_output_dir;
7070+ let clean_up_handle = tokio::spawn(async move {
7171+ if should_clear_dist {
7272+ let _ = fs::remove_dir_all(old_dist_tmp_dir.unwrap());
7373+ }
7474+ });
7575+5976 fs::create_dir_all(&dist_dir)?;
6077 fs::create_dir_all(&assets_dir)?;
6178···103120 let mut build_pages_styles: FxHashSet<assets::Style> = FxHashSet::default();
104121105122 let mut page_count = 0;
123123+124124+ // TODO: This is fully serial. Parallelizing it is trivial with Rayon and stuff, but it doesn't necessarily make it
125125+ // faster in all cases, making it sometimes even slower due to the overhead. It'd be great to investigate and benchmark
126126+ // this.
106127 for route in routes {
107128 let params_def = extract_params_from_raw_route(&route.route_raw());
108129 let route_type = get_route_type_from_route_params(¶ms_def);
···313334314335 info!(target: "SKIP_FORMAT", "{}", "");
315336 info!(target: "build", "{}", format!("Build completed in {}", format_elapsed_time(build_start.elapsed(), §ion_format_options).unwrap()).bold());
337337+338338+ clean_up_handle.await.unwrap();
316339317340 Ok(build_metadata)
318341}
+6
crates/maudit/src/build/options.rs
···3030/// assets_dir: "_assets".to_string(),
3131/// static_dir: "static".to_string(),
3232/// tailwind_binary_path: "./node_modules/.bin/tailwindcss".to_string(),
3333+/// ..Default::default()
3334/// },
3435/// )
3536/// }
···3940 pub assets_dir: String,
4041 pub static_dir: String,
4142 pub tailwind_binary_path: String,
4343+ /// Whether to clean the output directory before building.
4444+ ///
4545+ /// At the speed Maudit operates at, not cleaning the output directory may offer a significant performance improvement at the cost of potentially serving stale content.
4646+ pub clean_output_dir: bool,
4247}
43484449/// Provides default values for [`crate::coronate()`]. Designed to work for most projects.
···6469 assets_dir: "_maudit".to_string(),
6570 static_dir: "static".to_string(),
6671 tailwind_binary_path: "./node_modules/.bin/tailwindcss".to_string(),
7272+ clean_output_dir: true,
6773 }
6874 }
6975}