-
Notifications
You must be signed in to change notification settings - Fork 130
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
add exports field to package.json #659
Conversation
package.json
Outdated
@@ -8,6 +8,21 @@ | |||
"main": "dist/a11y-dialog.js", | |||
"module": "dist/a11y-dialog.esm.js", | |||
"types": "dist/a11y-dialog.d.ts", | |||
"exports": { | |||
".": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you do import A11yDialog from 'a11y-dialog'
you get ./dist/a11y-dialog.esm.js
, and when you do const A11yDialog = require('a11y-dialog)
you get ./dist/a11y-dialog.js
package.json
Outdated
"dist/*.esm.js": "./dist/*.esm.js", | ||
"dist/*.js": "./dist/*.js", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If someone is importing using an exact extension, give them what they asked for
package.json
Outdated
"dist/*": { | ||
"import": "./dist/*.esm.js", | ||
"require": "./dist/*.js", | ||
"types": "./dist/*.d.ts" | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise if the extension is omitted, use the appropriate extension depending on import or require
"require": "./dist/*.js", | ||
"types": "./dist/*.d.ts" | ||
}, | ||
"package.json": "./package.json" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
general best practice to allow importing your package.json
Thank you so much for the PR and the explanations! Do you think we should do that in v9 or is it safe to ship as is? Is there any import that could potentially break when setting this up? |
@bschlenk Hello! 👋 I’m finally getting back to this PR after so long. Note to myself, this is how I tested it.I followed the
I tried it as is, and this is the error I got (both using
Following the content of the error, I tried updating the configuration to look like this (basically just removing the "exports": {
"import": "./dist/a11y-dialog.esm.js",
"require": "./dist/a11y-dialog.js",
"types": "./dist/a11y-dialog.d.ts",
"dist/*.esm.js": "./dist/*.esm.js",
"dist/*.js": "./dist/*.js",
"dist/*": {
"import": "./dist/*.esm.js",
"require": "./dist/*.js",
"types": "./dist/*.d.ts"
},
"package.json": "./package.json"
}, This did fix the case of using
My understanding is that we should either remove |
👋🏻 Hi! (I work with @bschlenk and he’s out for a while)
Right it’s better to keep
It won’t necessarily be a breaking change! As long as it’s set up correctly. But yes all CommonJS output should be in Whether ESM and CJS lives in the same folder or separate folders is a matter of preference, and doesn’t matter. How we prevent breaking changes is what @bschlenk added in his comment above—you can actually remap exact filepaths to different files in the |
@drwpow-figma Hello and thank you very much for getting back to me with some very valuable information! I issued a few commits to update the pull-request based on the information I understood. Would you mind doing a code review to see if I didn’t mess up anything? It’s mostly important that we don’t cause any breaking change. 😅 |
@@ -29,7 +29,7 @@ <h1 id="my-dialog-title">Dialog title</h1> | |||
</div> | |||
</div> | |||
|
|||
<script src="./a11y-dialog.js"></script> | |||
<script src="./a11y-dialog.cjs"></script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the reason we have to update these local paths because they go strictly through the file system and not at all through the package.json file (therefore ignoring the exports field)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 I’m actually not sure about that one. Whatever gets tests to pass works 🤷🏻 (it’s been a while since I’ve used Cypress, but if it works, it works)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, though, .cjs
files should never be in <script>
tags, because they won’t be supported in browsers. My guess is that Cypress just doesn’t support ESM, or something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this has nothing to do with Cypress per se, it’s just a fixture, so I think I could be using the ESM version here. I’ll try.
@@ -8,6 +8,20 @@ | |||
"main": "dist/a11y-dialog.js", | |||
"module": "dist/a11y-dialog.esm.js", | |||
"types": "dist/a11y-dialog.d.ts", | |||
"exports": { | |||
"import": "./dist/a11y-dialog.esm.js", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh this is actually incorrect. The default exports needs to be under "."
, which is a special reserved character for the default. In other words, we need to rename exports.import
, exports.require
, and exports.types
to exports['.'].import
, exports['.'].require
, and exports['.'].types
respectively.
"import": "./dist/a11y-dialog.esm.js", | |
".": { | |
"import": "./dist/a11y-dialog.esm.js", | |
"require": "./dist/a11y-dialog.cjs", | |
"types": "./dist/a11y-dialog.d.ts", | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned in one of my messages, it fails if we use both "."
and defined paths:
Error [ERR_INVALID_PACKAGE_CONFIG]: Invalid package config /Users/kitty/Sites/a11y-dialog-bschlenk-test/node_modules/a11y-dialog/package.json. "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.
Oh so on closer inspection, renaming UMD files to
Sorry I didn’t catch this earlier; I was still orienting myself to the package. So your question about the Cypress changes was in fact spot-on: those shouldn’t have changed. Let me spend a little time on an improvement for what you’ve done to fix the issues in a backwards-compatible way (this is tricky because I don’t think the tests comprehensively test all the scenarios that could break for people). |
What we can also do if that’s easier is to assume this change cannot safely be done in a minor, and plan it for v9. And therefore come up with the state-of-the-art setup for all cases. |
Opened a PR here: #701 because I don’t know how to open a PR against this one. But you get the gist (you can just cherry-pick the commit; I don’t care about attribution). In my PR, I leave the
Ah right—you know that syntax may be valid too now that I think of it; I’m just far more used to the dot-preceder. I followed that syntax in my PR, but it may just be personal preference (may not matter). |
Closing in favor of #702. Thank you for the super insightful comments here! |
Oh also just FYI: in |
fixes #646
Left some inline comments on what each part is for. Mostly I'm being defensive, so that if any of these import patterns are already in use, they'll still work and this can be a minor version bump.
I'd recommend at some point locking this down to just the
.
and thepackage.json
entries and making that a major version bump.