Skip to content

Commit

Permalink
Fix isConditionalExports and run update script
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Oct 5, 2024
1 parent d5e7073 commit 3b31508
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 16 additions & 31 deletions scripts/utils/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,45 +337,25 @@ function gitHubTgzUrl(user, project, sha) {
}

function isConditionalExports(entryExports) {
let hasKeys = false
if (isObjectObject(entryExports)) {
const keys = Object.getOwnPropertyNames(entryExports)
const { length } = keys
hasKeys = length > 0
for (let i = 0; i < length; i += 1) {
// Conditional entry exports do NOT contain keys starting with '.'.
// Entry exports cannot contain some keys starting with '.' and some not.
// The exports object MUST either be an object of package subpath keys OR
// an object of main entry condition name keys only.
if (keys[i].charCodeAt(0) === 46 /*'.'*/) {
return false
}
}
}
return hasKeys || isConditionalExportsMainSugar(entryExports)
}

// Based on Node's internal helper.
// https://github.com/nodejs/node/blob/v22.9.0/lib/internal/modules/esm/resolve.js#L537
function isConditionalExportsMainSugar(entryExports) {
if (typeof entryExports === 'string' || Array.isArray(entryExports)) {
return true
}
if (!isObject(entryExports)) {
if (!isObjectObject(entryExports)) {
return false
}
const keys = Object.getOwnPropertyNames(entryExports)
const { length } = keys
if (!length) {
return false
}
let result = keys[0] === '' || keys[0].charCodeAt(0) === 46 /*'.'*/
for (let i = 1; i < length; i += 1) {
if (result !== (keys[i] === '' || keys[i].charCodeAt(0) === 46) /*'.'*/) {
// Conditional entry exports do NOT contain keys starting with '.'.
// Entry exports cannot contain some keys starting with '.' and some not.
// The exports object MUST either be an object of package subpath keys OR
// an object of main entry condition name keys only.
for (let i = 0; i < length; i += 1) {
const key = keys[i]
if (key.length > 0 && key.charCodeAt(0) === 46 /*'.'*/) {
return false
}
}
return result
return true
}

function isSubpathExports(entryExports) {
Expand Down Expand Up @@ -523,10 +503,15 @@ function resolvePackageJsonDirname(filepath) {
}

function resolvePackageJsonEntryExports(entryExports) {
if (isConditionalExportsMainSugar(entryExports)) {
// If conditional exports main sugar
// https://nodejs.org/api/packages.html#exports-sugar
if (typeof entryExports === 'string' || Array.isArray(entryExports)) {
return { '.': entryExports }
}
return isObjectObject(entryExports) ? entryExports : undefined
if (isConditionalExports(entryExports)) {
return entryExports
}
return isObject(entryExports) ? entryExports : undefined
}

function resolvePackageJsonPath(filepath) {
Expand Down

0 comments on commit 3b31508

Please sign in to comment.