How to share and consume modules across microfrontends with JavaScript Module Federation

Microfrontends have become a popular architectural pattern for building large-scale web applications. They enable development teams to work independently on different parts of the application, which can lead to improved scalability, maintainability, and developer efficiency. However, one challenge when using microfrontends is how to share and consume modules across different microfrontends.

In this blog post, we will explore how to tackle this challenge using JavaScript Module Federation, a powerful feature introduced in webpack 5.

What is JavaScript Module Federation?

JavaScript Module Federation is a feature in webpack 5 that allows you to share and consume modules across different applications or microfrontends. It enables you to create reusable and shareable modules that can be dynamically loaded at runtime. This means that you can develop and deploy microfrontends independently while still being able to share and consume modules between them.

Sharing Modules

To share modules using JavaScript Module Federation, you need to expose the modules you want to make available to other microfrontends. This is done by configuring the exposes option in your webpack configuration.

module.exports = {
  // ...
  plugins: [
    new ModuleFederationPlugin({
      name: "shared",
      filename: "remoteEntry.js",
      exposes: {
        "./Button": "./src/components/Button.js",
        "./Modal": "./src/components/Modal.js"
      },
      shared: ["react", "react-dom"]
    })
  ]
};

In this example, we are exposing two modules Button and Modal from their respective locations. These modules can now be consumed by other microfrontends.

Consuming Modules

To consume modules shared by other microfrontends, you need to configure the remotes option in your webpack configuration.

module.exports = {
  // ...
  plugins: [
    new ModuleFederationPlugin({
      name: "app",
      remotes: {
        shared: "shared@http://localhost:3001/remoteEntry.js"
      },
      shared: ["react", "react-dom"]
    })
  ]
};

In this example, we are consuming the modules exposed by another microfrontend with the name shared located at http://localhost:3001/remoteEntry.js. Now, you can easily import and use the shared modules within your microfrontend.

import { Button, Modal } from "shared";

// Use the shared modules
<Button />
<Modal />

Conclusion

JavaScript Module Federation in webpack 5 provides an effective solution for sharing and consuming modules across microfrontends. It allows development teams to work independently on different parts of the application while still being able to leverage shared modules. By utilizing this powerful feature, you can improve the scalability and maintainability of your microfrontend architecture.

#javascript #webpack