diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index bbb28584de..4a2a13d488 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -101,9 +101,15 @@ export const pluginsDeclaration: NuxtTemplate = { filename: 'types/plugins.d.ts', getContents: (ctx) => { const EXTENSION_RE = new RegExp(`(?<=\\w)(${ctx.nuxt.options.extensions.map(e => escapeRE(e)).join('|')})$`, 'g') - const tsImports = ctx.app.plugins - .filter(p => !isAbsolute(p.src) || existsSync(p.src) || existsSync(p.src.replace(EXTENSION_RE, '.d.ts'))) - .map(p => (isAbsolute(p.src) ? relative(join(ctx.nuxt.options.buildDir, 'types'), p.src) : p.src).replace(EXTENSION_RE, '')) + const tsImports: string[] = [] + for (const p of ctx.app.plugins) { + const sources = [p.src, p.src.replace(EXTENSION_RE, '.d.ts')] + if (!isAbsolute(p.src)) { + tsImports.push(p.src.replace(EXTENSION_RE, '')) + } else if (ctx.app.templates.some(t => t.write && t.dst && sources.includes(t.dst)) || sources.some(s => existsSync(s))) { + tsImports.push(relative(join(ctx.nuxt.options.buildDir, 'types'), p.src).replace(EXTENSION_RE, '')) + } + } return `// Generated by Nuxt' import type { Plugin } from '#app'