-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Suggestion: Bundling in v17, ESM, CJS, and the dual package hazard #4062
Comments
Just to give my 2c, I'm not sure if On the topic of this, I find this section a lot more interesting and that's often been on my mind:
In my opinion, with This might of course force some level of API-intercompatibility with imho the problem of ESM and CJS exports being both imported and then intercompatible, which can sometimes happen in Node, is an optimisation-opportunity/issue that's to be fixed and discovered by users, but it's highly inconvenient that the |
More modern build chains (e.g. vite) would pick them up. It won't be an immediate solution, more of a long-term hope ;)
I totally agree, and I'd like to address the CJS <-> ESM problem first. |
Can you detail why |
overall this looks really well thought out and it's great that it apparently is in use successfully in other packages! one question I have is whether it is a problem for any non-node, non-bundler environments that the "import" condition points to esm that imports cjs rather than just esm. Is there an esm-only environment that this might block? Not asking from a place of knowledge, but just something I noticed. Asking from a different direction => if we have an esm only that reimports cjs, why do we need the pure esm version? which environments does that help specifcally, and are they all covered by "module"? |
Because if you write: export const foo = 42 You don't expect this to be a valid import: import _ns from 'pkg'
pkg.foo This file effectively "normalizes" the shape of the module back to what was authored.
I haven't found any - given the node's popularity everyone is trying to be compatible with it and handle CJS. If absolutely needed you could always fight it back with some more specific conditions put above it (like if u'd have to target Deno then u could use the
That pure ESM version is mainly targeting bundlers so they can benefit from tree-shaking and other optimizations that are often only implemented for ESM files. Most bundlers automatically use module condition - if they don't have that logic enabled by default... well, they should still work since they should behave like node. They just won't benefit from those optimizations. |
Thanks for the clarification @Andarist! 🙌 |
Some interesting reading on the impact of this on TypeScript users (which I believe is already addressed in the solution above, albeit in a slightly different way to this article): https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseESM.md |
Can I ask what is the progress here? Is there a solution being worked on? Do we have some timeline? Or progress with issue? Thanks! |
Submitting here as an issue to reference it in the next WG agenda.
Some historical context puzzle pieces:
graphql-esm
package and alatest-esm
tag on thegraphql
package itselfgraphql
v17.There was some discussion in Provide a clear path on how to support graphql-js 16 and 17 at the same time as a esm and cjs library author #3603
Unfortunately, there isn't a lot of documented discussion and I couldn't find any meeting notes for the above graphql-js-wg meeting.
Status Quo
With many tools like Vitest that try to use ESM (but also support CJS), the dual-package hazard still seems to be a real problem.
Many packages depending on
graphql
are still CJS only, so as soon as you try to start in an ESM world (which Vitest does) and import the wrong dependency, you end up in a mix of CJS and ESM, and theinstanceof
checks break on you.That makes
graphql
very hard to use in modern setups.Proposed solution
I want to suggest a bundling/publishing scheme that @Andarist is successfully using for many packages he maintains, e.g.
XState
orreact-textarea-autosize
.Here's an example entrypoint from https://unpkg.com/browse/xstate@5.11.0/package.json
Let's zoom in for a second:
Here,
module
would be picked up by bundlers, andimport
/default
would be picked up by runtimes likenode
.import
andrequire
- they don't care.require
calls innode
.node
forimport
calls and its contents are:This ensures that bundlers can pick up a modern ESM build, but in runtime environments like
node
where the dual-package hazard exists, only one variant of the package exists. With current limitations, that is the CJS build.I would suggest that we go forward with a packaging scheme like this for v17, as it could solve the dual package hazard, not lock out CJS users and enable gradual adoption of ESM (currently this is very painful and probably holding back a bunch of users from making the switch).
Once ESM is more of an option, v18 could still be ESM only.
From my understanding, the
exports
field has not been highly adopted when the current decisions around ESM were made, but by now, it is pretty widely supported.PS:
A nice side effect of introducing an
exports
field would be that we could also usedevelopment
andproduction
conditions, making aprocess.env.NODE_ENV
check necessary only in a fallback import that would be used if these conditions are not supported by a consumer.That would be follow-up work, though.
The text was updated successfully, but these errors were encountered: