-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(plugin-shiki): lazy load languages
- Loading branch information
1 parent
108c888
commit af8c80c
Showing
14 changed files
with
164 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { rollupBundle } from '../../../scripts/rollup.js' | ||
|
||
export default rollupBundle('node/index', { | ||
external: ['@shikijs/transformers', 'nanoid', 'shiki'], | ||
}) | ||
export default rollupBundle( | ||
{ base: 'node', files: ['index', 'resolveLang'] }, | ||
{ | ||
external: ['@shikijs/transformers', 'nanoid', 'shiki', 'synckit'], | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export type * from './options.js' | ||
export * from './shiki.js' | ||
export * from './shikiPlugin.js' | ||
export type * from './types.js' |
65 changes: 54 additions & 11 deletions
65
plugins/markdown/plugin-shiki/src/node/markdown/highlighter/createShikiHighlighter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,70 @@ | ||
import type { BundledLanguage, BundledTheme, HighlighterGeneric } from 'shiki' | ||
import { createHighlighter } from 'shiki' | ||
import { bundledLanguageNames } from '../../shiki.js' | ||
import { createRequire } from 'node:module' | ||
import type { | ||
BundledLanguage, | ||
BundledTheme, | ||
HighlighterGeneric, | ||
LanguageRegistration, | ||
} from 'shiki' | ||
import { createHighlighter, isSpecialLang } from 'shiki' | ||
import { createSyncFn } from 'synckit' | ||
import type { ShikiResolveLang } from '../../resolveLang.js' | ||
import type { ShikiHighlightOptions } from '../../types.js' | ||
|
||
const require = createRequire(import.meta.url) | ||
|
||
const resolveLangSync = createSyncFn<ShikiResolveLang>( | ||
require.resolve('@vuepress/plugin-shiki/resolveLang'), | ||
) | ||
|
||
export type ShikiLoadLang = (lang: LanguageRegistration | string) => boolean | ||
|
||
export const createShikiHighlighter = async ({ | ||
langs = bundledLanguageNames, | ||
langs = [], | ||
langAlias = {}, | ||
defaultLang, | ||
shikiSetup, | ||
...options | ||
}: ShikiHighlightOptions = {}): Promise< | ||
HighlighterGeneric<BundledLanguage, BundledTheme> | ||
> => { | ||
const shikiHighlighter = await createHighlighter({ | ||
langs, | ||
}: ShikiHighlightOptions = {}): Promise<{ | ||
highlighter: HighlighterGeneric<BundledLanguage, BundledTheme> | ||
loadLang: (lang: LanguageRegistration | string) => boolean | ||
}> => { | ||
const highlighter = await createHighlighter({ | ||
langs: [...langs, ...Object.values(langAlias)], | ||
langAlias, | ||
themes: | ||
'themes' in options | ||
? Object.values(options.themes) | ||
: [options.theme ?? 'nord'], | ||
}) | ||
|
||
await shikiSetup?.(shikiHighlighter) | ||
const loadLang = (langConfig: LanguageRegistration | string): boolean => { | ||
const lang = typeof langConfig === 'string' ? langConfig : langConfig.name | ||
|
||
if ( | ||
!isSpecialLang(lang) && | ||
!highlighter.getLoadedLanguages().includes(lang) | ||
) { | ||
const resolvedLang = resolveLangSync(lang) | ||
|
||
if (!resolvedLang.length) return false | ||
|
||
console.log('loading lang', lang) | ||
Check warning on line 51 in plugins/markdown/plugin-shiki/src/node/markdown/highlighter/createShikiHighlighter.ts GitHub Actions / build-check
Check warning on line 51 in plugins/markdown/plugin-shiki/src/node/markdown/highlighter/createShikiHighlighter.ts GitHub Actions / build-check
Check warning on line 51 in plugins/markdown/plugin-shiki/src/node/markdown/highlighter/createShikiHighlighter.ts GitHub Actions / bundle-check
|
||
|
||
highlighter.loadLanguageSync(resolvedLang) | ||
} | ||
return true | ||
} | ||
|
||
// patch for twoslash - https://github.com/vuejs/vitepress/issues/4334 | ||
const rawGetLanguage = highlighter.getLanguage | ||
|
||
highlighter.getLanguage = (name) => { | ||
loadLang(name) | ||
|
||
return rawGetLanguage.call(highlighter, name) | ||
} | ||
|
||
await shikiSetup?.(highlighter) | ||
|
||
return shikiHighlighter | ||
return { highlighter, loadLang } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
plugins/markdown/plugin-shiki/src/node/markdown/highlighter/getLanguage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { | ||
DynamicImportLanguageRegistration, | ||
LanguageRegistration, | ||
} from 'shiki' | ||
import { bundledLanguages } from 'shiki' | ||
import { runAsWorker } from 'synckit' | ||
|
||
async function resolveLang(lang: string): Promise<LanguageRegistration[]> { | ||
return ( | ||
( | ||
bundledLanguages as Record< | ||
string, | ||
DynamicImportLanguageRegistration | undefined | ||
> | ||
) | ||
[lang]?.() | ||
.then((m) => m.default) ?? [] | ||
) | ||
} | ||
|
||
runAsWorker(resolveLang) | ||
|
||
export type ShikiResolveLang = typeof resolveLang |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.