How to create a Micro Frontend bundle with Webpack that shares libraries with the container application? How to create a Micro Frontend bundle with Webpack that shares libraries with the container application? reactjs reactjs

How to create a Micro Frontend bundle with Webpack that shares libraries with the container application?


Update:

Just realized, that your question is directed at Micro Frontends (not only micro services) and therefore is not about sharing libraries with Webpack in general. Added Micro Frontend to your tags/title and updated the answer to be more focused on this topic.


So my problem just one: I don't want to duplicate vendor libraries like react, react-dom (any others). And I want to make them shared among other [Micro Frontends] (which is bundled with webpack)

What you can do is exclude dependencies from the output bundle of your Micro Frontends by adding a Webpack externals property to the config.

webpack config of your Micro Frontends:

module.exports = {  ...  externals = {    react: 'React',    'react-dom': 'ReactDOM'  }}

Above config would exclude react and react-dom and expect them in the global variables React and ReactDOM. You can then share those dependencies by including the libraries in a script inside index.html of your root applicationn aka stitching layer:

<html>  ...  <body>    ...    <script src="<your-host>/react.prod-16.9.0.min.js"></script>    <script src="<your-host>/react-dom.prod-16.9.0.min.js"></script>  </body></html>

If you have other common components to share, you can also integrate the library scripts in a component library.

The reason for the include as script is: We do not want that our container has to require/import the Micro Frontends at build time in order to avoid a coupling of build/release/version management between all apps. Instead one purpose of Micro Frontends is to achieve fully independent deployment of the parts, which include continuous delivery steps from build, test to release.

I know what is the bad practice to have some global stuff (it's violate the whole idea of bundeling with webpack).

Of course, you create some form of coupling between the apps. But if you have a mature, stable and common library shared by all parts, it is a reasonable decision.

Hope, it helps (now)!


The best way to achieve this today is using Webpack's new Module Federation technology released in v5. This approach does not use SystemJS but rather the internals of Webpack. We tried several different micro frontend approaches, but this one outshines them all & is currently running successfully for us in production. There are definitely some challenges to setting it up but it was worth the developer productivity gains.

Here is the info site produced by the creator Zack Jackson which should provide all the resources you need: https://module-federation.github.io/

Here is the link to webpack docs which deal more with technicalities rather than how to practically set up a full micro frontend architecture: https://webpack.js.org/concepts/module-federation/