Read a package's declared entries (see PackageEntries); null when its manifest declares none.
(projectRoot: string, dirRel: string)
| 239 | |
| 240 | /** Read a package's declared entries (see PackageEntries); null when its manifest declares none. */ |
| 241 | function readPackageEntries(projectRoot: string, dirRel: string): PackageEntries | null { |
| 242 | let pkg: Record<string, unknown> | null; |
| 243 | try { |
| 244 | pkg = JSON.parse(fs.readFileSync(path.join(projectRoot, dirRel, 'package.json'), 'utf-8')) as Record<string, unknown> | null; |
| 245 | } catch { |
| 246 | return null; |
| 247 | } |
| 248 | if (!pkg || typeof pkg !== 'object') return null; |
| 249 | const inPackage = (target: string): string | null => packageTarget(projectRoot, dirRel, target); |
| 250 | const out: PackageEntries = {}; |
| 251 | const exp = pkg.exports; |
| 252 | if (exp !== undefined && exp !== null) { |
| 253 | const exports = new Map<string, string[]>(); |
| 254 | const browser = new Map<string, string[]>(); |
| 255 | const subpaths = typeof exp === 'object' && !Array.isArray(exp) && Object.keys(exp).some((key) => key.startsWith('.')); |
| 256 | const entries: Array<[string, unknown]> = subpaths |
| 257 | ? Object.entries(exp as Record<string, unknown>).filter(([key]) => key.startsWith('.')) |
| 258 | : [['.', exp]]; |
| 259 | for (const [key, value] of entries) { |
| 260 | exports.set(key, [...new Set(exportTargets(value).map(inPackage).filter((t): t is string => t !== null))]); |
| 261 | const forBrowser = browserTargets(value).map(inPackage).filter((t): t is string => t !== null); |
| 262 | if (forBrowser.length > 0) browser.set(key, forBrowser); |
| 263 | } |
| 264 | out.exports = exports; |
| 265 | if (browser.size > 0) out.browser = browser; |
| 266 | } |
| 267 | const main = MAIN_FIELDS |
| 268 | .map((field) => pkg![field]) |
| 269 | .filter((value): value is string => typeof value === 'string') |
| 270 | .map(inPackage) |
| 271 | .filter((t): t is string => t !== null); |
| 272 | if (main.length > 0) out.main = [...new Set(main)]; |
| 273 | return out.exports || out.main ? out : null; |
| 274 | } |
| 275 | |
| 276 | /** A package-relative target (`./src/index.ts`) → projectRoot-relative posix, or null if it leaves the package. */ |
| 277 | function packageTarget(projectRoot: string, dirRel: string, target: string): string | null { |
no test coverage detected