@@ -50,6 +50,21 @@ interface EntryPointPackageJson {
50
50
typings ?: string ; // TypeScript .d.ts files
51
51
}
52
52
53
+ /**
54
+ * Parses the JSON from a package.json file.
55
+ * @param packageJsonPath the absolute path to the package.json file.
56
+ * @returns JSON from the package.json file if it is valid, `null` otherwise.
57
+ */
58
+ function loadEntryPointPackage ( packageJsonPath : string ) : { [ key : string ] : any } | null {
59
+ try {
60
+ return JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) ) ;
61
+ } catch ( e ) {
62
+ // We may have run into a package.json with unexpected symbols
63
+ console . warn ( `Failed to read entry point info from ${ packageJsonPath } with error ${ e } .` ) ;
64
+ return null ;
65
+ }
66
+ }
67
+
53
68
/**
54
69
* Try to get entry point info from the given path.
55
70
* @param pkgPath the absolute path to the containing npm package
@@ -62,6 +77,11 @@ export function getEntryPointInfo(pkgPath: string, entryPoint: string): EntryPoi
62
77
return null ;
63
78
}
64
79
80
+ const entryPointPackageJson = loadEntryPointPackage ( packageJsonPath ) ;
81
+ if ( ! entryPointPackageJson ) {
82
+ return null ;
83
+ }
84
+
65
85
// If there is `esm2015` then `es2015` will be FESM2015, otherwise ESM2015.
66
86
// If there is `esm5` then `module` will be FESM5, otherwise it will be ESM5.
67
87
const {
@@ -75,8 +95,7 @@ export function getEntryPointInfo(pkgPath: string, entryPoint: string): EntryPoi
75
95
esm2015,
76
96
esm5,
77
97
main
78
- } : EntryPointPackageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) ) ;
79
-
98
+ } = entryPointPackageJson ;
80
99
// Minimum requirement is that we have typings and one of esm2015 or fesm2015 formats.
81
100
if ( ! typings || ! ( fesm2015 || esm2015 ) ) {
82
101
return null ;
0 commit comments