Diffinery

homeflare/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

Worker: upload the docs content and the Shiki grammars as text modules (#25)

The main module bundled to 14.4 MB, which hosts that cap each uploaded part refuse (open-compute: 8 MiB, #24). The generated docs content (4.4 MB) and Shiki's grammars (3.2 MB, inlined by the bundler) now upload as text modules, like the Kumo islands, and are parsed once from JSON. The main module is 6.8 MB; the total upload is unchanged.

Refs #24
Canberk Demirci authored · codician-team committed 1 parent 059a44fcommit bbfcbd52a52ee2bee36e0afb24e2c0f9d41dc7e4Browse files
9 files changed+155−85
Files (9)
modifiedCHANGELOG.md+7−0View file
@@ -6,6 +6,13 @@
66
77 ## [Unreleased]
88
9+### Self-hosting
10+
11+- The Worker's main module is about 7 MB instead of 14 MB: the docs, the downloads and the
12+ syntax highlighting grammars upload as separate text modules, none larger than about
13+ 4.5 MB. Hosts that cap each part of an upload, like open-compute (8 MiB), take it, and
14+ a cold start parses less JavaScript.
15+
916 ## [0.2.0] - 2026-10-31
1017
1118 Development moved to [GitHub](https://github.com/codician-team/diffinery). GitHub
modifiedapps/diffinery/.gitignore+1−0View file
@@ -1,2 +1,3 @@
11 build/
22 src/docs/content.generated.ts
3+src/web/grammars.generated.ts
modifiedapps/diffinery/docs/self-hosting/architecture.md+2−0View file
@@ -68,6 +68,8 @@
6868
6969 Some work happens in the Worker itself: profile pictures are checked, cropped and resized to small WebP files with WebAssembly image codecs, and code in the web UI and the docs is syntax highlighted with Shiki on the server.
7070
71+The Worker uploads as one JavaScript module of about 7 MB plus separate modules for the bulky content: the public docs, the downloads and the syntax highlighting grammars as text modules, which `pnpm build` writes to `apps/diffinery/build/` (`scripts/build-docs.mjs`, `scripts/build-grammars.mjs`), and the image codecs as WebAssembly. No module is larger than about 4.5 MB, so the Worker also deploys to hosts that cap each part of an upload, like open-compute's 8 MiB. The source map is larger than that: on such a host, set `upload_source_maps` to `false`.
72+
7173 The Worker holds no state of its own. It authenticates, checks the caller's role on the repository against D1, and then calls the repository's Durable Object over RPC.
7274
7375 Before it calls the object, the Worker also decides whether the request is a read or a write. `GET` and `HEAD` are reads, and so are the two `POST` requests that only fetch data: git's `git-upload-pack` (clone and fetch) and a Git LFS batch request for `download`. MCP tools marked read-only count as reads too. Every other request is a write:
modifiedapps/diffinery/docs/self-hosting/configuration.md+1−1View file
@@ -300,7 +300,7 @@
300300 | `routes` | `[{ "pattern": "diffinery.com/*", "zone_name": "diffinery.com" }]` | A zone route, not a Custom Domain. The hostname's DNS record must be proxied. On deploy, `scripts/render-config.mjs` replaces it with one route, `<DIFFINERY_DOMAIN>/*` in the zone `DIFFINERY_ZONE` (by default the hostname's last two labels), so you don't edit it by hand. |
301301 | `observability` | Enabled, `head_sampling_rate: 1` | Every request is logged to Workers Logs. |
302302 | `version_metadata` | Binding `CF_VERSION_METADATA` | The deployed version's id and time. [IndexNow](/docs/admin/site-administration#search-engines) checks the site's pages once for each new version and ignores older versions still running during a rollout. Optional: without it, it checks whenever the pages' content differs from what it last submitted. |
303-| `upload_source_maps` | `true` | Readable stack traces in the logs. |
303+| `upload_source_maps` | `true` | Readable stack traces in the logs. The source map is uploaded as one part of about 12 MB: on a host that caps each part (open-compute's limit is 8 MiB), set it to `false`. |
304304 | `limits.cpu_ms` | `300000` | Up to 5 minutes of CPU time per request (large pushes, diffs and merges). |
305305 | `containers` | `RunnerContainer`: `standard-4`, `max_instances: 100`; `RunnerContainerMedium`, `RunnerContainerSmall`, `RunnerContainerTiny`: `standard-3`, `standard-1`, `basic`, `max_instances: 100` each (smaller hosted runners; remove them and their `RUNNER_*` bindings to run every job on `standard-4`); `SandboxContainer`: `standard-1`, `max_instances: 50`; `ImportContainer`: `standard-4`, `max_instances: 20`; `rollout_active_grace_period`: 23400 for runners and 25200 for imports (longer than a job or transfer can run, so a deploy never replaces a running one), 3600 for sandboxes; `rollout_step_percentage: 100`, so containers started after a deploy get the new image | All three build `containers/runner/Dockerfile` with `containers/runner` as context. Raise `max_instances` for more concurrent jobs, sandboxes or import transfers. |
306306 | `triggers.crons` | `["*/5 * * * *"]` | Token expiry reminders, email log pruning, registry cleanup, cancelling draft imports older than 7 days, and setting the workflow scheduler's alarm if it isn't set. Scheduled workflows run from that alarm, not from the cron trigger. |
modifiedapps/diffinery/package.json+2−2View file
@@ -12,8 +12,8 @@
1212 "cf-typegen": "wrangler types",
1313 "e2e": "node --test --test-concurrency=1 --test-timeout=600000 test/e2e/e2e.test.mjs test/e2e/passkeys.test.mjs test/e2e/accounts.test.mjs test/e2e/registry.test.mjs test/e2e/email.test.mjs test/e2e/security-txt.test.mjs test/e2e/security.test.mjs test/e2e/security-limits.test.mjs test/e2e/agentic.test.mjs test/e2e/tasks.test.mjs test/e2e/terminals.test.mjs test/e2e/review.test.mjs",
1414 "db:migrate:local": "wrangler d1 migrations apply diffinery-dev --local -e dev",
15- "build": "node islands/build.mjs && node scripts/build-docs.mjs",
16- "predev": "node islands/build.mjs && node scripts/build-docs.mjs",
15+ "build": "node islands/build.mjs && node scripts/build-docs.mjs && node scripts/build-grammars.mjs",
16+ "predev": "node islands/build.mjs && node scripts/build-docs.mjs && node scripts/build-grammars.mjs",
1717 "build:docs": "node scripts/build-docs.mjs"
1818 },
1919 "dependencies": {
modifiedapps/diffinery/scripts/build-docs.mjs+38−10View file
@@ -1,4 +1,6 @@
1-// Builds the public documentation into src/docs/content.generated.ts:
1+// Builds the public documentation into src/docs/content.generated.ts, with the bulky values (pages, images and
2+// the downloads) in build/docs/content.txt, which the Worker imports as a separate text module and parses once:
3+// that keeps the Worker's main module small (hosts that cap each uploaded part, like open-compute's 8 MiB, take it).
24 // - every Markdown page under docs/ (with its frontmatter), in the order of docs/nav.json
35 // - the public site's pages under site/ (the product, comparison and company pages around the
46 // home page), in the order of site/nav.json
@@ -39,6 +41,7 @@
3941 /** The Claude Code plugins Diffinery serves for upload to claude.ai, by name. */
4042 const CLAUDE_PLUGIN_NAMES = new Set(["diffinery", "diffinery-ci"]);
4143 const out = path.join(app, "src/docs/content.generated.ts");
44+const dataOut = path.join(app, "build/docs/content.txt");
4245
4346 /** Pages produced in code rather than from a Markdown file. */
4447 const RUNTIME_PAGES = new Set(["api", "reference/mcp-tools"]);
@@ -315,38 +318,63 @@
315318 }
316319
317320 const ordered = Object.fromEntries(navSlugs.filter((s) => pages[s]).map((s) => [s, pages[s]]));
321+/** The bulky values, in build/docs/content.txt (JSON). */
322+const data = {
323+ DOCS_PAGES: ordered,
324+ SITE_PAGES: Object.fromEntries(sitePaths.filter((u) => site[u]).map((u) => [u, site[u]])),
325+ DOCS_IMAGES: images,
326+ CLI_BUNDLE: cli.bin,
327+ EDITOR_EXTENSION_VSIX_B64: editor.vsix.toString("base64"),
328+ JETBRAINS_PLUGIN_ZIP_B64: jetbrains.zip.toString("base64"),
329+ CLAUDE_PLUGINS: claudePlugins,
330+};
318331 const src = `// Generated by scripts/build-docs.mjs from docs/, site/, claude-plugins/, apps/cli, apps/vscode and apps/jetbrains/dist. Do not edit.
319332 /* eslint-disable */
333+import DATA from "../../build/docs/content.txt";
334+
320335 export interface DocPage { title: string; description: string; markdown: string; updated?: string }
321336 export interface DocSection { title: string; pages: string[] }
322-export const DOCS_NAV: DocSection[] = ${JSON.stringify(nav)};
323-export const DOCS_PAGES: Record<string, DocPage> = ${JSON.stringify(ordered)};
324-/** docs/images/: content type and base64 bytes by file name. */
325337 export interface SitePage { title: string; description: string; markdown: string; updated: string; seoTitle: string | null; published?: string }
326338 export interface SiteSection { title: string; pages: string[] }
339+
340+const data = JSON.parse(DATA) as {
341+ DOCS_PAGES: Record<string, DocPage>;
342+ SITE_PAGES: Record<string, SitePage>;
343+ DOCS_IMAGES: Record<string, { type: string; data: string }>;
344+ CLI_BUNDLE: string;
345+ EDITOR_EXTENSION_VSIX_B64: string;
346+ JETBRAINS_PLUGIN_ZIP_B64: string;
347+ CLAUDE_PLUGINS: Record<string, { version: string; sha256: string; zip: string }>;
348+};
349+
350+export const DOCS_NAV: DocSection[] = ${JSON.stringify(nav)};
351+export const DOCS_PAGES = data.DOCS_PAGES;
327352 /** The public site's pages (site/), by URL path, and their sections in reading order. The home page is code (web/site.tsx). */
328353 export const SITE_NAV: SiteSection[] = ${JSON.stringify(siteNav)};
329-export const SITE_PAGES: Record<string, SitePage> = ${JSON.stringify(Object.fromEntries(sitePaths.filter((u) => site[u]).map((u) => [u, site[u]])))};
330-export const DOCS_IMAGES: Record<string, { type: string; data: string }> = ${JSON.stringify(images)};
354+export const SITE_PAGES = data.SITE_PAGES;
355+/** docs/images/: content type and base64 bytes by file name. */
356+export const DOCS_IMAGES = data.DOCS_IMAGES;
331357 export const CLI_VERSION = ${JSON.stringify(cli.VERSION)};
332-export const CLI_BUNDLE = ${JSON.stringify(cli.bin)};
358+export const CLI_BUNDLE = data.CLI_BUNDLE;
333359 export const EDITOR_EXTENSION_VERSION = ${JSON.stringify(editor.version)};
334360 export const EDITOR_EXTENSION_SHA256 = ${JSON.stringify(createHash("sha256").update(editor.vsix).digest("hex"))};
335-export const EDITOR_EXTENSION_VSIX_B64 = ${JSON.stringify(editor.vsix.toString("base64"))};
361+export const EDITOR_EXTENSION_VSIX_B64 = data.EDITOR_EXTENSION_VSIX_B64;
336362 export const EDITOR_INSTALL_SH = ${JSON.stringify(await readFile(path.join(editorInstall, "install.sh"), "utf8"))};
337363 export const EDITOR_INSTALL_PS1 = ${JSON.stringify(await readFile(path.join(editorInstall, "install.ps1"), "utf8"))};
338364 export const JETBRAINS_PLUGIN_VERSION = ${JSON.stringify(jetbrains.version)};
339365 export const JETBRAINS_PLUGIN_SINCE_BUILD = ${JSON.stringify(jetbrains.sinceBuild)};
340366 export const JETBRAINS_PLUGIN_DESCRIPTION = ${JSON.stringify(jetbrains.description)};
341367 export const JETBRAINS_PLUGIN_SHA256 = ${JSON.stringify(createHash("sha256").update(jetbrains.zip).digest("hex"))};
342-export const JETBRAINS_PLUGIN_ZIP_B64 = ${JSON.stringify(jetbrains.zip.toString("base64"))};
368+export const JETBRAINS_PLUGIN_ZIP_B64 = data.JETBRAINS_PLUGIN_ZIP_B64;
343369 /** claude-plugins/: the Claude Code plugins served at /-/claude/<name>.zip, base64. */
344-export const CLAUDE_PLUGINS: Record<string, { version: string; sha256: string; zip: string }> = ${JSON.stringify(claudePlugins)};
370+export const CLAUDE_PLUGINS = data.CLAUDE_PLUGINS;
345371 export const CLI_INSTALL_SH = ${JSON.stringify(await readFile(path.join(cliInstall, "install.sh"), "utf8"))};
346372 export const CLI_INSTALL_PS1 = ${JSON.stringify(await readFile(path.join(cliInstall, "install.ps1"), "utf8"))};
347373 export const CLI_SHA256 = ${JSON.stringify(createHash("sha256").update(cli.bin).digest("hex"))};
348374 export const AGENT_SETUP_SH = ${JSON.stringify(await readFile(path.join(cliInstall, "agent-setup.sh"), "utf8"))};
349375 `;
350376 await mkdir(path.dirname(out), { recursive: true });
377+await mkdir(path.dirname(dataOut), { recursive: true });
378+await writeFile(dataOut, JSON.stringify(data));
351379 await writeFile(out, src);
352380 console.log(`docs: ${Object.keys(ordered).length} pages (+${RUNTIME_PAGES.size} generated at runtime), ${Object.keys(site).length} site pages, ${Object.keys(images).length} images, CLI ${cli.VERSION} (${Math.round(cli.bin.length / 1024)} KiB), editor extension ${editor.version} (${Math.round(editor.vsix.length / 1024)} KiB), JetBrains plugin ${jetbrains.version} (${Math.round(jetbrains.zip.length / 1024)} KiB), Claude Code plugins ${Object.entries(claudePlugins).map(([n, p]) => `${n} ${p.version}`).join(", ")}`);
addedapps/diffinery/scripts/build-grammars.mjs+58−0View file
@@ -1,0 +1,58 @@
1+// Builds the Shiki grammars the Worker highlights code with into text modules in build/grammars/, one JSON file
2+// per grammar (a grammar embedded by several languages, like JavaScript, is there once), and writes
3+// src/web/grammars.generated.ts, which imports them. As JavaScript the grammars would add about 3 MB to the Worker's
4+// main module; as text modules they are separate parts of the upload, parsed only when a file in that language is
5+// shown. That keeps the main module small (hosts that cap each uploaded part, like
6+// open-compute's 8 MiB, take it).
7+//
8+// Run by `pnpm build` and `pnpm dev`. A language added to LANGS needs no other change: src/web/highlight.ts
9+// takes its list from the generated file.
10+import { mkdir, rm, writeFile } from "node:fs/promises";
11+import path from "node:path";
12+import { fileURLToPath } from "node:url";
13+
14+const app = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
15+const dir = path.join(app, "build/grammars");
16+const out = path.join(app, "src/web/grammars.generated.ts");
17+
18+/** Shiki's names for the languages Diffinery highlights (src/web/highlight.ts maps file names and fences to them). */
19+const LANGS = ["astro", "bat", "c", "clojure", "cmake", "cpp", "csharp", "css", "dart", "diff", "docker", "dotenv", "elixir", "erlang", "fish", "fsharp", "go", "graphql", "groovy", "haskell", "hcl", "html", "ini", "java", "javascript", "json", "json5", "jsonc", "jsx", "julia", "kotlin", "less", "lua", "make", "markdown", "nginx", "nix", "objective-c", "ocaml", "perl", "php", "powershell", "proto", "python", "r", "ruby", "rust", "sass", "scala", "scss", "shellscript", "sql", "svelte", "swift", "terraform", "toml", "tsx", "typescript", "vue", "xml", "yaml", "zig"];
20+
21+/** Each grammar's JSON by name, and each language's grammars in registration order (the ones it embeds, then its own). */
22+const grammars = new Map();
23+const order = {};
24+for (const lang of LANGS) {
25+ const { default: regs } = await import(`shiki/langs/${lang}.mjs`);
26+ order[lang] = [];
27+ for (const reg of regs) {
28+ const json = JSON.stringify(reg);
29+ if (grammars.has(reg.name) && grammars.get(reg.name) !== json) throw new Error(`grammars: two different grammars are named "${reg.name}"`);
30+ grammars.set(reg.name, json);
31+ if (!order[lang].includes(reg.name)) order[lang].push(reg.name);
32+ }
33+}
34+
35+await rm(dir, { recursive: true, force: true });
36+await mkdir(dir, { recursive: true });
37+for (const [name, json] of grammars) await writeFile(path.join(dir, `${name}.txt`), json);
38+
39+const names = [...grammars.keys()].sort();
40+const imports = names.map((n, i) => `import g${i} from "../../build/grammars/${n}.txt";`);
41+const texts = names.map((n, i) => ` ${JSON.stringify(n)}: g${i},`);
42+await writeFile(
43+ out,
44+ `// Generated by scripts/build-grammars.mjs from Shiki's grammars. Do not edit.
45+/* eslint-disable */
46+${imports.join("\n")}
47+
48+export const GRAMMAR_LANGS = ${JSON.stringify(LANGS)} as const;
49+/** Each language's grammars in registration order: the ones it embeds, then its own. */
50+export const GRAMMAR_ORDER: Record<(typeof GRAMMAR_LANGS)[number], string[]> = ${JSON.stringify(order)};
51+/** Each grammar's JSON, by name. */
52+export const GRAMMAR_TEXT: Record<string, string> = {
53+${texts.join("\n")}
54+};
55+`,
56+);
57+const bytes = [...grammars.values()].reduce((n, j) => n + j.length, 0);
58+console.log(`grammars: ${LANGS.length} languages, ${grammars.size} grammars (${Math.round(bytes / 1024)} KiB)`);
modifiedapps/diffinery/src/web/highlight.ts+19−72View file
@@ -1,82 +1,29 @@
11 /**
22 * Syntax highlighting with Shiki (JavaScript regex engine, fine-grained grammars
3- * loaded on first use). Tokens carry both GitHub themes as CSS variables
4- * (--shiki-light / --shiki-dark); the site stylesheet picks one. Everything here
5- * fails soft: an unknown language, an oversized input or an error means plain text.
3+ * parsed on first use from the text modules scripts/build-grammars.mjs writes).
4+ * Tokens carry both GitHub themes as CSS variables (--shiki-light / --shiki-dark);
5+ * the site stylesheet picks one. Everything here fails soft: an unknown language,
6+ * an oversized input or an error means plain text.
67 */
78 import { createHighlighterCore, type HighlighterCore, type LanguageRegistration, type ThemedToken, type ThemeRegistration } from "shiki/core";
89 import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
10+import { GRAMMAR_LANGS, GRAMMAR_ORDER, GRAMMAR_TEXT } from "./grammars.generated.js";
911 import { escapeHtml } from "./markdown.js";
1012
11-type Grammar = () => Promise<{ default: LanguageRegistration[] }>;
13+export type Lang = (typeof GRAMMAR_LANGS)[number];
14+export const LANGS: Lang[] = [...GRAMMAR_LANGS];
15+const KNOWN = new Set<string>(GRAMMAR_LANGS);
1216
13-const GRAMMARS = {
14- astro: () => import("shiki/langs/astro.mjs"),
15- bat: () => import("shiki/langs/bat.mjs"),
16- c: () => import("shiki/langs/c.mjs"),
17- clojure: () => import("shiki/langs/clojure.mjs"),
18- cmake: () => import("shiki/langs/cmake.mjs"),
19- cpp: () => import("shiki/langs/cpp.mjs"),
20- csharp: () => import("shiki/langs/csharp.mjs"),
21- css: () => import("shiki/langs/css.mjs"),
22- dart: () => import("shiki/langs/dart.mjs"),
23- diff: () => import("shiki/langs/diff.mjs"),
24- docker: () => import("shiki/langs/docker.mjs"),
25- dotenv: () => import("shiki/langs/dotenv.mjs"),
26- elixir: () => import("shiki/langs/elixir.mjs"),
27- erlang: () => import("shiki/langs/erlang.mjs"),
28- fish: () => import("shiki/langs/fish.mjs"),
29- fsharp: () => import("shiki/langs/fsharp.mjs"),
30- go: () => import("shiki/langs/go.mjs"),
31- graphql: () => import("shiki/langs/graphql.mjs"),
32- groovy: () => import("shiki/langs/groovy.mjs"),
33- haskell: () => import("shiki/langs/haskell.mjs"),
34- hcl: () => import("shiki/langs/hcl.mjs"),
35- html: () => import("shiki/langs/html.mjs"),
36- ini: () => import("shiki/langs/ini.mjs"),
37- java: () => import("shiki/langs/java.mjs"),
38- javascript: () => import("shiki/langs/javascript.mjs"),
39- json: () => import("shiki/langs/json.mjs"),
40- json5: () => import("shiki/langs/json5.mjs"),
41- jsonc: () => import("shiki/langs/jsonc.mjs"),
42- jsx: () => import("shiki/langs/jsx.mjs"),
43- julia: () => import("shiki/langs/julia.mjs"),
44- kotlin: () => import("shiki/langs/kotlin.mjs"),
45- less: () => import("shiki/langs/less.mjs"),
46- lua: () => import("shiki/langs/lua.mjs"),
47- make: () => import("shiki/langs/make.mjs"),
48- markdown: () => import("shiki/langs/markdown.mjs"),
49- nginx: () => import("shiki/langs/nginx.mjs"),
50- nix: () => import("shiki/langs/nix.mjs"),
51- "objective-c": () => import("shiki/langs/objective-c.mjs"),
52- ocaml: () => import("shiki/langs/ocaml.mjs"),
53- perl: () => import("shiki/langs/perl.mjs"),
54- php: () => import("shiki/langs/php.mjs"),
55- powershell: () => import("shiki/langs/powershell.mjs"),
56- proto: () => import("shiki/langs/proto.mjs"),
57- python: () => import("shiki/langs/python.mjs"),
58- r: () => import("shiki/langs/r.mjs"),
59- ruby: () => import("shiki/langs/ruby.mjs"),
60- rust: () => import("shiki/langs/rust.mjs"),
61- sass: () => import("shiki/langs/sass.mjs"),
62- scala: () => import("shiki/langs/scala.mjs"),
63- scss: () => import("shiki/langs/scss.mjs"),
64- shellscript: () => import("shiki/langs/shellscript.mjs"),
65- sql: () => import("shiki/langs/sql.mjs"),
66- svelte: () => import("shiki/langs/svelte.mjs"),
67- swift: () => import("shiki/langs/swift.mjs"),
68- terraform: () => import("shiki/langs/terraform.mjs"),
69- toml: () => import("shiki/langs/toml.mjs"),
70- tsx: () => import("shiki/langs/tsx.mjs"),
71- typescript: () => import("shiki/langs/typescript.mjs"),
72- vue: () => import("shiki/langs/vue.mjs"),
73- xml: () => import("shiki/langs/xml.mjs"),
74- yaml: () => import("shiki/langs/yaml.mjs"),
75- zig: () => import("shiki/langs/zig.mjs"),
76-} satisfies Record<string, Grammar>;
17+const parsed = new Map<string, LanguageRegistration>();
7718
78-export type Lang = keyof typeof GRAMMARS;
79-export const LANGS = Object.keys(GRAMMARS) as Lang[];
19+/** A language's grammars (the ones it embeds, then its own), each parsed from its text module once. */
20+function grammar(lang: Lang): LanguageRegistration[] {
21+ return GRAMMAR_ORDER[lang].map((name) => {
22+ let g = parsed.get(name);
23+ if (!g) parsed.set(name, (g = JSON.parse(GRAMMAR_TEXT[name]!) as LanguageRegistration));
24+ return g;
25+ });
26+}
8027
8128 const EXT: Record<string, Lang> = {
8229 ts: "typescript", mts: "typescript", cts: "typescript", tsx: "tsx",
@@ -137,7 +84,7 @@
13784 /** Shiki language for a Markdown fence info string ("ts", "yaml", "c++" …), or null. */
13885 export function langForFence(info: string): Lang | null {
13986 const name = info.trim().toLowerCase().replace(/^\{?\.?/, "").replace(/\}$/, "");
140- return name in GRAMMARS ? (name as Lang) : (ALIASES[name] ?? null);
87+ return KNOWN.has(name) ? (name as Lang) : (ALIASES[name] ?? null);
14188 }
14289
14390 // ---------------------------------------------------------------- highlighter
@@ -207,7 +154,7 @@
207154 let p = loading.get(l);
208155 if (!p) {
209156 p = hl
210- .loadLanguage(GRAMMARS[l]())
157+ .loadLanguage(...grammar(l))
211158 // Compile the grammar's top-level rules now: on a cold Worker that alone can outlast the
212159 // per-line time limit, which would leave the first line of a file unhighlighted.
213160 .then(() => void hl.codeToTokens("x", { lang: l, theme: PAIR }))
addedapps/diffinery/test/unit/main-module-size.test.ts+27−0View file
@@ -1,0 +1,27 @@
1+/**
2+ * The bulky generated content stays out of the Worker's main module, in text modules that upload as parts of their
3+ * own (scripts/build-docs.mjs, scripts/build-grammars.mjs). With it inlined the main module was 14 MB, which hosts
4+ * that cap each uploaded part refuse (open-compute: 8 MiB). Needs `pnpm build`.
5+ */
6+import { statSync } from "node:fs";
7+import { describe, expect, it } from "vitest";
8+import { DOCS_PAGES } from "../../src/docs/content.generated.js";
9+import { GRAMMAR_LANGS, GRAMMAR_ORDER, GRAMMAR_TEXT } from "../../src/web/grammars.generated.js";
10+
11+const size = (rel: string) => statSync(new URL(rel, import.meta.url)).size;
12+
13+describe("main module size", () => {
14+ it("keeps the docs' pages, images and downloads in a text module", () => {
15+ expect(size("../../src/docs/content.generated.ts")).toBeLessThan(64 * 1024);
16+ expect(size("../../build/docs/content.txt")).toBeGreaterThan(1024 * 1024);
17+ expect(DOCS_PAGES.quickstart?.title).toBeTruthy();
18+ });
19+
20+ it("keeps the highlighting grammars in text modules, each grammar once", () => {
21+ expect(size("../../src/web/grammars.generated.ts")).toBeLessThan(32 * 1024);
22+ for (const lang of GRAMMAR_LANGS) {
23+ expect(GRAMMAR_ORDER[lang].length).toBeGreaterThan(0);
24+ for (const name of GRAMMAR_ORDER[lang]) expect(JSON.parse(GRAMMAR_TEXT[name]!).name).toBe(name);
25+ }
26+ });
27+});