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.

diffinery/apps/diffinery/scripts/build-grammars.mjs
58 lines · 3.3 KBRaw History
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.
10import { mkdir, rm, writeFile } from "node:fs/promises";
11import path from "node:path";
12import { fileURLToPath } from "node:url";
13
14const app = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
15const dir = path.join(app, "build/grammars");
16const 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). */
19const 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). */
22const grammars = new Map();
23const order = {};
24for (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
35await rm(dir, { recursive: true, force: true });
36await mkdir(dir, { recursive: true });
37for (const [name, json] of grammars) await writeFile(path.join(dir, `${name}.txt`), json);
38
39const names = [...grammars.keys()].sort();
40const imports = names.map((n, i) => `import g${i} from "../../build/grammars/${n}.txt";`);
41const texts = names.map((n, i) => ` ${JSON.stringify(n)}: g${i},`);
42await 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
48export const GRAMMAR_LANGS = ${JSON.stringify(LANGS)} as const;
49/** Each language's grammars in registration order: the ones it embeds, then its own. */
50export const GRAMMAR_ORDER: Record<(typeof GRAMMAR_LANGS)[number], string[]> = ${JSON.stringify(order)};
51/** Each grammar's JSON, by name. */
52export const GRAMMAR_TEXT: Record<string, string> = {
53${texts.join("\n")}
54};
55`,
56);
57const bytes = [...grammars.values()].reduce((n, j) => n + j.length, 0);
58console.log(`grammars: ${LANGS.length} languages, ${grammars.size} grammars (${Math.round(bytes / 1024)} KiB)`);