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

Support import.meta.resolve #13

Merged
merged 7 commits into from
Feb 22, 2023
Merged

Support import.meta.resolve #13

merged 7 commits into from
Feb 22, 2023

Conversation

grainstackdev
Copy link
Contributor

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 package grainbox properly for CDN usage.

This exports field is needed for import.meta.resolve to find the file location of a node_modules import.
package.json Outdated
Comment on lines 10 to 12
"exports": {
"import": "./dist/qss.mjs"
},
Copy link

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:

Suggested change
"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"
},

Copy link
Contributor Author

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.

Copy link
Owner

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.

Copy link

@ljharb ljharb Feb 21, 2023

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.

Copy link
Contributor Author

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 a type: 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 a dist/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

Copy link

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.

@lukeed lukeed merged commit e539d02 into lukeed:master Feb 22, 2023
@lukeed
Copy link
Owner

lukeed commented Feb 22, 2023

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"
Copy link

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?

Copy link
Owner

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.

Copy link

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.

Copy link

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 :-)

@grainstackdev
Copy link
Contributor Author

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants