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

Add explicit exports for node #611

Closed
wants to merge 6 commits into from
Closed

Conversation

pi0
Copy link

@pi0 pi0 commented Nov 7, 2023

Resolves #610

By using import export condition first, we ask bundlers such as rollup to prefer an export that is compatible with native ESM (ie: directly importing in Node.js when externalized in build). Using node export condition to prefer conditions that Node.js also natively prefers in runtime.

See #610 (comment) for more context.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Comment on lines 28 to 30
"import": "./dist/proxy/index.js",
"require": "./dist/cjs/index.js",
"import": "./dist/proxy/index.js"
"module": "./dist/esm/index.js"
Copy link
Member

Choose a reason for hiding this comment

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

This will cause webpack, rollup, wmr (ref), esbuild (ref) to choose "import" over "module".

I don't think we can do that, because dist/proxy/index.js is an ESM wrapper that re-exports CJS to avoid the dual package hazard. I'm pretty sure it will cause problems for tree shaking, and possibly other issues.

For reference, here is how the current exports were tested: connectrpc/examples-es#1002

I think some other solution is necessary to fix the issue, while maintaining behavior for other consumers. I'm not entirely sure which one yet 🙂

Copy link
Author

Choose a reason for hiding this comment

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

I'm pretty sure we can find some better solutions to support dual package hazard and replace proxy with an alternative, yet make a fully standard ESM package with a standard import export condition that works natively. (if this is your main concern to resolve) -- Feel free to direct me in discord (pi0), i can share my 50 cents of experience with dealing with ESM/CJS dual exports.

@pi0 pi0 changed the title build(protobuf, protoplugin): reorder exports field for native esm compatibility build(protobuf, protoplugin): add node export condition Nov 8, 2023
@pi0
Copy link
Author

pi0 commented Nov 8, 2023

Updated PR as per #610 (comment) suggestion to use node export condition as a quick fix.

I also used native dist not sure if proxy is needed but let me know or feel free to directly use proxy for native node. If it is tracked with right condition, it should work.

Note: I have not e2e tested it against nitro. In theory it should work as we apply node export condition when tracing. @daviddomkar if not too much trouble, would be nice if you can check by patching in node_modules.

@daviddomkar
Copy link

@pi0 thanks for modifying the PR with a quick fix. I can confirm that when I patch the package in node_modules with changes from this PR, the problem disappears. 🎉🎉

@smaye81
Copy link
Member

smaye81 commented Nov 29, 2023

Hi, @pi0 appreciate your patience on this. We've been testing this extensively in Connect-ES also because we've noticed some similar issues with exports there. After a lot of investigation, we landed on the format specifed in this PR: connectrpc/connect-es#921.

Would you be able to modify this PR so that the exports are in the same order and configured the same way?

@smaye81 smaye81 changed the title build(protobuf, protoplugin): add node export condition Add explicit exports for node Dec 4, 2023
@smaye81
Copy link
Member

smaye81 commented Dec 4, 2023

Hi @pi0. I actually pushed up the changes to your branch, I hope you don't mind. We'd like to get this fixed in all our npm packages as soon as possible. If things seem OK to you, we can merge this branch in.

cc @timostamm

@nicksnyder
Copy link
Member

Hi @pi0 , can you sign the CLA?

@smaye81
Copy link
Member

smaye81 commented Dec 6, 2023

Hi @pi0. We unfortunately had to land our own PR (#645) to remedy this because we wanted to close the loop. Thank you for calling this to our attention, though. Appreciate you digging into this.

@smaye81 smaye81 closed this Dec 6, 2023
@pi0
Copy link
Author

pi0 commented Dec 6, 2023

Thanks for following and considering the change ❤️ #645 looks good!

@pi0 pi0 deleted the build/exports-order branch December 6, 2023 15:25
@smaye81
Copy link
Member

smaye81 commented Dec 7, 2023

Also, FYI, this was released in v1.5.1 today.

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