From bd3be456b65cdd3e1e120bd0a081abbb6d65e57e Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Thu, 12 Sep 2024 13:21:20 +0200 Subject: [PATCH] fix(shiki): dynamic import highlighter packages (#253) --- src/runtime/highlighter/shiki.ts | 42 ++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/runtime/highlighter/shiki.ts b/src/runtime/highlighter/shiki.ts index 60c433a5..46099277 100644 --- a/src/runtime/highlighter/shiki.ts +++ b/src/runtime/highlighter/shiki.ts @@ -1,13 +1,6 @@ import type { CodeToHastOptions } from 'shiki/core' -import { createHighlighterCore, addClassToHast, isSpecialLang, isSpecialTheme } from 'shiki/core' import type { HighlighterCore, LanguageInput, ShikiTransformer, ThemeInput } from 'shiki' import type { Element } from 'hast' -import { - transformerNotationDiff, - transformerNotationErrorLevel, - transformerNotationFocus, - transformerNotationHighlight -} from '@shikijs/transformers' import type { MdcConfig, Highlighter } from '@nuxtjs/mdc' export interface CreateShikiHighlighterOptions { @@ -33,11 +26,14 @@ export function createShikiHighlighter({ getMdcConfigs, options: shikiOptions }: CreateShikiHighlighterOptions = {}): Highlighter { - let shiki: Promise | undefined + let shiki: ReturnType | undefined let configs: Promise | undefined async function _getShiki() { - const shiki = await createHighlighterCore({ + const { createHighlighterCore, addClassToHast, isSpecialLang, isSpecialTheme } = await import('shiki/core') + const { transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight } = await import('@shikijs/transformers') + + const shiki: HighlighterCore = await createHighlighterCore({ langs, themes, loadWasm: () => import('shiki/wasm') @@ -47,7 +43,18 @@ export function createShikiHighlighter({ await config.shiki?.setup?.(shiki) } - return shiki + return { + shiki, + addClassToHast, + isSpecialLang, + isSpecialTheme, + transformers: [ + transformerNotationDiff(), + transformerNotationErrorLevel(), + transformerNotationFocus(), + transformerNotationHighlight() + ] as ShikiTransformer[] + } } async function getShiki() { @@ -64,15 +71,14 @@ export function createShikiHighlighter({ return configs } - const baseTransformers: ShikiTransformer[] = [ - transformerNotationDiff(), - transformerNotationFocus(), - transformerNotationHighlight(), - transformerNotationErrorLevel() - ] - const highlighter: Highlighter = async (code, lang, theme, options = {}) => { - const shiki = await getShiki() + const { + shiki, + addClassToHast, + isSpecialLang, + isSpecialTheme, + transformers: baseTransformers + } = await getShiki() const codeToHastOptions: Partial> = { defaultColor: false,