Skip to content
Myr

shiki.ts

This file

Path

apps/web/src/server/shiki.ts

46 lines · 1.4 KiB · TypeScript

Last changed by

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

History of this file

Take it

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

lines 1–2why, one step back
1// `shiki` for the Worker: the package's entry maps every grammar and theme to a dynamic import (about 10 MB),
2// and its Oniguruma engine compiles WebAssembly, which a Worker may not. The server attaches its few grammars itself.

Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2

lines 3–46why, one step back
3import {
4 createBundledHighlighter,5 createSingletonShorthands,6 guessEmbeddedLanguages,7} from "shiki/core";8import type {9 DynamicImportLanguageRegistration,10 DynamicImportThemeRegistration,11 RegexEngine,12} from "shiki/core";13import { createJavaScriptRegexEngine } from "shiki/engine/javascript";1415export * from "shiki/core";1617export { createJavaScriptRegexEngine } from "shiki/engine/javascript";1819export const bundledLanguages: Record<20 string,21 DynamicImportLanguageRegistration22> = {};2324export const bundledThemes: Record<string, DynamicImportThemeRegistration> = {};2526/** Oniguruma compiles WebAssembly, which a Worker may not: never on the server. */27export const createOnigurumaEngine = (): Promise<RegexEngine> =>28 Promise.reject(29 new Error("The Worker highlights with the JavaScript engine only")30 );3132export const createHighlighter = createBundledHighlighter<string, string>({33 engine: () => createJavaScriptRegexEngine(),34 langs: bundledLanguages,35 themes: bundledThemes,36});3738export const {39 codeToHast,40 codeToHtml,41 codeToTokens,42 codeToTokensBase,43 codeToTokensWithThemes,44 getLastGrammarState,45 getSingletonHighlighter,46} = createSingletonShorthands(createHighlighter, { guessEmbeddedLanguages });