-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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: support of imports field #13723
Conversation
@SimenB 🍻 |
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.
can you remove the copy.mjs
in root and add a link to this PR in the changelog entry I added in #13705?
Sure, don't know why that |
Updated changelog and removed copy.mjs
I think I know why the test fails. The function createResolveOptions(
conditions: Array<string> | undefined,
): ResolveExportsOptions {
return conditions
? {conditions, unsafe: true}
: // no conditions were passed - let's assume this is Jest internal and it should be `require`
{browser: false, require: true};
} Is specific to the |
Those options are just for specifying |
I checked |
What do you use for |
https://github.com/lukeed/resolve.exports/blob/master/src/index.js#L71-L73
I don't think that is correct as: {
"exports": {
"node": {
"import": "./a.js"
},
"import": {
"node": "./b.js"
}
}
} will get different result. I'll clarify that in the Node.js PR. |
Order of conditions shouldn't matter, as we use the order of keys in package.json. First match wins. https://nodejs.org/api/packages.html#conditional-exports
The order of entries in the |
Yes, for conditional exports it is checking by the key order. But for the nested condition case, it should matter, IMO. My gut feeling is that this is a limitation due to legacy code. Likely nested conditions are added later on so can't change ship. I'll update |
If you're interested, I have an resolver for exports and imports, both tested exhaustively against Node.js for accuracy: |
Thanks. I'm actually fixing this: https://github.com/privatenumber/resolve-pkg-maps#why-do-the-apis-return-an-array-of-paths The Node.js implementation does not work that way. It returns a single entry. The actual implementation in Node.js: |
You may have missed this part. If you want Node.js behavior, you can just use the first element in the array. The array is for use-cases where users may want to implement TypeScript or Webpack behavior. |
Yeah, just read that. Let me check. That is a good take. But based on what I understand, that doesn't work as the array can be: [{ "node": "./node.js" }, "./browser.js"] Which the caller should not handle. |
@privatenumber Nicely written. I spot a few differences:
|
The user should correct the message via try-catch if they want.
I'm not sure what this means. Can you elaborate?
That logic is from Node.js: Their regex is a little cryptic because they cover encoded values. But it would still work with PnP because imports can only contain bare-specifiers (package names) as opposed to |
This is what I'm referring to.
I think it is for resolving the target, not the key/specifier. so I think the resolver code should not need to check that. It's a different concern. (also it's a question on which code is responsible of what. in And also about the array pattern, I think it should be handled by the resolver: the array can be: [{ "node": "./node.js" }, "./browser.js"] Which the caller should not handle. So yeah, it sucks that webpack and typescript are handling it differently. So it seems like there is no way to support them and Node.js at the same time. |
@SimenB any suggestion on this test? In my own But I don't know how to do that with The error message contains the package.json and base file path, meaning it will be different in CI and everyone's environment.
|
I have fixed most of the errors except one. Both
Will be checking the current changes in, and continue to look at that one. |
I'm not completely sure what you mean, but Is there a test-case you have in mind?
This is handled by Demo: A lot of what you're pointing out is already covered in the tests and easy to verify on your end. |
Oh, the test actually passes in CI. Seems like a WSL issue locally then. |
@privatenumber I really like the look of your package - a single one support both imports and exports seems very reasonable to me. 👍 Definitely something we should consider using. Is that a package that will be used by other packages in the near future? (I had faith in
Would you be up for adding options for the user to pass in the needed data so you can build up the matching error message? |
I implemented it for the TypeScript resolver I'm working on for tsx. The TS resolver is still in-progress but the exports/imports resolver is ready to use.
Sure, I'm open to discussing this. |
When I working on this I also tried to use The JSONTypes need to be casted before passing in. Maybe the param type needs to be loosen. |
fixing the error message issue
Do you want to change this PR to use |
we can do it in a separate PR and remove |
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.
thanks!
This is now supported in |
Exciting! |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
use resolve.imports to handle imports field.
It supports more patterns supported by Node.js and closely tracks the actual implementation within Node.js.
fixes #13707
Test plan
Added a few tests, including nested pattern and array pattern.