Rollup merge of #84150 - jsha:defer-search-js, r=GuillaumeGomez

rustdoc: move some search code into search.js

This reduces main.s from 3094 lines to 1587. Also it saves some bytes
of download in the case where search isn't used.

There were a fair number of variables that needed to be accessible in
both main.js and search.js, but I didn't want to put too many symbols in
the global namespace, so I consolidated much of the search-related
state and functions into a new object `window.searchState`.

Demo at https://hoffman-andrews.com/rust/move-search/std/?search=foo
This commit is contained in:
Dylan DPC 2021-04-17 22:31:32 +02:00 committed by GitHub
commit b5ce9c44a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 1685 additions and 1696 deletions

View File

@ -113,7 +113,8 @@ crate fn render<T: Print, S: Print>(
<section class=\"footer\"></section>\
{after_content}\
<div id=\"rustdoc-vars\" data-root-path=\"{root_path}\" data-current-crate=\"{krate}\" \
data-search-js=\"{root_path}search-index{suffix}.js\"></div>
data-search-index-js=\"{root_path}search-index{suffix}.js\" \
data-search-js=\"{static_root_path}search{suffix}.js\"></div>
<script src=\"{static_root_path}main{suffix}.js\"></script>\
{extra_scripts}\
</body>\

View File

@ -222,6 +222,7 @@ pub(super) fn write_shared(
&format!(" = {}", serde_json::to_string(&themes).unwrap()),
),
)?;
write_minify("search.js", static_files::SEARCH_JS)?;
write_minify("settings.js", static_files::SETTINGS_JS)?;
if cx.shared.include_sources {
write_minify("source-script.js", static_files::sidebar::SOURCE_SCRIPT)?;
@ -409,7 +410,7 @@ pub(super) fn write_shared(
write_crate("search-index.js", &|| {
let mut v = String::from("var searchIndex = JSON.parse('{\\\n");
v.push_str(&all_indexes.join(",\\\n"));
v.push_str("\\\n}');\ninitSearch(searchIndex);");
v.push_str("\\\n}');\nif (window.initSearch) {window.initSearch(searchIndex)};");
Ok(v.into_bytes())
})?;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -24,6 +24,9 @@ crate static NORMALIZE_CSS: &str = include_str!("static/normalize.css");
/// including search behavior and docblock folding, among others.
crate static MAIN_JS: &str = include_str!("static/main.js");
/// The file contents of `search.js`, which contains the search behavior.
crate static SEARCH_JS: &str = include_str!("static/search.js");
/// The file contents of `settings.js`, which contains the JavaScript used to handle the settings
/// page.
crate static SETTINGS_JS: &str = include_str!("static/settings.js");

View File

@ -246,7 +246,7 @@ function lookForEntry(entry, data) {
return null;
}
function loadMainJsAndIndex(mainJs, searchIndex, storageJs, crate) {
function loadSearchJsAndIndex(searchJs, searchIndex, storageJs, crate) {
if (searchIndex[searchIndex.length - 1].length === 0) {
searchIndex.pop();
}
@ -270,9 +270,9 @@ function loadMainJsAndIndex(mainJs, searchIndex, storageJs, crate) {
ALIASES = {};
finalJS += 'window = { "currentCrate": "' + crate + '", rootPath: "../" };\n';
finalJS += loadThings(["hasOwnProperty", "onEach"], 'function', extractFunction, storageJs);
finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);
finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, searchJs);
finalJS += loadThings(variablesToLoad, 'variable', extractVariable, searchJs);
finalJS += loadThings(functionsToLoad, 'function', extractFunction, searchJs);
var loaded = loadContent(finalJS);
var index = loaded.buildIndex(searchIndex.rawSearchIndex);
@ -382,12 +382,12 @@ function runChecks(testFile, loaded, index) {
}
function load_files(doc_folder, resource_suffix, crate) {
var mainJs = readFile(path.join(doc_folder, "main" + resource_suffix + ".js"));
var searchJs = readFile(path.join(doc_folder, "search" + resource_suffix + ".js"));
var storageJs = readFile(path.join(doc_folder, "storage" + resource_suffix + ".js"));
var searchIndex = readFile(
path.join(doc_folder, "search-index" + resource_suffix + ".js")).split("\n");
return loadMainJsAndIndex(mainJs, searchIndex, storageJs, crate);
return loadSearchJsAndIndex(searchJs, searchIndex, storageJs, crate);
}
function showHelp() {