fix(nuxt): add module declarations for virtual files (#27311)

This commit is contained in:
Daniel Roe 2024-05-22 23:18:58 +01:00 committed by GitHub
parent f256210d55
commit 5b1ca73886
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 0 deletions

View File

@ -129,6 +129,8 @@ async function initNuxt (nuxt: Nuxt) {
if (nuxt.options.typescript.shim) {
opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/vue-shim.d.ts') })
}
// Add shims for `#build/*` imports that do not already have matching types
opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/build.d.ts') })
// Add module augmentations directly to NuxtConfig
opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/schema.d.ts') })
opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/app.config.d.ts') })

View File

@ -413,3 +413,29 @@ export const nuxtConfigTemplate: NuxtTemplate = {
].join('\n\n')
},
}
const TYPE_FILENAME_RE = /\.([cm])?[jt]s$/
const DECLARATION_RE = /\.d\.[cm]?ts$/
export const buildTypeTemplate: NuxtTemplate = {
filename: 'types/build.d.ts',
getContents ({ app }) {
let declarations = ''
for (const file of app.templates) {
if (file.write || !file.filename || DECLARATION_RE.test(file.filename)) {
continue
}
if (TYPE_FILENAME_RE.test(file.filename)) {
const typeFilenames = new Set([file.filename.replace(TYPE_FILENAME_RE, '.d.$1ts'), file.filename.replace(TYPE_FILENAME_RE, '.d.ts')])
if (app.templates.some(f => f.filename && typeFilenames.has(f.filename))) {
continue
}
}
declarations += 'declare module ' + JSON.stringify(join('#build', file.filename)) + ';\n'
}
return declarations
},
}