-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Support import.meta.resolve #13
Conversation
This exports field is needed for import.meta.resolve to find the file location of a node_modules import.
package.json
Outdated
"exports": { | ||
"import": "./dist/qss.mjs" | ||
}, |
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.
Note that adding "exports" is a breaking change, and to be maximally backwards compatible you need a string fallback, as well as the "default" condition specified.
Also, as-is, this will break, so here's how you'd need it to look:
"exports": { | |
"import": "./dist/qss.mjs" | |
}, | |
"exports": { | |
".": [ | |
{ | |
"import": "./dist/qss.mjs", | |
"require": "dist/qss.js", | |
"default": "dist/qss.js" | |
}, | |
"dist/qss.js" | |
], | |
"./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.
Whoa, thank you for catching that.
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.
Array exports are the least compatible of all types. Please prepare an object only. As mentioned, this is a breaking change by the mere presence of “exports” alone due to wildly varying resolution support across early Node versions, so not going to step into “exports” territory with the buggiest/least-supported 1st foot.
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.
@lukeed i'm not sure what you mean, every single implementation of the "exports" field i'm aware of supports array fallbacks, and i use it on hundreds of packages with zero reported issues.
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 have added more commits.
Changes:
- Added a
.cjs
file so that require from atype: module
can work. I.E. fixes error: "To treat it as a CommonJS script, rename it to use the '.cjs' file extension." - Now using
.mjs
instead of.m.js
to be consistent. Currently, there is adist/qss.mjs
but I couldn't figure out how it gets generated, so instead, I just renamed the only file I could see,.m.js
to.mjs
. - The exports object is only using features written here: https://nodejs.org/api/packages.html#exports. I couldn't find docs on using arrays, so I don't feel too confident using them.
- The exports object allows direct importing of the full file path, including extension. I think this should make it backwards compatible.
- I tested and found these working:
- importing from a type: module project
- requiring from a type: commonjs project
- direct imports to full path w/ extension
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.
Note that without the array, the engines field should be modified to exclude node 13.0-13.2.
I went with my usual exports mapping & made sure to include the types condition. This will be a 3.0 Thanks~ |
".": { | ||
"types": "./qss.d.ts", | ||
"import": "./dist/qss.mjs", | ||
"require": "./dist/qss.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.
without "default", it won't work in a number of node versions included in your engines declaration. can you add that?
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.
Every node resolver always either has import or require flagged. This is true for all node versions I’m aware of.
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.
It's not true for node 13.3 - 13.6 (node 13.0-13.2 only support the string form, but do support array fallbacks).
Your engines say >= 4
, which explicitly includes these versions.
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.
You can peruse the test cases in https://www.npmjs.com/package/node-exports-info if you like :-)
Thanks!! |
This exports field is needed for import.meta.resolve to find the file location of a node_modules import.
exports: https://nodejs.org/api/packages.html#exports
import.meta.resolve: https://www.npmjs.com/package/import-meta-resolve
I need this in order to get my package
web-imports
to bundle my other packagegrainbox
properly for CDN usage.