Skip to content

Commit 7124990

Browse files
authored
feat: support unwasm condition for core (#97)
1 parent c1e9cc9 commit 7124990

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

packages/shikiji-core/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export * from './utils'
44
export * from './types'
55

66
export { loadWasm } from './oniguruma'
7-
export { getShikiInternal } from './internal'
7+
export { getShikiInternal, setDefaultWasmLoader } from './internal'
88
export { codeToThemedTokens } from './tokenizer'
99
export { tokenizeAnsiWithTheme } from './tokenizer-ansi'
1010
export { codeToHast } from './renderer-hast'

packages/shikiji-core/src/internal.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import type { HighlighterCoreOptions, LanguageInput, MaybeGetter, ShikiInternal, ThemeInput, ThemeRegistrationResolved } from './types'
2+
import type { LoadWasmOptions } from './oniguruma'
23
import { createOnigScanner, createOnigString, loadWasm } from './oniguruma'
34
import { Registry } from './registry'
45
import { Resolver } from './resolver'
56
import { normalizeTheme } from './normalize'
67

8+
let _defaultWasmLoader: LoadWasmOptions | undefined
9+
/**
10+
* Set the default wasm loader for `loadWasm`.
11+
* @internal
12+
*/
13+
export function setDefaultWasmLoader(_loader: LoadWasmOptions) {
14+
_defaultWasmLoader = _loader
15+
}
16+
717
/**
818
* Get the minimal shiki context for rendering.
919
*/
@@ -18,13 +28,15 @@ export async function getShikiInternal(options: HighlighterCoreOptions = {}): Pr
1828
)).flat()))
1929
}
2030

31+
const wasmLoader = options.loadWasm || _defaultWasmLoader
32+
2133
const [
2234
themes,
2335
langs,
2436
] = await Promise.all([
2537
Promise.all((options.themes || []).map(normalizeGetter)).then(r => r.map(normalizeTheme)),
2638
resolveLangs(options.langs || []),
27-
options.loadWasm ? loadWasm(options.loadWasm) : undefined,
39+
wasmLoader ? loadWasm(wasmLoader) : undefined,
2840
] as const)
2941

3042
const resolver = new Resolver(

packages/shikiji/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"./core": {
2525
"types": "./dist/core.d.mts",
26+
"unwasm": "./dist/core-unwasm.mjs",
2627
"default": "./dist/core.mjs"
2728
},
2829
"./wasm": {

packages/shikiji/rollup.config.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import fg from 'fast-glob'
1313
const entries = [
1414
'src/index.ts',
1515
'src/core.ts',
16+
'src/core-unwasm.ts',
1617
'src/types.ts',
1718
'src/themes.ts',
1819
'src/langs.ts',

packages/shikiji/src/core-unwasm.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* In environments where WebAssembly can be treated as native ESM and https://github.com/unjs/unwasm,
3+
* We add the wasm file as the dependency so users don't need to call `loadWasm` manually.
4+
*/
5+
6+
import { setDefaultWasmLoader } from 'shikiji-core'
7+
8+
setDefaultWasmLoader(() => import('shikiji/wasm'))
9+
10+
export * from 'shikiji-core'

0 commit comments

Comments
 (0)