@@ -1026,12 +1026,13 @@ namespace ts {
10261026 type ResolutionKindSpecificLoader = ( extensions : Extensions , candidate : string , onlyRecordFailures : boolean , state : ModuleResolutionState ) => Resolved | undefined ;
10271027
10281028 /**
1029- * Any module resolution kind can be augmented with optional settings: 'baseUrl', 'paths' and 'rootDirs' - they are used to
1029+ * Any module resolution kind can be augmented with optional settings: 'baseUrl', 'resolveFromOutDir', ' paths' and 'rootDirs' - they are used to
10301030 * mitigate differences between design time structure of the project and its runtime counterpart so the same import name
10311031 * can be resolved successfully by TypeScript compiler and runtime module loader.
10321032 * If these settings are set then loading procedure will try to use them to resolve module name and it can of failure it will
10331033 * fallback to standard resolution routine.
10341034 *
1035+ * 'resolveFromOutDir': TODO document the semantics
10351036 * - baseUrl - this setting controls how non-relative module names are resolved. If this setting is specified then non-relative
10361037 * names will be resolved relative to baseUrl: i.e. if baseUrl is '/a/b' then candidate location to resolve module name 'c/d' will
10371038 * be '/a/b/c/d'
@@ -1088,6 +1089,9 @@ namespace ts {
10881089 function tryLoadModuleUsingOptionalResolutionSettings ( extensions : Extensions , moduleName : string , containingDirectory : string , loader : ResolutionKindSpecificLoader ,
10891090 state : ModuleResolutionState ) : Resolved | undefined {
10901091
1092+ const resolvedFromOutDir = tryLoadModuleUsingOutDirIfEligible ( extensions , moduleName , containingDirectory , loader , state ) ;
1093+ if ( resolvedFromOutDir ) return resolvedFromOutDir ;
1094+
10911095 const resolved = tryLoadModuleUsingPathsIfEligible ( extensions , moduleName , loader , state ) ;
10921096 if ( resolved ) return resolved . value ;
10931097
@@ -1114,6 +1118,51 @@ namespace ts {
11141118 }
11151119 }
11161120
1121+ function tryLoadModuleUsingOutDirIfEligible ( extensions : Extensions , moduleName : string , containingDirectory : string , loader : ResolutionKindSpecificLoader , state : ModuleResolutionState ) : Resolved | undefined {
1122+ const { baseUrl, resolveFromOutDir, outDir, rootDir } = state . compilerOptions ;
1123+ if ( ! resolveFromOutDir ) {
1124+ return undefined ;
1125+ }
1126+ if ( ! outDir ) {
1127+ return undefined ;
1128+ }
1129+ if ( state . traceEnabled ) {
1130+ trace ( state . host , Diagnostics . resolveFromOutDir_option_is_set_using_it_to_resolve_relative_module_name_0 , moduleName ) ;
1131+ }
1132+
1133+ // COMMENT FOR REVIEWER: Is there a more robust way to determine the base directory here?
1134+ var baseDirectory = baseUrl ;
1135+ if ( ! baseDirectory && state . host . getCurrentDirectory ) {
1136+ baseDirectory = state . host . getCurrentDirectory ( ) ;
1137+ }
1138+ if ( ! baseDirectory ) {
1139+ return undefined ;
1140+ }
1141+
1142+ // COMMENT FOR REVIEWER: I've seen rootDir be relative path and and absolute path so
1143+ // handling both cases here to come up with an absolute normalizedPrefix path
1144+ var normalizedPrefix = rootDir && ts . startsWith ( rootDir , ts . directorySeparator ) ?
1145+ ts . normalizePath ( rootDir ) :
1146+ ts . normalizePath ( ts . combinePaths ( baseDirectory , rootDir ) ) ;
1147+ var candidate = ts . normalizePath ( ts . combinePaths ( containingDirectory , moduleName ) ) ;
1148+
1149+ // COMMENT FOR REVIEWER: No ts.relativePath() function that I could find. Is there one
1150+ // somewhere that I'm not aware of?
1151+ var suffix = require ( "path" ) . relative ( normalizedPrefix , candidate ) ;
1152+ candidate = ts . normalizePath ( ts . combinePaths ( baseDirectory , outDir , suffix ) )
1153+ if ( state . traceEnabled ) {
1154+ trace ( state . host , Diagnostics . Loading_0_from_the_out_dir_1_candidate_location_2 , suffix , outDir , candidate ) ;
1155+ }
1156+ const resolvedFileName = loader ( extensions , candidate , ! directoryProbablyExists ( containingDirectory , state . host ) , state ) ;
1157+ if ( resolvedFileName ) {
1158+ return resolvedFileName ;
1159+ }
1160+ if ( state . traceEnabled ) {
1161+ trace ( state . host , Diagnostics . Module_resolution_using_outDir_has_failed ) ;
1162+ }
1163+ return undefined ;
1164+ }
1165+
11171166 function tryLoadModuleUsingRootDirs ( extensions : Extensions , moduleName : string , containingDirectory : string , loader : ResolutionKindSpecificLoader ,
11181167 state : ModuleResolutionState ) : Resolved | undefined {
11191168
0 commit comments