@@ -108,6 +108,8 @@ import {
108
108
versionMajorMinor ,
109
109
VersionRange ,
110
110
} from "./_namespaces/ts.js" ;
111
+ import { getPnpTypeRoots } from "./pnp.js" ;
112
+ import { getPnpApi } from "./pnpapi.js" ;
111
113
112
114
/** @internal */
113
115
export function trace ( host : ModuleResolutionHost , message : DiagnosticMessage , ...args : any [ ] ) : void {
@@ -493,7 +495,7 @@ export function getEffectiveTypeRoots(options: CompilerOptions, host: GetEffecti
493
495
* Returns the path to every node_modules/@types directory from some ancestor directory.
494
496
* Returns undefined if there are none.
495
497
*/
496
- function getDefaultTypeRoots ( currentDirectory : string ) : string [ ] | undefined {
498
+ function getNodeModulesTypeRoots ( currentDirectory : string ) {
497
499
let typeRoots : string [ ] | undefined ;
498
500
forEachAncestorDirectory ( normalizePath ( currentDirectory ) , directory => {
499
501
const atTypes = combinePaths ( directory , nodeModulesAtTypes ) ;
@@ -508,6 +510,18 @@ function arePathsEqual(path1: string, path2: string, host: ModuleResolutionHost)
508
510
return comparePaths ( path1 , path2 , ! useCaseSensitiveFileNames ) === Comparison . EqualTo ;
509
511
}
510
512
513
+ function getDefaultTypeRoots ( currentDirectory : string ) : string [ ] | undefined {
514
+ const nmTypes = getNodeModulesTypeRoots ( currentDirectory ) ;
515
+ const pnpTypes = getPnpTypeRoots ( currentDirectory ) ;
516
+
517
+ if ( nmTypes ?. length ) {
518
+ return [ ...nmTypes , ...pnpTypes ] ;
519
+ }
520
+ else if ( pnpTypes . length ) {
521
+ return pnpTypes ;
522
+ }
523
+ }
524
+
511
525
function getOriginalAndResolvedFileName ( fileName : string , host : ModuleResolutionHost , traceEnabled : boolean ) {
512
526
const resolvedFileName = realPath ( fileName , host , traceEnabled ) ;
513
527
const pathsAreEqual = arePathsEqual ( fileName , resolvedFileName , host ) ;
@@ -788,6 +802,18 @@ export function resolvePackageNameToPackageJson(
788
802
) : PackageJsonInfo | undefined {
789
803
const moduleResolutionState = getTemporaryModuleResolutionState ( cache ?. getPackageJsonInfoCache ( ) , host , options ) ;
790
804
805
+ const pnpapi = getPnpApi ( containingDirectory ) ;
806
+ if ( pnpapi ) {
807
+ try {
808
+ const resolution = pnpapi . resolveToUnqualified ( packageName , `${ containingDirectory } /` , { considerBuiltins : false } ) ;
809
+ const candidate = normalizeSlashes ( resolution ) . replace ( / \/ $ / , "" ) ;
810
+ return getPackageJsonInfo ( candidate , /*onlyRecordFailures*/ false , moduleResolutionState ) ;
811
+ }
812
+ catch {
813
+ return ;
814
+ }
815
+ }
816
+
791
817
return forEachAncestorDirectory ( containingDirectory , ancestorDirectory => {
792
818
if ( getBaseFileName ( ancestorDirectory ) !== "node_modules" ) {
793
819
const nodeModulesFolder = combinePaths ( ancestorDirectory , "node_modules" ) ;
@@ -3002,7 +3028,16 @@ function loadModuleFromNearestNodeModulesDirectoryWorker(extensions: Extensions,
3002
3028
}
3003
3029
3004
3030
function lookup ( extensions : Extensions ) {
3005
- return forEachAncestorDirectory ( normalizeSlashes ( directory ) , ancestorDirectory => {
3031
+ const issuer = normalizeSlashes ( directory ) ;
3032
+ if ( getPnpApi ( issuer ) ) {
3033
+ const resolutionFromCache = tryFindNonRelativeModuleNameInCache ( cache , moduleName , mode , issuer , redirectedReference , state ) ;
3034
+ if ( resolutionFromCache ) {
3035
+ return resolutionFromCache ;
3036
+ }
3037
+ return toSearchResult ( loadModuleFromImmediateNodeModulesDirectoryPnP ( extensions , moduleName , issuer , state , typesScopeOnly , cache , redirectedReference ) ) ;
3038
+ }
3039
+
3040
+ return forEachAncestorDirectory ( issuer , ancestorDirectory => {
3006
3041
if ( getBaseFileName ( ancestorDirectory ) !== "node_modules" ) {
3007
3042
const resolutionFromCache = tryFindNonRelativeModuleNameInCache ( cache , moduleName , mode , ancestorDirectory , redirectedReference , state ) ;
3008
3043
if ( resolutionFromCache ) {
@@ -3041,11 +3076,34 @@ function loadModuleFromImmediateNodeModulesDirectory(extensions: Extensions, mod
3041
3076
}
3042
3077
}
3043
3078
3079
+ function loadModuleFromImmediateNodeModulesDirectoryPnP ( extensions : Extensions , moduleName : string , directory : string , state : ModuleResolutionState , typesScopeOnly : boolean , cache : ModuleResolutionCache | undefined , redirectedReference : ResolvedProjectReference | undefined ) : Resolved | undefined {
3080
+ const issuer = normalizeSlashes ( directory ) ;
3081
+
3082
+ if ( ! typesScopeOnly ) {
3083
+ const packageResult = tryLoadModuleUsingPnpResolution ( extensions , moduleName , issuer , state , cache , redirectedReference ) ;
3084
+ if ( packageResult ) {
3085
+ return packageResult ;
3086
+ }
3087
+ }
3088
+
3089
+ if ( extensions & Extensions . Declaration ) {
3090
+ return tryLoadModuleUsingPnpResolution ( Extensions . Declaration , `@types/${ mangleScopedPackageNameWithTrace ( moduleName , state ) } ` , issuer , state , cache , redirectedReference ) ;
3091
+ }
3092
+ }
3093
+
3044
3094
function loadModuleFromSpecificNodeModulesDirectory ( extensions : Extensions , moduleName : string , nodeModulesDirectory : string , nodeModulesDirectoryExists : boolean , state : ModuleResolutionState , cache : ModuleResolutionCache | undefined , redirectedReference : ResolvedProjectReference | undefined ) : Resolved | undefined {
3045
3095
const candidate = normalizePath ( combinePaths ( nodeModulesDirectory , moduleName ) ) ;
3046
3096
const { packageName, rest } = parsePackageName ( moduleName ) ;
3047
3097
const packageDirectory = combinePaths ( nodeModulesDirectory , packageName ) ;
3098
+ return loadModuleFromSpecificNodeModulesDirectoryImpl ( extensions , nodeModulesDirectoryExists , state , cache , redirectedReference , candidate , rest , packageDirectory ) ;
3099
+ }
3100
+
3101
+ function loadModuleFromPnpResolution ( extensions : Extensions , packageDirectory : string , rest : string , state : ModuleResolutionState , cache : ModuleResolutionCache | undefined , redirectedReference : ResolvedProjectReference | undefined ) : Resolved | undefined {
3102
+ const candidate = normalizePath ( combinePaths ( packageDirectory , rest ) ) ;
3103
+ return loadModuleFromSpecificNodeModulesDirectoryImpl ( extensions , /*nodeModulesDirectoryExists*/ true , state , cache , redirectedReference , candidate , rest , packageDirectory ) ;
3104
+ }
3048
3105
3106
+ function loadModuleFromSpecificNodeModulesDirectoryImpl ( extensions : Extensions , nodeModulesDirectoryExists : boolean , state : ModuleResolutionState , cache : ModuleResolutionCache | undefined , redirectedReference : ResolvedProjectReference | undefined , candidate : string , rest : string , packageDirectory : string ) : Resolved | undefined {
3049
3107
let rootPackageInfo : PackageJsonInfo | undefined ;
3050
3108
// First look for a nested package.json, as in `node_modules/foo/bar/package.json`.
3051
3109
let packageInfo = getPackageJsonInfo ( candidate , ! nodeModulesDirectoryExists , state ) ;
@@ -3377,3 +3435,22 @@ function useCaseSensitiveFileNames(state: ModuleResolutionState) {
3377
3435
typeof state . host . useCaseSensitiveFileNames === "boolean" ? state . host . useCaseSensitiveFileNames :
3378
3436
state . host . useCaseSensitiveFileNames ( ) ;
3379
3437
}
3438
+
3439
+ function loadPnpPackageResolution ( packageName : string , containingDirectory : string ) {
3440
+ try {
3441
+ const resolution = getPnpApi ( containingDirectory ) . resolveToUnqualified ( packageName , `${ containingDirectory } /` , { considerBuiltins : false } ) ;
3442
+ return normalizeSlashes ( resolution ) . replace ( / \/ $ / , "" ) ;
3443
+ }
3444
+ catch {
3445
+ // Nothing to do
3446
+ }
3447
+ }
3448
+
3449
+ function tryLoadModuleUsingPnpResolution ( extensions : Extensions , moduleName : string , containingDirectory : string , state : ModuleResolutionState , cache : ModuleResolutionCache | undefined , redirectedReference : ResolvedProjectReference | undefined ) {
3450
+ const { packageName, rest } = parsePackageName ( moduleName ) ;
3451
+
3452
+ const packageResolution = loadPnpPackageResolution ( packageName , containingDirectory ) ;
3453
+ return packageResolution
3454
+ ? loadModuleFromPnpResolution ( extensions , packageResolution , rest , state , cache , redirectedReference )
3455
+ : undefined ;
3456
+ }
0 commit comments