Closed Innei closed 2 months ago
Yes because highlighting is sync (required in many integrations), while the loading is async, so it has to be done ahead of time.
If your integration are acceptable for async context, you could do loading yourself
if (!shiki.getLanguages().includes('typescript'))
await shiki.loadLanguage(import('shiki/langs/typescript.mjs'))
const html = shiki.codeToHtml(code, { lang: 'ts' })
Thank you for your response. I am using this method now and he is working fine.
use(
useMemo(async () => {
async function loadShikiLanguage(language: string, languageModule: any) {
const shiki = codeHighlighter?.codeHighlighter
if (!shiki) return
if (!shiki.getLoadedLanguages().includes(language)) {
await shiki.loadLanguage(await languageModule())
}
}
const { bundledLanguages } = await import('shiki/langs')
if (!language) return
const importFn = (bundledLanguages as any)[language]
if (!importFn) return
return loadShikiLanguage(language || '', importFn)
}, [codeHighlighter?.codeHighlighter, language]),
)
Validations
Describe the bug
Description
I am using Shiki's code highlighting library to implement code splitting and lazy loading for language lexers and themes. However, despite specifying only one language in the configuration, Shiki is loading all the configured language lexers, leading to unnecessary loading of resources.
Steps to Reproduce
Set up the
getHighlighterCore
with multiple languages configured as lazy-loadable modules. Use codeHighlighter function specifying only one language (e.g., 'typescript') to highlight. Observe that all configured language modules are loaded, not just the one required.For the code above, I registered many languages, but I only used one. But it will download all the js files
Expected Behavior
Only the lexer for the specified language should be loaded. For example, if 'typescript' is specified, only the Typescript lexer should be dynamically imported and used.
Or I hope shiki can add automatic language recognition and download lexical js files like prism.
https://github.com/PrismJS/prism/issues/1313#issuecomment-535526127
Actual Behavior
All lexers specified in the getHighlighterCore setup are being loaded regardless of the language specified in codeHighlighter.
Reproduction
https://github.com/Innei/shiki-bundle-and-import-repro
Contributes