@@ -131,7 +131,7 @@ namespace ts {
131
131
resultFromCache ?: ResolvedModuleWithFailedLookupLocations ;
132
132
packageJsonInfoCache : PackageJsonInfoCache | undefined ;
133
133
features : NodeResolutionFeatures ;
134
- conditions : string [ ] ;
134
+ conditions : readonly string [ ] ;
135
135
requestContainingDirectory : string | undefined ;
136
136
reportResolutionDiagnostic : ( d : ResolutionDiagnostic ) => void ;
137
137
}
@@ -506,18 +506,7 @@ namespace ts {
506
506
host : ModuleResolutionHost ,
507
507
cache : ModuleResolutionCache | undefined ,
508
508
) : PackageJsonInfo | undefined {
509
- const moduleResolutionState : ModuleResolutionState = {
510
- compilerOptions : options ,
511
- host,
512
- traceEnabled : isTraceEnabled ( options , host ) ,
513
- failedLookupLocations : [ ] ,
514
- affectingLocations : [ ] ,
515
- packageJsonInfoCache : cache ?. getPackageJsonInfoCache ( ) ,
516
- conditions : emptyArray ,
517
- features : NodeResolutionFeatures . None ,
518
- requestContainingDirectory : containingDirectory ,
519
- reportResolutionDiagnostic : noop
520
- } ;
509
+ const moduleResolutionState = getTemporaryModuleResolutionState ( cache ?. getPackageJsonInfoCache ( ) , host , options ) ;
521
510
522
511
return forEachAncestorDirectory ( containingDirectory , ancestorDirectory => {
523
512
if ( getBaseFileName ( ancestorDirectory ) !== "node_modules" ) {
@@ -1440,8 +1429,8 @@ namespace ts {
1440
1429
}
1441
1430
1442
1431
function node16ModuleNameResolver ( moduleName : string , containingFile : string , compilerOptions : CompilerOptions ,
1443
- host : ModuleResolutionHost , cache ?: ModuleResolutionCache , redirectedReference ?: ResolvedProjectReference ,
1444
- resolutionMode ?: ResolutionMode ) : ResolvedModuleWithFailedLookupLocations {
1432
+ host : ModuleResolutionHost , cache ?: ModuleResolutionCache , redirectedReference ?: ResolvedProjectReference ,
1433
+ resolutionMode ?: ResolutionMode ) : ResolvedModuleWithFailedLookupLocations {
1445
1434
return nodeNextModuleNameResolverWorker (
1446
1435
NodeResolutionFeatures . Node16Default ,
1447
1436
moduleName ,
@@ -1455,8 +1444,8 @@ namespace ts {
1455
1444
}
1456
1445
1457
1446
function nodeNextModuleNameResolver ( moduleName : string , containingFile : string , compilerOptions : CompilerOptions ,
1458
- host : ModuleResolutionHost , cache ?: ModuleResolutionCache , redirectedReference ?: ResolvedProjectReference ,
1459
- resolutionMode ?: ResolutionMode ) : ResolvedModuleWithFailedLookupLocations {
1447
+ host : ModuleResolutionHost , cache ?: ModuleResolutionCache , redirectedReference ?: ResolvedProjectReference ,
1448
+ resolutionMode ?: ResolutionMode ) : ResolvedModuleWithFailedLookupLocations {
1460
1449
return nodeNextModuleNameResolverWorker (
1461
1450
NodeResolutionFeatures . NodeNextDefault ,
1462
1451
moduleName ,
@@ -1861,18 +1850,9 @@ namespace ts {
1861
1850
let entrypoints : string [ ] | undefined ;
1862
1851
const extensions = resolveJs ? Extensions . JavaScript : Extensions . TypeScript ;
1863
1852
const features = getDefaultNodeResolutionFeatures ( options ) ;
1864
- const requireState : ModuleResolutionState = {
1865
- compilerOptions : options ,
1866
- host,
1867
- traceEnabled : isTraceEnabled ( options , host ) ,
1868
- failedLookupLocations : [ ] ,
1869
- affectingLocations : [ ] ,
1870
- packageJsonInfoCache : cache ?. getPackageJsonInfoCache ( ) ,
1871
- conditions : [ "node" , "require" , "types" ] ,
1872
- features,
1873
- requestContainingDirectory : packageJsonInfo . packageDirectory ,
1874
- reportResolutionDiagnostic : noop
1875
- } ;
1853
+ const requireState = getTemporaryModuleResolutionState ( cache ?. getPackageJsonInfoCache ( ) , host , options ) ;
1854
+ requireState . conditions = [ "node" , "require" , "types" ] ;
1855
+ requireState . requestContainingDirectory = packageJsonInfo . packageDirectory ;
1876
1856
const requireResolution = loadNodeModuleFromDirectoryWorker (
1877
1857
extensions ,
1878
1858
packageJsonInfo . packageDirectory ,
@@ -1958,6 +1938,22 @@ namespace ts {
1958
1938
}
1959
1939
}
1960
1940
1941
+ /*@internal */
1942
+ export function getTemporaryModuleResolutionState ( packageJsonInfoCache : PackageJsonInfoCache | undefined , host : ModuleResolutionHost , options : CompilerOptions ) : ModuleResolutionState {
1943
+ return {
1944
+ host,
1945
+ compilerOptions : options ,
1946
+ traceEnabled : isTraceEnabled ( options , host ) ,
1947
+ failedLookupLocations : noopPush ,
1948
+ affectingLocations : noopPush ,
1949
+ packageJsonInfoCache,
1950
+ features : NodeResolutionFeatures . None ,
1951
+ conditions : emptyArray ,
1952
+ requestContainingDirectory : undefined ,
1953
+ reportResolutionDiagnostic : noop
1954
+ } ;
1955
+ }
1956
+
1961
1957
/*@internal */
1962
1958
interface PackageJsonInfo {
1963
1959
packageDirectory : string ;
@@ -1972,31 +1968,7 @@ namespace ts {
1972
1968
* A function for locating the package.json scope for a given path
1973
1969
*/
1974
1970
/*@internal */
1975
- export function getPackageScopeForPath ( fileName : Path , packageJsonInfoCache : PackageJsonInfoCache | undefined , host : ModuleResolutionHost , options : CompilerOptions ) : PackageJsonInfo | undefined {
1976
- const state : {
1977
- host : ModuleResolutionHost ;
1978
- compilerOptions : CompilerOptions ;
1979
- traceEnabled : boolean ;
1980
- failedLookupLocations : Push < string > ;
1981
- affectingLocations : Push < string > ;
1982
- resultFromCache ?: ResolvedModuleWithFailedLookupLocations ;
1983
- packageJsonInfoCache : PackageJsonInfoCache | undefined ;
1984
- features : number ;
1985
- conditions : never [ ] ;
1986
- requestContainingDirectory : string | undefined ;
1987
- reportResolutionDiagnostic : ( d : ResolutionDiagnostic ) => void ;
1988
- } = {
1989
- host,
1990
- compilerOptions : options ,
1991
- traceEnabled : isTraceEnabled ( options , host ) ,
1992
- failedLookupLocations : [ ] ,
1993
- affectingLocations : [ ] ,
1994
- packageJsonInfoCache,
1995
- features : 0 ,
1996
- conditions : [ ] ,
1997
- requestContainingDirectory : undefined ,
1998
- reportResolutionDiagnostic : noop
1999
- } ;
1971
+ export function getPackageScopeForPath ( fileName : Path , state : ModuleResolutionState ) : PackageJsonInfo | undefined {
2000
1972
const parts = getPathComponents ( fileName ) ;
2001
1973
parts . pop ( ) ;
2002
1974
while ( parts . length > 0 ) {
@@ -2175,7 +2147,7 @@ namespace ts {
2175
2147
function loadModuleFromSelfNameReference ( extensions : Extensions , moduleName : string , directory : string , state : ModuleResolutionState , cache : ModuleResolutionCache | undefined , redirectedReference : ResolvedProjectReference | undefined ) : SearchResult < Resolved > {
2176
2148
const useCaseSensitiveFileNames = typeof state . host . useCaseSensitiveFileNames === "function" ? state . host . useCaseSensitiveFileNames ( ) : state . host . useCaseSensitiveFileNames ;
2177
2149
const directoryPath = toPath ( combinePaths ( directory , "dummy" ) , state . host . getCurrentDirectory ?.( ) , createGetCanonicalFileName ( useCaseSensitiveFileNames === undefined ? true : useCaseSensitiveFileNames ) ) ;
2178
- const scope = getPackageScopeForPath ( directoryPath , state . packageJsonInfoCache , state . host , state . compilerOptions ) ;
2150
+ const scope = getPackageScopeForPath ( directoryPath , state ) ;
2179
2151
if ( ! scope || ! scope . packageJsonContent . exports ) {
2180
2152
return undefined ;
2181
2153
}
@@ -2237,7 +2209,7 @@ namespace ts {
2237
2209
}
2238
2210
const useCaseSensitiveFileNames = typeof state . host . useCaseSensitiveFileNames === "function" ? state . host . useCaseSensitiveFileNames ( ) : state . host . useCaseSensitiveFileNames ;
2239
2211
const directoryPath = toPath ( combinePaths ( directory , "dummy" ) , state . host . getCurrentDirectory ?.( ) , createGetCanonicalFileName ( useCaseSensitiveFileNames === undefined ? true : useCaseSensitiveFileNames ) ) ;
2240
- const scope = getPackageScopeForPath ( directoryPath , state . packageJsonInfoCache , state . host , state . compilerOptions ) ;
2212
+ const scope = getPackageScopeForPath ( directoryPath , state ) ;
2241
2213
if ( ! scope ) {
2242
2214
if ( state . traceEnabled ) {
2243
2215
trace ( state . host , Diagnostics . Directory_0_has_no_containing_package_json_scope_Imports_will_not_resolve , directoryPath ) ;
0 commit comments