feat(kit): get module options with getNuxtModuleOptions

This commit is contained in:
Harlan Wilton 2023-07-25 15:11:23 +03:00
parent b650207fa5
commit 4f61aa3d6f
2 changed files with 62 additions and 3 deletions

View File

@ -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()
})
})

View File

@ -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
}