mod assets;
mod html;
mod path;
mod static_assets;
use std::{
collections::HashMap,
fmt, fs,
ops::BitOr,
path::{Path, PathBuf},
str::FromStr,
};
use walkdir::WalkDir;
use assets::asset_process;
use html::{html_asset_refs_extract, html_minify, html_rewrite};
use path::{asset_ref_path_resolve, asset_ref_rewrite, dist_guard};
use static_assets::static_asset_copy;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Minify(u8);
impl Minify {
pub const NONE: Minify = Minify(0);
pub const CSS: Minify = Minify(1 << 0);
pub const JS: Minify = Minify(1 << 1);
pub const HTML: Minify = Minify(1 << 2);
pub const ALL: Minify = Minify(0b111);
pub const fn contains(self, targets: Minify) -> bool {
self.0 & targets.0 == targets.0
}
pub const fn union(self, other: Minify) -> Minify {
Minify(self.0 | other.0)
}
pub const fn is_empty(self) -> bool {
self.0 == 0
}
pub const fn is_all(self) -> bool {
self.0 == Minify::ALL.0
}
}
impl BitOr for Minify {
type Output = Minify;
fn bitor(self, other: Minify) -> Minify {
self.union(other)
}
}
impl FromStr for Minify {
type Err = String;
fn from_str(value: &str) -> Result<Self, Self::Err> {
let value = value.trim();
if value.is_empty() {
return Err("must not be empty (expected: css, js, html, all, none)".into());
}
let parts: Vec<&str> = value.split(',').map(str::trim).collect();
let multiple = parts.len() > 1;
let mut targets = Minify::NONE;
for part in parts {
let lower = part.to_ascii_lowercase();
let target = match lower.as_str() {
"css" => Minify::CSS,
"js" => Minify::JS,
"html" => Minify::HTML,
"all" | "none" if multiple => {
return Err(format!("{lower:?} cannot be combined with other targets"));
}
"all" => Minify::ALL,
"none" => Minify::NONE,
other => {
return Err(format!(
"unknown minify target {other:?} (expected: css, js, html, all, none)"
));
}
};
targets = targets.union(target);
}
Ok(targets)
}
}
impl fmt::Display for Minify {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.is_empty() {
return f.write_str("none");
}
if self.is_all() {
return f.write_str("all");
}
let mut sep = "";
for (target, name) in [
(Minify::CSS, "css"),
(Minify::JS, "js"),
(Minify::HTML, "html"),
] {
if self.contains(target) {
write!(f, "{sep}{name}")?;
sep = ",";
}
}
Ok(())
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct BuildOptions {
pub minify: Minify,
pub sourcemap: bool,
pub hash: bool,
pub base: Option<String>,
}
impl BuildOptions {
pub const DEV: BuildOptions = BuildOptions {
minify: Minify::NONE,
sourcemap: true,
hash: false,
base: None,
};
pub const PROD: BuildOptions = BuildOptions {
minify: Minify::ALL,
sourcemap: false,
hash: true,
base: None,
};
pub fn server_base(&self) -> String {
self.base.clone().unwrap_or_default()
}
}
pub async fn build(src: &Path, dist: &Path, options: &BuildOptions) -> Result<Vec<String>, String> {
if !src.is_dir() {
return Err(format!("source directory not found: {}", src.display()));
}
let src = src
.canonicalize()
.map_err(|err| format!("cannot resolve {}: {err}", src.display()))?;
dist_guard(&src, dist)?;
let page_path_list = page_path_list_build(&src);
if page_path_list.is_empty() {
return Err(format!("no .html pages found in {}", src.display()));
}
let mut warnings = Vec::new();
let dist_tmp = dist
.parent()
.unwrap_or_else(|| Path::new("."))
.join(format!(
".{}.tmp-{}",
dist.file_name()
.and_then(|name| name.to_str())
.unwrap_or("out"),
std::process::id()
));
if let Err(err) = static_asset_copy(&src, &dist_tmp, &mut warnings) {
let _ = fs::remove_dir_all(&dist_tmp);
return Err(err);
}
if let Err(err) =
page_list_bundle(&page_path_list, &src, &dist_tmp, options, &mut warnings).await
{
let _ = fs::remove_dir_all(&dist_tmp);
return Err(err);
}
if dist.exists() {
fs::remove_dir_all(dist)
.map_err(|err| format!("failed to clean {}: {err}", dist.display()))?;
}
fs::rename(&dist_tmp, dist)
.map_err(|err| format!("failed to move build into {}: {err}", dist.display()))?;
Ok(warnings)
}
async fn page_list_bundle(
page_path_list: &[PathBuf],
src: &Path,
dist: &Path,
options: &BuildOptions,
warnings: &mut Vec<String>,
) -> Result<(), String> {
fs::create_dir_all(dist)
.map_err(|err| format!("failed to create {}: {err}", dist.display()))?;
let mut cache: HashMap<PathBuf, String> = HashMap::new();
for page_path in page_path_list {
page_bundle(page_path, src, dist, options, &mut cache, warnings).await?;
}
Ok(())
}
fn page_path_list_build(dir: &Path) -> Vec<PathBuf> {
let mut pages: Vec<PathBuf> = WalkDir::new(dir)
.into_iter()
.filter_map(Result::ok)
.map(|e| e.into_path())
.filter(|p| {
p.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("html"))
})
.collect();
pages.sort();
pages
}
async fn page_bundle(
page_path: &Path,
src: &Path,
dist: &Path,
options: &BuildOptions,
cache: &mut HashMap<PathBuf, String>,
warnings: &mut Vec<String>,
) -> Result<(), String> {
let html = fs::read_to_string(page_path)
.map_err(|err| format!("{} unreadable: {err}", page_path.display()))?;
let asset_refs = html_asset_refs_extract(&html, page_path, warnings)?;
let mut asset_ref_rewritten_map: HashMap<String, String> = HashMap::new();
for asset_ref in asset_refs {
if let Some(asset_path) =
asset_ref_path_resolve(src, page_path, &asset_ref, options.base.as_deref())?
{
let file_name = match cache.get(&asset_path) {
Some(file_name) => file_name.clone(),
None => {
let name =
asset_process(&asset_path, src, dist, options, cache, warnings).await?;
cache.insert(asset_path.clone(), name.clone());
name
}
};
let asset_ref_rewritten = asset_ref_rewrite(&asset_ref, &file_name);
asset_ref_rewritten_map.insert(asset_ref, asset_ref_rewritten);
} else if options.base.is_none() && path::is_root_relative(asset_ref.trim()) {
warnings.push(path::asset_ref_skip_warn(&asset_ref, page_path));
}
}
let html = html_rewrite(&html, &asset_ref_rewritten_map)?;
let html = if options.minify.contains(Minify::HTML) {
html_minify(&html, options.minify)?
} else {
html
};
let out_page_path = dist.join(
page_path
.strip_prefix(src)
.expect("page path is always under src"),
);
if let Some(parent) = out_page_path.parent() {
fs::create_dir_all(parent)
.map_err(|err| format!("failed to create {}: {err}", parent.display()))?;
}
fs::write(&out_page_path, html)
.map_err(|err| format!("failed to write {}: {err}", out_page_path.display()))
}
#[cfg(test)]
mod tests {
use std::fs;
use tempfile::tempdir;
use super::*;
#[test]
fn server_base_maps_none_and_some() {
assert_eq!(BuildOptions::DEV.server_base(), "");
let app = BuildOptions {
base: Some("/app".into()),
..BuildOptions::DEV
};
assert_eq!(app.server_base(), "/app");
let root = BuildOptions {
base: Some("/".into()),
..BuildOptions::DEV
};
assert_eq!(root.server_base(), "/");
}
#[test]
fn page_path_list_build_finds_and_sorts_html() {
let dir = tempdir().unwrap();
let root = fs::canonicalize(dir.path()).unwrap();
for rel in [
"index.html",
"ABOUT.HTML",
"a/b.html",
"a/c.html",
"styles.css",
"noext",
"a/b.txt",
] {
let path = root.join(rel);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).unwrap();
}
fs::write(path, "").unwrap();
}
let pages = page_path_list_build(&root);
let names: Vec<String> = pages
.iter()
.map(|p| {
p.strip_prefix(&root)
.unwrap()
.to_string_lossy()
.replace('\\', "/")
})
.collect();
assert_eq!(names, ["ABOUT.HTML", "a/b.html", "a/c.html", "index.html"]);
}
#[test]
fn page_path_list_build_empty_returns_empty() {
let dir = tempdir().unwrap();
let root = fs::canonicalize(dir.path()).unwrap();
assert!(page_path_list_build(&root).is_empty());
}
#[tokio::test]
async fn page_bundle_unreadable_page_errs() {
let dir = tempdir().unwrap();
let root = fs::canonicalize(dir.path()).unwrap();
let src = root.join("src");
let dist = root.join("dist");
fs::create_dir_all(&src).unwrap();
let mut cache = HashMap::new();
let err = page_bundle(
&src.join("missing.html"),
&src,
&dist,
&BuildOptions::PROD,
&mut cache,
&mut vec![],
)
.await
.unwrap_err();
assert!(err.contains("unreadable"), "got: {err}");
}
#[tokio::test]
async fn page_bundle_prod_minifies_and_hashes() {
let dir = tempdir().unwrap();
let root = fs::canonicalize(dir.path()).unwrap();
let src = root.join("src");
let dist = root.join("dist");
fs::create_dir_all(src.join("styles")).unwrap();
fs::create_dir_all(src.join("scripts")).unwrap();
fs::write(src.join("styles/main.css"), "body { color: black; }\n").unwrap();
fs::write(src.join("scripts/app.js"), "console.log('app');\n").unwrap();
fs::write(
src.join("index.html"),
"<html>\n <head>\n <link rel=\"stylesheet\" href=\"./styles/main.css\" />\n <script type=\"module\" src=\"./scripts/app.js\"></script>\n </head>\n <body>\n <p>hello world</p>\n </body>\n</html>\n",
)
.unwrap();
let html_source = fs::read_to_string(src.join("index.html")).unwrap();
let mut cache = HashMap::new();
page_bundle(
&src.join("index.html"),
&src,
&dist,
&BuildOptions::PROD,
&mut cache,
&mut vec![],
)
.await
.unwrap();
let css_name = cache.get(&src.join("styles/main.css")).unwrap();
assert!(css_name.starts_with("main-"), "got: {css_name}");
let js_name = cache.get(&src.join("scripts/app.js")).unwrap();
assert!(js_name.starts_with("app-"), "got: {js_name}");
let out_html = fs::read_to_string(dist.join("index.html")).unwrap();
assert!(out_html.contains(css_name));
assert!(out_html.contains(js_name));
assert!(!out_html.contains("main.css") && !out_html.contains("app.js"));
assert!(
out_html.len() < html_source.len(),
"expected minified output, got: {out_html}"
);
}
}