Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(devtools): optimize shiki bundle size #321

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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