rustdoc: Move sidebar items into shared JavaScript.
It had been a source of huge bloat in rustdoc outputs. Of course, we can simply disable compiler docs (as `rustc` generates over 90M of HTML) but this approach fares better even after such decision. Each directory now has `sidebar-items.js`, which immediately calls `initSidebarItems` with a JSON sidebar data. This file is shared throughout every item in the sidebar. The current item is highlighted via a separate JS snippet (`window.sidebarCurrent`). The JS file is designed to be loaded asynchronously, as the sidebar is rendered before the content and slow sidebar loading blocks the entire rendering. For the minimal accessibility without JS, links to the parent items are left in HTML. In the future, it might also be possible to integrate crates data with the same fashion: `sidebar-items.js` at the root path will do that. (Currently rustdoc skips writing JS in that case.) This has a huge impact on the size of rustdoc outputs. Originally it was 326MB uncompressed (37.7MB gzipped, 6.1MB xz compressed); it is 169MB uncompressed (11.9MB gzipped, 5.9MB xz compressed) now. The sidebar JS only takes 10MB uncompressed & 0.3MB gzipped.
This commit is contained in:
@@ -15,6 +15,27 @@
|
||||
"use strict";
|
||||
var resizeTimeout, interval;
|
||||
|
||||
// This mapping table should match the discriminants of
|
||||
// `rustdoc::html::item_type::ItemType` type in Rust.
|
||||
var itemTypes = ["mod",
|
||||
"externcrate",
|
||||
"import",
|
||||
"struct",
|
||||
"enum",
|
||||
"fn",
|
||||
"type",
|
||||
"static",
|
||||
"trait",
|
||||
"impl",
|
||||
"tymethod",
|
||||
"method",
|
||||
"structfield",
|
||||
"variant",
|
||||
"macro",
|
||||
"primitive",
|
||||
"associatedtype",
|
||||
"constant"];
|
||||
|
||||
$('.js-only').removeClass('js-only');
|
||||
|
||||
function getQueryStringParams() {
|
||||
@@ -552,27 +573,6 @@
|
||||
showResults(results);
|
||||
}
|
||||
|
||||
// This mapping table should match the discriminants of
|
||||
// `rustdoc::html::item_type::ItemType` type in Rust.
|
||||
var itemTypes = ["mod",
|
||||
"externcrate",
|
||||
"import",
|
||||
"struct",
|
||||
"enum",
|
||||
"fn",
|
||||
"type",
|
||||
"static",
|
||||
"trait",
|
||||
"impl",
|
||||
"tymethod",
|
||||
"method",
|
||||
"structfield",
|
||||
"variant",
|
||||
"macro",
|
||||
"primitive",
|
||||
"associatedtype",
|
||||
"constant"];
|
||||
|
||||
function itemTypeFromName(typename) {
|
||||
for (var i = 0; i < itemTypes.length; ++i) {
|
||||
if (itemTypes[i] === typename) return i;
|
||||
@@ -708,6 +708,50 @@
|
||||
|
||||
window.initSearch = initSearch;
|
||||
|
||||
// delayed sidebar rendering.
|
||||
function initSidebarItems(items) {
|
||||
var sidebar = $('.sidebar');
|
||||
var current = window.sidebarCurrent;
|
||||
|
||||
function block(shortty, longty) {
|
||||
var filtered = items[shortty];
|
||||
if (!filtered) return;
|
||||
|
||||
var div = $('<div>').attr('class', 'block ' + shortty);
|
||||
div.append($('<h2>').text(longty));
|
||||
|
||||
for (var i = 0; i < filtered.length; ++i) {
|
||||
var item = filtered[i];
|
||||
var name = item[0];
|
||||
var desc = item[1]; // can be null
|
||||
|
||||
var klass = shortty;
|
||||
if (name === current.name && shortty == current.ty) {
|
||||
klass += ' current';
|
||||
}
|
||||
var path;
|
||||
if (shortty === 'mod') {
|
||||
path = name + '/index.html';
|
||||
} else {
|
||||
path = shortty + '.' + name + '.html';
|
||||
}
|
||||
div.append($('<a>', {'href': current.relpath + path,
|
||||
'title': desc,
|
||||
'class': klass}).text(name));
|
||||
}
|
||||
sidebar.append(div);
|
||||
}
|
||||
|
||||
block("mod", "Modules");
|
||||
block("struct", "Structs");
|
||||
block("enum", "Enums");
|
||||
block("trait", "Traits");
|
||||
block("fn", "Functions");
|
||||
block("macro", "Macros");
|
||||
}
|
||||
|
||||
window.initSidebarItems = initSidebarItems;
|
||||
|
||||
window.register_implementors = function(imp) {
|
||||
var list = $('#implementors-list');
|
||||
var libs = Object.getOwnPropertyNames(imp);
|
||||
|
||||
Reference in New Issue
Block a user