mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
fix(nuxt): provide types for modules as array (#18416)
Co-authored-by: Daniel Roe <daniel@roe.dev>
This commit is contained in:
parent
585e8be2e0
commit
98d292ae85
@ -121,14 +121,17 @@ export const schemaTemplate: NuxtTemplate<TemplateContext> = {
|
|||||||
})).filter(m => m.configKey && m.name && !adHocModules.includes(m.name))
|
})).filter(m => m.configKey && m.name && !adHocModules.includes(m.name))
|
||||||
|
|
||||||
const relativeRoot = relative(resolve(nuxt.options.buildDir, 'types'), nuxt.options.rootDir)
|
const relativeRoot = relative(resolve(nuxt.options.buildDir, 'types'), nuxt.options.rootDir)
|
||||||
|
const getImportName = (name: string) => (name.startsWith('.') ? './' + join(relativeRoot, name) : name).replace(/\.\w+$/, '')
|
||||||
|
const modules = moduleInfo.map(meta => [genString(meta.configKey), getImportName(meta.importName)])
|
||||||
|
|
||||||
return [
|
return [
|
||||||
"import { NuxtModule } from '@nuxt/schema'",
|
"import { NuxtModule } from '@nuxt/schema'",
|
||||||
"declare module '@nuxt/schema' {",
|
"declare module '@nuxt/schema' {",
|
||||||
' interface NuxtConfig {',
|
' interface NuxtConfig {',
|
||||||
...moduleInfo.filter(Boolean).map(meta =>
|
...modules.map(([configKey, importName]) =>
|
||||||
` [${genString(meta.configKey)}]?: typeof ${genDynamicImport(meta.importName.startsWith('.') ? './' + join(relativeRoot, meta.importName) : meta.importName, { wrapper: false })}.default extends NuxtModule<infer O> ? Partial<O> : Record<string, any>`
|
` [${configKey}]?: typeof ${genDynamicImport(importName, { wrapper: false })}.default extends NuxtModule<infer O> ? Partial<O> : Record<string, any>`
|
||||||
),
|
),
|
||||||
|
modules.length > 0 ? ` modules?: (NuxtModule | string | [NuxtModule | string, Record<string, any>] | ${modules.map(([configKey, importName]) => `[${genString(importName)}, NuxtConfig[${configKey}]]`).join(' | ')})[],` : '',
|
||||||
' }',
|
' }',
|
||||||
generateTypes(await resolveSchema(Object.fromEntries(Object.entries(nuxt.options.runtimeConfig).filter(([key]) => key !== 'public'))),
|
generateTypes(await resolveSchema(Object.fromEntries(Object.entries(nuxt.options.runtimeConfig).filter(([key]) => key !== 'public'))),
|
||||||
{
|
{
|
||||||
|
3
test/fixtures/basic/modules/example.ts
vendored
3
test/fixtures/basic/modules/example.ts
vendored
@ -3,7 +3,8 @@ import { defineNuxtModule, addPlugin, useNuxt } from '@nuxt/kit'
|
|||||||
|
|
||||||
export default defineNuxtModule({
|
export default defineNuxtModule({
|
||||||
defaults: {
|
defaults: {
|
||||||
enabled: true
|
enabled: true,
|
||||||
|
typeTest: (value: boolean) => typeof value === 'boolean'
|
||||||
},
|
},
|
||||||
meta: {
|
meta: {
|
||||||
name: 'my-module',
|
name: 'my-module',
|
||||||
|
9
test/fixtures/basic/nuxt.config.ts
vendored
9
test/fixtures/basic/nuxt.config.ts
vendored
@ -55,7 +55,16 @@ export default defineNuxtConfig({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
modules: [
|
modules: [
|
||||||
|
[
|
||||||
'~/modules/example',
|
'~/modules/example',
|
||||||
|
{
|
||||||
|
typeTest (val) {
|
||||||
|
// @ts-expect-error module type defines val as boolean
|
||||||
|
const b: string = val
|
||||||
|
return !!b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
function (_, nuxt) {
|
function (_, nuxt) {
|
||||||
if (process.env.TEST_WITH_WEBPACK) { return }
|
if (process.env.TEST_WITH_WEBPACK) { return }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user