Skip to content

Commit cd23932

Browse files
authored
fix(core): simplify cloudflare wasm loading (#670)
1 parent c8ea942 commit cd23932

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

packages/core/src/oniguruma/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,14 @@ export function loadWasm(options: LoadWasmOptions): Promise<void> {
426426
else if (isArrayBuffer(instance)) {
427427
instance = await _makeArrayBufferLoader(instance)(info)
428428
}
429+
// import("shiki/onig.wasm") returns `{ default: WebAssembly.Module }` on cloudflare workers
430+
// https://developers.cloudflare.com/workers/wrangler/bundling/
431+
else if (instance instanceof WebAssembly.Module) {
432+
instance = await _makeArrayBufferLoader(instance)(info)
433+
}
434+
else if ('default' in instance && instance.default instanceof WebAssembly.Module) {
435+
instance = await _makeArrayBufferLoader(instance.default)(info)
436+
}
429437
}
430438

431439
if ('instance' in instance)
@@ -440,7 +448,7 @@ export function loadWasm(options: LoadWasmOptions): Promise<void> {
440448
return initPromise
441449
}
442450

443-
function _makeArrayBufferLoader(data: ArrayBufferView | ArrayBuffer): WebAssemblyInstantiator {
451+
function _makeArrayBufferLoader(data: ArrayBufferView | ArrayBuffer | WebAssembly.Module): WebAssemblyInstantiator {
444452
return importObject => WebAssembly.instantiate(data, importObject)
445453
}
446454
function _makeResponseStreamingLoader(data: Response): WebAssemblyInstantiator {

packages/shiki/test/cf.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import js from 'shiki/langs/javascript.mjs'
88
// eslint-disable-next-line antfu/no-import-dist
99
import wasm from '../dist/onig.wasm'
1010

11-
await loadWasm(obj => WebAssembly.instantiate(wasm, obj))
11+
await loadWasm(wasm)
12+
13+
// cloudflare also supports dynamic import
14+
// await loadWasm(import('../dist/onig.wasm'))
1215

1316
export default {
1417
async fetch() {

packages/shiki/test/wasm5.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { expect, it } from 'vitest'
2+
import { getHighlighterCore } from '../src/core'
3+
4+
import js from '../src/assets/langs/javascript'
5+
import nord from '../src/assets/themes/nord'
6+
7+
// eslint-disable-next-line antfu/no-import-dist
8+
import { wasmBinary } from '../../core/dist/wasm-inlined.mjs'
9+
10+
it('loadWasm: WebAssembly.Module', async () => {
11+
const shiki = await getHighlighterCore({
12+
themes: [nord],
13+
langs: [js as any],
14+
loadWasm: WebAssembly.compile(wasmBinary) as any,
15+
})
16+
17+
expect(shiki.codeToHtml('1 + 1', { lang: 'javascript', theme: 'nord' }))
18+
.toMatchInlineSnapshot(`"<pre class="shiki nord" style="background-color:#2e3440ff;color:#d8dee9ff" tabindex="0"><code><span class="line"><span style="color:#B48EAD">1</span><span style="color:#81A1C1"> +</span><span style="color:#B48EAD"> 1</span></span></code></pre>"`)
19+
})

packages/shiki/test/wasm6.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { expect, it } from 'vitest'
2+
import { getHighlighterCore } from '../src/core'
3+
4+
import js from '../src/assets/langs/javascript'
5+
import nord from '../src/assets/themes/nord'
6+
7+
// eslint-disable-next-line antfu/no-import-dist
8+
import { wasmBinary } from '../../core/dist/wasm-inlined.mjs'
9+
10+
it('loadWasm: { default: WebAssembly.Module }', async () => {
11+
const shiki = await getHighlighterCore({
12+
themes: [nord],
13+
langs: [js as any],
14+
loadWasm: Promise.resolve({ default: await WebAssembly.compile(wasmBinary) }) as any,
15+
})
16+
17+
expect(shiki.codeToHtml('1 + 1', { lang: 'javascript', theme: 'nord' }))
18+
.toMatchInlineSnapshot(`"<pre class="shiki nord" style="background-color:#2e3440ff;color:#d8dee9ff" tabindex="0"><code><span class="line"><span style="color:#B48EAD">1</span><span style="color:#81A1C1"> +</span><span style="color:#B48EAD"> 1</span></span></code></pre>"`)
19+
})

0 commit comments

Comments
 (0)