-
Notifications
You must be signed in to change notification settings - Fork 751
Add support for multiple require.context with addition of useContexts (2)
#1221
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
Conversation
|
I have a failing test on master which persists with this change: |
|
Might need a bit of a rethink, at least as far as my usage goes. I think the problem stems back to comments you can see in this issue (#885), where it's possible to have multiple ReactUJS instances mounted. So imagine a scenario where we have three entry points to split our JavaScript down, plus the server_rendering.js for server-side rendering (SSR):
Say we're in checkout, and loading the checkout.js bundle only. (We're on a page with deliberately reduced functionality, so we don't need the shared components.) This works fine, we get SSR and then we load only the checkout pack to hydrate our components. But then when we're on the search page, we have an issue. We get SSR on the page, but then we try to load both the shared application pack and the route-specific search pack. At this point, we wind up with two instances of ReactUJS (at least, I think this is the issue), and although we have the initial markup on screen, it's never hydrated with the client-side JS and we wind up with errors like [Edit] Maybe I'm just thinking about this the wrong way. Perhaps it's better to have just a single pack tag per route, making the shared components an included directory within them, rather than a separate entry point / pack. So going back to my scenario above, I could have the following entry points which load multiple component directories making use of the new
Then I can continue to make use of split chunks to extract common modules between route-split packs. Moving to a single pack tag method would also be closer to the way Shakapacker behaves. [Edit 2] Yep, that works, and is probably better anyway. 😃 I suspect I was trying to "micro-manage" Webpack too much here by defining my own shared bundle, when that's exactly what split chunks config is for. I might need to tweak the wording of my README changes a little to better communicate the use case. But for route-based entry points, particularly on larger apps, this is looking like a great help. |
|
@RiccardoMargiotta Thanks for taking this on -- I'm just catching up. |
Hey @cymen! If you'd prefer, I'd be more than happy for you to bring your PR up to date with master, and use my suggested documentation changes if you like. I absolutely don't want to take credit for your changes, but I'd love to see them released - this would be hugely helpful for my application! |
|
Closed as we'll continue this work in the original PR #1144. 🎉 |
Summary
In some cases, it is desirable to have multiple
require.contextentries and attempt to resolve in each before falling back to global. This PR addsuseContexts(in the pattern ofuseContext) which accepts an array ofrequire.contextand attempts to resolve the component/module in each context before falling back to global.In a larger application, you might find it helpful to split your JavaScript by routes/controllers to avoid serving unused components and improve your site performance by keeping bundles smaller. For example, you might have separate bundles for homepage, search, and checkout routes. In that scenario, you can add multiple
require.contextcomponent directory paths viauseContextstoserver_rendering.js, so that you can server side render across all of your routes.Other Information
This code change was originally authored by @cymen in #1144. I have added additional documentation and brought it up to date with master to help get it released.