-
Notifications
You must be signed in to change notification settings - Fork 32
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: reverse node export conditions #309
Conversation
@@ -40,8 +40,8 @@ | |||
"source": "./src/index.ts", | |||
"require": "./dist/index.cjs", | |||
"node": { | |||
"module": "./dist/index.js", | |||
"import": "./dist/index.cjs.js" | |||
"import": "./dist/index.cjs.js", |
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.
I agree with changes of module
ordering 👍🏼 But worth to note that this is still not a valid condition as import
field is pointing to a CommonJS entry. Using an ESM stub + require
field could be nice (internally using createModule
to reexport)
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.
confusingly, despite the name, it's not a CJS entry:
https://unpkg.com/browse/@sanity/[email protected]/dist/index.cjs.js
Bundlers like Webpack in Next.js target the export condition pattern we're using. We specifically need only nodejs to use the node.import condition because it's the only environment that comprehends the ESM CJS re-export pattern. When bundlers like those in Astro encounter this pattern, they fail. In Next.js, it also bloats the bundle due to added cruft from the re-export and module format change. node.module is a condition ignored by nodejs but targeted by bundlers like webpack. It signals transpilation expectation for bundlers. We use both patterns to avoid issues in native nodejs environments, especially since some dependencies in @sanity/client aren't node-esm-ready. Regarding your error, it seems to be trying to use node.import rather than node.module. So, removing node.module might not solve the issue. |
The error message in the linked run is a little confusing. We use http://github.com/vercel/nft in Nuxt and Nitro to trace dependencies, and it is picking the 'module' condition and only copying the module across, but of course node is looking for the 'import' condition and this file is missing. I have tested and confirmed this fix works for us (as does removing the 'module' condition entirely) but of course it does need to work for the rest of the ecosystem too. Do you have a Next.js project I can test with to confirm? |
We have a matrix you can use. If your |
We would of course prefer to be able to drop the |
Thanks, that's helpful. I'll investigate and see if I can find a fix which works with the matrix. FWIW, we don't want the module dependency. We want the |
But you're also using a bundler, which the |
No, we're not using a bundler in this case. We're copying across files to a new tree-shaken |
Copying files without a bundler! I see, then copying |
Exactly! Check out https://github.com/vercel/nft - it's pretty cool. |
Yeah, did an internal search and in one place we're using conditions: ['module', 'node'] Maybe try that? |
There's precedence for it but not a lot of information out there. In fact, there's not a lot of information on how to ship ESM and CJS, or JS that works universally with the current npm ecostystem, is there 😅 |
If I may weigh in, we have been supporting ESM modules for nuxt since 2018 alongside webpack 4 and early versions. Back in time, there were no concrete standards for ESM modules, and there was a top-level Once Node.js tried to make I wish it could be called Solving previous mistakes is not easy and bundlers like rollup can easily not apply This way, the I hope the above context will be helpful for finally making your decision. |
Hi @pi0 @danielroe, Thanks for your feedback. Our initial need for node.module was for Astro compatibility. However, with Astro v3, this issue seems resolved. While I value the bundlesize savings in frameworks like Next.js, I'll prioritize ecosystem compatibility. If I don't find a workaround by EOD, I'll remove node.module. Thanks for your understanding! 🙌 |
resolves #308
this tries to fix an issue we're experiencing triggered by the
module
export condition being added. Because the first matching condition is picked, it would likely be safer to add theimport
beforemodule
bundler.(I would also suggest removing the
module
condition as it's not supported by node, and this is within the node condition. But I'm not sure why it was added in the first place so I'm reluctant to make changes.)