Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a9730c3

Browse files
committed
Auto merge of #136253 - notriddle:notriddle/aot-minify, r=GuillaumeGomez
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 d4bdd1e + 68646e9 commit a9730c3

File tree

5 files changed

+35
-32
lines changed

5 files changed

+35
-32
lines changed

‎src/librustdoc/Cargo.toml

Lines changed: 1 addition & 0 deletions
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

Lines changed: 20 additions & 4 deletions
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
@@ -40,14 +43,27 @@ fn main() {
4043
for path in files {
4144
let inpath = format!("html/{path}");
4245
println!("cargo::rerun-if-changed={inpath}");
43-
let bytes = std::fs::read(inpath).expect("static path exists");
44-
use sha2::Digest;
45-
let bytes = sha2::Sha256::digest(bytes);
46-
let mut digest = format!("-{bytes:x}");
46+
let data_bytes = std::fs::read(&inpath).expect("static path exists");
47+
let hash_bytes = sha2::Sha256::digest(&data_bytes);
48+
let mut digest = format!("-{hash_bytes:x}");
4749
digest.truncate(9);
4850
let outpath = std::path::PathBuf::from(format!("{out_dir}/{path}.sha256"));
4951
std::fs::create_dir_all(outpath.parent().expect("all file paths are in a directory"))
5052
.expect("should be able to write to out_dir");
5153
std::fs::write(&outpath, digest.as_bytes()).expect("write to out_dir");
54+
let minified_path = std::path::PathBuf::from(format!("{out_dir}/{path}.min"));
55+
if path.ends_with(".js") || path.ends_with(".css") {
56+
let minified: String = if path.ends_with(".css") {
57+
minifier::css::minify(str::from_utf8(&data_bytes).unwrap())
58+
.unwrap()
59+
.to_string()
60+
.into()
61+
} else {
62+
minifier::js::minify(str::from_utf8(&data_bytes).unwrap()).to_string().into()
63+
};
64+
std::fs::write(&minified_path, minified.as_bytes()).expect("write to out_dir");
65+
} else {
66+
std::fs::copy(&inpath, &minified_path).unwrap();
67+
}
5268
}
5369
}

‎src/librustdoc/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ impl Options {
489489
let to_check = matches.opt_strs("check-theme");
490490
if !to_check.is_empty() {
491491
let mut content =
492-
std::str::from_utf8(static_files::STATIC_FILES.rustdoc_css.bytes).unwrap();
492+
std::str::from_utf8(static_files::STATIC_FILES.rustdoc_css.src_bytes).unwrap();
493493
if let Some((_, inside)) = content.split_once("/* Begin theme: light */") {
494494
content = inside;
495495
}
@@ -638,7 +638,7 @@ impl Options {
638638
let mut themes = Vec::new();
639639
if matches.opt_present("theme") {
640640
let mut content =
641-
std::str::from_utf8(static_files::STATIC_FILES.rustdoc_css.bytes).unwrap();
641+
std::str::from_utf8(static_files::STATIC_FILES.rustdoc_css.src_bytes).unwrap();
642642
if let Some((_, inside)) = content.split_once("/* Begin theme: light */") {
643643
content = inside;
644644
}

‎src/librustdoc/html/render/write_shared.rs

Lines changed: 2 additions & 8 deletions
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

Lines changed: 10 additions & 18 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /