@@ -878,12 +878,13 @@ namespace ts {
878
878
type ResolutionKindSpecificLoader = ( extensions : Extensions , candidate : string , onlyRecordFailures : boolean , state : ModuleResolutionState ) => Resolved | undefined ;
879
879
880
880
/**
881
- * Any module resolution kind can be augmented with optional settings: 'baseUrl', 'paths' and 'rootDirs' - they are used to
881
+ * Any module resolution kind can be augmented with optional settings: 'baseUrl', 'resolveFromOutDir', ' paths' and 'rootDirs' - they are used to
882
882
* mitigate differences between design time structure of the project and its runtime counterpart so the same import name
883
883
* can be resolved successfully by TypeScript compiler and runtime module loader.
884
884
* If these settings are set then loading procedure will try to use them to resolve module name and it can of failure it will
885
885
* fallback to standard resolution routine.
886
886
*
887
+ * 'resolveFromOutDir': TODO document the semantics
887
888
* - baseUrl - this setting controls how non-relative module names are resolved. If this setting is specified then non-relative
888
889
* names will be resolved relative to baseUrl: i.e. if baseUrl is '/a/b' then candidate location to resolve module name 'c/d' will
889
890
* be '/a/b/c/d'
@@ -940,6 +941,9 @@ namespace ts {
940
941
function tryLoadModuleUsingOptionalResolutionSettings ( extensions : Extensions , moduleName : string , containingDirectory : string , loader : ResolutionKindSpecificLoader ,
941
942
state : ModuleResolutionState ) : Resolved | undefined {
942
943
944
+ const resolvedFromOutDir = tryLoadModuleUsingOutDirIfEligible ( extensions , moduleName , containingDirectory , loader , state ) ;
945
+ if ( resolvedFromOutDir ) return resolvedFromOutDir ;
946
+
943
947
const resolved = tryLoadModuleUsingPathsIfEligible ( extensions , moduleName , loader , state ) ;
944
948
if ( resolved ) return resolved . value ;
945
949
@@ -965,6 +969,51 @@ namespace ts {
965
969
}
966
970
}
967
971
972
+ function tryLoadModuleUsingOutDirIfEligible ( extensions : Extensions , moduleName : string , containingDirectory : string , loader : ResolutionKindSpecificLoader , state : ModuleResolutionState ) : Resolved | undefined {
973
+ const { baseUrl, resolveFromOutDir, outDir, rootDir } = state . compilerOptions ;
974
+ if ( ! resolveFromOutDir ) {
975
+ return undefined ;
976
+ }
977
+ if ( ! outDir ) {
978
+ return undefined ;
979
+ }
980
+ if ( state . traceEnabled ) {
981
+ trace ( state . host , Diagnostics . resolveFromOutDir_option_is_set_using_it_to_resolve_relative_module_name_0 , moduleName ) ;
982
+ }
983
+
984
+ // COMMENT FOR REVIEWER: Is there a more robust way to determine the base directory here?
985
+ var baseDirectory = baseUrl ;
986
+ if ( ! baseDirectory && state . host . getCurrentDirectory ) {
987
+ baseDirectory = state . host . getCurrentDirectory ( ) ;
988
+ }
989
+ if ( ! baseDirectory ) {
990
+ return undefined ;
991
+ }
992
+
993
+ // COMMENT FOR REVIEWER: I've seen rootDir be relative path and and absolute path so
994
+ // handling both cases here to come up with an absolute normalizedPrefix path
995
+ var normalizedPrefix = rootDir && ts . startsWith ( rootDir , ts . directorySeparator ) ?
996
+ ts . normalizePath ( rootDir ) :
997
+ ts . normalizePath ( ts . combinePaths ( baseDirectory , rootDir ) ) ;
998
+ var candidate = ts . normalizePath ( ts . combinePaths ( containingDirectory , moduleName ) ) ;
999
+
1000
+ // COMMENT FOR REVIEWER: No ts.relativePath() function that I could find. Is there one
1001
+ // somewhere that I'm not aware of?
1002
+ var suffix = require ( "path" ) . relative ( normalizedPrefix , candidate ) ;
1003
+ candidate = ts . normalizePath ( ts . combinePaths ( baseDirectory , outDir , suffix ) )
1004
+ if ( state . traceEnabled ) {
1005
+ trace ( state . host , Diagnostics . Loading_0_from_the_out_dir_1_candidate_location_2 , suffix , outDir , candidate ) ;
1006
+ }
1007
+ const resolvedFileName = loader ( extensions , candidate , ! directoryProbablyExists ( containingDirectory , state . host ) , state ) ;
1008
+ if ( resolvedFileName ) {
1009
+ return resolvedFileName ;
1010
+ }
1011
+ if ( state . traceEnabled ) {
1012
+ trace ( state . host , Diagnostics . Module_resolution_using_outDir_has_failed ) ;
1013
+ }
1014
+ return undefined ;
1015
+ }
1016
+
968
1017
function tryLoadModuleUsingRootDirs ( extensions : Extensions , moduleName : string , containingDirectory : string , loader : ResolutionKindSpecificLoader ,
969
1018
state : ModuleResolutionState ) : Resolved | undefined {
970
1019
0 commit comments