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

feat: support exports in package.json #11961

Merged
merged 7 commits into from Feb 10, 2022
Merged

feat: support exports in package.json #11961

merged 7 commits into from Feb 10, 2022

Conversation

SimenB
Copy link
Member

@SimenB SimenB commented Oct 14, 2021

Summary

Add support for exports in package.json.

This is missing a bunch of tests. Also, we need to add node condition, and then use shouldLoadAsEsm on the returned value in the runtime.

I also think we need to ensure conditions is always passed. So some stuff in normalize have to move (specifically the stuff that is loaded in the user's env like setuFiles etc - that needs to be provided the correct conditions)

Fixes: #9771

Test plan

Will be interesting to see how CI likes this.

Some basic tests added, will add way more involved ones before merging

@SimenB SimenB changed the title add bare minimum support for exports in package.json add somewhat hacky support for exports in package.json Oct 14, 2021
@@ -0,0 +1,6 @@
{
"name": "NODE_PATH_dir",
Copy link
Member Author

Choose a reason for hiding this comment

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

I had to create this and the one below otherwise resolve finds the package.json of jest-runtime itself (which has exports, thus it fails)... Not sure how to deal with this - is it ok? If so, it's at least a breaking change

@codecov-commenter
Copy link

codecov-commenter commented Oct 15, 2021

Codecov Report

Merging #11961 (c7ed620) into main (df0c496) will increase coverage by 0.01%.
The diff coverage is 91.30%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #11961      +/-   ##
==========================================
+ Coverage   68.46%   68.47%   +0.01%     
==========================================
  Files         324      324              
  Lines       16961    16968       +7     
  Branches     5055     5060       +5     
==========================================
+ Hits        11612    11619       +7     
  Misses       5317     5317              
  Partials       32       32              
Impacted Files Coverage Δ
packages/jest-resolve/src/defaultResolver.ts 91.17% <90.90%> (+3.17%) ⬆️
packages/jest-runtime/src/index.ts 55.41% <100.00%> (-0.11%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update df0c496...c7ed620. Read the comment docs.

@SimenB
Copy link
Member Author

SimenB commented Oct 15, 2021

Since I've had to add a few package.json here and there, I'm afraid this is a breaking change so will have to wait for Jest 28 (even if I do think the semantics are more correct).

Alternative is to add it behind a flag, but... meh

@SimenB SimenB added this to the Jest 28 milestone Oct 15, 2021
@SimenB SimenB force-pushed the exports branch 4 times, most recently from 3b0ee8e to 2bf869c Compare October 18, 2021 07:56
examples/angular/package.json Outdated Show resolved Hide resolved
@SimenB
Copy link
Member Author

SimenB commented Feb 10, 2022

OK, I think this is good to go now. Just need to add some more tests, but from manual testing this seems to work

@SimenB SimenB changed the title add somewhat hacky support for exports in package.json feat: support exports in package.json Feb 10, 2022
@antongolub
Copy link
Contributor

@SimenB, I have a lemming question: if we load some cjs package, which requires another package with dual export ({"type": "module", "main": "...", "exports": {...}), what will be the entry point for the latter?

@piranna
Copy link
Contributor

piranna commented Feb 10, 2022

@SimenB, I have a lemming question: if we load some cjs package, which requires another package with dual export ({"type": "module", "main": "...", "exports": {...}), what will be the entry point for the latter?

In latests versions of Node.js, if there's an exports entry, main one is ignored.

@antongolub
Copy link
Contributor

Аha. It should be. But we have a custom resolver here)

@SimenB
Copy link
Member Author

SimenB commented Feb 10, 2022

Yep, same here. If exports, main is ignored. If the require/import comes from your own code or in node_modules doesn't matter

@SimenB
Copy link
Member Author

SimenB commented Feb 10, 2022

I'll publish a beta release after landing this (probably tomorrow), at which point people testing it would be great 🙂 There are probably still bugs here

@SimenB SimenB marked this pull request as ready for review February 10, 2022 14:22
@SimenB SimenB merged commit b9e3a55 into jestjs:main Feb 10, 2022
@SimenB SimenB deleted the exports branch February 10, 2022 18:08
@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support package exports in jest-resolve
5 participants