Skip to content

Commit 7433b8a

Browse files
authored
feat: introduce precompiled-grammars (#880)
1 parent d28d797 commit 7433b8a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1588
-594
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ cache
2121
.eslintcache
2222
report-engine-js-compat.json
2323
scripts/compares
24+
tm-grammars-themes

bench/bundle-test/bundle.bench.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* eslint-disable ts/ban-ts-comment */
2+
/* eslint-disable antfu/no-import-dist */
3+
4+
import { bench, describe } from 'vitest'
5+
// @ts-ignore - ignore type error
6+
import { highlight as highlightA } from './dist/index-lite.min.mjs'
7+
// @ts-ignore - ignore type error
8+
import { highlight as highlightB } from './dist/index-wasm.min.mjs'
9+
10+
const code = `
11+
import { ref } from 'vue'
12+
13+
const message = ref('Hello World!')
14+
15+
function reverseMessage() {
16+
// Access/mutate the value of a ref via
17+
// its .value property.
18+
message.value = message.value.split('').reverse().join('')
19+
}
20+
21+
function notify() {
22+
alert('navigation was prevented.')
23+
}
24+
`
25+
26+
describe('bundle', () => {
27+
bench('js-precompiled', async () => {
28+
await highlightA(code)
29+
})
30+
31+
bench('wasm', async () => {
32+
await highlightB(code)
33+
})
34+
})

bench/bundle-test/index-lite.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { codeToHtml, createShikiInternal } from '@shikijs/core'
2+
import { createJavaScriptRawEngine } from '@shikijs/engine-javascript/raw'
3+
4+
const shiki = createShikiInternal(
5+
{
6+
langs: [
7+
import('@shikijs/langs-precompiled/ts'),
8+
],
9+
themes: [
10+
import('@shikijs/themes/vitesse-dark'),
11+
],
12+
engine: createJavaScriptRawEngine(),
13+
},
14+
)
15+
16+
export async function highlight(code: string): Promise<string> {
17+
return codeToHtml(await shiki, code, { lang: 'ts', theme: 'vitesse-dark' })
18+
}

bench/bundle-test/index-wasm.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { codeToHtml, createShikiInternal } from '@shikijs/core'
2+
import { createWasmOnigEngine } from '@shikijs/engine-oniguruma'
3+
4+
const shiki = createShikiInternal(
5+
{
6+
langs: [
7+
import('@shikijs/langs/ts'),
8+
],
9+
themes: [
10+
import('@shikijs/themes/vitesse-dark'),
11+
],
12+
engine: createWasmOnigEngine(import('@shikijs/engine-oniguruma/wasm-inlined')),
13+
},
14+
)
15+
16+
export async function highlight(code: string): Promise<string> {
17+
return codeToHtml(await shiki, code, { lang: 'ts', theme: 'vitesse-dark' })
18+
}

bench/bundle-test/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"bench:prepare": "rollup -c && du -h dist/*"
5+
}
6+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import resolve from '@rollup/plugin-node-resolve'
2+
import esbuild from 'rollup-plugin-esbuild'
3+
4+
const plugins = [
5+
resolve(),
6+
esbuild({
7+
minify: true,
8+
target: 'esnext',
9+
}),
10+
]
11+
12+
export default [
13+
{
14+
input: 'index-wasm.ts',
15+
output: {
16+
file: 'dist/index-wasm.min.mjs',
17+
format: 'es',
18+
inlineDynamicImports: true,
19+
},
20+
plugins,
21+
},
22+
{
23+
input: 'index-lite.ts',
24+
output: {
25+
file: 'dist/index-lite.min.mjs',
26+
format: 'es',
27+
inlineDynamicImports: true,
28+
},
29+
plugins,
30+
},
31+
]

bench/engines.bench.ts renamed to bench/engines/engines.bench.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
/* eslint-disable no-console */
22
import type { BundledLanguage } from 'shiki'
3-
import type { ReportItem } from '../scripts/report-engine-js-compat'
3+
import type { ReportItem } from '../../scripts/report-engine-js-compat'
44
import fs from 'node:fs/promises'
5-
import { createHighlighter, createJavaScriptRegexEngine, createOnigurumaEngine } from 'shiki'
5+
import { createJavaScriptRawEngine, createJavaScriptRegexEngine } from '@shikijs/engine-javascript'
6+
import { createHighlighter, createOnigurumaEngine } from 'shiki'
67
import { bench, describe } from 'vitest'
78

89
const js = createJavaScriptRegexEngine()
10+
const jsRaw = createJavaScriptRawEngine()
911
const wasm = await createOnigurumaEngine(() => import('shiki/wasm'))
1012

1113
const RANGE = [0, 20]
1214

1315
// Run `npx jiti scripts/report-engine-js-compat.ts` to generate the report first
14-
const report = await fs.readFile(new URL('../scripts/report-engine-js-compat.json', import.meta.url), 'utf-8').then(JSON.parse) as ReportItem[]
16+
const report = await fs.readFile(new URL('../../scripts/report-engine-js-compat.json', import.meta.url), 'utf-8').then(JSON.parse) as ReportItem[]
1517
const langs = report.filter(i => i.highlightMatch === true).map(i => i.lang).slice(...RANGE) as BundledLanguage[]
1618
// Clone https://github.com/shikijs/textmate-grammars-themes to `../tm-grammars-themes`
17-
const samples = await Promise.all(langs.map(lang => fs.readFile(`../tm-grammars-themes/samples/${lang}.sample`, 'utf-8')))
19+
const samples = await Promise.all(langs.map(lang => fs.readFile(new URL(`../../tm-grammars-themes/samples/${lang}.sample`, import.meta.url), 'utf-8')))
1820

1921
console.log('Benchmarking engines with', langs.length, 'languages')
2022

@@ -30,14 +32,26 @@ const shikiWasm = await createHighlighter({
3032
engine: wasm,
3133
})
3234

35+
const shikiJsPrecompiled = await createHighlighter({
36+
langs: await Promise.all(langs.map(lang => import(`@shikijs/langs-precompiled/${lang}`))),
37+
themes: ['vitesse-dark'],
38+
engine: jsRaw,
39+
})
40+
3341
for (const lang of langs) {
3442
describe(lang, () => {
43+
const code = samples[langs.indexOf(lang)]
44+
3545
bench('js', () => {
36-
shikiJs.codeToTokensBase(samples[langs.indexOf(lang)], { lang, theme: 'vitesse-dark' })
46+
shikiJs.codeToTokensBase(code, { lang, theme: 'vitesse-dark' })
47+
})
48+
49+
bench('js-precompiled', () => {
50+
shikiJsPrecompiled.codeToTokensBase(code, { lang, theme: 'vitesse-dark' })
3751
})
3852

3953
bench('wasm', () => {
40-
shikiWasm.codeToTokensBase(samples[langs.indexOf(lang)], { lang, theme: 'vitesse-dark' })
54+
shikiWasm.codeToTokensBase(code, { lang, theme: 'vitesse-dark' })
4155
})
4256
})
4357
}

docs/package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@
1212
},
1313
"devDependencies": {
1414
"@iconify-json/svg-spinners": "catalog:",
15-
"@shikijs/colorized-brackets": "workspace:*",
16-
"@shikijs/transformers": "workspace:*",
17-
"@shikijs/twoslash": "workspace:*",
1815
"@unocss/reset": "catalog:",
1916
"@vueuse/core": "catalog:",
2017
"floating-vue": "catalog:",
2118
"pinia": "catalog:",
22-
"shiki": "workspace:*",
2319
"unocss": "catalog:",
2420
"unplugin-vue-components": "catalog:",
2521
"vitepress": "catalog:",

docs/packages/vitepress.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ export default defineConfig({
3939
And then in your [`.vitepress/theme/index.ts`](https://vitepress.dev/guide/custom-theme), install the Vue plugin and import the css with `@shikijs/vitepress-twoslash/styles.css`.
4040

4141
```ts twoslash
42-
import type { EnhanceAppContext } from 'vitepress' // [!code hl]
4342
// @noErrors: true
4443
// .vitepress/theme/index.ts
44+
import type { EnhanceAppContext } from 'vitepress' // [!code hl]
4545
import TwoslashFloatingVue from '@shikijs/vitepress-twoslash/client'
4646
import Theme from 'vitepress/theme'
4747

@@ -86,7 +86,7 @@ console.log('hello')
8686
// ^?
8787
```
8888

89-
<br> <!-- leaving some space for the query above -->
89+
<div class="h-100" /> <!-- leaving some space for the query above -->
9090

9191
### Vue Single File Component
9292

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"docs": "pnpm -C docs run docs:dev",
1414
"docs:build": "pnpm -C docs run docs:build",
1515
"report-engine-js": "esno scripts/report-engine-js-compat.ts",
16-
"bench": "vitest bench",
16+
"bench": "pnpm -r run bench:prepare && vitest bench",
1717
"prepare": "simple-git-hooks"
1818
},
1919
"devDependencies": {
@@ -26,12 +26,15 @@
2626
"@rollup/plugin-node-resolve": "catalog:",
2727
"@rollup/plugin-replace": "catalog:",
2828
"@rollup/plugin-terser": "catalog:",
29+
"@shikijs/colorized-brackets": "workspace:*",
2930
"@shikijs/engine-javascript": "workspace:*",
3031
"@shikijs/engine-oniguruma": "workspace:*",
3132
"@shikijs/markdown-it": "workspace:*",
3233
"@shikijs/monaco": "workspace:*",
3334
"@shikijs/rehype": "workspace:*",
3435
"@shikijs/transformers": "workspace:*",
36+
"@shikijs/twoslash": "workspace:*",
37+
"@shikijs/types": "workspace:*",
3538
"@shikijs/vitepress-twoslash": "workspace:*",
3639
"@types/fs-extra": "catalog:",
3740
"@types/hast": "catalog:",
@@ -62,7 +65,6 @@
6265
"rollup-plugin-copy": "catalog:",
6366
"rollup-plugin-dts": "catalog:",
6467
"rollup-plugin-esbuild": "catalog:",
65-
"rollup-plugin-typescript2": "catalog:",
6668
"shiki": "workspace:*",
6769
"simple-git-hooks": "catalog:",
6870
"taze": "catalog:",
@@ -76,6 +78,7 @@
7678
"wrangler": "catalog:"
7779
},
7880
"resolutions": {
81+
"@shikijs/colorized-brackets": "workspace:*",
7982
"@shikijs/compat": "workspace:*",
8083
"@shikijs/core": "workspace:*",
8184
"@shikijs/engine-javascript": "workspace:*",
@@ -84,6 +87,7 @@
8487
"@shikijs/rehype": "workspace:*",
8588
"@shikijs/transformers": "workspace:*",
8689
"@shikijs/twoslash": "workspace:*",
90+
"@shikijs/types": "workspace:*",
8791
"@shikijs/vitepress-twoslash": "workspace:*",
8892
"@types/hast": "catalog:",
8993
"@types/mdast": "catalog:",

0 commit comments

Comments
 (0)