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

createReactContext breaks with Webpack 5 & relay-hooks 5.x (ESM) #194

Closed
cedrickring opened this issue Sep 28, 2021 · 8 comments
Closed
Labels
bug Something isn't working

Comments

@cedrickring
Copy link

For some reason, webpack introduced a "fake namespace object" which is provided first to createReactContext. When the actual module is loaded, the real react object will be passed to createReactContext resulting in the invariant failing here.

I don't really know how to provide a workaround for that.

@morrys
Copy link
Member

morrys commented Sep 29, 2021

Hi @cedrickring,
can you create an example project on github where the error is reproduced?
So I can analyze it.

Thanks

@stan-sack
Copy link

I've got this too. just blew up my site in prod

@stan-sack
Copy link

@cedrickring rolling back to 4.2.0 fixed it for me.

@morrys morrys changed the title createReactContext breaks with Webpack 5 createReactContext breaks with Webpack 5 & relay-hooks 5.x (ESM) Sep 30, 2021
@morrys morrys added the bug Something isn't working label Sep 30, 2021
@morrys
Copy link
Member

morrys commented Sep 30, 2021

The problem is caused by the version 5.x of webpack (not in version 4), in practice for this type of import webpack enter here https://github.com/webpack/webpack/blob/main/lib/RuntimeTemplate.js#L775-L788 and the react variable is saved in another variable managed by webpack.

I tried replacing that piece of code with return $ {importVar}; and everything works fine.

I'd like to hear from a Webpack maintainer (@sokra) and / or open an issue in their repository.

@cedrickring rolling back to 4.2.0 fixed it for me.

This problem is present in relay-hooks version 5.x as ESM support has been added

@morrys
Copy link
Member

morrys commented Oct 14, 2021

Version 5.0.1 released which resolves this issue.

@stan-sack @cedrickring let me know if there are other problems or if it works so I proceed to close the issue 👍

@cedrickring
Copy link
Author

cedrickring commented Oct 14, 2021

Works great! I'll close this issue as #195 fixed this issue 👍🏽

@sokra
Copy link

sokra commented Oct 15, 2021

The fake namespace object is needed for CommonJs-ESM-interop. React is a commonjs module, but import * as xxx gives a esm namespace object (which has an additional default property here).

But you are right webpack doesn't behaves fully spec-complient here. Calling import * as xxx from multiple modules should always return the same esm namespace object, but it doesn't in webpack. I never thought someone would check the identity of these objects.

Fixing that in webpack would be a bit more involved, since we probably have to cache the namespace object somewhere globally (probably in the module object)...

@morrys
Copy link
Member

morrys commented Oct 15, 2021

Hi @sokra,
Thank you for your answer 👍
I didn't open an issue in webpack just because I imagined that fixing it in webpack would be more complex and risky.

I never thought someone would check the identity of these objects.

I understand you, thinking about everything is almost impossible, luckily some problems have simple solutions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants