Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't generate minification variables if not needed #59400

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,8 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
Json::Object(crate_data))
}

const MINIFIED_VARIABLES: &str = "var N=null,E=\"\",T=\"t\",U=\"u\",searchIndex={};";

fn write_shared(
cx: &Context,
krate: &clean::Crate,
Expand Down Expand Up @@ -952,10 +954,11 @@ themePicker.onblur = handleThemeButtonsBlur;
krate: &str,
key: &str,
for_search_index: bool,
) -> io::Result<(Vec<String>, Vec<String>, Vec<String>)> {
) -> io::Result<(Vec<String>, Vec<String>, Vec<String>, bool)> {
let mut ret = Vec::new();
let mut krates = Vec::new();
let mut variables = Vec::new();
let mut is_minified = false;

if path.exists() {
for line in BufReader::new(File::open(path)?).lines() {
Expand All @@ -964,10 +967,11 @@ themePicker.onblur = handleThemeButtonsBlur;
variables.push(line.clone());
continue;
}
if !line.starts_with(key) {
if !line.starts_with(key) || line.starts_with(&format!(r#"{}["{}"]"#, key, krate)) {
continue;
}
if line.starts_with(&format!(r#"{}["{}"]"#, key, krate)) {
if line.starts_with(MINIFIED_VARIABLES) {
is_minified = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is dead code. The above:

if !line.starts_with(key) || line.starts_with(&format!(r#"{}["{}"]"#, key, krate)) {
    continue;
}

means that line.starts_with(MINIFIED_VARIABLES) can never be true. This means that the minification variables are missing if the last call to rustdoc used --disable-minification but previous ones didn't.

continue;
}
ret.push(line.to_string());
Expand All @@ -977,7 +981,7 @@ themePicker.onblur = handleThemeButtonsBlur;
.unwrap_or_else(|| String::new()));
}
}
Ok((ret, krates, variables))
Ok((ret, krates, variables, is_minified))
}

fn show_item(item: &IndexItem, krate: &str) -> String {
Expand All @@ -992,7 +996,8 @@ themePicker.onblur = handleThemeButtonsBlur;

let dst = cx.dst.join(&format!("aliases{}.js", cx.shared.resource_suffix));
{
let (mut all_aliases, _, _) = try_err!(collect(&dst, &krate.name, "ALIASES", false), &dst);
let (mut all_aliases, _, _, _) = try_err!(collect(&dst, &krate.name, "ALIASES", false),
&dst);
let mut w = try_err!(File::create(&dst), &dst);
let mut output = String::with_capacity(100);
for (alias, items) in &cache.aliases {
Expand Down Expand Up @@ -1088,33 +1093,37 @@ themePicker.onblur = handleThemeButtonsBlur;
}

let dst = cx.dst.join(&format!("source-files{}.js", cx.shared.resource_suffix));
let (mut all_sources, _krates, _) = try_err!(collect(&dst, &krate.name, "sourcesIndex",
false),
&dst);
let (mut all_sources, _krates, _, _) = try_err!(collect(&dst, &krate.name, "sourcesIndex",
false),
&dst);
all_sources.push(format!("sourcesIndex[\"{}\"] = {};",
&krate.name,
hierarchy.to_json_string()));
all_sources.sort();
let mut w = try_err!(File::create(&dst), &dst);
try_err!(writeln!(&mut w,
"var N = null;var sourcesIndex = {{}};\n{}\ncreateSourceSidebar();",
"var N=null,sourcesIndex={{}};\n{}\ncreateSourceSidebar();",
all_sources.join("\n")),
&dst);
}

// Update the search index
let dst = cx.dst.join(&format!("search-index{}.js", cx.shared.resource_suffix));
let (mut all_indexes, mut krates, variables) = try_err!(collect(&dst,
&krate.name,
"searchIndex",
true), &dst);
let (mut all_indexes, mut krates, variables, is_minified) = try_err!(collect(&dst,
&krate.name,
"searchIndex",
true), &dst);
all_indexes.push(search_index);

// Sort the indexes by crate so the file will be generated identically even
// with rustdoc running in parallel.
all_indexes.sort();
let mut w = try_err!(File::create(&dst), &dst);
try_err!(writeln!(&mut w, "var N=null,E=\"\",T=\"t\",U=\"u\",searchIndex={{}};"), &dst);
if is_minified || options.enable_minification {
try_err!(writeln!(&mut w, "{}", MINIFIED_VARIABLES), &dst);
} else {
try_err!(writeln!(&mut w, "var searchIndex={{}};"), &dst);
}
try_err!(write_minify_replacer(&mut w,
&format!("{}\n{}", variables.join(""), all_indexes.join("\n")),
options.enable_minification),
Expand Down Expand Up @@ -1219,9 +1228,9 @@ themePicker.onblur = handleThemeButtonsBlur;
remote_item_type.css_class(),
remote_path[remote_path.len() - 1]));

let (mut all_implementors, _, _) = try_err!(collect(&mydst, &krate.name, "implementors",
false),
&mydst);
let (mut all_implementors, _, _, _) = try_err!(collect(&mydst, &krate.name, "implementors",
false),
&mydst);
all_implementors.push(implementors);
// Sort the implementors by crate so the file will be generated
// identically even with rustdoc running in parallel.
Expand Down