@@ -62,6 +62,7 @@ namespace ts {
62
62
TypeScript , /** '.ts', '.tsx', or '.d.ts' */
63
63
JavaScript , /** '.js' or '.jsx' */
64
64
Json , /** '.json' */
65
+ TSConfig , /** '.json' with `tsconfig` used instead of `index` */
65
66
DtsOnly /** Only '.d.ts' */
66
67
}
67
68
@@ -98,6 +99,7 @@ namespace ts {
98
99
types ?: string ;
99
100
typesVersions ?: MapLike < MapLike < string [ ] > > ;
100
101
main ?: string ;
102
+ tsconfig ?: string ;
101
103
}
102
104
103
105
interface PackageJson extends PackageJsonPathFields {
@@ -126,7 +128,7 @@ namespace ts {
126
128
return value ;
127
129
}
128
130
129
- function readPackageJsonPathField < K extends "typings" | "types" | "main" > ( jsonContent : PackageJson , fieldName : K , baseDirectory : string , state : ModuleResolutionState ) : PackageJson [ K ] | undefined {
131
+ function readPackageJsonPathField < K extends "typings" | "types" | "main" | "tsconfig" > ( jsonContent : PackageJson , fieldName : K , baseDirectory : string , state : ModuleResolutionState ) : PackageJson [ K ] | undefined {
130
132
const fileName = readPackageJsonField ( jsonContent , fieldName , "string" , state ) ;
131
133
if ( fileName === undefined ) return ;
132
134
const path = normalizePath ( combinePaths ( baseDirectory , fileName ) ) ;
@@ -141,6 +143,10 @@ namespace ts {
141
143
|| readPackageJsonPathField ( jsonContent , "types" , baseDirectory , state ) ;
142
144
}
143
145
146
+ function readPackageJsonTSConfigField ( jsonContent : PackageJson , baseDirectory : string , state : ModuleResolutionState ) {
147
+ return readPackageJsonPathField ( jsonContent , "tsconfig" , baseDirectory , state ) ;
148
+ }
149
+
144
150
function readPackageJsonMainField ( jsonContent : PackageJson , baseDirectory : string , state : ModuleResolutionState ) {
145
151
return readPackageJsonPathField ( jsonContent , "main" , baseDirectory , state ) ;
146
152
}
@@ -853,25 +859,27 @@ namespace ts {
853
859
return resolvedModule && resolvedModule . resolvedFileName ;
854
860
}
855
861
862
+ const jsOnlyExtensions = [ Extensions . JavaScript ] ;
863
+ const tsExtensions = [ Extensions . TypeScript , Extensions . JavaScript ] ;
864
+ const tsPlusJsonExtensions = [ ...tsExtensions , Extensions . Json ] ;
865
+ const tsconfigExtensions = [ Extensions . TSConfig ] ;
856
866
function tryResolveJSModuleWorker ( moduleName : string , initialDir : string , host : ModuleResolutionHost ) : ResolvedModuleWithFailedLookupLocations {
857
- return nodeModuleNameResolverWorker ( moduleName , initialDir , { moduleResolution : ModuleResolutionKind . NodeJs , allowJs : true } , host , /*cache*/ undefined , /*redirectedReference*/ undefined , /*jsOnly */ true ) ;
867
+ return nodeModuleNameResolverWorker ( moduleName , initialDir , { moduleResolution : ModuleResolutionKind . NodeJs , allowJs : true } , host , /*cache*/ undefined , jsOnlyExtensions , /*redirectedReferences */ undefined ) ;
858
868
}
859
869
860
- export function nodeModuleNameResolver ( moduleName : string , containingFile : string , compilerOptions : CompilerOptions , host : ModuleResolutionHost , cache ?: ModuleResolutionCache , redirectedReference ?: ResolvedProjectReference ) : ResolvedModuleWithFailedLookupLocations {
861
- return nodeModuleNameResolverWorker ( moduleName , getDirectoryPath ( containingFile ) , compilerOptions , host , cache , redirectedReference , /*jsOnly*/ false ) ;
870
+ export function nodeModuleNameResolver ( moduleName : string , containingFile : string , compilerOptions : CompilerOptions , host : ModuleResolutionHost , cache ?: ModuleResolutionCache , redirectedReference ?: ResolvedProjectReference ) : ResolvedModuleWithFailedLookupLocations ;
871
+ /* @internal */ export function nodeModuleNameResolver ( moduleName : string , containingFile : string , compilerOptions : CompilerOptions , host : ModuleResolutionHost , cache ?: ModuleResolutionCache , redirectedReference ?: ResolvedProjectReference , lookupConfig ?: boolean ) : ResolvedModuleWithFailedLookupLocations ; // tslint:disable-line unified-signatures
872
+ export function nodeModuleNameResolver ( moduleName : string , containingFile : string , compilerOptions : CompilerOptions , host : ModuleResolutionHost , cache ?: ModuleResolutionCache , redirectedReference ?: ResolvedProjectReference , lookupConfig ?: boolean ) : ResolvedModuleWithFailedLookupLocations {
873
+ return nodeModuleNameResolverWorker ( moduleName , getDirectoryPath ( containingFile ) , compilerOptions , host , cache , lookupConfig ? tsconfigExtensions : ( compilerOptions . resolveJsonModule ? tsPlusJsonExtensions : tsExtensions ) , redirectedReference ) ;
862
874
}
863
875
864
- function nodeModuleNameResolverWorker ( moduleName : string , containingDirectory : string , compilerOptions : CompilerOptions , host : ModuleResolutionHost , cache : ModuleResolutionCache | undefined , redirectedReference : ResolvedProjectReference | undefined , jsOnly : boolean ) : ResolvedModuleWithFailedLookupLocations {
876
+ function nodeModuleNameResolverWorker ( moduleName : string , containingDirectory : string , compilerOptions : CompilerOptions , host : ModuleResolutionHost , cache : ModuleResolutionCache | undefined , extensions : Extensions [ ] , redirectedReference : ResolvedProjectReference | undefined ) : ResolvedModuleWithFailedLookupLocations {
865
877
const traceEnabled = isTraceEnabled ( compilerOptions , host ) ;
866
878
867
879
const failedLookupLocations : string [ ] = [ ] ;
868
880
const state : ModuleResolutionState = { compilerOptions, host, traceEnabled, failedLookupLocations } ;
869
881
870
- const result = jsOnly ?
871
- tryResolve ( Extensions . JavaScript ) :
872
- ( tryResolve ( Extensions . TypeScript ) ||
873
- tryResolve ( Extensions . JavaScript ) ||
874
- ( compilerOptions . resolveJsonModule ? tryResolve ( Extensions . Json ) : undefined ) ) ;
882
+ const result = forEach ( extensions , ext => tryResolve ( ext ) ) ;
875
883
if ( result && result . value ) {
876
884
const { resolved, isExternalLibraryImport } = result . value ;
877
885
return createResolvedModuleWithFailedLookupLocations ( resolved , isExternalLibraryImport , failedLookupLocations ) ;
@@ -1019,9 +1027,9 @@ namespace ts {
1019
1027
* in cases when we know upfront that all load attempts will fail (because containing folder does not exists) however we still need to record all failed lookup locations.
1020
1028
*/
1021
1029
function loadModuleFromFile ( extensions : Extensions , candidate : string , onlyRecordFailures : boolean , state : ModuleResolutionState ) : PathAndExtension | undefined {
1022
- if ( extensions === Extensions . Json ) {
1030
+ if ( extensions === Extensions . Json || extensions === Extensions . TSConfig ) {
1023
1031
const extensionLess = tryRemoveExtension ( candidate , Extension . Json ) ;
1024
- return extensionLess === undefined ? undefined : tryAddingExtensions ( extensionLess , extensions , onlyRecordFailures , state ) ;
1032
+ return ( extensionLess === undefined && extensions === Extensions . Json ) ? undefined : tryAddingExtensions ( extensionLess || candidate , extensions , onlyRecordFailures , state ) ;
1025
1033
}
1026
1034
1027
1035
// First, try adding an extension. An import of "foo" could be matched by a file "foo.ts", or "foo.js" by "foo.js.ts"
@@ -1059,6 +1067,7 @@ namespace ts {
1059
1067
return tryExtension ( Extension . Ts ) || tryExtension ( Extension . Tsx ) || tryExtension ( Extension . Dts ) ;
1060
1068
case Extensions . JavaScript :
1061
1069
return tryExtension ( Extension . Js ) || tryExtension ( Extension . Jsx ) ;
1070
+ case Extensions . TSConfig :
1062
1071
case Extensions . Json :
1063
1072
return tryExtension ( Extension . Json ) ;
1064
1073
}
@@ -1156,11 +1165,27 @@ namespace ts {
1156
1165
}
1157
1166
1158
1167
function loadNodeModuleFromDirectoryWorker ( extensions : Extensions , candidate : string , onlyRecordFailures : boolean , state : ModuleResolutionState , jsonContent : PackageJsonPathFields | undefined , versionPaths : VersionPaths | undefined ) : PathAndExtension | undefined {
1159
- const packageFile = jsonContent && ( extensions !== Extensions . JavaScript && extensions !== Extensions . Json
1160
- ? readPackageJsonTypesFields ( jsonContent , candidate , state ) ||
1161
- // When resolving typescript modules, try resolving using main field as well
1162
- ( extensions === Extensions . TypeScript ? readPackageJsonMainField ( jsonContent , candidate , state ) : undefined )
1163
- : readPackageJsonMainField ( jsonContent , candidate , state ) ) ;
1168
+ let packageFile : string | undefined ;
1169
+ if ( jsonContent ) {
1170
+ switch ( extensions ) {
1171
+ case Extensions . JavaScript :
1172
+ case Extensions . Json :
1173
+ packageFile = readPackageJsonMainField ( jsonContent , candidate , state ) ;
1174
+ break ;
1175
+ case Extensions . TypeScript :
1176
+ // When resolving typescript modules, try resolving using main field as well
1177
+ packageFile = readPackageJsonTypesFields ( jsonContent , candidate , state ) || readPackageJsonMainField ( jsonContent , candidate , state ) ;
1178
+ break ;
1179
+ case Extensions . DtsOnly :
1180
+ packageFile = readPackageJsonTypesFields ( jsonContent , candidate , state ) ;
1181
+ break ;
1182
+ case Extensions . TSConfig :
1183
+ packageFile = readPackageJsonTSConfigField ( jsonContent , candidate , state ) ;
1184
+ break ;
1185
+ default :
1186
+ return Debug . assertNever ( extensions ) ;
1187
+ }
1188
+ }
1164
1189
1165
1190
const loader : ResolutionKindSpecificLoader = ( extensions , candidate , onlyRecordFailures , state ) => {
1166
1191
const fromFile = tryFile ( candidate , onlyRecordFailures , state ) ;
@@ -1182,7 +1207,7 @@ namespace ts {
1182
1207
1183
1208
const onlyRecordFailuresForPackageFile = packageFile ? ! directoryProbablyExists ( getDirectoryPath ( packageFile ) , state . host ) : undefined ;
1184
1209
const onlyRecordFailuresForIndex = onlyRecordFailures || ! directoryProbablyExists ( candidate , state . host ) ;
1185
- const indexPath = combinePaths ( candidate , "index" ) ;
1210
+ const indexPath = combinePaths ( candidate , extensions === Extensions . TSConfig ? "tsconfig" : "index" ) ;
1186
1211
1187
1212
if ( versionPaths && ( ! packageFile || containsPath ( candidate , packageFile ) ) ) {
1188
1213
const moduleName = getRelativePathFromDirectory ( candidate , packageFile || indexPath , /*ignoreCase*/ false ) ;
@@ -1213,6 +1238,7 @@ namespace ts {
1213
1238
switch ( extensions ) {
1214
1239
case Extensions . JavaScript :
1215
1240
return extension === Extension . Js || extension === Extension . Jsx ;
1241
+ case Extensions . TSConfig :
1216
1242
case Extensions . Json :
1217
1243
return extension === Extension . Json ;
1218
1244
case Extensions . TypeScript :
@@ -1264,7 +1290,7 @@ namespace ts {
1264
1290
if ( packageResult ) {
1265
1291
return packageResult ;
1266
1292
}
1267
- if ( extensions !== Extensions . JavaScript && extensions !== Extensions . Json ) {
1293
+ if ( extensions === Extensions . TypeScript || extensions === Extensions . DtsOnly ) {
1268
1294
const nodeModulesAtTypes = combinePaths ( nodeModulesFolder , "@types" ) ;
1269
1295
let nodeModulesAtTypesExists = nodeModulesFolderExists ;
1270
1296
if ( nodeModulesFolderExists && ! directoryProbablyExists ( nodeModulesAtTypes , state . host ) ) {
0 commit comments