Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: give node export condition higher priority #2134

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions config/scripts/validate-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function validatePackageExports() {
console.log('✅ Validated package.json exports')
}

function validateExportConditions(pointer, conditions) {
function validateExportConditions(pointer, conditions, level = 0) {
if (typeof conditions === 'string') {
invariant(
fs.existsSync(conditions),
Expand All @@ -103,7 +103,7 @@ function validateExportConditions(pointer, conditions) {

const keys = Object.keys(conditions)

if (conditions[keys[0]] !== null) {
if (level == 0 && conditions[keys[0]] !== null) {
invariant(keys[0] === 'types', 'FS')
}

Expand All @@ -115,6 +115,15 @@ function validateExportConditions(pointer, conditions) {
return
}

if (typeof relativeExportPath === 'object') {
validateExportConditions(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch! Thank you for spotting this. We didn't have any support for nested export conditions. This is nice ❇️

`${pointer}.${key}`,
relativeExportPath,
level + 1,
)
return
}

const exportPath = fromRoot(relativeExportPath)
invariant(
fs.existsSync(exportPath),
Expand Down
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,34 @@
"default": "./lib/core/index.js"
},
"./browser": {
"node": null,
"types": "./lib/browser/index.d.ts",
"browser": {
"require": "./lib/browser/index.js",
"import": "./lib/browser/index.mjs"
},
"node": null,
"require": "./lib/browser/index.js",
"import": "./lib/browser/index.mjs",
"default": "./lib/browser/index.js"
},
"./node": {
"browser": null,
"types": "./lib/node/index.d.ts",
"node": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do the same for /native export too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, a reverse fix for /browser? Adding a browser field with a higher priority. Folks experience the reverse problem as well for msw/browser.

"require": "./lib/node/index.js",
"import": "./lib/node/index.mjs"
},
"browser": null,
"require": "./lib/node/index.js",
"import": "./lib/node/index.mjs",
"default": "./lib/node/index.mjs"
},
"./native": {
"browser": null,
"types": "./lib/native/index.d.ts",
"react-native": {
"require": "./lib/native/index.js",
"import": "./lib/native/index.mjs"
},
"browser": null,
"require": "./lib/native/index.js",
"import": "./lib/native/index.mjs",
"default": "./lib/native/index.js"
Expand Down
Loading