From 5b1ca73886d942db634fc8f939a4ee3db16e47f1 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 22 May 2024 23:18:58 +0100 Subject: [PATCH] fix(nuxt): add module declarations for virtual files (#27311) --- packages/nuxt/src/core/nuxt.ts | 2 ++ packages/nuxt/src/core/templates.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index e3aac36469..259a6f20ce 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -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') }) diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index 520f3986e7..a5e85c74c4 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -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 + }, +}