From aa37de48f7131686b8040eb7a55883e6b8feb452 Mon Sep 17 00:00:00 2001 From: Julien Huang Date: Wed, 23 Aug 2023 17:23:17 +0200 Subject: [PATCH] fix(nuxt): warn when ignoring duplicate island/server component (#22709) --- packages/nuxt/src/components/scan.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/nuxt/src/components/scan.ts b/packages/nuxt/src/components/scan.ts index 3ca401816c..bad558dd38 100644 --- a/packages/nuxt/src/components/scan.ts +++ b/packages/nuxt/src/components/scan.ts @@ -95,10 +95,7 @@ export async function scanComponents (dirs: ComponentsDir[], srcDir: string): Pr const componentName = resolveComponentName(fileName, prefixParts) if (resolvedNames.has(componentName + suffix) || resolvedNames.has(componentName)) { - console.warn(`[nuxt] Two component files resolving to the same name \`${componentName}\`:\n` + - `\n - ${filePath}` + - `\n - ${resolvedNames.get(componentName)}` - ) + warnAboutDuplicateComponent(componentName, filePath, resolvedNames.get(componentName) || resolvedNames.get(componentName + suffix)!) continue } resolvedNames.set(componentName + suffix, filePath) @@ -136,10 +133,14 @@ export async function scanComponents (dirs: ComponentsDir[], srcDir: string): Pr continue } - // Ignore component if component is already defined (with same mode) - if (!components.some(c => c.pascalName === component.pascalName && ['all', component.mode].includes(c.mode))) { - components.push(component) + const existingComponent = components.find(c => c.pascalName === component.pascalName && ['all', component.mode].includes(c.mode)) + if (existingComponent) { + // Ignore component if component is already defined (with same mode) + warnAboutDuplicateComponent(componentName, filePath, existingComponent.filePath) + continue } + + components.push(component) } scannedPaths.push(dir.path) } @@ -174,3 +175,10 @@ export function resolveComponentName (fileName: string, prefixParts: string[]) { return pascalCase(componentNameParts) + pascalCase(fileNameParts) } + +function warnAboutDuplicateComponent (componentName: string, filePath: string, duplicatePath: string) { + console.warn(`[nuxt] Two component files resolving to the same name \`${componentName}\`:\n` + + `\n - ${filePath}` + + `\n - ${duplicatePath}` + ) +}