-
-
Notifications
You must be signed in to change notification settings - Fork 383
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: add "exports" field to package.json and rename ESM bundle to .mjs to fix ESM imports on node.js #989
Conversation
🤔 having Quite a similar issue was reported to React-Focus-Lock with @wojtekmaj proposing a solution theKashey/react-focus-lock#264 very close to the one in PR description, except the mjs comment. AFAIK recent dramma with bun caused node to move away from mjs, which was always a little strange idea. Pretty sure it's not required. I hope @wojtekmaj have a few insights to share |
Hey @theKashey, Thanks for the quick reply, I really appreciate it! 😃 I'm not sure what you mean by node moving away from .mjs. Sure, you can use I think we could call this "Solution 3", since I've given two other options in my first comment. Or, since this is a React library and 99% of the time it goes through a bundler, getting rid of the CommonJS bundle and adding the So this would be "Solution 4" which completely removes CommonJS support. For me it doesn't matter which one is picked as long as the issue is resolved before Vite 6.0.0 is released and the deprecated legacy ESM proxy for CommonJS modules is removed. In case you don't have the time to implement a solution I'm pretty much open to contributing once a solution is picked and agreed on. 👨💻 Cheers! |
Thanks for @-ing me :D Indeed, arethetypeswrong/cli points us to some errors: exports map proposed by OP:
makes total sense to me. This kills two birds with one stone, by fixing ESM file extension in CJS-by-default package and adding exports map to ensure all bundlers know what to do. I've had to do some manual work to verify this:
but I can confirm that after these fixes this looks very good: |
(but yeah, you likely don't need CJS build anymore, although removing it would be a breaking change, and the above seems to be an easy, non-breaking fix) |
Hey @wojtekmaj, thanks for jumping in and testing solution number 2! @theKashey, what would be your preferred solution, and do we need to sync with somebody else before actually doing any work here? As I said I've got a bit of free time on my hands until Christmas, so I can support you fixing this issue in case you need a helping hand. 😃 |
@wojtekmaj +++, I would really prefer not to do a major bump if possible as it will take years for consumers to catch up. Doing something within version ranges creates some technical debt for library, but also reduced for the end product. I think the end product is more important. |
@theKashey, I think it should as expected now when it comes to importing from ESM modules. I added a new commit to this PR, which fixes imports and the extension (reverting the previous named export change). Afterwards I ran all the tests - all seems to be good. I also tested the built bundle with Vite 5.0.0 with the legacy ESM proxy disabled and everything seems to be working as expected. |
…to .mjs to allow node.js to properly import esm version of package.
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.
Will release tomorrow morning
Released as 5.16.2 |
Summary
Description of issue
Vite 5.0.0+ now removes the
exoports.default
todefault
ESM export proxy it used previously.Unfortunately this breaks the following import
import loadable from '@loadable/component'
because the dependency is treated as CommonJS export andvite
doesn't proxy thedefault
export for CommonJS files anymore.There is still a flag to enable the old behaviour, but that flag is due to be removed in Vite 6.0.0+, which would break the default import with this extension
What this fix does
This is a quick and dirty fix duplicates the default export as a named export called
loadable
, so we can also importimport { loadable } from '@loadable/component'
which works with Vite 5.0.0+.More info about the deprecation: https://vitejs.dev/guide/migration#ssr-externalized-modules-value-now-matches-production
Alternative (more laborious, but future proof) solution that can be implemented instead of this
I'm currently open to implement this instead since it's way better to have proper exports in terms of future
node.js
&vite
ESM compatibility.Here is what needs to be done to future-proof the package:
'@loadable/component' ESM build should have the
.mjs
extension.exports
map is in thepackage.json
file (see issue 2 for more info).'@loadable/component' should have
exports
map is set to properly point toloadable.esm.mjs
for ESM andloadable.cjs.js
for CommonJS respectively.Example
exports
map in package.json:Test plan
I made a build with the current code and tested that the
import { loadable } from '@loadable/component'
actually works.Feel free to point me out in case some tests or documentation needs to be updated also.