From 6fe8e344bf969f1eaeb9f233218ee37405bc404e Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Tue, 19 Mar 2024 15:33:39 +0100 Subject: [PATCH 1/4] fix(shiki): enable WASM on CF deployment --- src/module.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/module.ts b/src/module.ts index 83b38f85..584c9811 100644 --- a/src/module.ts +++ b/src/module.ts @@ -67,16 +67,20 @@ export default defineNuxtModule({ nuxt.hook('ready', () => { const nitro = useNitro() const addWasmSupport = (_nitro: typeof nitro) => { - if (nitro.options.experimental?.wasm) { return } + if (nitro.options.experimental?.wasm) { + return + } _nitro.options.externals = _nitro.options.externals || {} _nitro.options.externals.inline = _nitro.options.externals.inline || [] - _nitro.options.externals.inline.push(id => id.endsWith('.wasm')) + _nitro.options.externals.inline.push((id) => id.endsWith('.wasm')) _nitro.hooks.hook('rollup:before', async (_, rollupConfig) => { const { rollup: unwasm } = await import('unwasm/plugin') rollupConfig.plugins = rollupConfig.plugins || [] - ; (rollupConfig.plugins as any[]).push(unwasm({ - ..._nitro.options.wasm as any, - })) + ;(rollupConfig.plugins as any[]).push( + unwasm({ + ...(_nitro.options.wasm as any), + }), + ) }) } addWasmSupport(nitro) @@ -137,16 +141,20 @@ export default defineNuxtModule({ getContents: templates.mdcConfigs, options: { configs: mdcConfigs }, }) - + // Add highlighter + const nitroPreset = nuxt.options.nitro.preset as string || process.env.NITRO_PRESET || process.env.SERVER_PRESET || '' + const useWasmAssets = !nuxt.options.dev && ( + !!nuxt.options.nitro.experimental?.wasm || + ['cloudflare-pages', 'cloudflare'].includes(nitroPreset) + ) registerTemplate({ filename: 'mdc-highlighter.mjs', getContents: templates.mdcHighlighter, options: { shikiPath: resolver.resolve('../dist/runtime/highlighter/shiki'), options: options.highlight, - // When WASM support enabled in Nitro, we could use the .wasm file directly for Cloudflare Workers - useWasmAssets: !nuxt.options.dev && !!nuxt.options.nitro.experimental?.wasm + useWasmAssets }, }) From 77e7914787cec9e93c3cea821a242e622a1698d2 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Tue, 19 Mar 2024 15:34:21 +0100 Subject: [PATCH 2/4] chore: clean spaces --- src/module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/module.ts b/src/module.ts index 584c9811..3a352bc3 100644 --- a/src/module.ts +++ b/src/module.ts @@ -141,7 +141,7 @@ export default defineNuxtModule({ getContents: templates.mdcConfigs, options: { configs: mdcConfigs }, }) - + // Add highlighter const nitroPreset = nuxt.options.nitro.preset as string || process.env.NITRO_PRESET || process.env.SERVER_PRESET || '' const useWasmAssets = !nuxt.options.dev && ( From 4c9d0e084457f8f52d1a21429998b7573e7dd1b1 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Tue, 19 Mar 2024 15:40:23 +0100 Subject: [PATCH 3/4] chore: extract util --- src/module.ts | 26 ++------------------------ src/utils/index.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 src/utils/index.ts diff --git a/src/module.ts b/src/module.ts index 3a352bc3..d96a4d00 100644 --- a/src/module.ts +++ b/src/module.ts @@ -6,6 +6,7 @@ import { registerMDCSlotTransformer } from './utils/vue-mdc-slot' import { resolve } from 'pathe' import type { BundledLanguage } from 'shiki' import * as templates from './templates' +import { addWasmSupport } from './utils' export const DefaultHighlightLangs: BundledLanguage[] = [ 'js', @@ -64,30 +65,7 @@ export default defineNuxtModule({ if (options.highlight) { // Enable unwasm for shiki - nuxt.hook('ready', () => { - const nitro = useNitro() - const addWasmSupport = (_nitro: typeof nitro) => { - if (nitro.options.experimental?.wasm) { - return - } - _nitro.options.externals = _nitro.options.externals || {} - _nitro.options.externals.inline = _nitro.options.externals.inline || [] - _nitro.options.externals.inline.push((id) => id.endsWith('.wasm')) - _nitro.hooks.hook('rollup:before', async (_, rollupConfig) => { - const { rollup: unwasm } = await import('unwasm/plugin') - rollupConfig.plugins = rollupConfig.plugins || [] - ;(rollupConfig.plugins as any[]).push( - unwasm({ - ...(_nitro.options.wasm as any), - }), - ) - }) - } - addWasmSupport(nitro) - nitro.hooks.hook('prerender:init', (prerenderer) => { - addWasmSupport(prerenderer) - }) - }) + addWasmSupport(nuxt) // Add server handlers addServerHandler({ diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 00000000..7b259bba --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,29 @@ +import { useNitro } from '@nuxt/kit' +import type { Nuxt } from '@nuxt/schema' + +export function addWasmSupport(nuxt: Nuxt) { + nuxt.hook('ready', () => { + const nitro = useNitro() + const _addWasmSupport = (_nitro: typeof nitro) => { + if (nitro.options.experimental?.wasm) { + return + } + _nitro.options.externals = _nitro.options.externals || {} + _nitro.options.externals.inline = _nitro.options.externals.inline || [] + _nitro.options.externals.inline.push((id) => id.endsWith('.wasm')) + _nitro.hooks.hook('rollup:before', async (_, rollupConfig) => { + const { rollup: unwasm } = await import('unwasm/plugin') + rollupConfig.plugins = rollupConfig.plugins || [] + ;(rollupConfig.plugins as any[]).push( + unwasm({ + ...(_nitro.options.wasm as any), + }), + ) + }) + } + _addWasmSupport(nitro) + nitro.hooks.hook('prerender:init', (prerenderer) => { + _addWasmSupport(prerenderer) + }) + }) +} \ No newline at end of file From e1cf434170824a76ebc2aa8044e554b074677c5b Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Tue, 19 Mar 2024 15:42:11 +0100 Subject: [PATCH 4/4] chore: unused import --- src/module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/module.ts b/src/module.ts index d96a4d00..47340b97 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,4 +1,4 @@ -import { defineNuxtModule, extendViteConfig, addComponent, addComponentsDir, createResolver, addServerHandler, addTemplate, addImports, addServerImports, useNitro } from '@nuxt/kit' +import { defineNuxtModule, extendViteConfig, addComponent, addComponentsDir, createResolver, addServerHandler, addTemplate, addImports, addServerImports } from '@nuxt/kit' import fs from 'fs' import type { ModuleOptions } from './types' import { defu } from 'defu'