File tree Expand file tree Collapse file tree 4 files changed +51
-2
lines changed Expand file tree Collapse file tree 4 files changed +51
-2
lines changed Original file line number Diff line number Diff 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}
446454function _makeResponseStreamingLoader ( data : Response ) : WebAssemblyInstantiator {
Original file line number Diff line number Diff line change @@ -8,7 +8,10 @@ import js from 'shiki/langs/javascript.mjs'
88// eslint-disable-next-line antfu/no-import-dist
99import 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
1316export default {
1417 async fetch ( ) {
Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments