Skip to content

Commit 56d1a48

Browse files
authoredMar 15, 2024··
perf: bundle wasm binary in chunk for shiki/wasm and @shikijs/core/wasm-inlined (#624)
1 parent 44a5d76 commit 56d1a48

14 files changed

+38
-30
lines changed
 

‎docs/packages/markdown-it.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ By default, the full bundle of `shiki` will be imported. If you are using a [fin
3535
import MarkdownIt from 'markdown-it'
3636
import { fromHighlighter } from '@shikijs/markdown-it/core'
3737
import { getHighlighterCore } from 'shiki/core'
38-
import getWasm from 'shiki/wasm'
3938

4039
const highlighter = await getHighlighterCore({
4140
themes: [
@@ -44,7 +43,7 @@ const highlighter = await getHighlighterCore({
4443
langs: [
4544
import('shiki/langs/javascript.mjs'),
4645
],
47-
loadWasm: getWasm
46+
loadWasm: import('shiki/wasm')
4847
})
4948

5049
const md = MarkdownIt()

‎docs/packages/rehype.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import rehypeStringify from 'rehype-stringify'
5151
import rehypeShikiFromHighlighter from '@shikijs/rehype/core'
5252

5353
import { getHighlighterCore } from 'shiki/core'
54-
import getWasm from 'shiki/wasm'
5554

5655
const highlighter = await getHighlighterCore({
5756
themes: [
@@ -60,7 +59,7 @@ const highlighter = await getHighlighterCore({
6059
langs: [
6160
import('shiki/langs/javascript.mjs'),
6261
],
63-
loadWasm: getWasm
62+
loadWasm: import('shiki/wasm')
6463
})
6564

6665
const raw = await fs.readFile('./input.md')

‎packages/core/rollup.config.mjs

+1-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ export default defineConfig([
4040
dir: 'dist',
4141
format: 'esm',
4242
entryFileNames: '[name].mjs',
43-
chunkFileNames: (f) => {
44-
if (f.name === 'onig')
45-
return 'onig.mjs'
43+
chunkFileNames: () => {
4644
return 'chunks-[name].mjs'
4745
},
4846
},

‎packages/core/src/types/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ export * from './tokens'
77
export * from './transformers'
88
export * from './utils'
99
export * from './decorations'
10+
11+
export { WebAssemblyInstantiator } from '../oniguruma'

‎packages/core/src/wasm-inlined.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// @ts-expect-error this will be compiled to ArrayBuffer
2+
import binary from 'vscode-oniguruma/release/onig.wasm'
13
import type { WebAssemblyInstantiator } from './oniguruma'
24

3-
const getWasm: WebAssemblyInstantiator = async (info) => {
4-
// @ts-expect-error this will be compiled to ArrayBuffer
5-
const binray: ArrayBuffer = await import('vscode-oniguruma/release/onig.wasm').then(m => m.default)
6-
return WebAssembly.instantiate(binray, info).then(wasm => wasm.instance.exports)
5+
export const wasmBinary = binary as ArrayBuffer
6+
7+
export const getWasmInstance: WebAssemblyInstantiator = async (info) => {
8+
return WebAssembly.instantiate(wasmBinary, info).then(wasm => wasm.instance.exports)
79
}
810

9-
export default getWasm
11+
export default getWasmInstance

‎packages/shiki/src/bundle-full.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import type { HighlighterGeneric } from '@shikijs/core'
2-
import getWasm from 'shiki/wasm'
32
import { createSingletonShorthands, createdBundledHighlighter } from './core'
43
import type { BundledLanguage } from './assets/langs-bundle-full'
54
import type { BundledTheme } from './themes'
65
import { bundledLanguages } from './assets/langs-bundle-full'
76
import { bundledThemes } from './themes'
7+
import { getWasmInlined } from './wasm-dynamic'
88

99
export * from './core'
1010
export * from './themes'
1111
export * from './assets/langs-bundle-full'
12-
export { default as getWasmInlined } from 'shiki/wasm'
12+
13+
export { getWasmInlined }
1314

1415
export type Highlighter = HighlighterGeneric<BundledLanguage, BundledTheme>
1516

@@ -29,7 +30,7 @@ export const getHighlighter = /* @__PURE__ */ createdBundledHighlighter<
2930
>(
3031
bundledLanguages,
3132
bundledThemes,
32-
getWasm,
33+
getWasmInlined,
3334
)
3435

3536
export const {

‎packages/shiki/src/bundle-web.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import type { HighlighterGeneric } from '@shikijs/core'
2-
import getWasm from 'shiki/wasm'
32
import { createSingletonShorthands, createdBundledHighlighter } from './core'
43
import type { BundledLanguage } from './assets/langs-bundle-web'
54
import type { BundledTheme } from './themes'
65
import { bundledLanguages } from './assets/langs-bundle-web'
76
import { bundledThemes } from './themes'
7+
import { getWasmInlined } from './wasm-dynamic'
88

99
export * from './core'
1010
export * from './themes'
1111
export * from './assets/langs-bundle-web'
12-
export { default as getWasmInlined } from 'shiki/wasm'
12+
13+
export { getWasmInlined }
1314

1415
export type Highlighter = HighlighterGeneric<BundledLanguage, BundledTheme>
1516

@@ -29,7 +30,7 @@ export const getHighlighter = /* @__PURE__ */ createdBundledHighlighter<
2930
>(
3031
bundledLanguages,
3132
bundledThemes,
32-
getWasm,
33+
getWasmInlined,
3334
)
3435

3536
export const {

‎packages/shiki/src/wasm-dynamic.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { WebAssemblyInstantiator } from './types'
2+
3+
export const getWasmInlined: WebAssemblyInstantiator = async (info) => {
4+
return import('shiki/wasm').then(wasm => wasm.default(info))
5+
}

‎packages/shiki/test/core.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import nord from '../src/assets/themes/nord'
77
import mtp from '../src/assets/themes/material-theme-palenight'
88

99
// eslint-disable-next-line antfu/no-import-dist
10-
import onig from '../../core/dist/onig.mjs'
10+
import { wasmBinary } from '../../core/dist/wasm-inlined.mjs'
1111

1212
describe('should', () => {
1313
it('works', async () => {
1414
const shiki = await getHighlighterCore({
1515
themes: [nord],
1616
langs: [js as any],
1717
loadWasm: {
18-
instantiator: obj => WebAssembly.instantiate(onig, obj),
18+
instantiator: obj => WebAssembly.instantiate(wasmBinary, obj),
1919
},
2020
})
2121

@@ -32,7 +32,7 @@ describe('should', () => {
3232
],
3333
loadWasm: {
3434
// https://github.com/WebAssembly/esm-integration/tree/main/proposals/esm-integration
35-
instantiator: obj => WebAssembly.instantiate(onig, obj).then(r => r.instance.exports),
35+
instantiator: obj => WebAssembly.instantiate(wasmBinary, obj).then(r => r.instance.exports),
3636
},
3737
})
3838

@@ -151,7 +151,7 @@ describe('errors', () => {
151151
themes: [nord],
152152
langs: [js as any],
153153
loadWasm: {
154-
instantiator: obj => WebAssembly.instantiate(onig, obj),
154+
instantiator: obj => WebAssembly.instantiate(wasmBinary, obj),
155155
},
156156
})
157157

‎packages/shiki/test/wasm1.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import js from '../src/assets/langs/javascript'
55
import nord from '../src/assets/themes/nord'
66

77
// eslint-disable-next-line antfu/no-import-dist
8-
import onig from '../../core/dist/onig.mjs'
8+
import { wasmBinary } from '../../core/dist/wasm-inlined.mjs'
99

1010
it('wasm', async () => {
1111
const shiki = await getHighlighterCore({
1212
themes: [nord],
1313
langs: [js as any],
1414
loadWasm: {
15-
instantiator: obj => WebAssembly.instantiate(onig, obj),
15+
instantiator: obj => WebAssembly.instantiate(wasmBinary, obj),
1616
},
1717
})
1818

‎packages/shiki/test/wasm2.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import js from '../src/assets/langs/javascript'
55
import nord from '../src/assets/themes/nord'
66

77
// eslint-disable-next-line antfu/no-import-dist
8-
import onig from '../../core/dist/onig.mjs'
8+
import { wasmBinary } from '../../core/dist/wasm-inlined.mjs'
99

1010
it('wasm', async () => {
1111
const shiki = await getHighlighterCore({
1212
themes: [nord],
1313
langs: [js as any],
1414
loadWasm: {
15-
default: obj => WebAssembly.instantiate(onig, obj).then(r => r.instance.exports),
15+
default: obj => WebAssembly.instantiate(wasmBinary, obj).then(r => r.instance.exports),
1616
},
1717
})
1818

‎packages/shiki/test/wasm3.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import js from '../src/assets/langs/javascript'
55
import nord from '../src/assets/themes/nord'
66

77
// eslint-disable-next-line antfu/no-import-dist
8-
import onig from '../../core/dist/onig.mjs'
8+
import { wasmBinary } from '../../core/dist/wasm-inlined.mjs'
99

1010
it('wasm', async () => {
1111
const shiki = await getHighlighterCore({
1212
themes: [nord],
1313
langs: [js as any],
14-
loadWasm: obj => WebAssembly.instantiate(onig, obj).then(r => r.instance),
14+
loadWasm: obj => WebAssembly.instantiate(wasmBinary, obj).then(r => r.instance),
1515
})
1616

1717
expect(shiki.codeToHtml('1 + 1', { lang: 'javascript', theme: 'nord' }))

‎packages/shiki/test/wasm4.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import js from '../src/assets/langs/javascript'
55
import nord from '../src/assets/themes/nord'
66

77
// eslint-disable-next-line antfu/no-import-dist
8-
import onig from '../../core/dist/onig.mjs'
8+
import { wasmBinary } from '../../core/dist/wasm-inlined.mjs'
99

1010
it('wasm', async () => {
1111
const shiki = await getHighlighterCore({
1212
themes: [nord],
1313
langs: [js as any],
14-
loadWasm: Promise.resolve().then(() => obj => WebAssembly.instantiate(onig, obj).then(r => r.instance)),
14+
loadWasm: Promise.resolve().then(() => obj => WebAssembly.instantiate(wasmBinary, obj).then(r => r.instance)),
1515
})
1616

1717
expect(shiki.codeToHtml('1 + 1', { lang: 'javascript', theme: 'nord' }))

‎tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"allowJs": true,
3030
"strict": true,
3131
"strictNullChecks": true,
32+
"noEmit": true,
3233
"preserveValueImports": false,
3334
"esModuleInterop": true,
3435
"skipDefaultLibCheck": true,

0 commit comments

Comments
 (0)
Please sign in to comment.