mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
fix(kit): handle tuple-format modules in hasNuxtModule
(#22316)
This commit is contained in:
parent
e7139b5104
commit
694f13b18f
@ -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 () => {
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user