Skip to content

Commit c2ca307

Browse files
committed
Auto merge of #136253 - notriddle:notriddle/aot-minify, r=<try>
rustdoc: run css and html minifier at build instead of runtime This way, adding a bunch of comments to the JS files won't make rustdoc slower. Meant to address #136161 (comment)
2 parents a1d7676 + 68646e9 commit c2ca307

File tree

5 files changed

+35
-32
lines changed

5 files changed

+35
-32
lines changed

src/librustdoc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"]
3333

3434
[build-dependencies]
3535
sha2 = "0.10.8"
36+
minifier = { version = "0.3.2", default-features = false }
3637

3738
[dev-dependencies]
3839
expect-test = "1.4.0"

src/librustdoc/build.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use std::str;
2+
3+
use sha2::Digest;
14
fn main() {
25
// generate sha256 files
36
// this avoids having to perform hashing at runtime
@@ -35,14 +38,27 @@ fn main() {
3538
for path in files {
3639
let inpath = format!("html/{path}");
3740
println!("cargo::rerun-if-changed={inpath}");
38-
let bytes = std::fs::read(inpath).expect("static path exists");
39-
use sha2::Digest;
40-
let bytes = sha2::Sha256::digest(bytes);
41-
let mut digest = format!("-{bytes:x}");
41+
let data_bytes = std::fs::read(&inpath).expect("static path exists");
42+
let hash_bytes = sha2::Sha256::digest(&data_bytes);
43+
let mut digest = format!("-{hash_bytes:x}");
4244
digest.truncate(9);
4345
let outpath = std::path::PathBuf::from(format!("{out_dir}/{path}.sha256"));
4446
std::fs::create_dir_all(outpath.parent().expect("all file paths are in a directory"))
4547
.expect("should be able to write to out_dir");
4648
std::fs::write(&outpath, digest.as_bytes()).expect("write to out_dir");
49+
let minified_path = std::path::PathBuf::from(format!("{out_dir}/{path}.min"));
50+
if path.ends_with(".js") || path.ends_with(".css") {
51+
let minified: String = if path.ends_with(".css") {
52+
minifier::css::minify(str::from_utf8(&data_bytes).unwrap())
53+
.unwrap()
54+
.to_string()
55+
.into()
56+
} else {
57+
minifier::js::minify(str::from_utf8(&data_bytes).unwrap()).to_string().into()
58+
};
59+
std::fs::write(&minified_path, minified.as_bytes()).expect("write to out_dir");
60+
} else {
61+
std::fs::copy(&inpath, &minified_path).unwrap();
62+
}
4763
}
4864
}

src/librustdoc/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ impl Options {
458458
let to_check = matches.opt_strs("check-theme");
459459
if !to_check.is_empty() {
460460
let mut content =
461-
std::str::from_utf8(static_files::STATIC_FILES.rustdoc_css.bytes).unwrap();
461+
std::str::from_utf8(static_files::STATIC_FILES.rustdoc_css.src_bytes).unwrap();
462462
if let Some((_, inside)) = content.split_once("/* Begin theme: light */") {
463463
content = inside;
464464
}
@@ -607,7 +607,7 @@ impl Options {
607607
let mut themes = Vec::new();
608608
if matches.opt_present("theme") {
609609
let mut content =
610-
std::str::from_utf8(static_files::STATIC_FILES.rustdoc_css.bytes).unwrap();
610+
std::str::from_utf8(static_files::STATIC_FILES.rustdoc_css.src_bytes).unwrap();
611611
if let Some((_, inside)) = content.split_once("/* Begin theme: light */") {
612612
content = inside;
613613
}

src/librustdoc/html/render/write_shared.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,8 @@ fn write_static_files(
207207
if opt.emit.is_empty() || opt.emit.contains(&EmitType::Toolchain) {
208208
static_files::for_each(|f: &static_files::StaticFile| {
209209
let filename = static_dir.join(f.output_filename());
210-
let contents: &[u8];
211-
let contents_vec: Vec<u8>;
212-
if opt.disable_minification {
213-
contents = f.bytes;
214-
} else {
215-
contents_vec = f.minified();
216-
contents = &contents_vec;
217-
};
210+
let contents: &[u8] =
211+
if opt.disable_minification { f.src_bytes } else { f.minified_bytes };
218212
fs::write(&filename, contents).map_err(|e| PathError::new(e, &filename))
219213
})?;
220214
}

src/librustdoc/html/static_files.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,18 @@ use std::{fmt, str};
88

99
pub(crate) struct StaticFile {
1010
pub(crate) filename: PathBuf,
11-
pub(crate) bytes: &'static [u8],
11+
pub(crate) src_bytes: &'static [u8],
12+
pub(crate) minified_bytes: &'static [u8],
1213
}
1314

1415
impl StaticFile {
15-
fn new(filename: &str, bytes: &'static [u8], sha256: &'static str) -> StaticFile {
16-
Self { filename: static_filename(filename, sha256), bytes }
17-
}
18-
19-
pub(crate) fn minified(&self) -> Vec<u8> {
20-
let extension = match self.filename.extension() {
21-
Some(e) => e,
22-
None => return self.bytes.to_owned(),
23-
};
24-
if extension == "css" {
25-
minifier::css::minify(str::from_utf8(self.bytes).unwrap()).unwrap().to_string().into()
26-
} else if extension == "js" {
27-
minifier::js::minify(str::from_utf8(self.bytes).unwrap()).to_string().into()
28-
} else {
29-
self.bytes.to_owned()
30-
}
16+
fn new(
17+
filename: &str,
18+
src_bytes: &'static [u8],
19+
minified_bytes: &'static [u8],
20+
sha256: &'static str,
21+
) -> StaticFile {
22+
Self { filename: static_filename(filename, sha256), src_bytes, minified_bytes }
3123
}
3224

3325
pub(crate) fn output_filename(&self) -> &Path {
@@ -68,7 +60,7 @@ macro_rules! static_files {
6860

6961
// sha256 files are generated in build.rs
7062
pub(crate) static STATIC_FILES: std::sync::LazyLock<StaticFiles> = std::sync::LazyLock::new(|| StaticFiles {
71-
$($field: StaticFile::new($file_path, include_bytes!($file_path), include_str!(concat!(env!("OUT_DIR"), "/", $file_path, ".sha256"))),)+
63+
$($field: StaticFile::new($file_path, include_bytes!($file_path), include_bytes!(concat!(env!("OUT_DIR"), "/", $file_path, ".min")), include_str!(concat!(env!("OUT_DIR"), "/", $file_path, ".sha256"))),)+
7264
});
7365

7466
pub(crate) fn for_each<E>(f: impl Fn(&StaticFile) -> Result<(), E>) -> Result<(), E> {

0 commit comments

Comments
 (0)