diff --git a/packages/kit/src/module/compatibility.test.ts b/packages/kit/src/module/compatibility.test.ts index 6f73c07420..fa212bd964 100644 --- a/packages/kit/src/module/compatibility.test.ts +++ b/packages/kit/src/module/compatibility.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest' import { loadNuxt } from '../loader/nuxt' -import { getNuxtModuleVersion, hasNuxtModule, hasNuxtModuleCompatibility } from './compatibility' +import { getNuxtModuleOptions, getNuxtModuleVersion, hasNuxtModule, hasNuxtModuleCompatibility } from './compatibility' import { defineNuxtModule } from './define' describe('nuxt module compatibility', () => { @@ -42,4 +42,38 @@ describe('nuxt module compatibility', () => { expect(await hasNuxtModuleCompatibility(module, '^2.0.0', nuxt)).toStrictEqual(false) await nuxt.close() }) + + it('get module options string', async () => { + const fooModule = defineNuxtModule<{ defaultVal?: 'foo'; inlineVal?: 'bar'; number?: number }>({ + meta: { + name: 'nuxt-module-foo', + configKey: 'foo' + }, + defaults: { + defaultVal: 'foo' + } + }) + const nuxt = await loadNuxt({ + overrides: { + modules: [ + [ + fooModule, + { + inlineVal: 'bar' + } + ] + ], + // @ts-expect-error runtime + foo: { number: 10 } + } + }) + expect(await getNuxtModuleOptions(fooModule, nuxt)).toStrictEqual( + { + defaultVal: 'foo', + inlineVal: 'bar', + number: 10 + } + ) + await nuxt.close() + }) }) diff --git a/packages/kit/src/module/compatibility.ts b/packages/kit/src/module/compatibility.ts index 860ed1fb76..fc3110ffa6 100644 --- a/packages/kit/src/module/compatibility.ts +++ b/packages/kit/src/module/compatibility.ts @@ -12,7 +12,7 @@ import { loadNuxtModuleInstance } from './install' */ export function hasNuxtModule (moduleName: string, nuxt: Nuxt = useNuxt()) : boolean { return nuxt.options._installedModules.some(({ meta }) => meta.name === moduleName) || - nuxt.options.modules.includes(moduleName) + nuxt.options.modules.includes(moduleName) } /** @@ -40,7 +40,7 @@ export async function getNuxtModuleVersion (module: string | NuxtModule, nuxt: N if (!moduleMeta.name) { return false } // maybe the version got attached within the installed module instance? const version = nuxt.options._installedModules - // @ts-expect-error _installedModules is not typed + // @ts-expect-error _installedModules is not typed .filter(m => m.meta.name === moduleMeta.name).map(m => m.meta.version)?.[0] if (version) { return version @@ -52,3 +52,28 @@ export async function getNuxtModuleVersion (module: string | NuxtModule, nuxt: N } return false } + +/** + * Get the user provided options for a Nuxt module. + * + * These options may not be the resolved options that the module actually uses. + */ +export async function getNuxtModuleOptions (module: string | NuxtModule, nuxt: Nuxt = useNuxt()) { + const moduleMeta = (typeof module === 'string' ? { name: module } : await module.getMeta?.()) || {} + const { nuxtModule } = (await loadNuxtModuleInstance(module, nuxt)) + const inlineOptions = ( + await Promise.all( + nuxt.options.modules + .filter(async (m) => { + if (!Array.isArray(m)) { return false } + const _module = m[0] + return typeof module === 'object' + ? (await (_module as any as NuxtModule).getMeta?.() === moduleMeta.name) + : _module === moduleMeta.name + }) + .map(m => m?.[1 as keyof typeof m]) + ) + )[0] || {} + if (nuxtModule.getOptions) { return nuxtModule.getOptions(inlineOptions, nuxt) } + return inlineOptions +}