@@ -275,6 +275,7 @@ function computeModuleSpecifiers(
275
275
// 4. Relative paths
276
276
let nodeModulesSpecifiers : string [ ] | undefined ;
277
277
let pathsSpecifiers : string [ ] | undefined ;
278
+ let redirectPathsSpecifiers : string [ ] | undefined ;
278
279
let relativeSpecifiers : string [ ] | undefined ;
279
280
for ( const modulePath of modulePaths ) {
280
281
const specifier = tryGetModuleNameAsNodeModule ( modulePath , info , importingSourceFile , host , compilerOptions , userPreferences , /*packageNameOnly*/ undefined , options . overrideImportMode ) ;
@@ -285,9 +286,23 @@ function computeModuleSpecifiers(
285
286
return nodeModulesSpecifiers ! ;
286
287
}
287
288
288
- if ( ! specifier && ! modulePath . isRedirect ) {
289
- const local = getLocalModuleSpecifier ( modulePath . path , info , compilerOptions , host , options . overrideImportMode || importingSourceFile . impliedNodeFormat , preferences ) ;
290
- if ( pathIsBareSpecifier ( local ) ) {
289
+ if ( ! specifier ) {
290
+ const local = getLocalModuleSpecifier (
291
+ modulePath . path ,
292
+ info ,
293
+ compilerOptions ,
294
+ host ,
295
+ options . overrideImportMode || importingSourceFile . impliedNodeFormat ,
296
+ preferences ,
297
+ /*pathsOnly*/ modulePath . isRedirect ,
298
+ ) ;
299
+ if ( ! local ) {
300
+ continue ;
301
+ }
302
+ if ( modulePath . isRedirect ) {
303
+ redirectPathsSpecifiers = append ( redirectPathsSpecifiers , local ) ;
304
+ }
305
+ else if ( pathIsBareSpecifier ( local ) ) {
291
306
pathsSpecifiers = append ( pathsSpecifiers , local ) ;
292
307
}
293
308
else if ( ! importedFileIsInNodeModules || modulePath . isInNodeModules ) {
@@ -306,6 +321,7 @@ function computeModuleSpecifiers(
306
321
}
307
322
308
323
return pathsSpecifiers ?. length ? pathsSpecifiers :
324
+ redirectPathsSpecifiers ?. length ? redirectPathsSpecifiers :
309
325
nodeModulesSpecifiers ?. length ? nodeModulesSpecifiers :
310
326
Debug . checkDefined ( relativeSpecifiers ) ;
311
327
}
@@ -322,32 +338,42 @@ function getInfo(importingSourceFileName: Path, host: ModuleSpecifierResolutionH
322
338
return { getCanonicalFileName, importingSourceFileName, sourceDirectory } ;
323
339
}
324
340
325
- function getLocalModuleSpecifier ( moduleFileName : string , info : Info , compilerOptions : CompilerOptions , host : ModuleSpecifierResolutionHost , importMode : ResolutionMode , { ending, relativePreference } : Preferences ) : string {
341
+ function getLocalModuleSpecifier ( moduleFileName : string , info : Info , compilerOptions : CompilerOptions , host : ModuleSpecifierResolutionHost , importMode : ResolutionMode , { ending, relativePreference } : Preferences ) : string ;
342
+ function getLocalModuleSpecifier ( moduleFileName : string , info : Info , compilerOptions : CompilerOptions , host : ModuleSpecifierResolutionHost , importMode : ResolutionMode , { ending, relativePreference } : Preferences , pathsOnly ?: boolean ) : string | undefined ;
343
+ function getLocalModuleSpecifier ( moduleFileName : string , info : Info , compilerOptions : CompilerOptions , host : ModuleSpecifierResolutionHost , importMode : ResolutionMode , { ending, relativePreference } : Preferences , pathsOnly ?: boolean ) : string | undefined {
326
344
const { baseUrl, paths, rootDirs } = compilerOptions ;
345
+ if ( pathsOnly && ! paths ) {
346
+ return undefined ;
347
+ }
348
+
327
349
const { sourceDirectory, getCanonicalFileName } = info ;
328
350
const relativePath = rootDirs && tryGetModuleNameFromRootDirs ( rootDirs , moduleFileName , sourceDirectory , getCanonicalFileName , ending , compilerOptions ) ||
329
351
removeExtensionAndIndexPostFix ( ensurePathIsNonModuleName ( getRelativePathFromDirectory ( sourceDirectory , moduleFileName , getCanonicalFileName ) ) , ending , compilerOptions ) ;
330
352
if ( ! baseUrl && ! paths || relativePreference === RelativePreference . Relative ) {
331
- return relativePath ;
353
+ return pathsOnly ? undefined : relativePath ;
332
354
}
333
355
334
356
const baseDirectory = getNormalizedAbsolutePath ( getPathsBasePath ( compilerOptions , host ) || baseUrl ! , host . getCurrentDirectory ( ) ) ;
335
357
const relativeToBaseUrl = getRelativePathIfInDirectory ( moduleFileName , baseDirectory , getCanonicalFileName ) ;
336
358
if ( ! relativeToBaseUrl ) {
337
- return relativePath ;
359
+ return pathsOnly ? undefined : relativePath ;
338
360
}
339
361
340
362
const fromPaths = paths && tryGetModuleNameFromPaths ( relativeToBaseUrl , paths , getAllowedEndings ( ending , compilerOptions , importMode ) , host , compilerOptions ) ;
341
- const nonRelative = fromPaths === undefined && baseUrl !== undefined ? removeExtensionAndIndexPostFix ( relativeToBaseUrl , ending , compilerOptions ) : fromPaths ;
342
- if ( ! nonRelative ) {
363
+ if ( pathsOnly ) {
364
+ return fromPaths ;
365
+ }
366
+
367
+ const maybeNonRelative = fromPaths === undefined && baseUrl !== undefined ? removeExtensionAndIndexPostFix ( relativeToBaseUrl , ending , compilerOptions ) : fromPaths ;
368
+ if ( ! maybeNonRelative ) {
343
369
return relativePath ;
344
370
}
345
371
346
- if ( relativePreference === RelativePreference . NonRelative ) {
347
- return nonRelative ;
372
+ if ( relativePreference === RelativePreference . NonRelative && ! pathIsRelative ( maybeNonRelative ) ) {
373
+ return maybeNonRelative ;
348
374
}
349
375
350
- if ( relativePreference === RelativePreference . ExternalNonRelative ) {
376
+ if ( relativePreference === RelativePreference . ExternalNonRelative && ! pathIsRelative ( maybeNonRelative ) ) {
351
377
const projectDirectory = compilerOptions . configFilePath ?
352
378
toPath ( getDirectoryPath ( compilerOptions . configFilePath ) , host . getCurrentDirectory ( ) , info . getCanonicalFileName ) :
353
379
info . getCanonicalFileName ( host . getCurrentDirectory ( ) ) ;
@@ -363,7 +389,7 @@ function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOpt
363
389
// lib/ | (path crosses tsconfig.json)
364
390
// imported.ts <---
365
391
//
366
- return nonRelative ;
392
+ return maybeNonRelative ;
367
393
}
368
394
369
395
const nearestTargetPackageJson = getNearestAncestorDirectoryWithPackageJson ( host , getDirectoryPath ( modulePath ) ) ;
@@ -378,16 +404,14 @@ function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOpt
378
404
// package.json |
379
405
// component.ts <---
380
406
//
381
- return nonRelative ;
407
+ return maybeNonRelative ;
382
408
}
383
409
384
410
return relativePath ;
385
411
}
386
412
387
- if ( relativePreference !== RelativePreference . Shortest ) Debug . assertNever ( relativePreference ) ;
388
-
389
413
// Prefer a relative import over a baseUrl import if it has fewer components.
390
- return isPathRelativeToParent ( nonRelative ) || countPathComponents ( relativePath ) < countPathComponents ( nonRelative ) ? relativePath : nonRelative ;
414
+ return isPathRelativeToParent ( maybeNonRelative ) || countPathComponents ( relativePath ) < countPathComponents ( maybeNonRelative ) ? relativePath : maybeNonRelative ;
391
415
}
392
416
393
417
/** @internal */
0 commit comments