@@ -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 ) ;
@@ -286,11 +287,25 @@ function computeModuleSpecifiers(
286
287
}
287
288
288
289
if ( ! specifier ) {
289
- const local = getLocalModuleSpecifier ( modulePath . path , info , compilerOptions , host , options . overrideImportMode || importingSourceFile . impliedNodeFormat , preferences ) ;
290
- if ( pathIsBareSpecifier ( local ) ) {
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
- else if ( ! modulePath . isRedirect && ( ! importedFileIsInNodeModules || modulePath . isInNodeModules ) ) {
308
+ else if ( ! importedFileIsInNodeModules || modulePath . isInNodeModules ) {
294
309
// Why this extra conditional, not just an `else`? If some path to the file contained
295
310
// 'node_modules', but we can't create a non-relative specifier (e.g. "@foo/bar/path/to/file"),
296
311
// that means we had to go through a *sibling's* node_modules, not one we can access directly.
@@ -306,8 +321,9 @@ function computeModuleSpecifiers(
306
321
}
307
322
308
323
return pathsSpecifiers ?. length ? pathsSpecifiers :
324
+ redirectPathsSpecifiers ?. length ? redirectPathsSpecifiers :
309
325
nodeModulesSpecifiers ?. length ? nodeModulesSpecifiers :
310
- Debug . checkDefined ( relativeSpecifiers ) ;
326
+ Debug . checkDefined ( relativeSpecifiers ) ;
311
327
}
312
328
313
329
interface Info {
@@ -322,22 +338,32 @@ 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 ) ;
363
+ if ( pathsOnly ) {
364
+ return fromPaths ;
365
+ }
366
+
341
367
const maybeNonRelative = fromPaths === undefined && baseUrl !== undefined ? removeExtensionAndIndexPostFix ( relativeToBaseUrl , ending , compilerOptions ) : fromPaths ;
342
368
if ( ! maybeNonRelative ) {
343
369
return relativePath ;
0 commit comments