resolvePathAlias attempts to resolve an import using TypeScript path aliases e.g., "@modules/auth" with alias "@modules/*" -> ["src/modules/*"] becomes "src/modules/auth"
(imp string, pathAliases map[string][]string, baseURL string, idx *fileIndex)
| 453 | // resolvePathAlias attempts to resolve an import using TypeScript path aliases |
| 454 | // e.g., "@modules/auth" with alias "@modules/*" -> ["src/modules/*"] becomes "src/modules/auth" |
| 455 | func resolvePathAlias(imp string, pathAliases map[string][]string, baseURL string, idx *fileIndex) []string { |
| 456 | // Try each alias pattern |
| 457 | for pattern, targets := range pathAliases { |
| 458 | var prefix, suffix string |
| 459 | if starIdx := strings.Index(pattern, "*"); starIdx >= 0 { |
| 460 | prefix = pattern[:starIdx] |
| 461 | suffix = pattern[starIdx+1:] |
| 462 | } else { |
| 463 | // Exact match pattern (no wildcard) |
| 464 | if imp == pattern { |
| 465 | for _, target := range targets { |
| 466 | resolved := target |
| 467 | if baseURL != "" && !filepath.IsAbs(resolved) { |
| 468 | resolved = filepath.Join(baseURL, resolved) |
| 469 | } |
| 470 | if files := tryExactMatch(resolved, idx); len(files) > 0 { |
| 471 | return files |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | continue |
| 476 | } |
| 477 | |
| 478 | // Check if import matches this pattern |
| 479 | if !strings.HasPrefix(imp, prefix) { |
| 480 | continue |
| 481 | } |
| 482 | if suffix != "" && !strings.HasSuffix(imp, suffix) { |
| 483 | continue |
| 484 | } |
| 485 | |
| 486 | // Extract the wildcard portion |
| 487 | wildcardPart := imp[len(prefix):] |
| 488 | if suffix != "" { |
| 489 | wildcardPart = wildcardPart[:len(wildcardPart)-len(suffix)] |
| 490 | } |
| 491 | |
| 492 | // Try each target mapping |
| 493 | for _, target := range targets { |
| 494 | resolved := target |
| 495 | if starIdx := strings.Index(target, "*"); starIdx >= 0 { |
| 496 | // Replace wildcard with captured portion |
| 497 | resolved = target[:starIdx] + wildcardPart + target[starIdx+1:] |
| 498 | } |
| 499 | |
| 500 | // Apply baseUrl if set |
| 501 | if baseURL != "" && !filepath.IsAbs(resolved) { |
| 502 | resolved = filepath.Join(baseURL, resolved) |
| 503 | } |
| 504 | |
| 505 | // Try to find matching files |
| 506 | if files := tryExactMatch(resolved, idx); len(files) > 0 { |
| 507 | return files |
| 508 | } |
| 509 | if files := trySuffixMatch(resolved, idx); len(files) > 0 { |
| 510 | return files |
| 511 | } |
| 512 | } |