From 2fe50e6a8319b6a7d3f98bbaef003d0d7605d8d6 Mon Sep 17 00:00:00 2001 From: Chunpeng Huo Date: Tue, 24 Dec 2024 11:31:44 +1000 Subject: [PATCH] fix: some npm package uses array in package.json exports condition --- lib/package-reader.js | 9 ++++++++- package.json | 2 +- test/package-reader.spec.js | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/package-reader.js b/lib/package-reader.js index 9712adb..9ce91e9 100644 --- a/lib/package-reader.js +++ b/lib/package-reader.js @@ -398,13 +398,20 @@ function pickCondition(obj) { let env = process.env.NODE_ENV || ''; if (env === 'undefined') env = ''; + if (Array.isArray(obj)) { + for (const a of obj) { + const result = pickCondition(a); + if (result) return result; + } + } + // use env (NODE_ENV) to support "development" and "production" for (const condition of ['import', 'module', 'browser', 'require', env, 'default']) { // Recursive call to support nested conditions. if (condition && condition in obj) return pickCondition(obj[condition]); } - return null; + throw new Error("Unexpected exports condition: " + JSON.stringify(obj)); } function _exportsMain(exports) { diff --git a/package.json b/package.json index 113988d..12608a7 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "dumber-module-loader": "^1.2.5", "eslint-scope": "^8.0.2", "events": "^3.3.0", - "fs-browser-stub": "^1.0.1", + "fs-browser-stub": "^1.1.1", "https-browserify": "^1.0.0", "meriyah": "^6.0.1", "modify-code": "^2.1.4", diff --git a/test/package-reader.spec.js b/test/package-reader.spec.js index da1c294..3f1bf0f 100644 --- a/test/package-reader.spec.js +++ b/test/package-reader.spec.js @@ -1723,7 +1723,7 @@ test('packageReader reads exports subpaths in package.json', async t => { ".": "./index.js", "./package.json": "./package.json", "./a": "./a.js", - "./b": "./be.js", + "./b": ["./be.js"], "./c": {"import": "./lib/c.js"}, "./d/*": "./lib/d/*.js", "./e": null