diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index f6f3b9e6e7..1ea4d2abf3 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -121,14 +121,17 @@ export const schemaTemplate: NuxtTemplate = { })).filter(m => m.configKey && m.name && !adHocModules.includes(m.name)) 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 [ "import { NuxtModule } from '@nuxt/schema'", "declare module '@nuxt/schema' {", ' interface NuxtConfig {', - ...moduleInfo.filter(Boolean).map(meta => - ` [${genString(meta.configKey)}]?: typeof ${genDynamicImport(meta.importName.startsWith('.') ? './' + join(relativeRoot, meta.importName) : meta.importName, { wrapper: false })}.default extends NuxtModule ? Partial : Record` + ...modules.map(([configKey, importName]) => + ` [${configKey}]?: typeof ${genDynamicImport(importName, { wrapper: false })}.default extends NuxtModule ? Partial : Record` ), + modules.length > 0 ? ` modules?: (NuxtModule | string | [NuxtModule | string, Record] | ${modules.map(([configKey, importName]) => `[${genString(importName)}, NuxtConfig[${configKey}]]`).join(' | ')})[],` : '', ' }', generateTypes(await resolveSchema(Object.fromEntries(Object.entries(nuxt.options.runtimeConfig).filter(([key]) => key !== 'public'))), { diff --git a/test/fixtures/basic/modules/example.ts b/test/fixtures/basic/modules/example.ts index 9f28b73c11..d55c2c4253 100644 --- a/test/fixtures/basic/modules/example.ts +++ b/test/fixtures/basic/modules/example.ts @@ -3,7 +3,8 @@ import { defineNuxtModule, addPlugin, useNuxt } from '@nuxt/kit' export default defineNuxtModule({ defaults: { - enabled: true + enabled: true, + typeTest: (value: boolean) => typeof value === 'boolean' }, meta: { name: 'my-module', diff --git a/test/fixtures/basic/nuxt.config.ts b/test/fixtures/basic/nuxt.config.ts index 426d32db41..4545b827d7 100644 --- a/test/fixtures/basic/nuxt.config.ts +++ b/test/fixtures/basic/nuxt.config.ts @@ -55,7 +55,16 @@ export default defineNuxtConfig({ } }, modules: [ - '~/modules/example', + [ + '~/modules/example', + { + typeTest (val) { + // @ts-expect-error module type defines val as boolean + const b: string = val + return !!b + } + } + ], function (_, nuxt) { if (process.env.TEST_WITH_WEBPACK) { return }