Scenario:
I would like to embed React components in a web page as isolated react apps. As an example, lets say a date picker and also a colour picker.
I don't want to bundle React, or other common dependencies twice (or as many times as I end up using this approach) for each isolated bundle. I understand that I can use webpack externals to achieve this.
Why using webpack externals is not ideal
webpack externals means loading one version of React via a script tag and making it available as a global object. For the lifetime of the website I'm working on, I'd be stuck:
- Using one version of React
- Painfully updating all of the components if I want to use a new version
Ideal solution
Webpack bundles the individual applications into their own scope, if I package up different React versions without making them external it appears to work without issue as each instance is isolated.
Is there some way of configuring webpack to look for an external module (e.g. a specific version of React from a CDN) at runtime but crucially not attach it to the global namespace and load it within the scope of the bundled code?
The major benefit would be allowing React to be cached by the browser and avoiding adding the same download footprint every time I build a new component.
Thanks for your time and consideration.