Skip to content

Commit 18ebc3f

Browse files
committed
Start using @gerrit0/mini-shiki instead of Shiki
This removes ~20 unused dependencies from TypeDoc's dependency graph with no user facing change as the API remains the same.
1 parent c5aee13 commit 18ebc3f

File tree

10 files changed

+60
-519
lines changed

10 files changed

+60
-519
lines changed

package-lock.json

Lines changed: 12 additions & 464 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
"node": ">= 18"
2626
},
2727
"dependencies": {
28+
"@gerrit0/mini-shiki": "^1.23.2",
2829
"lunr": "^2.3.9",
2930
"markdown-it": "^14.1.0",
3031
"minimatch": "^9.0.5",
31-
"shiki": "^1.23.1",
3232
"yaml": "^2.6.1"
3333
},
3434
"peerDependencies": {

src/lib/application.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import {
4646
Internationalization,
4747
type TranslatedString,
4848
} from "./internationalization/internationalization.js";
49-
import { loadShikiMetadata } from "./utils/highlighter.js";
5049
import { ValidatingFileRegistry, FileRegistry } from "./models/FileRegistry.js";
5150
import { readFileSync } from "fs";
5251
import { fileURLToPath } from "url";
@@ -228,7 +227,6 @@ export class Application extends AbstractComponent<
228227
options: Partial<TypeDocOptions> = {},
229228
readers: readonly OptionsReader[] = DEFAULT_READERS,
230229
): Promise<Application> {
231-
await loadShikiMetadata();
232230
const app = new Application(DETECTOR);
233231
readers.forEach((r) => app.options.addReader(r));
234232
app.options.reset();
@@ -258,7 +256,6 @@ export class Application extends AbstractComponent<
258256
options: Partial<TypeDocOptions> = {},
259257
readers: readonly OptionsReader[] = DEFAULT_READERS,
260258
): Promise<Application> {
261-
await loadShikiMetadata();
262259
const app = new Application(DETECTOR);
263260
readers.forEach((r) => app.options.addReader(r));
264261
await app._bootstrap(options);

src/lib/output/renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { loadHighlighter } from "../utils/highlighter.js";
2626
import type {
2727
BundledLanguage,
2828
BundledTheme as ShikiTheme,
29-
} from "shiki" with { "resolution-mode": "import" };
29+
} from "@gerrit0/mini-shiki";
3030
import { type Comment, Reflection } from "../models/index.js";
3131
import type { JsxElement } from "../utils/jsx.elements.js";
3232
import type { DefaultThemeRenderContext } from "./themes/default/DefaultThemeRenderContext.js";

src/lib/output/themes/MarkedPlugin.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ContextAwareRendererComponent } from "../components.js";
66
import { MarkdownEvent, RendererEvent, type PageEvent } from "../events.js";
77
import { Option, renderElement, assertNever } from "../../utils/index.js";
88
import { highlight, isLoadedLanguage, isSupportedLanguage } from "../../utils/highlighter.js";
9-
import type { BundledTheme } from "shiki" with { "resolution-mode": "import" };
9+
import type { BundledTheme } from "@gerrit0/mini-shiki";
1010
import { escapeHtml } from "../../utils/html.js";
1111
import type { DefaultThemeRenderContext, Renderer } from "../index.js";
1212
import { anchorIcon } from "./default/partials/anchor-icon.js";

src/lib/output/themes/default/assets/typedoc/components/Search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function initSearch() {
5353
if (!searchEl) return;
5454

5555
const state: SearchState = {
56-
base: searchEl.dataset["base"] + "/",
56+
base: document.documentElement.dataset.base! + "/",
5757
};
5858

5959
const searchScript = document.getElementById(

src/lib/utils/highlighter.tsx

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,47 @@
1-
import { ok as assert, ok } from "assert";
2-
import type {
3-
BundledLanguage,
4-
BundledTheme,
5-
Highlighter,
6-
TokenStyles,
7-
} from "shiki" with { "resolution-mode": "import" };
1+
import * as shiki from "@gerrit0/mini-shiki";
2+
import type { ShikiInternal } from "@shikijs/types";
83
import * as JSX from "./jsx.js";
94
import { unique } from "./array.js";
5+
import assert from "assert";
106

117
const aliases = new Map<string, string>();
12-
let supportedLanguagesWithoutAliases: string[] = [];
13-
let supportedLanguages: string[] = [];
14-
let supportedThemes: string[] = [];
15-
16-
const plaintextLanguages = ["txt", "text"];
17-
18-
export async function loadShikiMetadata() {
19-
if (aliases.size) return;
20-
21-
const shiki = await import("shiki");
22-
for (const lang of shiki.bundledLanguagesInfo) {
23-
for (const alias of lang.aliases || []) {
24-
aliases.set(alias, lang.id);
25-
}
8+
for (const lang of shiki.bundledLanguagesInfo) {
9+
for (const alias of lang.aliases || []) {
10+
aliases.set(alias, lang.id);
2611
}
12+
}
2713

28-
supportedLanguages = unique([
29-
...plaintextLanguages,
30-
...aliases.keys(),
31-
...shiki.bundledLanguagesInfo.map((lang) => lang.id),
32-
]).sort();
14+
const plaintextLanguages = ["txt", "text"];
3315

34-
supportedLanguagesWithoutAliases = unique([
35-
...plaintextLanguages,
36-
...shiki.bundledLanguagesInfo.map((lang) => lang.id),
37-
]);
16+
const supportedLanguagesWithoutAliases = unique([
17+
...plaintextLanguages,
18+
...shiki.bundledLanguagesInfo.map((lang) => lang.id),
19+
]);
20+
const supportedLanguages: string[] = unique([
21+
...plaintextLanguages,
22+
...aliases.keys(),
23+
...shiki.bundledLanguagesInfo.map((lang) => lang.id),
24+
]).sort();
3825

39-
supportedThemes = Object.keys(shiki.bundledThemes);
40-
}
26+
const supportedThemes: string[] = Object.keys(shiki.bundledThemes);
4127

4228
class DoubleHighlighter {
4329
private schemes = new Map<string, string>();
4430

4531
constructor(
46-
private highlighter: Highlighter,
47-
private light: BundledTheme,
48-
private dark: BundledTheme,
32+
private highlighter: ShikiInternal,
33+
private light: shiki.BundledTheme,
34+
private dark: shiki.BundledTheme,
4935
) {}
5036

5137
supports(lang: string) {
5238
return this.highlighter.getLoadedLanguages().includes(lang);
5339
}
5440

5541
highlight(code: string, lang: string) {
56-
const tokens = this.highlighter.codeToTokensWithThemes(code, {
42+
const tokens = shiki.codeToTokensWithThemes(this.highlighter, code, {
5743
themes: { light: this.light, dark: this.dark },
58-
lang: lang as BundledLanguage,
44+
lang: lang as shiki.BundledLanguage,
5945
});
6046

6147
const docEls: JSX.Element[] = [];
@@ -121,7 +107,7 @@ class DoubleHighlighter {
121107
return style.join("\n");
122108
}
123109

124-
private getClass(variants: Record<string, TokenStyles>): string {
110+
private getClass(variants: Record<string, shiki.TokenStyles>): string {
125111
const key = `${variants["light"].color} | ${variants["dark"].color}`;
126112
let scheme = this.schemes.get(key);
127113
if (scheme == null) {
@@ -132,13 +118,26 @@ class DoubleHighlighter {
132118
}
133119
}
134120

121+
let shikiEngine: shiki.RegexEngine | undefined;
135122
let highlighter: DoubleHighlighter | undefined;
136123

137-
export async function loadHighlighter(lightTheme: BundledTheme, darkTheme: BundledTheme, langs: BundledLanguage[]) {
124+
export async function loadHighlighter(
125+
lightTheme: shiki.BundledTheme,
126+
darkTheme: shiki.BundledTheme,
127+
langs: shiki.BundledLanguage[],
128+
) {
138129
if (highlighter) return;
139130

140-
const shiki = await import("shiki");
141-
const hl = await shiki.createHighlighter({ themes: [lightTheme, darkTheme], langs });
131+
if (!shikiEngine) {
132+
await shiki.loadBuiltinWasm();
133+
shikiEngine = await shiki.createOnigurumaEngine();
134+
}
135+
136+
const hl = await shiki.createShikiInternal({
137+
engine: shikiEngine,
138+
themes: [shiki.bundledThemes[lightTheme], shiki.bundledThemes[darkTheme]],
139+
langs: langs.map((lang) => shiki.bundledLanguages[lang]),
140+
});
142141
highlighter = new DoubleHighlighter(hl, lightTheme, darkTheme);
143142
}
144143

@@ -147,17 +146,14 @@ export function isSupportedLanguage(lang: string) {
147146
}
148147

149148
export function getSupportedLanguages(): string[] {
150-
ok(supportedLanguages.length > 0, "loadShikiMetadata has not been called");
151149
return supportedLanguages;
152150
}
153151

154152
export function getSupportedLanguagesWithoutAliases(): string[] {
155-
ok(supportedLanguagesWithoutAliases.length > 0, "loadShikiMetadata has not been called");
156-
return supportedLanguages;
153+
return supportedLanguagesWithoutAliases;
157154
}
158155

159156
export function getSupportedThemes(): string[] {
160-
ok(supportedThemes.length > 0, "loadShikiMetadata has not been called");
161157
return supportedThemes;
162158
}
163159

src/lib/utils/options/declaration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BundledTheme as ShikiTheme } from "shiki" with { "resolution-mode": "import" };
1+
import type { BundledTheme as ShikiTheme } from "@gerrit0/mini-shiki";
22
import type { LogLevel } from "../loggers.js";
33
import type { SortStrategy } from "../sort.js";
44
import { isAbsolute, join, resolve } from "path";

src/lib/utils/options/defaults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BundledLanguage } from "shiki" with { "resolution-mode": "import" };
1+
import type { BundledLanguage } from "@gerrit0/mini-shiki";
22
import * as TagDefaults from "./tsdoc-defaults.js";
33
import type { EnumKeys } from "../enum.js";
44
import type { ReflectionKind } from "../../models/index.js";

src/lib/utils/options/sources/typedoc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ReflectionKind } from "../../../models/reflections/kind.js";
1313
import * as Validation from "../../validation.js";
1414
import { blockTags, inlineTags, modifierTags } from "../tsdoc-defaults.js";
1515
import { getEnumKeys } from "../../enum.js";
16-
import type { BundledTheme } from "shiki" with { "resolution-mode": "import" };
16+
import type { BundledTheme } from "@gerrit0/mini-shiki";
1717
import {
1818
getSupportedLanguagesWithoutAliases,
1919
getSupportedThemes,

0 commit comments

Comments
 (0)