|
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' |
2 | 2 | import fs from 'fs' |
3 | 3 | import type { ModuleOptions } from './types' |
4 | 4 | import { defu } from 'defu' |
5 | 5 | import { registerMDCSlotTransformer } from './utils/vue-mdc-slot' |
6 | 6 | import { resolve } from 'pathe' |
7 | 7 | import type { BundledLanguage } from 'shiki' |
8 | 8 | import * as templates from './templates' |
| 9 | +import { addWasmSupport } from './utils' |
9 | 10 |
|
10 | 11 | export const DefaultHighlightLangs: BundledLanguage[] = [ |
11 | 12 | 'js', |
@@ -64,26 +65,7 @@ export default defineNuxtModule<ModuleOptions>({ |
64 | 65 |
|
65 | 66 | if (options.highlight) { |
66 | 67 | // 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) |
87 | 69 |
|
88 | 70 | // Add server handlers |
89 | 71 | addServerHandler({ |
@@ -139,14 +121,18 @@ export default defineNuxtModule<ModuleOptions>({ |
139 | 121 | }) |
140 | 122 |
|
141 | 123 | // 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 | + ) |
142 | 129 | registerTemplate({ |
143 | 130 | filename: 'mdc-highlighter.mjs', |
144 | 131 | getContents: templates.mdcHighlighter, |
145 | 132 | options: { |
146 | 133 | shikiPath: resolver.resolve('../dist/runtime/highlighter/shiki'), |
147 | 134 | 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 |
150 | 136 | }, |
151 | 137 | }) |
152 | 138 |
|
|
0 commit comments