Skip to content

Commit 6ee3552

Browse files
authored
fix(shiki): enable WASM on CF deployment (#167)
1 parent eacac48 commit 6ee3552

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

src/module.ts

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { defineNuxtModule, extendViteConfig, addComponent, addComponentsDir, createResolver, addServerHandler, addTemplate, addImports, addServerImports, useNitro } from '@nuxt/kit'
1+
import { defineNuxtModule, extendViteConfig, addComponent, addComponentsDir, createResolver, addServerHandler, addTemplate, addImports, addServerImports } from '@nuxt/kit'
22
import fs from 'fs'
33
import type { ModuleOptions } from './types'
44
import { defu } from 'defu'
55
import { registerMDCSlotTransformer } from './utils/vue-mdc-slot'
66
import { resolve } from 'pathe'
77
import type { BundledLanguage } from 'shiki'
88
import * as templates from './templates'
9+
import { addWasmSupport } from './utils'
910

1011
export const DefaultHighlightLangs: BundledLanguage[] = [
1112
'js',
@@ -64,26 +65,7 @@ export default defineNuxtModule<ModuleOptions>({
6465

6566
if (options.highlight) {
6667
// Enable unwasm for shiki
67-
nuxt.hook('ready', () => {
68-
const nitro = useNitro()
69-
const addWasmSupport = (_nitro: typeof nitro) => {
70-
if (nitro.options.experimental?.wasm) { return }
71-
_nitro.options.externals = _nitro.options.externals || {}
72-
_nitro.options.externals.inline = _nitro.options.externals.inline || []
73-
_nitro.options.externals.inline.push(id => id.endsWith('.wasm'))
74-
_nitro.hooks.hook('rollup:before', async (_, rollupConfig) => {
75-
const { rollup: unwasm } = await import('unwasm/plugin')
76-
rollupConfig.plugins = rollupConfig.plugins || []
77-
; (rollupConfig.plugins as any[]).push(unwasm({
78-
..._nitro.options.wasm as any,
79-
}))
80-
})
81-
}
82-
addWasmSupport(nitro)
83-
nitro.hooks.hook('prerender:init', (prerenderer) => {
84-
addWasmSupport(prerenderer)
85-
})
86-
})
68+
addWasmSupport(nuxt)
8769

8870
// Add server handlers
8971
addServerHandler({
@@ -139,14 +121,18 @@ export default defineNuxtModule<ModuleOptions>({
139121
})
140122

141123
// Add highlighter
124+
const nitroPreset = nuxt.options.nitro.preset as string || process.env.NITRO_PRESET || process.env.SERVER_PRESET || ''
125+
const useWasmAssets = !nuxt.options.dev && (
126+
!!nuxt.options.nitro.experimental?.wasm ||
127+
['cloudflare-pages', 'cloudflare'].includes(nitroPreset)
128+
)
142129
registerTemplate({
143130
filename: 'mdc-highlighter.mjs',
144131
getContents: templates.mdcHighlighter,
145132
options: {
146133
shikiPath: resolver.resolve('../dist/runtime/highlighter/shiki'),
147134
options: options.highlight,
148-
// When WASM support enabled in Nitro, we could use the .wasm file directly for Cloudflare Workers
149-
useWasmAssets: !nuxt.options.dev && !!nuxt.options.nitro.experimental?.wasm
135+
useWasmAssets
150136
},
151137
})
152138

src/utils/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useNitro } from '@nuxt/kit'
2+
import type { Nuxt } from '@nuxt/schema'
3+
4+
export function addWasmSupport(nuxt: Nuxt) {
5+
nuxt.hook('ready', () => {
6+
const nitro = useNitro()
7+
const _addWasmSupport = (_nitro: typeof nitro) => {
8+
if (nitro.options.experimental?.wasm) {
9+
return
10+
}
11+
_nitro.options.externals = _nitro.options.externals || {}
12+
_nitro.options.externals.inline = _nitro.options.externals.inline || []
13+
_nitro.options.externals.inline.push((id) => id.endsWith('.wasm'))
14+
_nitro.hooks.hook('rollup:before', async (_, rollupConfig) => {
15+
const { rollup: unwasm } = await import('unwasm/plugin')
16+
rollupConfig.plugins = rollupConfig.plugins || []
17+
;(rollupConfig.plugins as any[]).push(
18+
unwasm({
19+
...(_nitro.options.wasm as any),
20+
}),
21+
)
22+
})
23+
}
24+
_addWasmSupport(nitro)
25+
nitro.hooks.hook('prerender:init', (prerenderer) => {
26+
_addWasmSupport(prerenderer)
27+
})
28+
})
29+
}

0 commit comments

Comments
 (0)