-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(valaxy): on demand load shiki langs & only build esm
- Loading branch information
Showing
29 changed files
with
564 additions
and
458 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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# 优化笔记 | ||
|
||
记录一下开发过程中的问题优化。 |
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,91 @@ | ||
# Shiki 高亮耗时问题 | ||
|
||
Valaxy 使用 Shiki 实现代码高亮。 | ||
|
||
使用 `Object.keys(bundledLanguages)` 加载全部语言时将使得冷启动时间更久(在 M1 Pro 下增加了约 3s)。 | ||
|
||
此时应当使用按需加载配置。 | ||
|
||
> [Bundles](https://shiki.style/guide/bundles) | ||
```ts {18} | ||
const highlighter = await createHighlighter({ | ||
themes: | ||
typeof theme === 'object' && 'light' in theme && 'dark' in theme | ||
? [theme.light, theme.dark] | ||
: [theme], | ||
langs: [ | ||
// load long time, about 3s | ||
// ...Object.keys(bundledLanguages), | ||
...(options.languages || []), | ||
...Object.values(options.languageAlias || {}), | ||
], | ||
langAlias: options.languageAlias, | ||
}) | ||
|
||
// ref vitepress | ||
// 使用 worker 按需加载语言 | ||
const resolveLangSync = createSyncFn<ShikiResolveLang>( | ||
require.resolve('valaxy/dist/node/worker_shikiResolveLang.js'), | ||
) | ||
|
||
function loadLanguage(name: string | LanguageRegistration) { | ||
const lang = typeof name === 'string' ? name : name.name | ||
if ( | ||
!isSpecialLang(lang) | ||
&& !highlighter.getLoadedLanguages().includes(lang) | ||
) { | ||
const resolvedLang = resolveLangSync(lang) | ||
if (resolvedLang.length) | ||
highlighter.loadLanguageSync(resolvedLang) | ||
else return false | ||
} | ||
return true | ||
} | ||
|
||
const internal = highlighter.getInternalContext() | ||
const getLanguage = internal.getLanguage | ||
internal.getLanguage = (name) => { | ||
loadLanguage(name) | ||
return getLanguage.call(internal, name) | ||
} | ||
``` | ||
|
||
预先打包 `worker_shikiResolveLang.ts` 为 JS 以便调用。 | ||
|
||
```ts [worker_shikiResolveLang.ts] | ||
import { | ||
bundledLanguages, | ||
type DynamicImportLanguageRegistration, | ||
type LanguageRegistration, | ||
} from 'shiki' | ||
import { runAsWorker } from 'synckit' | ||
|
||
async function resolveLang(lang: string) { | ||
return ( | ||
( | ||
bundledLanguages as Record< | ||
string, | ||
DynamicImportLanguageRegistration | undefined | ||
> | ||
)[lang]?.() | ||
.then(m => m.default) || ([] as LanguageRegistration[]) | ||
) | ||
} | ||
|
||
runAsWorker(resolveLang) | ||
|
||
export type ShikiResolveLang = typeof resolveLang | ||
``` | ||
```ts [markdown/plugins/highlight.ts] | ||
// 加载对应语言,若无则 fallback to defaultLang 'txt' | ||
if (!loadLanguage(lang)) { | ||
logger.warn( | ||
c.yellow( | ||
`\nThe language '${lang}' is not loaded, falling back to '${defaultLang}' for syntax highlighting.`, | ||
), | ||
) | ||
lang = defaultLang | ||
} | ||
``` |
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,9 @@ | ||
import { startValaxyDev } from '../../../packages/valaxy/node' | ||
|
||
function main() { | ||
startValaxyDev({ | ||
port: 4860, | ||
}) | ||
} | ||
|
||
main() |
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 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
Oops, something went wrong.
b06b300
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://yun.valaxy.site as production
🚀 Deployed on https://675d82c404a2ba0ef1327d58--valaxy.netlify.app