diff --git a/src/utils/content/index.ts b/src/utils/content/index.ts index a7d4e0c1d..696e2c093 100644 --- a/src/utils/content/index.ts +++ b/src/utils/content/index.ts @@ -16,11 +16,10 @@ import { getOrderedSchemaKeys } from '../../runtime/internal/schema' import { transformContent } from './transformers' import pathMetaTransformer from './transformers/path-meta' -let parserOptions = { - mdcConfigs: [] as MdcConfig[], -} -export function setParserOptions(opts: Partial) { - parserOptions = defu(opts, parserOptions) +let _getMdcConfigs: (() => Promise) | undefined + +export function setMdcConfigResolver(fn: () => Promise) { + _getMdcConfigs = fn } type HighlighterOptions = Exclude & { compress: boolean } @@ -70,7 +69,7 @@ async function _getHighlightPlugin(key: string, options: HighlighterOptions) { // Configure the bundled languages bundledLangs: Object.fromEntries(bundledLangs), engine: createOnigurumaEngine(import('shiki/wasm')), - getMdcConfigs: () => Promise.resolve(parserOptions.mdcConfigs), + getMdcConfigs: () => _getMdcConfigs?.() ?? Promise.resolve([]), }) highlightPlugin = { diff --git a/src/utils/mdc.ts b/src/utils/mdc.ts index 863230b22..f44cfd9fc 100644 --- a/src/utils/mdc.ts +++ b/src/utils/mdc.ts @@ -3,7 +3,7 @@ import type { Nuxt } from '@nuxt/schema' import { extendViteConfig } from '@nuxt/kit' import { createJiti } from 'jiti' import type { ModuleOptions } from '../types' -import { setParserOptions } from './content' +import { setMdcConfigResolver } from './content' export async function configureMDCModule(contentOptions: ModuleOptions, nuxt: Nuxt) { const mdcOptions = (nuxt.options as unknown as { mdc: MDCModuleOptions }).mdc @@ -12,16 +12,21 @@ export async function configureMDCModule(contentOptions: ModuleOptions, nuxt: Nu ...(contentOptions.renderer.alias || {}), } - // Hook into mdc configs and store them for parser - nuxt.hook('mdc:configSources', async (mdcConfigs) => { - if (mdcConfigs.length) { + // Provide a lazy resolver for mdc configs. When the highlighter needs + // configs it fires mdc:configSources to collect paths from all modules + // (including file-based configs registered by @nuxtjs/mdc), then imports + // them. This avoids any dependency on modules:done hook ordering. + let _configs: MdcConfig[] | undefined + setMdcConfigResolver(async () => { + if (!_configs) { + const paths: string[] = [] + await nuxt.callHook('mdc:configSources', paths) const jiti = createJiti(nuxt.options.rootDir) - const configs = await Promise.all(mdcConfigs.map(path => jiti.import(path).then(m => (m as { default: MdcConfig }).default || m))) - - setParserOptions({ - mdcConfigs: configs, - }) + _configs = paths.length + ? await Promise.all(paths.map(path => jiti.import(path).then(m => (m as { default: MdcConfig }).default || m))) + : [] } + return _configs }) // Update mdc optimizeDeps options