@@ -1026,12 +1026,13 @@ namespace ts {
1026
1026
type ResolutionKindSpecificLoader = ( extensions : Extensions , candidate : string , onlyRecordFailures : boolean , state : ModuleResolutionState ) => Resolved | undefined ;
1027
1027
1028
1028
/**
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
1030
1030
* mitigate differences between design time structure of the project and its runtime counterpart so the same import name
1031
1031
* can be resolved successfully by TypeScript compiler and runtime module loader.
1032
1032
* If these settings are set then loading procedure will try to use them to resolve module name and it can of failure it will
1033
1033
* fallback to standard resolution routine.
1034
1034
*
1035
+ * 'resolveFromOutDir': TODO document the semantics
1035
1036
* - baseUrl - this setting controls how non-relative module names are resolved. If this setting is specified then non-relative
1036
1037
* names will be resolved relative to baseUrl: i.e. if baseUrl is '/a/b' then candidate location to resolve module name 'c/d' will
1037
1038
* be '/a/b/c/d'
@@ -1088,6 +1089,9 @@ namespace ts {
1088
1089
function tryLoadModuleUsingOptionalResolutionSettings ( extensions : Extensions , moduleName : string , containingDirectory : string , loader : ResolutionKindSpecificLoader ,
1089
1090
state : ModuleResolutionState ) : Resolved | undefined {
1090
1091
1092
+ const resolvedFromOutDir = tryLoadModuleUsingOutDirIfEligible ( extensions , moduleName , containingDirectory , loader , state ) ;
1093
+ if ( resolvedFromOutDir ) return resolvedFromOutDir ;
1094
+
1091
1095
const resolved = tryLoadModuleUsingPathsIfEligible ( extensions , moduleName , loader , state ) ;
1092
1096
if ( resolved ) return resolved . value ;
1093
1097
@@ -1114,6 +1118,51 @@ namespace ts {
1114
1118
}
1115
1119
}
1116
1120
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
+
1117
1166
function tryLoadModuleUsingRootDirs ( extensions : Extensions , moduleName : string , containingDirectory : string , loader : ResolutionKindSpecificLoader ,
1118
1167
state : ModuleResolutionState ) : Resolved | undefined {
1119
1168
0 commit comments