@@ -298,14 +298,12 @@ namespace ts {
298298
299299 let resolvedTypeReferenceDirective : ResolvedTypeReferenceDirective | undefined ;
300300 if ( resolved ) {
301- if ( ! options . preserveSymlinks ) {
302- resolved = { ...resolved , fileName : realPath ( resolved . fileName , host , traceEnabled ) } ;
303- }
304-
301+ const { fileName, packageId } = resolved ;
302+ const resolvedFileName = options . preserveSymlinks ? fileName : realPath ( fileName , host , traceEnabled ) ;
305303 if ( traceEnabled ) {
306- trace ( host , Diagnostics . Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2 , typeReferenceDirectiveName , resolved . fileName , primary ) ;
304+ trace ( host , Diagnostics . Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2 , typeReferenceDirectiveName , resolvedFileName , primary ) ;
307305 }
308- resolvedTypeReferenceDirective = { primary, resolvedFileName : resolved . fileName , packageId : resolved . packageId } ;
306+ resolvedTypeReferenceDirective = { primary, resolvedFileName, packageId, isExternalLibraryImport : pathContainsNodeModules ( fileName ) } ;
309307 }
310308
311309 return { resolvedTypeReferenceDirective, failedLookupLocations } ;
@@ -316,7 +314,7 @@ namespace ts {
316314 if ( traceEnabled ) {
317315 trace ( host , Diagnostics . Resolving_with_primary_search_path_0 , typeRoots . join ( ", " ) ) ;
318316 }
319- return forEach ( typeRoots , typeRoot => {
317+ return firstDefined ( typeRoots , typeRoot => {
320318 const candidate = combinePaths ( typeRoot , typeReferenceDirectiveName ) ;
321319 const candidateDirectory = getDirectoryPath ( candidate ) ;
322320 const directoryExists = directoryProbablyExists ( candidateDirectory , host ) ;
@@ -343,15 +341,16 @@ namespace ts {
343341 if ( traceEnabled ) {
344342 trace ( host , Diagnostics . Looking_up_in_node_modules_folder_initial_location_0 , initialLocationForSecondaryLookup ) ;
345343 }
346- let result : SearchResult < Resolved > | undefined ;
344+ let result : Resolved | undefined ;
347345 if ( ! isExternalModuleNameRelative ( typeReferenceDirectiveName ) ) {
348- result = loadModuleFromNearestNodeModulesDirectory ( Extensions . DtsOnly , typeReferenceDirectiveName , initialLocationForSecondaryLookup , moduleResolutionState , /*cache*/ undefined , /*redirectedReference*/ undefined ) ;
346+ const searchResult = loadModuleFromNearestNodeModulesDirectory ( Extensions . DtsOnly , typeReferenceDirectiveName , initialLocationForSecondaryLookup , moduleResolutionState , /*cache*/ undefined , /*redirectedReference*/ undefined ) ;
347+ result = searchResult && searchResult . value ;
349348 }
350349 else {
351350 const { path : candidate } = normalizePathAndParts ( combinePaths ( initialLocationForSecondaryLookup , typeReferenceDirectiveName ) ) ;
352- result = toSearchResult ( nodeLoadModuleByRelativeName ( Extensions . DtsOnly , candidate , /*onlyRecordFailures*/ false , moduleResolutionState , /*considerPackageJson*/ true ) ) ;
351+ result = nodeLoadModuleByRelativeName ( Extensions . DtsOnly , candidate , /*onlyRecordFailures*/ false , moduleResolutionState , /*considerPackageJson*/ true ) ;
353352 }
354- const resolvedFile = resolvedTypeScriptOnly ( result && result . value ) ;
353+ const resolvedFile = resolvedTypeScriptOnly ( result ) ;
355354 if ( ! resolvedFile && traceEnabled ) {
356355 trace ( host , Diagnostics . Type_reference_directive_0_was_not_resolved , typeReferenceDirectiveName ) ;
357356 }
@@ -883,7 +882,7 @@ namespace ts {
883882 const loader : ResolutionKindSpecificLoader = ( extensions , candidate , onlyRecordFailures , state ) => nodeLoadModuleByRelativeName ( extensions , candidate , onlyRecordFailures , state , /*considerPackageJson*/ true ) ;
884883 const resolved = tryLoadModuleUsingOptionalResolutionSettings ( extensions , moduleName , containingDirectory , loader , state ) ;
885884 if ( resolved ) {
886- return toSearchResult ( { resolved, isExternalLibraryImport : stringContains ( resolved . path , nodeModulesPathPart ) } ) ;
885+ return toSearchResult ( { resolved, isExternalLibraryImport : pathContainsNodeModules ( resolved . path ) } ) ;
887886 }
888887
889888 if ( ! isExternalModuleNameRelative ( moduleName ) ) {
@@ -960,6 +959,10 @@ namespace ts {
960959
961960 /*@internal */
962961 export const nodeModulesPathPart = "/node_modules/" ;
962+ /*@internal */
963+ export function pathContainsNodeModules ( path : string ) : boolean {
964+ return stringContains ( path , nodeModulesPathPart ) ;
965+ }
963966
964967 /**
965968 * This will be called on the successfully resolved path from `loadModuleFromFile`.
0 commit comments