To reduce the initial load of my app, i try to do code splitting. For exemple, i successfully split twilio-video, to load and initialize when user press "call"
import('twilio-video').then(Video => {
Video.connect(twilioInfo.data.token, options).then(room => {
...
});
});
Webpack correctly split the code and load it when needed.
Now i would like to do the same with components, or routes (to not load user profil while we stand on login page). Is there any equivalent of
const OtherComponent = React.lazy(() => import('./OtherComponent'));
or
const Home = lazy(() => import('./routes/Home'));
or any other way to load component when a route path is hitted?
It used to be very difficult. These days, Webpack Module Federation makes it a breathe.
They have tons of samples to get you started. For example, the basic-host-remote sample has a simple scenario that fits your need. Just fetch the entire Github repo,
cdinto this sample, and run it (as explained in each sample'sREADME).Keep an eye open for the Network tab. Components are not loaded until they are explicitely requested.
Basic Example
The basic-host-remote sample has two separate packages:
app1andapp2.app1wants to use components fromapp2app2to itsremotesin its WMF configapp2provides aButtoncomponentapp2exposesButtonin its WMF config.app1requests a component using dynamicimport.useEffect, or aRoute.rendercallback etc.app1can use thatButton, once it's loaded. While loading, it shows a loading message, usingSuspense:lazyandSuspense, you can just take the promise returned from theimport(...)statement and handle the asynchronous loading any way you prefer. Of course,WMFis not at all restricted toreactand can load any module dynamically.app1andapp2have the samesharedsetup, making sure that those shared dependencies are only loaded one time, and not bundled/duplicated with remotely loaded code:Note that
WMFdynamic loading must use dynamicimport(i.e.import(...)), because:require" cannot be bundled by webpack since browsers have no concept ofcommonjs(unless you use some hacks, in which case, you will lose the relevant "loadingpromise").WMF Samples
WMF takes a bit of learning, but all its samples have the following common elements:
webpack.config.jsdevelopmentmode, you usually serve up everything using the nicely integratedwebpack-dev-server.productionmode, you want to make some small adjustments to your config. Whenwebpack-dev-serveris out of the picture, your build just adds some additionalremoteEntry.jsfiles to your build output.sharedconfiguration. (Usually you want them to act as singletons, meaning, everyone should share common dependencies, and not pack their own.)exposeit and add its own (independent) components to its ownremotes. Not sure if it works, but its worth trying.Final Words
Again, it will take a bit of a learning curve, but I find it's definitely worth it. Feels like one of the most sophisticated dynamic build + load systems I have seen.
Most annoyingly, WMF still does not currently have proper API documentation on the Webpack page, but I'm sure it'll come soon enough. Currently, there is only a not all too polished collection of conceptual notes.
The WMF author himself promises to provide better documentation soon™️.
Luckily, for learning, the samples are really good, actively maintained and still being enhanced.