Generate static file hashes once
This commit is contained in:
parent
bf25334066
commit
0b0bf10533
2 changed files with 23 additions and 15 deletions
|
@ -85,12 +85,11 @@ pub(super) fn write_shared(
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.emit.is_empty() || options.emit.contains(&EmitType::Toolchain) {
|
if options.emit.is_empty() || options.emit.contains(&EmitType::Toolchain) {
|
||||||
for f in static_files::STATIC_FILES_LIST {
|
let static_dir = cx.dst.join(Path::new("static.files"));
|
||||||
let filename = cx.dst.join(
|
static_files::for_each(|f: &static_files::StaticFile| {
|
||||||
Path::new("static.files/").join(static_files::static_filename(f.filename, f.bytes)),
|
let filename = static_dir.join(f.output_filename());
|
||||||
);
|
cx.shared.fs.write(filename, f.minified())
|
||||||
cx.shared.fs.write(filename, f.minified())?;
|
})?;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read a file and return all lines that match the `"{crate}":{data},` format,
|
/// Read a file and return all lines that match the `"{crate}":{data},` format,
|
||||||
|
|
|
@ -5,15 +5,19 @@
|
||||||
|
|
||||||
use rustc_data_structures::fx::FxHasher;
|
use rustc_data_structures::fx::FxHasher;
|
||||||
use std::hash::Hasher;
|
use std::hash::Hasher;
|
||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
use std::{fmt, str};
|
use std::{fmt, str};
|
||||||
|
|
||||||
pub(crate) struct StaticFile {
|
pub(crate) struct StaticFile {
|
||||||
pub(crate) filename: &'static str,
|
pub(crate) filename: PathBuf,
|
||||||
pub(crate) bytes: &'static [u8],
|
pub(crate) bytes: &'static [u8],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StaticFile {
|
impl StaticFile {
|
||||||
|
fn new(filename: &str, bytes: &'static [u8]) -> StaticFile {
|
||||||
|
Self { filename: static_filename(filename, bytes), bytes }
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn minified(&self) -> Vec<u8> {
|
pub(crate) fn minified(&self) -> Vec<u8> {
|
||||||
if self.filename.ends_with(".css") {
|
if self.filename.ends_with(".css") {
|
||||||
minifier::css::minify(str::from_utf8(self.bytes).unwrap()).unwrap().to_string().into()
|
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 {
|
pub(crate) fn output_filename(&self) -> &Path {
|
||||||
static_filename(self.filename, self.bytes)
|
&self.filename
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,13 +70,18 @@ macro_rules! static_files {
|
||||||
$(pub $field: StaticFile,)+
|
$(pub $field: StaticFile,)+
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) const STATIC_FILES: StaticFiles = StaticFiles {
|
pub(crate) static STATIC_FILES: std::sync::LazyLock<StaticFiles> = std::sync::LazyLock::new(|| StaticFiles {
|
||||||
$($field: StaticFile { filename: $file_path, bytes: include_bytes!($file_path) },)+
|
$($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,)+
|
$(&STATIC_FILES.$field,)+
|
||||||
];
|
] {
|
||||||
|
f(sf)?
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue