|
| 1 | +use crate::render::fs::ensure_directory; |
| 2 | +use anyhow::Context; |
| 3 | +use rocket::serde::Serialize; |
| 4 | +use sass_rs::{Options, compile_file}; |
| 5 | +use std::fs; |
| 6 | +use std::hash::{DefaultHasher, Hasher}; |
| 7 | +use std::path::Path; |
| 8 | + |
| 9 | +fn write_file(path: &Path, bytes: &[u8]) -> anyhow::Result<()> { |
| 10 | + ensure_directory(path)?; |
| 11 | + Ok(fs::write(path, bytes)?) |
| 12 | +} |
| 13 | + |
| 14 | +fn hash_string(content: &str) -> String { |
| 15 | + let mut hasher = DefaultHasher::new(); |
| 16 | + hasher.write(content.as_bytes()); |
| 17 | + hasher.finish().to_string() |
| 18 | +} |
| 19 | + |
| 20 | +fn relative_url(path: &Path, out_dir: &Path) -> anyhow::Result<String> { |
| 21 | + Ok(path.strip_prefix(out_dir)?.to_str().unwrap().to_string()) |
| 22 | +} |
| 23 | + |
| 24 | +/// Compiles SASS file, stores it in `out_dir` and returns the relative URL to it. |
| 25 | +fn compile_sass(root_dir: &Path, out_dir: &Path, filename: &str) -> anyhow::Result<String> { |
| 26 | + let scss_file = root_dir |
| 27 | + .join("src") |
| 28 | + .join("styles") |
| 29 | + .join(format!("{filename}.scss")); |
| 30 | + |
| 31 | + let css = compile_file(&scss_file, Options::default()) |
| 32 | + .map_err(|e| anyhow::anyhow!("{e}")) |
| 33 | + .with_context(|| anyhow::anyhow!("couldn't compile sass: {}", scss_file.display()))?; |
| 34 | + |
| 35 | + let css_sha = format!("{filename}_{}", hash_string(&css)); |
| 36 | + let out_css_path = out_dir |
| 37 | + .join("static") |
| 38 | + .join("styles") |
| 39 | + .join(format!("{css_sha}.css")); |
| 40 | + |
| 41 | + write_file(&out_css_path, &css.into_bytes()) |
| 42 | + .with_context(|| anyhow::anyhow!("couldn't write css file: {}", out_css_path.display()))?; |
| 43 | + |
| 44 | + Ok(relative_url(&out_css_path, &out_dir)?) |
| 45 | +} |
| 46 | + |
| 47 | +fn concat_files( |
| 48 | + root_dir: &Path, |
| 49 | + out_dir: &Path, |
| 50 | + files: &[&str], |
| 51 | + directory: &str, |
| 52 | + extension: &str, |
| 53 | +) -> anyhow::Result<String> { |
| 54 | + let mut concatted = String::new(); |
| 55 | + for filestem in files { |
| 56 | + let vendor_path = root_dir |
| 57 | + .join("static") |
| 58 | + .join(directory) |
| 59 | + .join(format!("{filestem}.{extension}")); |
| 60 | + let contents = fs::read_to_string(vendor_path) |
| 61 | + .with_context(|| anyhow::anyhow!("couldn't read vendor {extension}"))?; |
| 62 | + concatted.push_str(&contents); |
| 63 | + } |
| 64 | + |
| 65 | + let file_sha = format!("vendor_{}", hash_string(&concatted)); |
| 66 | + let out_file_path = out_dir |
| 67 | + .join("static") |
| 68 | + .join(directory) |
| 69 | + .join(format!("{file_sha}.{extension}")); |
| 70 | + |
| 71 | + write_file(Path::new(&out_file_path), concatted.as_bytes()) |
| 72 | + .with_context(|| anyhow::anyhow!("couldn't write vendor {extension}"))?; |
| 73 | + |
| 74 | + Ok(relative_url(&out_file_path, &out_dir)?) |
| 75 | +} |
| 76 | + |
| 77 | +fn concat_vendor_css(root_dir: &Path, out_dir: &Path, files: Vec<&str>) -> anyhow::Result<String> { |
| 78 | + concat_files(root_dir, out_dir, &files, "styles", "css") |
| 79 | +} |
| 80 | + |
| 81 | +fn concat_app_js(root_dir: &Path, out_dir: &Path, files: Vec<&str>) -> anyhow::Result<String> { |
| 82 | + concat_files(root_dir, out_dir, &files, "scripts", "js") |
| 83 | +} |
| 84 | + |
| 85 | +#[derive(Serialize, Debug)] |
| 86 | +pub struct CSSFiles { |
| 87 | + app: String, |
| 88 | + fonts: String, |
| 89 | + vendor: String, |
| 90 | +} |
| 91 | + |
| 92 | +#[derive(Serialize, Debug)] |
| 93 | +pub struct JSFiles { |
| 94 | + app: String, |
| 95 | +} |
| 96 | + |
| 97 | +#[derive(Serialize, Debug)] |
| 98 | +pub struct AssetFiles { |
| 99 | + css: CSSFiles, |
| 100 | + js: JSFiles, |
| 101 | +} |
| 102 | + |
| 103 | +/// Compile all statics JS/CSS assets into the `out_dir` directory and return a structure |
| 104 | +/// that holds paths to them, based on the passed `baseurl`. |
| 105 | +pub fn compile_assets( |
| 106 | + root_dir: &Path, |
| 107 | + out_dir: &Path, |
| 108 | + baseurl: &str, |
| 109 | +) -> anyhow::Result<AssetFiles> { |
| 110 | + let app_css_file = compile_sass(root_dir, out_dir, "app")?; |
| 111 | + let fonts_css_file = compile_sass(root_dir, out_dir, "fonts")?; |
| 112 | + let vendor_css_file = concat_vendor_css(root_dir, out_dir, vec!["tachyons"])?; |
| 113 | + let app_js_file = concat_app_js(root_dir, out_dir, vec!["tools-install"])?; |
| 114 | + |
| 115 | + Ok(AssetFiles { |
| 116 | + css: CSSFiles { |
| 117 | + app: format!("{baseurl}{app_css_file}"), |
| 118 | + fonts: format!("{baseurl}{fonts_css_file}"), |
| 119 | + vendor: format!("{baseurl}{vendor_css_file}"), |
| 120 | + }, |
| 121 | + js: JSFiles { |
| 122 | + app: format!("{baseurl}{app_js_file}"), |
| 123 | + }, |
| 124 | + }) |
| 125 | +} |
0 commit comments