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

fix: Add export maps and .mjs to node ES modules (Fixes #10172) #10175

Closed
wants to merge 1 commit into from

Conversation

rhengles
Copy link

@rhengles rhengles commented Mar 8, 2023

This fixes an issue with Preact + Vite + SSR. Without the export maps, Vite SSR and Node.js gets confused and loads both the ESM and CJS versions of Preact. This breaks the hooks system of Preact as discussed here[1] and here[2].

During SSR, Vite starts as ESM and loads my modules (and Preact). But when I load React-Router, as it does not have the exports map, Node loads the CJS version, which loads the CJS version of Preact. Then Preact breaks because it can't find the hooks registered in the ESM code.

Also, in the version of Node that I tested, v16.14.0, Node printed the warning below, so I also changed the extension of the ES module for node from ".js" to ".mjs".

(node:5753) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

[1] preactjs/preact#3220 (comment)
[2] vitest-dev/vitest#747 (comment)

@changeset-bot
Copy link

changeset-bot bot commented Mar 8, 2023

🦋 Changeset detected

Latest commit: f54a03f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
react-router Patch
react-router-dom Patch
react-router-dom-v5-compat Patch
react-router-native Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Mar 8, 2023

Hi @rhengles,

Welcome, and thank you for contributing to React Router!

Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once.

You may review the CLA and sign it by adding your name to contributors.yml.

Once the CLA is signed, the CLA Signed label will be added to the pull request.

If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at [email protected].

Thanks!

- The Remix team

This fixes an issue with Preact + Vite + SSR. Without the export maps, Vite SSR and Node.js
gets confused and loads both the ESM and CJS versions of Preact. This breaks the hooks system
of Preact as discussed here[1] and here[2].

During SSR, Vite starts as ESM and loads my modules (and Preact). But when I load React-Router,
as it does not have the exports map, Node loads the CJS version, which loads the CJS version of
Preact. Then Preact breaks because it can't find the hooks registered in the ESM code.

Also, in the version of Node that I tested, v16.14.0, Node printed the warning below, so I also
changed the extension of the ES module for node from ".js" to ".mjs".

(node:5753) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

[1] preactjs/preact#3220 (comment)
[2] vitest-dev/vitest#747 (comment)
@timdorr
Copy link
Member

timdorr commented Mar 8, 2023

This can't be done in a minor release. This is a major, breaking change. You can see our prior comments on this: #8268 (comment)

I'm also not comfortable including such a change for a relatively esoteric build/library setup. If we're doing something major, it should be for a larger benefit. This really feels like something Vitest or Preact should fix.

@timdorr timdorr closed this Mar 8, 2023
@rhengles
Copy link
Author

rhengles commented Mar 8, 2023

@timdorr Awesome, I wish that issue had been more discoverable, it would've saved an afternoon for me 😅

It is great that this is considered for v7. I appreciate the great effort that has been put into this library. Personally I can't see any downsides with this small change though. To me it clearly seems to align with the future of the Node/ESM ecosystem. Also Vite is many leagues ahead of webpack in every metric, you couldn't pay me to work with webpack.

I respect this team's wishes and understand that I don't have the full picture. But I'll have to either monkey-patch it, fork it or switch to another router (one that I can use with React if required). Thankfully my app is still in the beginning...

Beyond unforeseen consequences, I'd appreciate if you could share any known downsides.

@rhengles
Copy link
Author

rhengles commented Mar 8, 2023

In theory, the same problem could happen with React + Vite SSR. I'll test that when I have time. Do you know if there is an example of this working out there ?

@rhengles
Copy link
Author

rhengles commented Mar 9, 2023

Here is the fork if anyone else needs it

https://www.npmjs.com/package/@arijs/react-router-dom
https://www.npmjs.com/package/@arijs/react-router
https://github.com/arijs/react-router/tree/arijs-release

Edit: After many test releases, I can happlily and proudly confirm that it is working!!! 🎉 🎉 🎉

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

Successfully merging this pull request may close these issues.

2 participants