Skip to content

Commit

Permalink
fix: some npm package uses array in package.json exports condition
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed Dec 24, 2024
1 parent 5387185 commit 2fe50e6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/package-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/package-reader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2fe50e6

Please sign in to comment.