fix(kit): handle tuple-format modules in hasNuxtModule (#22316)

This commit is contained in:
Harlan Wilton 2023-07-26 00:04:16 +03:00 committed by GitHub
parent e7139b5104
commit 694f13b18f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 4 deletions

View File

@ -12,11 +12,22 @@ describe('nuxt module compatibility', () => {
meta: { meta: {
name: 'nuxt-module-foo' name: 'nuxt-module-foo'
} }
}) }),
[
defineNuxtModule({
meta: {
name: 'module-instance-with-options'
}
}),
{
foo: 'bar'
}
]
] ]
} }
}) })
expect(hasNuxtModule('nuxt-module-foo', nuxt)).toStrictEqual(true) expect(hasNuxtModule('nuxt-module-foo', nuxt)).toStrictEqual(true)
expect(hasNuxtModule('module-instance-with-options', nuxt)).toStrictEqual(true)
await nuxt.close() await nuxt.close()
}) })
it('can retrieve module version from module instance', async () => { it('can retrieve module version from module instance', async () => {

View File

@ -1,9 +1,19 @@
import satisfies from 'semver/functions/satisfies.js' // npm/node-semver#381 import satisfies from 'semver/functions/satisfies.js' // npm/node-semver#381
import type { Nuxt, NuxtModule } from '@nuxt/schema' import type { Nuxt, NuxtModule, NuxtOptions } from '@nuxt/schema'
import { useNuxt } from '../context' import { useNuxt } from '../context'
import { normalizeSemanticVersion } from '../compatibility' import { normalizeSemanticVersion } from '../compatibility'
import { loadNuxtModuleInstance } from './install' import { loadNuxtModuleInstance } from './install'
function resolveNuxtModuleEntryName (m: NuxtOptions['modules'][number]): string | false {
if (typeof m === 'object' && !Array.isArray(m)) {
return (m as any as NuxtModule).name
}
if (Array.isArray(m)) {
return resolveNuxtModuleEntryName(m[0])
}
return m as string || false
}
/** /**
* Check if a Nuxt module is installed by name. * Check if a Nuxt module is installed by name.
* *
@ -11,8 +21,10 @@ import { loadNuxtModuleInstance } from './install'
* that it cannot detect if a module is _going to be_ installed programmatically by another module. * that it cannot detect if a module is _going to be_ installed programmatically by another module.
*/ */
export function hasNuxtModule (moduleName: string, nuxt: Nuxt = useNuxt()) : boolean { export function hasNuxtModule (moduleName: string, nuxt: Nuxt = useNuxt()) : boolean {
// check installed modules
return nuxt.options._installedModules.some(({ meta }) => meta.name === moduleName) || return nuxt.options._installedModules.some(({ meta }) => meta.name === moduleName) ||
nuxt.options.modules.includes(moduleName) // check modules to be installed
nuxt.options.modules.some(m => moduleName === resolveNuxtModuleEntryName(m))
} }
/** /**
@ -46,7 +58,7 @@ export async function getNuxtModuleVersion (module: string | NuxtModule, nuxt: N
return version return version
} }
// it's possible that the module will be installed, it just hasn't been done yet, preemptively load the instance // it's possible that the module will be installed, it just hasn't been done yet, preemptively load the instance
if (nuxt.options.modules.includes(moduleMeta.name)) { if (hasNuxtModule(moduleMeta.name)) {
const { buildTimeModuleMeta } = await loadNuxtModuleInstance(moduleMeta.name, nuxt) const { buildTimeModuleMeta } = await loadNuxtModuleInstance(moduleMeta.name, nuxt)
return buildTimeModuleMeta.version || false return buildTimeModuleMeta.version || false
} }