-
Notifications
You must be signed in to change notification settings - Fork 324
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
Ensure 3p developers can properly integrate with Hydrogen #1388
Comments
Originally posted by @frandiox in Shopify/hydrogen#341 (comment) @susannepeng Hi! To answer your question, though: Hydrogen is used as ESM in Vite. If you were to import a CJS version of Hydrogen in your library, the user would end up with 2 different copies of that Hydrogen component. Plus, Vite itself and the ecosystem are moving towards ESM-first. I think the problem here is that your library is imported as CJS, and then it imports Hydrogen as ESM, right? Could you make sure your lib is imported as ESM instead? For that, I think you might need to provide ESM-only version and perhaps play with Vite's If that still doesn't work, please report this again as a new issue with your findings. |
Hi @susannepeng, I just pulled down a fresh copy of Is there something else I need to do to reproduce the error? |
Hi @jplhomer and @frandiox, thanks so much for your responses! We got our third party component library to work alongside
|
Great to see you fixed it and that the pointers were useful!
I'll try to test a bit more with your code and see if there's a better solution but, unfortunately, I think this is the way things need to be in Vite right now. Perhaps this will be different in Vite 3 (already in alpha), where they are switching to ESM default for SSR. For the time being, you could provide a Vite plugin that simply adds your optimized deps to the |
I've checked and creating a plugin still seems to be the simplest way to make it work in Vite. Another way to do it would be bundling all your dependencies at build time (using Vite itself in library mode), but that would duplicate code if another library uses |
Thanks for looking into this @frandiox! The plugin route works nicely. We ended up providing a plugin in very much the same way as the Hydrogen plugin is provided. Cheers 🙂 |
Originally posted by @susannepeng in Shopify/hydrogen#341 (comment)
Hi all,
We're developing a third party component library for use with Hydrogen and running into the same problem when we try to import anything from
@shopify/hydrogen
from our component library.Our component library lists
@shopify/hydrogen
as a peer dependency and the components it exports are designed to be used by a merchant in their own Hydrogen app.Demo
The third party component
The third party component used inside of a Hydrogen app
For now, the third party component library is packaged via
npm pack
and listed as a dependency of the user's Hydrogen app. Both repos linked above were set up to help illustrate this issue.Errors
When running the app with
npm run dev
:And the dev server output shows:
Re: node module resolution
We think this may be a module resolution issue. Our component library expects to use certain Hydrogen components, hooks, etc.. We compile our modules to both CJS and ESM and the user app resolves to our CJS modules, which are in turn attempting to resolve modules from
@shopify/hydrogen
. Here we run into the problem -- it seems that certain Hydrogen foundation/framework modules are only compiled to ESM.Further investigation indicates that
@shopify/hydrogen
has been packaged this way due to its dependency on Vite. There has also been talk of not compiling to CJS at all.We tried setting the user app to use ESM module resolution and were met with the following error:
We tried to dig further into this but got lost in the weeds of Vite internals.
For compatibility purposes and encouraging the growth of an ecosystem of third party libraries surrounding Hydrogen, could
@shopify/hydrogen
also provide modules compiled to CJS?The text was updated successfully, but these errors were encountered: