Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(web): keep Shiki out of the Worker bundle (#587)

Gener584000512a37
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 10d7cc7fa..589901b0a 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -331,6 +331,18 @@ importers:
'@opennextjs/cloudflare':
specifier: ^1.20.2
+ '@shikijs/core':
+ specifier: 4.4.3
+ version: 4.4.3
+ '@shikijs/engine-oniguruma':
+ specifier: 4.4.3
+ version: 4.4.3
+ '@shikijs/langs':
+ specifier: 4.4.3
+ version: 4.4.3
+ '@shikijs/themes':
+ specifier: 4.4.3
+ version: 4.4.3
'@streamdown/cjk':
specifier: ^1.0.3
diff --git a/src/web/package.json b/src/web/package.json
index 0ed76dfee..053c5ffc2 100644
--- a/src/web/package.json
+++ b/src/web/package.json
@@ -43,6 +43,10 @@
"@next/third-parties": "^16.3.0",
"@number-flow/react": "^0.6.2",
"@opennextjs/cloudflare": "^1.20.2",
+ "@shikijs/core": "4.4.3",
+ "@shikijs/engine-oniguruma": "4.4.3",
+ "@shikijs/langs": "4.4.3",
+ "@shikijs/themes": "4.4.3",
"@streamdown/cjk": "^1.0.3",
"@streamdown/math": "^1.0.2",
"@streamdown/mermaid": "^1.0.2",
diff --git a/src/web/src/components/community/messages/attachment-preview-sheet.test.ts b/src/web/src/components/community/messages/attachment-preview-sheet.test.ts
index c20ec839b..185c03c7a 100644
--- a/src/web/src/components/community/messages/attachment-preview-sheet.test.ts
+++ b/src/web/src/components/community/messages/attachment-preview-sheet.test.ts
@@ -4,6 +4,21 @@ import { afterEach, describe, expect, it, vi } from "vitest"
import { AttachmentPreviewSheet, readAttachmentText } from "./attachment-preview-sheet"
import type { FileAttachment } from "@/lib/community/models/message"
+const dynamicMock = vi.hoisted(() => ({
+ options: null as null | Record<string, unknown>,
+}))
+
+vi.mock("next/dynamic", () => ({
+ default: (_loader: unknown, options: Record<string, unknown>) => {
+ dynamicMock.options = options
+ return ({ content, language }: { content: string; language: string | null }) => React.createElement(
+ "pre",
+ { "data-code-language": language ?? "plain" },
+ content,
+ )
+ },
+}))
+
vi.mock("@/components/community/shell/community-sheet", () => ({
CommunitySheet: ({
title,
@@ -89,6 +104,21 @@ describe("readAttachmentText", () => {
})
describe("AttachmentPreviewSheet", () => {
+ it("keeps the syntax-preview subtree client-only with a stable loading state", async () => {
+ expect(dynamicMock.options?.ssr).toBe(false)
+ const loading = dynamicMock.options?.loading as (() => React.ReactElement) | undefined
+ expect(loading).toBeTypeOf("function")
+ let renderer: TestRenderer.ReactTestRenderer
+ await act(async () => {
+ renderer = TestRenderer.create(loading!())
+ })
+ const state = renderer!.root.findByProps({
+ "data-testid": "community-code-preview",
+ })
+ expect(state.props.role).toBe("status")
+ expect(state.children.join("")).toContain("Loading syntax highlighter")
+ })
+
it("fetches private Markdown, renders it safely, and exposes metadata/download", async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response("# Hello", { status: 200 }))
vi.stubGlobal("fetch", fetchMock)
diff --git a/src/web/src/components/community/messages/attachment-preview-sheet.tsx b/src/web/src/components/community/messages/attachment-preview-sheet.tsx
index 72a2716fb..f1b9544aa 100644
--- a/src/web/src/components/community/messages/attachment-preview-sheet.tsx
+++ b/src/web/src/components/community/messages/attachment-preview-sheet.tsx
@@ -1,12 +1,12 @@
"use client"
import { useEffect, useState } from "react"
+import dynamic from "next/dynamic"
import { Download, Loader2 } from "lucide-react"
import { buttonVariants } from "@/components/ui/button"
import { CommunitySheet } from "@/components/community/shell/community-sheet"
import { cn } from "@/lib/utils"
import { MessageBody } from "./message-body"
-import { CodePreview } from "./code-preview"
import type { FileAttachment } from "@/lib/community/models/message"
import { tid } from "@/lib/community/testids"
import {
@@ -15,6 +15,23 @@ import {
resolveAttachmentPresentation,
} from "@/lib/community/attachment-presentation"
+const CodePreview = dynamic(
+ () => import("./code-preview").then((module) => module.CodePreview),
+ {
+ ssr: false,
+ loading: () => (
+ <div
+ data-testid={tid.codePreview}
+ role="status"
+ className="flex min-h-40 items-center justify-center gap-2 text-sm text-muted-foreground"
+ >
+ <Loader2 className="size-4 animate-spin" />
+ Loading syntax highlighter…
+ </div>
+ ),
+ },
+)
+
type PreviewState =
| { status: "idle" | "loading"; content: null; error: null }
| { status: "ready"; content: string; error: null }
diff --git a/src/web/src/lib/community/code-highlight.test.ts b/src/web/src/lib/community/code-highlight.test.ts
index 9df25234a..8e011f831 100644
--- a/src/web/src/lib/community/code-highlight.test.ts
+++ b/src/web/src/lib/community/code-highlight.test.ts
@@ -1,4 +1,45 @@
import { beforeEach, describe, expect, it, vi } from "vitest"
+import type { ShikiLanguage } from "./attachment-presentation"
+
+const SUPPORTED_LANGUAGES = [
+ "bash",
+ "c",
+ "cpp",
+ "csharp",
+ "css",
+ "diff",
+ "dockerfile",
+ "dotenv",
+ "go",
+ "graphql",
+ "hcl",
+ "html",
+ "ini",
+ "java",
+ "javascript",
+ "json",
+ "jsonl",
+ "jsx",
+ "kotlin",
+ "log",
+ "makefile",
+ "mdx",
+ "php",
+ "proto",
+ "python",
+ "ruby",
+ "rust",
+ "scss",
+ "sql",
+ "svelte",
+ "swift",
+ "toml",
+ "tsx",
+ "typescript",
+ "vue",
+ "xml",
+ "yaml",
+] as const satisfies readonly ShikiLanguage[]
const shikiMocks = vi.hoisted(() => {
const highlighter = {
@@ -13,32 +54,32 @@ const shikiMocks = vi.hoisted(() => {
}
})
-vi.mock("shiki/core", () => ({
+vi.mock("@shikijs/core", () => ({
createHighlighterCore: shikiMocks.createHighlighterCore,
}))
-vi.mock("shiki/engine/oniguruma", () => ({
+vi.mock("@shikijs/engine-oniguruma", () => ({
createOnigurumaEngine: shikiMocks.createOnigurumaEngine,
}))
-vi.mock("shiki/wasm", () => ({ default: { wasm: true } }))
+vi.mock("@shikijs/engine-oniguruma/wasm-inlined", () => ({ default: { wasm: true } }))
-vi.mock("shiki/langs", () => ({
+vi.mock("@shikijs/langs", () => ({
get bundledLanguages() {
throw new Error("Broad language registry must not load")
},
}))
-vi.mock("shiki/themes", () => ({
+vi.mock("@shikijs/themes", () => ({
get bundledThemes() {
throw new Error("Broad theme registry must not load")
},
}))
-vi.mock("shiki/langs/json.mjs", () => ({ default: [{ id: "json" }] }))
-vi.mock("shiki/langs/typescript.mjs", () => ({ default: [{ id: "typescript" }] }))
-vi.mock("shiki/themes/github-light.mjs", () => ({ default: { name: "github-light" } }))
-vi.mock("shiki/themes/github-dark.mjs", () => ({ default: { name: "github-dark" } }))
+vi.mock("@shikijs/langs/json", () => ({ default: [{ id: "json" }] }))
+vi.mock("@shikijs/langs/typescript", () => ({ default: [{ id: "typescript" }] }))
+vi.mock("@shikijs/themes/github-light", () => ({ default: { name: "github-light" } }))
+vi.mock("@shikijs/themes/github-dark", () => ({ default: { name: "github-dark" } }))
beforeEach(() => {
vi.resetModules()
@@ -98,6 +139,16 @@ describe("highlightCode", () => {
expect(second.kind).toBe("highlighted")
})
+ it("resolves every supported language through its explicit package entry", async () => {
+ shikiMocks.highlighter.codeToTokensWithThemes.mockReturnValue([])
+ const { highlightCode } = await import("./code-highlight")
+
+ await Promise.all(SUPPORTED_LANGUAGES.map((language) => highlightCode("source", language)))
+
+ expect(shikiMocks.highlighter.loadLanguage).toHaveBeenCalledTimes(SUPPORTED_LANGUAGES.length)
+ expect(shikiMocks.highlighter.codeToTokensWithThemes).toHaveBeenCalledTimes(SUPPORTED_LANGUAGES.length)
+ })
+
it("skips Shiki above the byte or line token budget", async () => {
const {
highlightCode,
diff --git a/src/web/src/lib/community/code-highlight.ts b/src/web/src/lib/community/code-highlight.ts
index 6d2efc9bc..690445f05 100644
--- a/src/web/src/lib/community/code-highlight.ts
+++ b/src/web/src/lib/community/code-highlight.ts
@@ -1,5 +1,5 @@
import type { ShikiLanguage } from "./attachment-presentation"
-import type { LanguageRegistration } from "shiki/core"
+import type { LanguageRegistration } from "@shikijs/core"
export const MAX_CODE_HIGHLIGHT_BYTES = 256 * 1024
export const MAX_CODE_HIGHLIGHT_LINES = 2_000
@@ -37,43 +37,43 @@ let runtimePromise: Promise<Runtime> | null = null
const languageLoads = new Map<ShikiLanguage, Promise<void>>()
const LANGUAGE_LOADERS = {
- bash: () => import("shiki/langs/bash.mjs").then((module) => module.default),
- c: () => import("shiki/langs/c.mjs").then((module) => module.default),
- cpp: () => import("shiki/langs/cpp.mjs").then((module) => module.default),
- csharp: () => import("shiki/langs/csharp.mjs").then((module) => module.default),
- css: () => import("shiki/langs/css.mjs").then((module) => module.default),
- diff: () => import("shiki/langs/diff.mjs").then((module) => module.default),
- dockerfile: () => import("shiki/langs/dockerfile.mjs").then((module) => module.default),
- dotenv: () => import("shiki/langs/dotenv.mjs").then((module) => module.default),
- go: () => import("shiki/langs/go.mjs").then((module) => module.default),
- graphql: () => import("shiki/langs/graphql.mjs").then((module) => module.default),
- hcl: () => import("shiki/langs/hcl.mjs").then((module) => module.default),
- html: () => import("shiki/langs/html.mjs").then((module) => module.default),
- ini: () => import("shiki/langs/ini.mjs").then((module) => module.default),
- java: () => import("shiki/langs/java.mjs").then((module) => module.default),
- javascript: () => import("shiki/langs/javascript.mjs").then((module) => module.default),
- json: () => import("shiki/langs/json.mjs").then((module) => module.default),
- jsonl: () => import("shiki/langs/jsonl.mjs").then((module) => module.default),
- jsx: () => import("shiki/langs/jsx.mjs").then((module) => module.default),
- kotlin: () => import("shiki/langs/kotlin.mjs").then((module) => module.default),
- log: () => import("shiki/langs/log.mjs").then((module) => module.default),
- makefile: () => import("shiki/langs/makefile.mjs").then((module) => module.default),
- mdx: () => import("shiki/langs/mdx.mjs").then((module) => module.default),
- php: () => import("shiki/langs/php.mjs").then((module) => module.default),
- proto: () => import("shiki/langs/proto.mjs").then((module) => module.default),
- python: () => import("shiki/langs/python.mjs").then((module) => module.default),
- ruby: () => import("shiki/langs/ruby.mjs").then((module) => module.default),
- rust: () => import("shiki/langs/rust.mjs").then((module) => module.default),
- scss: () => import("shiki/langs/scss.mjs").then((module) => module.default),
- sql: () => import("shiki/langs/sql.mjs").then((module) => module.default),
- svelte: () => import("shiki/langs/svelte.mjs").then((module) => module.default),
- swift: () => import("shiki/langs/swift.mjs").then((module) => module.default),
- toml: () => import("shiki/langs/toml.mjs").then((module) => module.default),
- tsx: () => import("shiki/langs/tsx.mjs").then((module) => module.default),
- typescript: () => import("shiki/langs/typescript.mjs").then((module) => module.default),
- vue: () => import("shiki/langs/vue.mjs").then((module) => module.default),
- xml: () => import("shiki/langs/xml.mjs").then((module) => module.default),
- yaml: () => import("shiki/langs/yaml.mjs").then((module) => module.default),
+ bash: () => import("@shikijs/langs/bash").then((module) => module.default),
+ c: () => import("@shikijs/langs/c").then((module) => module.default),
+ cpp: () => import("@shikijs/langs/cpp").then((module) => module.default),
+ csharp: () => import("@shikijs/langs/csharp").then((module) => module.default),
+ css: () => import("@shikijs/langs/css").then((module) => module.default),
+ diff: () => import("@shikijs/langs/diff").then((module) => module.default),
+ dockerfile: () => import("@shikijs/langs/dockerfile").then((module) => module.default),
+ dotenv: () => import("@shikijs/langs/dotenv").then((module) => module.default),
+ go: () => import("@shikijs/langs/go").then((module) => module.default),
+ graphql: () => import("@shikijs/langs/graphql").then((module) => module.default),
+ hcl: () => import("@shikijs/langs/hcl").then((module) => module.default),
+ html: () => import("@shikijs/langs/html").then((module) => module.default),
+ ini: () => import("@shikijs/langs/ini").then((module) => module.default),
+ java: () => import("@shikijs/langs/java").then((module) => module.default),
+ javascript: () => import("@shikijs/langs/javascript").then((module) => module.default),
+ json: () => import("@shikijs/langs/json").then((module) => module.default),
+ jsonl: () => import("@shikijs/langs/jsonl").then((module) => module.default),
+ jsx: () => import("@shikijs/langs/jsx").then((module) => module.default),
+ kotlin: () => import("@shikijs/langs/kotlin").then((module) => module.default),
+ log: () => import("@shikijs/langs/log").then((module) => module.default),
+ makefile: () => import("@shikijs/langs/makefile").then((module) => module.default),
+ mdx: () => import("@shikijs/langs/mdx").then((module) => module.default),
+ php: () => import("@shikijs/langs/php").then((module) => module.default),
+ proto: () => import("@shikijs/langs/proto").then((module) => module.default),
+ python: () => import("@shikijs/langs/python").then((module) => module.default),
+ ruby: () => import("@shikijs/langs/ruby").then((module) => module.default),
+ rust: () => import("@shikijs/langs/rust").then((module) => module.default),
+ scss: () => import("@shikijs/langs/scss").then((module) => module.default),
+ sql: () => import("@shikijs/langs/sql").then((module) => module.default),
+ svelte: () => import("@shikijs/langs/svelte").then((module) => module.default),
+ swift: () => import("@shikijs/langs/swift").then((module) => module.default),
+ toml: () => import("@shikijs/langs/toml").then((module) => module.default),
+ tsx: () => import("@shikijs/langs/tsx").then((module) => module.default),
+ typescript: () => import("@shikijs/langs/typescript").then((module) => module.default),
+ vue: () => import("@shikijs/langs/vue").then((module) => module.default),
+ xml: () => import("@shikijs/langs/xml").then((module) => module.default),
+ yaml: () => import("@shikijs/langs/yaml").then((module) => module.default),
} satisfies Record<ShikiLanguage, () => Promise<LanguageRegistration[]>>
function lineCount(content: string): number {
@@ -97,11 +97,11 @@ function codeHighlightBudgetReason(content: string): string | null {
async function createRuntime(): Promise<Runtime> {
const [core, engine, wasm, lightTheme, darkTheme] = await Promise.all([
- import("shiki/core"),
- import("shiki/engine/oniguruma"),
- import("shiki/wasm"),
- import("shiki/themes/github-light.mjs").then((module) => module.default),
- import("shiki/themes/github-dark.mjs").then((module) => module.default),
+ import("@shikijs/core"),
+ import("@shikijs/engine-oniguruma"),
+ import("@shikijs/engine-oniguruma/wasm-inlined"),
+ import("@shikijs/themes/github-light").then((module) => module.default),
+ import("@shikijs/themes/github-dark").then((module) => module.default),
])
const highlighter = await core.createHighlighterCore({
engine: engine.createOnigurumaEngine(wasm.default),