autodoc: split json payload per field

this will make s3 re-enable compression for the stdlib's autodoc
and improve loading times (and data usage) for users

alongside this commit the deploy script for the official website is also
being updated
This commit is contained in:
Loris Cro 2023-09-17 18:21:31 +02:00
parent 33f3a4d840
commit c1e94b28a3
3 changed files with 73 additions and 61 deletions

View file

@ -1226,7 +1226,17 @@
</div> </div>
</div> </div>
</div> </div>
<script src="data.js"></script> <script src="data-typeKinds.js"></script>
<script src="data-rootMod.js"></script>
<script src="data-modules.js"></script>
<script src="data-files.js"></script>
<script src="data-calls.js"></script>
<script src="data-types.js"></script>
<script src="data-decls.js"></script>
<script src="data-exprs.js"></script>
<script src="data-astNodes.js"></script>
<script src="data-comptimeExprs.js"></script>
<script src="data-guideSections.js"></script>
<script src="commonmark.js"></script> <script src="commonmark.js"></script>
<script src="ziglexer.js"></script> <script src="ziglexer.js"></script>
<script src="main.js"></script> <script src="main.js"></script>

View file

@ -1,6 +1,19 @@
"use strict"; "use strict";
var zigAnalysis; var zigAnalysis = {
typeKinds,
rootMod,
modules,
astNodes,
calls,
files,
decls,
exprs,
types,
comptimeExprs,
guideSections
};
let skipNextHashChange = null; let skipNextHashChange = null;
const NAV_MODES = { const NAV_MODES = {
@ -200,11 +213,8 @@ var scrollHistory = {};
window.addEventListener("keydown", onWindowKeyDown, false); window.addEventListener("keydown", onWindowKeyDown, false);
onHashChange(); onHashChange();
let langRefVersion = zigAnalysis.params.zigVersion; // TODO: fix this once langref becomes part of autodoc
if (!/^\d+\.\d+\.\d+$/.test(langRefVersion)) { let langRefVersion = "master";
// the version is probably not released yet
langRefVersion = "master";
}
domLangRefLink.href = `https://ziglang.org/documentation/${langRefVersion}/`; domLangRefLink.href = `https://ziglang.org/documentation/${langRefVersion}/`;
function renderTitle() { function renderTitle() {
@ -528,8 +538,8 @@ var scrollHistory = {};
} }
let activeGuide = undefined; let activeGuide = undefined;
outer: for (let i = 0; i < zigAnalysis.guide_sections.length; i += 1) { outer: for (let i = 0; i < zigAnalysis.guideSections.length; i += 1) {
const section = zigAnalysis.guide_sections[i]; const section = zigAnalysis.guideSections[i];
for (let j = 0; j < section.guides.length; j += 1) { for (let j = 0; j < section.guides.length; j += 1) {
const guide = section.guides[j]; const guide = section.guides[j];
if (guide.name == curNav.activeGuide) { if (guide.name == curNav.activeGuide) {
@ -595,7 +605,7 @@ var scrollHistory = {};
domGuideTocList.classList.add("hidden"); domGuideTocList.classList.add("hidden");
domGuideTocListEmtpy.classList.remove("hidden"); domGuideTocListEmtpy.classList.remove("hidden");
if (zigAnalysis.guide_sections.length > 1 || (zigAnalysis.guide_sections[0].guides.length > 0)) { if (zigAnalysis.guideSections.length > 1 || (zigAnalysis.guideSections[0].guides.length > 0)) {
renderGuidesIndex(); renderGuidesIndex();
} else { } else {
noGuidesAtAll(); noGuidesAtAll();
@ -609,8 +619,8 @@ var scrollHistory = {};
// TODO: ensure unique hashes // TODO: ensure unique hashes
// TODO: hash also guides and their headings // TODO: hash also guides and their headings
function computeGuideHashes() { function computeGuideHashes() {
for (let i = 1; i < zigAnalysis.guide_sections.length; i += 1) { for (let i = 1; i < zigAnalysis.guideSections.length; i += 1) {
const section = zigAnalysis.guide_sections[i]; const section = zigAnalysis.guideSections[i];
section.hash = "section-" + slugify(section.name || i); section.hash = "section-" + slugify(section.name || i);
} }
} }
@ -619,8 +629,8 @@ var scrollHistory = {};
// main content // main content
{ {
let html = ""; let html = "";
for (let i = 0; i < zigAnalysis.guide_sections.length; i += 1) { for (let i = 0; i < zigAnalysis.guideSections.length; i += 1) {
const section = zigAnalysis.guide_sections[i]; const section = zigAnalysis.guideSections[i];
if (i != 0) { // first section is the default section if (i != 0) { // first section is the default section
html += "<h2 id='"+ section.hash +"'>" + section.name + "</h2>"; html += "<h2 id='"+ section.hash +"'>" + section.name + "</h2>";
} }
@ -635,10 +645,10 @@ var scrollHistory = {};
// sidebar / fast navigation // sidebar / fast navigation
{ {
domGuidesMenuTitle.textContent = "Sections"; domGuidesMenuTitle.textContent = "Sections";
if (zigAnalysis.guide_sections.length > 1) { if (zigAnalysis.guideSections.length > 1) {
let html = ""; let html = "";
for (let i = 1; i < zigAnalysis.guide_sections.length; i += 1) { for (let i = 1; i < zigAnalysis.guideSections.length; i += 1) {
const section = zigAnalysis.guide_sections[i]; const section = zigAnalysis.guideSections[i];
html += "<li><a href='"+ NAV_MODES.GUIDES + ":" + section.hash +"'>" + section.name + "</a></li>"; html += "<li><a href='"+ NAV_MODES.GUIDES + ":" + section.hash +"'>" + section.name + "</a></li>";
} }
domGuideTocList.innerHTML = "<ul>"+html+"</ul>"; domGuideTocList.innerHTML = "<ul>"+html+"</ul>";
@ -3978,8 +3988,8 @@ Happy writing!
} }
function parseGuides() { function parseGuides() {
for (let j = 0; j < zigAnalysis.guide_sections.length; j += 1) { for (let j = 0; j < zigAnalysis.guideSections.length; j += 1) {
const section = zigAnalysis.guide_sections[j]; const section = zigAnalysis.guideSections[j];
for (let i = 0; i < section.guides.length; i += 1) { for (let i = 0; i < section.guides.length; i += 1) {
let reader = new commonmark.Parser({ smart: true }); let reader = new commonmark.Parser({ smart: true });
const guide = section.guides[i]; const guide = section.guides[i];

View file

@ -344,7 +344,6 @@ fn generateZirData(self: *Autodoc, output_dir: std.fs.Dir) !void {
} }
var data = DocData{ var data = DocData{
.params = .{},
.modules = self.modules, .modules = self.modules,
.files = self.files, .files = self.files,
.calls = self.calls.items, .calls = self.calls.items,
@ -353,28 +352,41 @@ fn generateZirData(self: *Autodoc, output_dir: std.fs.Dir) !void {
.exprs = self.exprs.items, .exprs = self.exprs.items,
.astNodes = self.ast_nodes.items, .astNodes = self.ast_nodes.items,
.comptimeExprs = self.comptime_exprs.items, .comptimeExprs = self.comptime_exprs.items,
.guide_sections = self.guide_sections, .guideSections = self.guide_sections,
}; };
{ inline for (comptime std.meta.tags(std.meta.FieldEnum(DocData))) |f| {
const data_js_f = try output_dir.createFile("data.js", .{}); const field_name = @tagName(f);
const file_name = "data-" ++ field_name ++ ".js";
const data_js_f = try output_dir.createFile(file_name, .{});
defer data_js_f.close(); defer data_js_f.close();
var buffer = std.io.bufferedWriter(data_js_f.writer());
var buffer = std.io.bufferedWriter(data_js_f.writer());
const out = buffer.writer(); const out = buffer.writer();
try out.print(
\\ /** @type {{DocData}} */ try out.print("var {s} =", .{field_name});
\\ var zigAnalysis=
, .{}); var jsw = std.json.writeStream(out, .{
try std.json.stringifyArbitraryDepth(
self.arena,
data,
.{
.whitespace = .minified, .whitespace = .minified,
.emit_null_optional_fields = true, .emit_null_optional_fields = true,
}, });
out,
); switch (f) {
.files => try writeFileTableToJson(data.files, data.modules, &jsw),
.guideSections => try writeGuidesToJson(data.guideSections, &jsw),
.modules => try jsw.write(data.modules.values()),
else => try jsw.write(@field(data, field_name)),
}
// try std.json.stringifyArbitraryDepth(
// self.arena,
// @field(data, field.name),
// .{
// .whitespace = .minified,
// .emit_null_optional_fields = true,
// },
// out,
// );
try out.print(";", .{}); try out.print(";", .{});
// last thing (that can fail) that we do is flush // last thing (that can fail) that we do is flush
@ -476,18 +488,12 @@ const Scope = struct {
/// The output of our analysis process. /// The output of our analysis process.
const DocData = struct { const DocData = struct {
// NOTE: editing fields of DocData requires also updating:
// - the deployment script for ziglang.org
// - imports in index.html
typeKinds: []const []const u8 = std.meta.fieldNames(DocTypeKinds), typeKinds: []const []const u8 = std.meta.fieldNames(DocTypeKinds),
rootMod: u32 = 0, rootMod: u32 = 0,
params: struct {
zigId: []const u8 = "arst",
zigVersion: []const u8 = build_options.version,
target: []const u8 = "arst",
builds: []const struct { target: []const u8 } = &.{
.{ .target = "arst" },
},
},
modules: std.AutoArrayHashMapUnmanaged(*Module, DocModule), modules: std.AutoArrayHashMapUnmanaged(*Module, DocModule),
errors: []struct {} = &.{},
// non-hardcoded stuff // non-hardcoded stuff
astNodes: []AstNode, astNodes: []AstNode,
@ -498,7 +504,7 @@ const DocData = struct {
exprs: []Expr, exprs: []Expr,
comptimeExprs: []ComptimeExpr, comptimeExprs: []ComptimeExpr,
guide_sections: std.ArrayListUnmanaged(Section), guideSections: std.ArrayListUnmanaged(Section),
const Call = struct { const Call = struct {
func: Expr, func: Expr,
@ -506,20 +512,6 @@ const DocData = struct {
ret: Expr, ret: Expr,
}; };
pub fn jsonStringify(self: DocData, jsw: anytype) !void {
try jsw.beginObject();
inline for (comptime std.meta.tags(std.meta.FieldEnum(DocData))) |f| {
const f_name = @tagName(f);
try jsw.objectField(f_name);
switch (f) {
.files => try writeFileTableToJson(self.files, self.modules, jsw),
.guide_sections => try writeGuidesToJson(self.guide_sections, jsw),
.modules => try jsw.write(self.modules.values()),
else => try jsw.write(@field(self, f_name)),
}
}
try jsw.endObject();
}
/// All the type "families" as described by `std.builtin.TypeId` /// All the type "families" as described by `std.builtin.TypeId`
/// plus a couple extra that are unique to our use case. /// plus a couple extra that are unique to our use case.
/// ///