|
1 | | -import { existsSync, realpathSync } from 'node:fs'; |
| 1 | +import { existsSync } from 'node:fs'; |
2 | 2 | import { isBuiltin } from 'node:module'; |
3 | 3 | import { DEFAULT_EXTENSIONS, DTS_EXTENSIONS, IS_DTS } from '../constants.ts'; |
4 | 4 | import { isFile } from '../util/fs.ts'; |
5 | 5 | import { getPackageNameFromFilePath, getPackageNameFromModuleSpecifier, sanitizeSpecifier } from '../util/modules.ts'; |
6 | 6 | import { timerify } from '../util/Performance.ts'; |
7 | 7 | import { dirname, extname, isAbsolute, isInNodeModules, join, toPosix } from '../util/path.ts'; |
8 | | -import { _createSyncModuleResolver, _resolveModuleSync } from '../util/resolve.ts'; |
| 8 | +import { _createSyncModuleResolver, _resolveModuleSync, resolvePackageManifestPath } from '../util/resolve.ts'; |
9 | 9 | import type { ToSourceFilePath, WorkspacePackageTargetHandler } from '../util/to-source-path.ts'; |
10 | 10 | import type { ResolveModule, ResolvedModule } from './ast-nodes.ts'; |
11 | 11 |
|
@@ -75,38 +75,19 @@ interface PathMapping { |
75 | 75 | } |
76 | 76 |
|
77 | 77 | const moduleResolutionCaches: Array<Map<string, Map<string, ResolvedModule | undefined>>> = []; |
78 | | -const installedPackageRootCache = new Map<string, string | undefined>(); |
79 | 78 |
|
80 | 79 | export function clearModuleResolutionCaches() { |
81 | 80 | for (const cache of moduleResolutionCaches) cache.clear(); |
82 | | - installedPackageRootCache.clear(); |
83 | | -} |
84 | | - |
85 | | -function getInstalledPackageRoot(candidate: string) { |
86 | | - if (installedPackageRootCache.has(candidate)) return installedPackageRootCache.get(candidate); |
87 | | - let packageRoot: string | undefined; |
88 | | - try { |
89 | | - packageRoot = toPosix(realpathSync(candidate)); |
90 | | - } catch {} |
91 | | - installedPackageRootCache.set(candidate, packageRoot); |
92 | | - return packageRoot; |
93 | 81 | } |
94 | 82 |
|
95 | 83 | function getAttributedPackageName(specifier: string, containingFile: string, resolvedFileName: string) { |
96 | 84 | const packageName = getPackageNameFromFilePath(resolvedFileName); |
97 | 85 | const specifierPackageName = getPackageNameFromModuleSpecifier(specifier); |
98 | 86 | if (!specifierPackageName || specifierPackageName === packageName) return packageName; |
99 | | - let dir = dirname(containingFile); |
100 | | - while (true) { |
101 | | - const packageRoot = getInstalledPackageRoot(join(dir, 'node_modules', specifierPackageName)); |
102 | | - if (packageRoot) { |
103 | | - const isResolvedInstall = resolvedFileName === packageRoot || resolvedFileName.startsWith(`${packageRoot}/`); |
104 | | - return isResolvedInstall ? specifierPackageName : packageName; |
105 | | - } |
106 | | - const parent = dirname(dir); |
107 | | - if (parent === dir) return packageName; |
108 | | - dir = parent; |
109 | | - } |
| 87 | + const manifestPath = resolvePackageManifestPath(specifierPackageName, dirname(containingFile)); |
| 88 | + if (!manifestPath) return packageName; |
| 89 | + const packageRoot = dirname(manifestPath); |
| 90 | + return resolvedFileName.startsWith(`${packageRoot}/`) ? specifierPackageName : packageName; |
110 | 91 | } |
111 | 92 |
|
112 | 93 | function compilePathMappings(scopedPaths: ScopedPaths | undefined): PathMapping[] | undefined { |
|
0 commit comments