11import { exports } from "@es-joy/resolve.exports" ;
2+ import { goTry } from "go-go-try" ;
23import type { NormalizedPackageJson } from "read-pkg" ;
34
45export class PackageTypesResolver {
@@ -15,18 +16,22 @@ export class PackageTypesResolver {
1516 }
1617
1718 getExportsMapTypes ( ) : string | undefined {
18- try {
19- const entries =
20- exports ( this . #pkgJson, this . #subpath, {
21- conditions : [ "!default" , "!node" , "import" , "types" ] ,
22- } ) ?? [ ] ;
23- const entry = entries . at ( 0 ) ;
24- if ( ! entry || ! this . #isTypesFile( entry ) ) return undefined ;
25- return entry ;
26- } catch {
27- // The package may not have an `exports` map.
28- return undefined ;
29- }
19+ // Try to resolve the `exports` map in `package.json`
20+ // with conditions `import` and `types` enabled to find
21+ // a valid TypeScript type definitions file.
22+ const [ err , entries = [ ] ] = goTry ( ( ) =>
23+ exports ( this . #pkgJson, this . #subpath, {
24+ conditions : [ "!default" , "!node" , "import" , "types" ] ,
25+ } ) ,
26+ ) ;
27+
28+ // The package may not have an `exports` map.
29+ if ( err !== undefined ) return undefined ;
30+
31+ // Return first entry, if valid.
32+ const entry = entries . at ( 0 ) ;
33+ if ( ! entry || ! this . #isTypesFile( entry ) ) return undefined ;
34+ return entry ;
3035 }
3136
3237 getTypesOrTypings ( ) : string | undefined {
0 commit comments