From b15f51fd6bc0d220d9e3b2f2da13c4127849ea63 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 9 May 2024 10:58:57 +0900 Subject: [PATCH 1/3] fix(core): simplify cloudflare wasm integration --- packages/core/src/oniguruma/index.ts | 10 +++++++++- packages/shiki/test/cf.ts | 5 ++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/core/src/oniguruma/index.ts b/packages/core/src/oniguruma/index.ts index f25b1e81b..db4e5e937 100644 --- a/packages/core/src/oniguruma/index.ts +++ b/packages/core/src/oniguruma/index.ts @@ -426,6 +426,14 @@ export function loadWasm(options: LoadWasmOptions): Promise { else if (isArrayBuffer(instance)) { instance = await _makeArrayBufferLoader(instance)(info) } + // cloudflare's import("xxx.wasm") returns `{ default: WebAssembly.Module }` + // https://developers.cloudflare.com/workers/wrangler/bundling/ + else if (instance instanceof WebAssembly.Module) { + instance = await _makeArrayBufferLoader(instance)(info) + } + else if ('default' in instance && instance.default instanceof WebAssembly.Module) { + instance = await _makeArrayBufferLoader(instance.default)(info) + } } if ('instance' in instance) @@ -440,7 +448,7 @@ export function loadWasm(options: LoadWasmOptions): Promise { return initPromise } -function _makeArrayBufferLoader(data: ArrayBufferView | ArrayBuffer): WebAssemblyInstantiator { +function _makeArrayBufferLoader(data: ArrayBufferView | ArrayBuffer | WebAssembly.Module): WebAssemblyInstantiator { return importObject => WebAssembly.instantiate(data, importObject) } function _makeResponseStreamingLoader(data: Response): WebAssemblyInstantiator { diff --git a/packages/shiki/test/cf.ts b/packages/shiki/test/cf.ts index b070a63b9..beb286964 100644 --- a/packages/shiki/test/cf.ts +++ b/packages/shiki/test/cf.ts @@ -8,7 +8,10 @@ import js from 'shiki/langs/javascript.mjs' // eslint-disable-next-line antfu/no-import-dist import wasm from '../dist/onig.wasm' -await loadWasm(obj => WebAssembly.instantiate(wasm, obj)) +await loadWasm(wasm) + +// cloudflare also supports dynamic import +// await loadWasm(import('../dist/onig.wasm')) export default { async fetch() { From b9b3acf015b4774f3e40f58be9e11e19783614b5 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 9 May 2024 11:02:35 +0900 Subject: [PATCH 2/3] chore: comment --- packages/core/src/oniguruma/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/oniguruma/index.ts b/packages/core/src/oniguruma/index.ts index db4e5e937..029210e73 100644 --- a/packages/core/src/oniguruma/index.ts +++ b/packages/core/src/oniguruma/index.ts @@ -426,7 +426,7 @@ export function loadWasm(options: LoadWasmOptions): Promise { else if (isArrayBuffer(instance)) { instance = await _makeArrayBufferLoader(instance)(info) } - // cloudflare's import("xxx.wasm") returns `{ default: WebAssembly.Module }` + // import("shiki/onig.wasm") returns `{ default: WebAssembly.Module }` on cloudflare workers // https://developers.cloudflare.com/workers/wrangler/bundling/ else if (instance instanceof WebAssembly.Module) { instance = await _makeArrayBufferLoader(instance)(info) From 9ae6fc27cba826b5b8018f892376d1ef46e43450 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 9 May 2024 13:52:21 +0900 Subject: [PATCH 3/3] test: add unit --- packages/shiki/test/wasm5.test.ts | 19 +++++++++++++++++++ packages/shiki/test/wasm6.test.ts | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 packages/shiki/test/wasm5.test.ts create mode 100644 packages/shiki/test/wasm6.test.ts diff --git a/packages/shiki/test/wasm5.test.ts b/packages/shiki/test/wasm5.test.ts new file mode 100644 index 000000000..24baf6548 --- /dev/null +++ b/packages/shiki/test/wasm5.test.ts @@ -0,0 +1,19 @@ +import { expect, it } from 'vitest' +import { getHighlighterCore } from '../src/core' + +import js from '../src/assets/langs/javascript' +import nord from '../src/assets/themes/nord' + +// eslint-disable-next-line antfu/no-import-dist +import { wasmBinary } from '../../core/dist/wasm-inlined.mjs' + +it('loadWasm: WebAssembly.Module', async () => { + const shiki = await getHighlighterCore({ + themes: [nord], + langs: [js as any], + loadWasm: WebAssembly.compile(wasmBinary) as any, + }) + + expect(shiki.codeToHtml('1 + 1', { lang: 'javascript', theme: 'nord' })) + .toMatchInlineSnapshot(`"
1 + 1
"`) +}) diff --git a/packages/shiki/test/wasm6.test.ts b/packages/shiki/test/wasm6.test.ts new file mode 100644 index 000000000..66e0c0743 --- /dev/null +++ b/packages/shiki/test/wasm6.test.ts @@ -0,0 +1,19 @@ +import { expect, it } from 'vitest' +import { getHighlighterCore } from '../src/core' + +import js from '../src/assets/langs/javascript' +import nord from '../src/assets/themes/nord' + +// eslint-disable-next-line antfu/no-import-dist +import { wasmBinary } from '../../core/dist/wasm-inlined.mjs' + +it('loadWasm: { default: WebAssembly.Module }', async () => { + const shiki = await getHighlighterCore({ + themes: [nord], + langs: [js as any], + loadWasm: Promise.resolve({ default: await WebAssembly.compile(wasmBinary) }) as any, + }) + + expect(shiki.codeToHtml('1 + 1', { lang: 'javascript', theme: 'nord' })) + .toMatchInlineSnapshot(`"
1 + 1
"`) +})