@@ -50,6 +50,21 @@ interface EntryPointPackageJson {
5050 typings ?: string ; // TypeScript .d.ts files
5151}
5252
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+
5368/**
5469 * Try to get entry point info from the given path.
5570 * @param pkgPath the absolute path to the containing npm package
@@ -62,6 +77,11 @@ export function getEntryPointInfo(pkgPath: string, entryPoint: string): EntryPoi
6277 return null ;
6378 }
6479
80+ const entryPointPackageJson = loadEntryPointPackage ( packageJsonPath ) ;
81+ if ( ! entryPointPackageJson ) {
82+ return null ;
83+ }
84+
6585 // If there is `esm2015` then `es2015` will be FESM2015, otherwise ESM2015.
6686 // If there is `esm5` then `module` will be FESM5, otherwise it will be ESM5.
6787 const {
@@ -75,8 +95,7 @@ export function getEntryPointInfo(pkgPath: string, entryPoint: string): EntryPoi
7595 esm2015,
7696 esm5,
7797 main
78- } : EntryPointPackageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) ) ;
79-
98+ } = entryPointPackageJson ;
8099 // Minimum requirement is that we have typings and one of esm2015 or fesm2015 formats.
81100 if ( ! typings || ! ( fesm2015 || esm2015 ) ) {
82101 return null ;
0 commit comments