mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-28 08:32:06 +00:00
feat(kit): get module options with getNuxtModuleOptions
This commit is contained in:
parent
b650207fa5
commit
4f61aa3d6f
@ -1,6 +1,6 @@
|
|||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it } from 'vitest'
|
||||||
import { loadNuxt } from '../loader/nuxt'
|
import { loadNuxt } from '../loader/nuxt'
|
||||||
import { getNuxtModuleVersion, hasNuxtModule, hasNuxtModuleCompatibility } from './compatibility'
|
import { getNuxtModuleOptions, getNuxtModuleVersion, hasNuxtModule, hasNuxtModuleCompatibility } from './compatibility'
|
||||||
import { defineNuxtModule } from './define'
|
import { defineNuxtModule } from './define'
|
||||||
|
|
||||||
describe('nuxt module compatibility', () => {
|
describe('nuxt module compatibility', () => {
|
||||||
@ -42,4 +42,38 @@ describe('nuxt module compatibility', () => {
|
|||||||
expect(await hasNuxtModuleCompatibility(module, '^2.0.0', nuxt)).toStrictEqual(false)
|
expect(await hasNuxtModuleCompatibility(module, '^2.0.0', nuxt)).toStrictEqual(false)
|
||||||
await nuxt.close()
|
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()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -12,7 +12,7 @@ import { loadNuxtModuleInstance } from './install'
|
|||||||
*/
|
*/
|
||||||
export function hasNuxtModule (moduleName: string, nuxt: Nuxt = useNuxt()) : boolean {
|
export function hasNuxtModule (moduleName: string, nuxt: Nuxt = useNuxt()) : boolean {
|
||||||
return nuxt.options._installedModules.some(({ meta }) => meta.name === moduleName) ||
|
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 }
|
if (!moduleMeta.name) { return false }
|
||||||
// maybe the version got attached within the installed module instance?
|
// maybe the version got attached within the installed module instance?
|
||||||
const version = nuxt.options._installedModules
|
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]
|
.filter(m => m.meta.name === moduleMeta.name).map(m => m.meta.version)?.[0]
|
||||||
if (version) {
|
if (version) {
|
||||||
return version
|
return version
|
||||||
@ -52,3 +52,28 @@ export async function getNuxtModuleVersion (module: string | NuxtModule, nuxt: N
|
|||||||
}
|
}
|
||||||
return false
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user