* Resolve a Vue component reference to its .vue file
( name: string, fromFile: string, context: ResolutionContext )
| 341 | * Resolve a Vue component reference to its .vue file |
| 342 | */ |
| 343 | function resolveComponent( |
| 344 | name: string, |
| 345 | fromFile: string, |
| 346 | context: ResolutionContext |
| 347 | ): string | null { |
| 348 | // Collect ALL basename matches first. The previous version returned the |
| 349 | // FIRST `Button.vue` found anywhere in the tree (its same-directory pass |
| 350 | // below was unreachable), so a multi-app monorepo with one `Button.vue` |
| 351 | // per app resolved to an arbitrary one (#764). |
| 352 | const matches: string[] = []; |
| 353 | for (const file of context.getAllFiles()) { |
| 354 | if (!file.endsWith('.vue')) continue; |
| 355 | const fileName = file.split(/[/\\]/).pop() || ''; |
| 356 | if (fileName.replace(/\.vue$/, '') === name) matches.push(file); |
| 357 | } |
| 358 | if (matches.length === 0) return null; |
| 359 | |
| 360 | const componentIn = (file: string): string | null => { |
| 361 | const nodes = context.getNodesInFile(file); |
| 362 | const component = nodes.find((n) => n.kind === 'component' && n.name === name); |
| 363 | return component ? component.id : null; |
| 364 | }; |
| 365 | |
| 366 | // The referencing file's folder, or one below it, first for specificity |
| 367 | const sameDir = inReferencingFolder(fromFile, matches, (f) => f); |
| 368 | if (sameDir.length > 0) return componentIn(sameDir[0]!); |
| 369 | |
| 370 | // No positional signal: only an UNAMBIGUOUS basename may resolve; |
| 371 | // ambiguity falls through to the name-matcher's proximity scoring. |
| 372 | return matches.length === 1 ? componentIn(matches[0]!) : null; |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Convert a file path to a Nuxt route path |
no test coverage detected