@@ -117,7 +117,7 @@ namespace ts {
117
117
resultFromCache ?: ResolvedModuleWithFailedLookupLocations ;
118
118
packageJsonInfoCache : PackageJsonInfoCache | undefined ;
119
119
features : NodeResolutionFeatures ;
120
- conditions : string [ ] ;
120
+ conditions : readonly string [ ] ;
121
121
requestContainingDirectory : string | undefined ;
122
122
reportDiagnostic : DiagnosticReporter ;
123
123
}
@@ -486,18 +486,7 @@ namespace ts {
486
486
host : ModuleResolutionHost ,
487
487
cache : ModuleResolutionCache | undefined ,
488
488
) : PackageJsonInfo | undefined {
489
- const moduleResolutionState : ModuleResolutionState = {
490
- compilerOptions : options ,
491
- host,
492
- traceEnabled : isTraceEnabled ( options , host ) ,
493
- failedLookupLocations : [ ] ,
494
- affectingLocations : [ ] ,
495
- packageJsonInfoCache : cache ?. getPackageJsonInfoCache ( ) ,
496
- conditions : emptyArray ,
497
- features : NodeResolutionFeatures . None ,
498
- requestContainingDirectory : containingDirectory ,
499
- reportDiagnostic : noop
500
- } ;
489
+ const moduleResolutionState = getTemporaryModuleResolutionState ( cache ?. getPackageJsonInfoCache ( ) , host , options ) ;
501
490
502
491
return forEachAncestorDirectory ( containingDirectory , ancestorDirectory => {
503
492
if ( getBaseFileName ( ancestorDirectory ) !== "node_modules" ) {
@@ -1692,18 +1681,9 @@ namespace ts {
1692
1681
let entrypoints : string [ ] | undefined ;
1693
1682
const extensions = resolveJs ? Extensions . JavaScript : Extensions . TypeScript ;
1694
1683
const features = getDefaultNodeResolutionFeatures ( options ) ;
1695
- const requireState : ModuleResolutionState = {
1696
- compilerOptions : options ,
1697
- host,
1698
- traceEnabled : isTraceEnabled ( options , host ) ,
1699
- failedLookupLocations : [ ] ,
1700
- affectingLocations : [ ] ,
1701
- packageJsonInfoCache : cache ?. getPackageJsonInfoCache ( ) ,
1702
- conditions : [ "node" , "require" , "types" ] ,
1703
- features,
1704
- requestContainingDirectory : packageJsonInfo . packageDirectory ,
1705
- reportDiagnostic : noop
1706
- } ;
1684
+ const requireState = getTemporaryModuleResolutionState ( cache ?. getPackageJsonInfoCache ( ) , host , options ) ;
1685
+ requireState . conditions = [ "node" , "require" , "types" ] ;
1686
+ requireState . requestContainingDirectory = packageJsonInfo . packageDirectory ;
1707
1687
const requireResolution = loadNodeModuleFromDirectoryWorker (
1708
1688
extensions ,
1709
1689
packageJsonInfo . packageDirectory ,
@@ -1789,6 +1769,22 @@ namespace ts {
1789
1769
}
1790
1770
}
1791
1771
1772
+ /*@internal */
1773
+ export function getTemporaryModuleResolutionState ( packageJsonInfoCache : PackageJsonInfoCache | undefined , host : ModuleResolutionHost , options : CompilerOptions ) : ModuleResolutionState {
1774
+ return {
1775
+ host,
1776
+ compilerOptions : options ,
1777
+ traceEnabled : isTraceEnabled ( options , host ) ,
1778
+ failedLookupLocations : noopPush ,
1779
+ affectingLocations : noopPush ,
1780
+ packageJsonInfoCache,
1781
+ features : NodeResolutionFeatures . None ,
1782
+ conditions : emptyArray ,
1783
+ requestContainingDirectory : undefined ,
1784
+ reportDiagnostic : noop
1785
+ } ;
1786
+ }
1787
+
1792
1788
/*@internal */
1793
1789
interface PackageJsonInfo {
1794
1790
packageDirectory : string ;
@@ -1802,31 +1798,7 @@ namespace ts {
1802
1798
* A function for locating the package.json scope for a given path
1803
1799
*/
1804
1800
/*@internal */
1805
- export function getPackageScopeForPath ( fileName : Path , packageJsonInfoCache : PackageJsonInfoCache | undefined , host : ModuleResolutionHost , options : CompilerOptions ) : PackageJsonInfo | undefined {
1806
- const state : {
1807
- host : ModuleResolutionHost ;
1808
- compilerOptions : CompilerOptions ;
1809
- traceEnabled : boolean ;
1810
- failedLookupLocations : Push < string > ;
1811
- affectingLocations : Push < string > ;
1812
- resultFromCache ?: ResolvedModuleWithFailedLookupLocations ;
1813
- packageJsonInfoCache : PackageJsonInfoCache | undefined ;
1814
- features : number ;
1815
- conditions : never [ ] ;
1816
- requestContainingDirectory : string | undefined ;
1817
- reportDiagnostic : DiagnosticReporter
1818
- } = {
1819
- host,
1820
- compilerOptions : options ,
1821
- traceEnabled : isTraceEnabled ( options , host ) ,
1822
- failedLookupLocations : [ ] ,
1823
- affectingLocations : [ ] ,
1824
- packageJsonInfoCache,
1825
- features : 0 ,
1826
- conditions : [ ] ,
1827
- requestContainingDirectory : undefined ,
1828
- reportDiagnostic : noop
1829
- } ;
1801
+ export function getPackageScopeForPath ( fileName : Path , state : ModuleResolutionState ) : PackageJsonInfo | undefined {
1830
1802
const parts = getPathComponents ( fileName ) ;
1831
1803
parts . pop ( ) ;
1832
1804
while ( parts . length > 0 ) {
@@ -2004,7 +1976,7 @@ namespace ts {
2004
1976
function loadModuleFromSelfNameReference ( extensions : Extensions , moduleName : string , directory : string , state : ModuleResolutionState , cache : ModuleResolutionCache | undefined , redirectedReference : ResolvedProjectReference | undefined ) : SearchResult < Resolved > {
2005
1977
const useCaseSensitiveFileNames = typeof state . host . useCaseSensitiveFileNames === "function" ? state . host . useCaseSensitiveFileNames ( ) : state . host . useCaseSensitiveFileNames ;
2006
1978
const directoryPath = toPath ( combinePaths ( directory , "dummy" ) , state . host . getCurrentDirectory ?.( ) , createGetCanonicalFileName ( useCaseSensitiveFileNames === undefined ? true : useCaseSensitiveFileNames ) ) ;
2007
- const scope = getPackageScopeForPath ( directoryPath , state . packageJsonInfoCache , state . host , state . compilerOptions ) ;
1979
+ const scope = getPackageScopeForPath ( directoryPath , state ) ;
2008
1980
if ( ! scope || ! scope . packageJsonContent . exports ) {
2009
1981
return undefined ;
2010
1982
}
@@ -2066,7 +2038,7 @@ namespace ts {
2066
2038
}
2067
2039
const useCaseSensitiveFileNames = typeof state . host . useCaseSensitiveFileNames === "function" ? state . host . useCaseSensitiveFileNames ( ) : state . host . useCaseSensitiveFileNames ;
2068
2040
const directoryPath = toPath ( combinePaths ( directory , "dummy" ) , state . host . getCurrentDirectory ?.( ) , createGetCanonicalFileName ( useCaseSensitiveFileNames === undefined ? true : useCaseSensitiveFileNames ) ) ;
2069
- const scope = getPackageScopeForPath ( directoryPath , state . packageJsonInfoCache , state . host , state . compilerOptions ) ;
2041
+ const scope = getPackageScopeForPath ( directoryPath , state ) ;
2070
2042
if ( ! scope ) {
2071
2043
if ( state . traceEnabled ) {
2072
2044
trace ( state . host , Diagnostics . Directory_0_has_no_containing_package_json_scope_Imports_will_not_resolve , directoryPath ) ;
0 commit comments