Skip to content

Conversation

hamidrezahanafi
Copy link
Contributor

@hamidrezahanafi hamidrezahanafi commented Jul 22, 2025

This PR implements support for the module-sync export condition in package.json exports, which is available in Node.js 22 and later versions. The feature enables packages to provide synchronous ES module loading behavior for enhanced performance in supported Node.js versions.
Unit tests added with process.versions.node mocked


it(`should correctly trace ${testSuffix}`, async () => {
// Mock Node.js version for module-sync-condition tests
if (testName === 'module-sync-condition') {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of mocking, lets just skip for older versions of node since we have a ci matrix already.

See the continue statements above.

Copy link
Member

Choose a reason for hiding this comment

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

Now that I think about it, we need this first:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep just sending a message that CI here doesn't run 22

(condition === 'import' && !cjsResolve) ||
(condition === 'module-sync' &&
getNodeMajorVersion() >= 22 &&
!cjsResolve) ||
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure about the !cjsResolve here?

The docs say

"module-sync" - matches no matter the package is loaded via import, import() or require()

https://nodejs.org/api/packages.html#conditional-exports

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 added because of the what's next come in the doc

The format is expected to be ES modules

I might be wrong though

Copy link
Member

Choose a reason for hiding this comment

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

Thats the format of the imported module and not the format of the module doing the importing, right?

@@ -0,0 +1,11 @@
{
"name": "test-pkg-sync-fallback",
"type": "module",
Copy link
Member

@styfle styfle Jul 22, 2025

Choose a reason for hiding this comment

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

Lets add another test with "type": "commonjs" to see what happens

Copy link
Contributor Author

@hamidrezahanafi hamidrezahanafi Jul 22, 2025

Choose a reason for hiding this comment

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

It passes with commonjs with/without cjeResolve check, I am a little bit confused, probably safe to remove the check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay tested again, tests are failing with cjs but it really doesn't need the check

Copy link
Member

Choose a reason for hiding this comment

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

I can't remember if cjeResolve is implying the current module was parsed as cjs or if its the module being imported. Thats why it might need to be unconditional.

Regardless, we should have a test for both types since that works with Node.js as far as I can tell.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes added, as soon as you merge your PR I will rebase and update

Copy link
Member

Choose a reason for hiding this comment

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

Done

Copy link
Member

@styfle styfle left a comment

Choose a reason for hiding this comment

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

Great work, thanks!

@styfle styfle merged commit 7e915aa into vercel:main Jul 23, 2025
22 of 23 checks passed
Copy link

🎉 This PR is included in version 0.30.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

Support for "module-sync" exports condition
2 participants