Skip to content

Commit

Permalink
perf(devtools): optimize shiki bundle size (#321)
Browse files Browse the repository at this point in the history
* perf: only bundle needed shiki langs and themes

Signed-off-by: ZTL-UwU <[email protected]>

* perf: use xml for svg highlighting

Signed-off-by: ZTL-UwU <[email protected]>

---------

Signed-off-by: ZTL-UwU <[email protected]>
  • Loading branch information
ZTL-UwU authored Feb 18, 2025
1 parent 880167f commit 67427ff
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion client/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ const currentPageFile = computed(() => {
SVG
</h3>
</template>
<OCodeBlock :code="debug?.svg.replaceAll('>', '>\n')" lang="html" />
<OCodeBlock :code="debug?.svg.replaceAll('>', '>\n')" lang="xml" />
</OSectionBlock>
<OSectionBlock>
<template #text>
Expand Down
3 changes: 1 addition & 2 deletions client/components/OCodeBlock.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<script setup lang="ts">
import type { BundledLanguage } from 'shiki'
import { computed } from 'vue'
import { renderCodeHighlight } from '../composables/shiki'
const props = withDefaults(
defineProps<{
code: string
lang?: BundledLanguage
lang: 'json' | 'xml'
lines?: boolean
transformRendered?: (code: string) => string
}>(),
Expand Down
22 changes: 12 additions & 10 deletions client/composables/shiki.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import type { MaybeRef } from '@vueuse/core'
import type { BundledLanguage, Highlighter } from 'shiki'
import { createHighlighter } from 'shiki'
import type { HighlighterCore } from 'shiki'
import { createHighlighterCore } from 'shiki/core'
import { createJavaScriptRegexEngine } from 'shiki/engine/javascript'
import { computed, ref, toValue } from 'vue'
import { devtools } from './rpc'

export const shiki = ref<Highlighter>()
export const shiki = ref<HighlighterCore>()

export async function loadShiki() {
// Only loading when needed
shiki.value = await createHighlighter({
shiki.value = await createHighlighterCore({
themes: [
'vitesse-dark',
'vitesse-light',
import('@shikijs/themes/vitesse-light'),
import('@shikijs/themes/vitesse-dark'),
],
langs: [
'html',
'json',
import('@shikijs/langs/xml'),
import('@shikijs/langs/json'),
],
engine: createJavaScriptRegexEngine(),
})

return shiki.value
}

export function renderCodeHighlight(code: MaybeRef<string>, lang: BundledLanguage) {
export function renderCodeHighlight(code: MaybeRef<string>, lang: 'json' | 'xml') {
return computed(() => {
const colorMode = devtools.value?.colorMode || 'light'
return shiki.value!.codeToHtml(toValue(code) || '', {
Expand Down
13 changes: 11 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
packages:
- .playground
- playground
- client

0 comments on commit 67427ff

Please sign in to comment.