Generate static file hashes once

This commit is contained in:
Jacob Hoffman-Andrews 2022-10-29 01:57:39 -07:00
parent bf25334066
commit 0b0bf10533
2 changed files with 23 additions and 15 deletions

View File

@ -85,12 +85,11 @@ pub(super) fn write_shared(
}
if options.emit.is_empty() || options.emit.contains(&EmitType::Toolchain) {
for f in static_files::STATIC_FILES_LIST {
let filename = cx.dst.join(
Path::new("static.files/").join(static_files::static_filename(f.filename, f.bytes)),
);
cx.shared.fs.write(filename, f.minified())?;
}
let static_dir = cx.dst.join(Path::new("static.files"));
static_files::for_each(|f: &static_files::StaticFile| {
let filename = static_dir.join(f.output_filename());
cx.shared.fs.write(filename, f.minified())
})?;
}
/// Read a file and return all lines that match the `"{crate}":{data},` format,

View File

@ -5,15 +5,19 @@
use rustc_data_structures::fx::FxHasher;
use std::hash::Hasher;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::{fmt, str};
pub(crate) struct StaticFile {
pub(crate) filename: &'static str,
pub(crate) filename: PathBuf,
pub(crate) bytes: &'static [u8],
}
impl StaticFile {
fn new(filename: &str, bytes: &'static [u8]) -> StaticFile {
Self { filename: static_filename(filename, bytes), bytes }
}
pub(crate) fn minified(&self) -> Vec<u8> {
if self.filename.ends_with(".css") {
minifier::css::minify(str::from_utf8(self.bytes).unwrap()).unwrap().to_string().into()
@ -24,8 +28,8 @@ impl StaticFile {
}
}
pub(crate) fn output_filename(&self) -> PathBuf {
static_filename(self.filename, self.bytes)
pub(crate) fn output_filename(&self) -> &Path {
&self.filename
}
}
@ -66,13 +70,18 @@ macro_rules! static_files {
$(pub $field: StaticFile,)+
}
pub(crate) const STATIC_FILES: StaticFiles = StaticFiles {
$($field: StaticFile { filename: $file_path, bytes: include_bytes!($file_path) },)+
};
pub(crate) static STATIC_FILES: std::sync::LazyLock<StaticFiles> = std::sync::LazyLock::new(|| StaticFiles {
$($field: StaticFile::new($file_path, include_bytes!($file_path)),)+
});
pub(crate) static STATIC_FILES_LIST: &[&'static StaticFile] = &[
pub(crate) fn for_each<E>(f: impl Fn(&StaticFile) -> Result<(), E>) -> Result<(), E> {
for sf in [
$(&STATIC_FILES.$field,)+
];
] {
f(sf)?
}
Ok(())
}
}
}