| 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)`); |