-
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(esm): support nuxt-module-build
#318
Conversation
package.json
Outdated
"import": "./dist/index.cjs.js" | ||
}, | ||
"import": "./dist/index.js", | ||
"module": "./dist/index.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.
cc @rexxars since we have "type": "module"
declared it's enough to point to the ESM in default
, no need to have both import
and default
.
The module
statement here is ignored in most environments and bundlers. Except for in rollup
and webpack
, which gives it the same precedence as it got when it lives at node.module
.
I'll run a double check to be sure and come back but so far all tests are green with this change.
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.
since (and as long as) value of module
and default
are the same, it should be fine. But i am worried if module
points to another entity in the future, same issue will pop out as rollup (which is used by nitro/nuxt) will pick it and it is really not easy to disable this behavior in rollup.
I guess also since module
and default
are same, it is unnecessary to keep module
condition (any implementation of exports
resolver, should fallback to default, which is the same)
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.
@pi0, we utilize module
to deter webpack from defaulting to node.import
as outlined here. It's true this also affects rollup
. Additionally, our @sanity/pkg-utils
tool, built on rollup
, requires module
to align with either default
or import
when set to `type: "module", ensuring consistent future references.
The primary role of exports.module
is directing bundlers away from node.import
to module
, eliminating the need for re-export wrappers. Our eventual aim is to phase out instanceof
checks, guaranteeing a smooth runtime even with mixed CJS and ESM @sanity/client
instances. This will enable us to streamline our exports, enhance compatibility, and hopefully resolve related issues.
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.
After some more end-to-end testing it turns out moving module
below node
negates it so it's not preferred after all. Updated the PR to just remove it as originally suggested.
Made a follow-up ticket to update @sanity/pkg-utils
to stop recommending the node.module
pattern and instead focus efforts on refactoring out of needing to protect against dual package hazards 🙌
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.
Much better and appreciated, thanks! (BTW do you think require
makes sense outside of Node condition? 🤔)
Unrelated: You might want to use types
+ default
for new Node.js type resolution. You can validate with https://arethetypeswrong.github.io/
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.
(BTW do you think
require
makes sense outside of Node condition? 🤔)
Maybe not, but let's say these two variants are functionally identical:
{
".": {
"require": "./dist/index.cjs",
"node": {
"import": "./dist/index.cjs.js"
}
}
}
{
".": {
"node": {
"require": "./dist/index.cjs",
"import": "./dist/index.cjs.js"
}
}
}
And in both cases only ever node
is loading up ./dist/index.cjs
if require('@sanity/client')
happens and it's likely ignored by all other bundlers.
If that turns out to be the case I still feel it's not worth the risk of someone's bundler setup breaking, since we don't gain much by making this change 😅
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.
Regarding the arethetypeswrong
tip thnx, made a note to follow up in @sanity/pkg-utils
to make the changes necessary to ship typings that report the correct module formats used to TypeScript.
I have to admit we haven't started using the new "moduleResolution": "node16"
or similar in our own repos. We're using either the less strict "moduleResolution": "bundler"
introduced by typescript
v5, or good old "moduleResolution": "node"
🙌
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 haven't tested but it might affect Bun (and Deno's compatibility) to prefer CJS over ESM. Let's try this way 👍🏼
1e7258b
to
6b0872a
Compare
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.
all good in my testing 👌
thank you ❤️
Fixes #308. Fixes #309.
Tested by checking out
@nuxtjs/sanity
and running these commands onmain
:Output:
@danielroe @pi0 if you can confirm the fix is working on the canary release on your end we'll merge and release promptly 🙌