@@ -180,27 +180,41 @@ namespace ts.moduleSpecifiers {
180
180
const bundledPkgReference = bundledPackageName ? combinePaths ( bundledPackageName , relativeToBaseUrl ) : relativeToBaseUrl ;
181
181
const importRelativeToBaseUrl = removeExtensionAndIndexPostFix ( bundledPkgReference , ending , compilerOptions ) ;
182
182
const fromPaths = paths && tryGetModuleNameFromPaths ( removeFileExtension ( bundledPkgReference ) , importRelativeToBaseUrl , paths ) ;
183
- const nonRelative = fromPaths === undefined ? importRelativeToBaseUrl : fromPaths . path ;
183
+ const nonRelative = fromPaths === undefined ? importRelativeToBaseUrl : fromPaths ;
184
184
185
185
if ( relativePreference === RelativePreference . NonRelative ) {
186
186
return nonRelative ;
187
187
}
188
188
189
189
if ( relativePreference === RelativePreference . ExternalNonRelative ) {
190
- Debug . assertIsDefined ( host . getNearestAncestorDirectoryWithPackageJson ) ;
191
190
const projectDirectory = host . getCurrentDirectory ( ) ;
192
191
const modulePath = toPath ( moduleFileName , projectDirectory , getCanonicalFileName ) ;
193
192
const sourceIsInternal = startsWith ( sourceDirectory , projectDirectory ) ;
194
193
const targetIsInternal = startsWith ( modulePath , projectDirectory ) ;
195
194
if ( sourceIsInternal && ! targetIsInternal || ! sourceIsInternal && targetIsInternal ) {
196
195
// 1. The import path crosses the boundary of the tsconfig.json-containing directory.
196
+ //
197
+ // src/
198
+ // tsconfig.json
199
+ // index.ts -------
200
+ // lib/ | (path crosses tsconfig.json)
201
+ // imported.ts <---
202
+ //
197
203
return nonRelative ;
198
204
}
199
205
200
- const nearestTargetPackageJson = host . getNearestAncestorDirectoryWithPackageJson ( getDirectoryPath ( modulePath ) ) ;
201
- const nearestSourcePackageJson = host . getNearestAncestorDirectoryWithPackageJson ( sourceDirectory ) ;
206
+ const nearestTargetPackageJson = getNearestAncestorDirectoryWithPackageJson ( host , getDirectoryPath ( modulePath ) ) ;
207
+ const nearestSourcePackageJson = getNearestAncestorDirectoryWithPackageJson ( host , sourceDirectory ) ;
202
208
if ( nearestSourcePackageJson !== nearestTargetPackageJson ) {
203
209
// 2. The importing and imported files are part of different packages.
210
+ //
211
+ // packages/a/
212
+ // package.json
213
+ // index.ts --------
214
+ // packages/b/ | (path crosses package.json)
215
+ // package.json |
216
+ // component.ts <---
217
+ //
204
218
return nonRelative ;
205
219
}
206
220
@@ -237,6 +251,15 @@ namespace ts.moduleSpecifiers {
237
251
) ;
238
252
}
239
253
254
+ function getNearestAncestorDirectoryWithPackageJson ( host : ModuleSpecifierResolutionHost , fileName : string ) {
255
+ if ( host . getNearestAncestorDirectoryWithPackageJson ) {
256
+ return host . getNearestAncestorDirectoryWithPackageJson ( fileName ) ;
257
+ }
258
+ return ! ! forEachAncestorDirectory ( fileName , directory => {
259
+ return host . fileExists ( combinePaths ( directory , "package.json" ) ) ? true : undefined ;
260
+ } ) ;
261
+ }
262
+
240
263
export function forEachFileNameOfModule < T > (
241
264
importingFileName : string ,
242
265
importedFileName : string ,
@@ -352,7 +375,7 @@ namespace ts.moduleSpecifiers {
352
375
}
353
376
}
354
377
355
- function tryGetModuleNameFromPaths ( relativeToBaseUrlWithIndex : string , relativeToBaseUrl : string , paths : MapLike < readonly string [ ] > ) : { key : string , path : string } | undefined {
378
+ function tryGetModuleNameFromPaths ( relativeToBaseUrlWithIndex : string , relativeToBaseUrl : string , paths : MapLike < readonly string [ ] > ) : string | undefined {
356
379
for ( const key in paths ) {
357
380
for ( const patternText of paths [ key ] ) {
358
381
const pattern = removeFileExtension ( normalizePath ( patternText ) ) ;
@@ -365,11 +388,11 @@ namespace ts.moduleSpecifiers {
365
388
endsWith ( relativeToBaseUrl , suffix ) ||
366
389
! suffix && relativeToBaseUrl === removeTrailingDirectorySeparator ( prefix ) ) {
367
390
const matchedStar = relativeToBaseUrl . substr ( prefix . length , relativeToBaseUrl . length - suffix . length ) ;
368
- return { key, path : key . replace ( "*" , matchedStar ) } ;
391
+ return key . replace ( "*" , matchedStar ) ;
369
392
}
370
393
}
371
394
else if ( pattern === relativeToBaseUrl || pattern === relativeToBaseUrlWithIndex ) {
372
- return { key, path : key } ;
395
+ return key ;
373
396
}
374
397
}
375
398
}
@@ -458,7 +481,7 @@ namespace ts.moduleSpecifiers {
458
481
versionPaths . paths
459
482
) ;
460
483
if ( fromPaths !== undefined ) {
461
- moduleFileToTry = combinePaths ( packageRootPath , fromPaths . path ) ;
484
+ moduleFileToTry = combinePaths ( packageRootPath , fromPaths ) ;
462
485
}
463
486
}
464
487
0 commit comments