chore(frontend): update frontend build configuration

This commit is contained in:
ZhenYi 2026-04-20 15:45:35 +08:00
parent 26865f8dcf
commit 0c64122b80
2 changed files with 22 additions and 5 deletions

View File

@ -1,3 +1,4 @@
use md5::compute as md5_hash;
use std::{env, fs, path::PathBuf, process::Command};
fn run_pnpm(args: &[&str], cwd: &str) {
@ -66,15 +67,24 @@ fn main() {
let safe_name = key.replace('/', "_").replace('\\', "_");
let blob_path = blob_dir.join(&safe_name);
fs::copy(&file, &blob_path).unwrap();
// Compute ETag (MD5 hex of content) at build time
let content = fs::read(&file).unwrap();
let hash = md5_hash(&content);
let etag_literal = format!("\"{:x}\"", hash);
let key_literal = format!("\"{}\"", key.replace('"', "\\\""));
strs.push(format!(" ({}, include_bytes!(\"dist_blobs/{}\")),", key_literal, safe_name));
strs.push(format!(
" ({}, include_bytes!(\"dist_blobs/{}\"), {}),",
key_literal, safe_name, etag_literal
));
}
let out_file = out_dir.join("frontend.rs");
let content = format!(
"lazy_static::lazy_static! {{\n pub static ref FRONTEND: Vec<(&'static str, &'static [u8])> = vec![\n{} ];\n}}\n",
let generated = format!(
"lazy_static::lazy_static! {{\n pub static ref FRONTEND: Vec<(&'static str, &'static [u8], &'static str)> = vec![\n{} ];\n}}\n",
strs.join("\n")
);
fs::write(&out_file, content).unwrap();
fs::write(&out_file, generated).unwrap();
println!("cargo:include={}", out_file.display());
}

View File

@ -4,5 +4,12 @@ include!(concat!(env!("OUT_DIR"), "/frontend.rs"));
/// Returns the embedded frontend static asset for the given path, or `None` if not found.
pub fn get_frontend_asset(path: &str) -> Option<&'static [u8]> {
FRONTEND.iter().find(|(k, _)| *k == path).map(|(_, v)| *v)
FRONTEND.iter().find(|(k, _, _)| *k == path).map(|(_, v, _)| *v)
}
/// Returns the embedded frontend static asset and its ETag for the given path.
pub fn get_frontend_asset_with_etag(path: &str) -> Option<(&'static [u8], &'static str)> {
FRONTEND.iter()
.find(|(k, _, _)| *k == path)
.map(|(_, v, etag)| (v as &_, etag as &_))
}