fix(nuxt): handle plugin type extensions more correctly (#28480)

This commit is contained in:
Daniel Roe 2024-08-09 12:31:04 +01:00 committed by GitHub
parent 487170c867
commit 68e153c71a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,6 @@
import { existsSync } from 'node:fs' import { existsSync } from 'node:fs'
import { genArrayFromRaw, genDynamicImport, genExport, genImport, genObjectFromRawEntries, genSafeVariableName, genString } from 'knitwork' import { genArrayFromRaw, genDynamicImport, genExport, genImport, genObjectFromRawEntries, genSafeVariableName, genString } from 'knitwork'
import { isAbsolute, join, relative, resolve } from 'pathe' import { join, relative, resolve } from 'pathe'
import type { JSValue } from 'untyped' import type { JSValue } from 'untyped'
import { generateTypes, resolveSchema } from 'untyped' import { generateTypes, resolveSchema } from 'untyped'
import escapeRE from 'escape-string-regexp' import escapeRE from 'escape-string-regexp'
@ -98,19 +98,36 @@ export const serverPluginTemplate: NuxtTemplate = {
export const pluginsDeclaration: NuxtTemplate = { export const pluginsDeclaration: NuxtTemplate = {
filename: 'types/plugins.d.ts', filename: 'types/plugins.d.ts',
getContents: async (ctx) => { getContents: async ({ nuxt, app }) => {
const EXTENSION_RE = new RegExp(`(?<=\\w)(${ctx.nuxt.options.extensions.map(e => escapeRE(e)).join('|')})$`, 'g') const EXTENSION_RE = new RegExp(`(?<=\\w)(${nuxt.options.extensions.map(e => escapeRE(e)).join('|')})$`, 'g')
const typesDir = join(nuxt.options.buildDir, 'types')
const tsImports: string[] = [] const tsImports: string[] = []
for (const p of ctx.app.plugins) { const pluginNames: string[] = []
const sources = [p.src, p.src.replace(EXTENSION_RE, '.d.ts')]
if (!isAbsolute(p.src)) { function exists (path: string) {
tsImports.push(p.src.replace(EXTENSION_RE, '')) return app.templates.some(t => t.write && path === t.dst) || existsSync(path)
} 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, ''))
}
} }
const pluginsName = (await annotatePlugins(ctx.nuxt, ctx.app.plugins)).filter(p => p.name).map(p => `'${p.name}'`) for (const plugin of await annotatePlugins(nuxt, app.plugins)) {
if (plugin.name) {
pluginNames.push(`'${plugin.name}'`)
}
const pluginPath = resolve(typesDir, plugin.src)
const relativePath = relative(typesDir, pluginPath)
const correspondingDeclaration = pluginPath.replace(/\.(?<letter>[cm])?jsx?$/, '.d.$<letter>ts')
if (correspondingDeclaration !== pluginPath && exists(correspondingDeclaration)) {
tsImports.push(relativePath)
continue
}
if (exists(pluginPath)) {
tsImports.push(relativePath.replace(EXTENSION_RE, ''))
continue
}
}
return `// Generated by Nuxt' return `// Generated by Nuxt'
import type { Plugin } from '#app' import type { Plugin } from '#app'
@ -126,7 +143,7 @@ declare module '#app' {
interface NuxtApp extends NuxtAppInjections { } interface NuxtApp extends NuxtAppInjections { }
interface NuxtAppLiterals { interface NuxtAppLiterals {
pluginName: ${pluginsName.join(' | ')} pluginName: ${pluginNames.join(' | ')}
} }
} }