Skip to content

Commit 4a7aeea

Browse files
authoredFeb 19, 2025··
perf: only bundle needed shiki langs and themes (#192)
Signed-off-by: ZTL-UwU <zhangtianli2006@163.com>
1 parent 747c569 commit 4a7aeea

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed
 

Diff for: ‎client/components/OCodeBlock.vue

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<script setup lang="ts">
2-
import type { BundledLanguage } from 'shiki'
32
import { computed } from 'vue'
43
import { renderCodeHighlight } from '../composables/shiki'
54
65
const props = withDefaults(
76
defineProps<{
87
code: string
9-
lang?: BundledLanguage | 'robots-txt'
8+
lang: 'json' | 'html' | 'bash' | 'robots-txt'
109
lines?: boolean
1110
transformRendered?: (code: string) => string
1211
}>(),

Diff for: ‎client/composables/shiki.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import type { MaybeRef } from '@vueuse/core'
2-
import type { BundledLanguage, Highlighter } from 'shiki'
3-
import { createHighlighter } from 'shiki'
2+
import type { HighlighterCore } from 'shiki'
3+
import { createHighlighterCore } from 'shiki/core'
4+
import { createJavaScriptRegexEngine } from 'shiki/engine/javascript'
45
import { computed, ref, toValue } from 'vue'
56
import { devtools } from './rpc'
67

7-
export const shiki = ref<Highlighter>()
8+
export const shiki = ref<HighlighterCore>()
89

910
export async function loadShiki() {
1011
// Only loading when needed
11-
shiki.value = await createHighlighter({
12+
shiki.value = await createHighlighterCore({
1213
themes: [
13-
'vitesse-dark',
14-
'vitesse-light',
14+
import('@shikijs/themes/vitesse-light'),
15+
import('@shikijs/themes/vitesse-dark'),
1516
],
1617
langs: [
17-
'html',
18-
'json',
19-
'bash',
18+
import('@shikijs/langs/json'),
19+
import('@shikijs/langs/html'),
20+
import('@shikijs/langs/bash'),
2021
Object.freeze({
2122
displayName: 'robots.txt',
2223
name: 'robots-txt',
@@ -70,11 +71,12 @@ export async function loadShiki() {
7071
scopeName: 'text.robots-txt',
7172
}),
7273
],
74+
engine: createJavaScriptRegexEngine(),
7375
})
7476
return shiki.value
7577
}
7678

77-
export function renderCodeHighlight(code: MaybeRef<string>, lang?: BundledLanguage | 'robots-txt') {
79+
export function renderCodeHighlight(code: MaybeRef<string>, lang: 'json' | 'html' | 'bash' | 'robots-txt') {
7880
return computed(() => {
7981
const colorMode = devtools.value?.colorMode || 'light'
8082
return shiki.value!.codeToHtml(toValue(code), {

0 commit comments

Comments
 (0)
Please sign in to comment.