Skip to content

Commit d43202f

Browse files
authored
Reduce bundle size of shiki (#2551)
1 parent 0964043 commit d43202f

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

.changeset/lovely-hounds-invent.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'gitbook': patch
3+
---
4+
5+
Optimize bundle size of the server output by reducing bundle size of shiki (skipping themes)

bun.lockb

0 Bytes
Binary file not shown.

packages/gitbook/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"remark-rehype": "^11.1.0",
6161
"rison": "^0.1.1",
6262
"server-only": "^0.0.1",
63-
"shiki": "^1.11.1",
63+
"shiki": "^1.22.0",
6464
"tailwind-merge": "^2.2.0",
6565
"tailwind-shades": "^1.1.2",
6666
"unified": "^11.0.4",

packages/gitbook/src/components/DocumentView/CodeBlock/highlight.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import { DocumentBlockCode, DocumentBlockCodeLine, DocumentInlineAnnotation } from '@gitbook/api';
22
import {
3-
loadWasm,
3+
createdBundledHighlighter,
44
ThemedToken,
5-
getHighlighter,
65
createCssVariablesTheme,
76
HighlighterGeneric,
8-
bundledLanguages,
9-
bundledThemes,
10-
} from 'shiki';
7+
} from 'shiki/core';
8+
import { loadWasm, createOnigurumaEngine } from 'shiki/engine/oniguruma';
9+
import { bundledLanguages } from 'shiki/langs';
1110
// @ts-ignore - onigWasm is a Wasm module
1211
import onigWasm from 'shiki/onig.wasm?module';
1312

1413
import { asyncMutexFunction, singleton } from '@/lib/async';
1514
import { getNodeText } from '@/lib/document';
1615
import { trace } from '@/lib/tracing';
1716

18-
import { DocumentContext } from '../DocumentView';
19-
2017
export type HighlightLine = {
2118
highlighted: boolean;
2219
tokens: HighlightToken[];
@@ -299,6 +296,12 @@ function cleanupLine(line: string): string {
299296
return line.replace(/\r/g, '');
300297
}
301298

299+
const createHighlighter = createdBundledHighlighter<any, any>({
300+
langs: bundledLanguages,
301+
themes: {},
302+
engine: () => createOnigurumaEngine(import('shiki/wasm')),
303+
});
304+
302305
/**
303306
* Load the highlighter, only once, and reuse it.
304307
* It makes sure to handle concurrent calls.
@@ -312,7 +315,8 @@ const loadHighlighter = singleton(async () => {
312315
// Otherwise for Vercel/Cloudflare, we need to load it ourselves.
313316
await loadWasm((obj) => WebAssembly.instantiate(onigWasm, obj));
314317
}
315-
const highlighter = await getHighlighter({
318+
319+
const highlighter = await createHighlighter({
316320
themes: [createCssVariablesTheme()],
317321
langs: [],
318322
});
@@ -322,7 +326,7 @@ const loadHighlighter = singleton(async () => {
322326

323327
const loadLanguagesMutex = asyncMutexFunction();
324328
async function loadHighlighterLanguage(
325-
highlighter: HighlighterGeneric<keyof typeof bundledLanguages, keyof typeof bundledThemes>,
329+
highlighter: HighlighterGeneric<any, any>,
326330
lang: keyof typeof bundledLanguages,
327331
) {
328332
await loadLanguagesMutex.runBlocking(async () => {

0 commit comments

Comments
 (0)