A Guide to Module Federation and Micro-Frontend Applications in Angular — Part 1

Hürkan UĞUR
4 min readMay 26, 2024

--

What’s Inside?

This guide will cover the following topics:

Introduction

Imagine you’re organizing a big birthday party. You have a massive cake to bake, decorations to put up, games to set up, and food to prepare. Trying to handle all of this alone can be overwhelming. But what if you could split the tasks among your friends? One friend bakes the cake, another decorates, another sets up games, and another handles the food. Everyone works independently but comes together to create an amazing party.

Similarly, as web applications grow larger and more complex, maintaining a single, massive codebase can become challenging. To address these issues, the concepts of micro-frontends and module federation have been introduced. By leveraging these concepts, developers can create modular, scalable, and maintainable web applications. This article will explore the fundamentals of micro-frontends, the features of Webpack 5, and provide a step-by-step guide on how to implement module federation in Angular projects.

What is a Micro-Frontend?

Think of a micro-frontend architecture like a bustling food court in a mall. Each food stall (micro-frontend) operates independently, serving its unique cuisine. There’s a pizza stall, a sushi stall, a burger stall, and so on. Each stall is managed, staffed, and stocked independently, but together they create a delightful dining experience for the mall visitors.

Micro-frontends apply this concept to web applications. A large web application is divided into smaller, semi-independent “micro” applications. Each micro-frontend can be developed, deployed, and maintained separately. This approach allows different teams to work on different parts of the frontend without interfering with each other, leading to improved scalability, maintainability, and the ability to use different technologies within the same application.

What is Webpack 5?

Imagine you’re organizing all the supplies for a camping trip. You need tents, food, clothes, and camping gear. Instead of just stuffing everything randomly into one huge bag, you use multiple smaller bags, each designated for a specific category. This makes packing, finding, and using items much easier and more efficient.

Webpack 5 is like the ultimate packing system for JavaScript applications. It’s a module bundler that organizes and packs all your JavaScript files, styles, images, and other assets efficiently. Here are some key features of Webpack 5:

  • Module Federation: Like sharing supplies between different campers, Module Federation allows multiple Webpack builds to work together, enabling code sharing between applications.
  • Improved Caching: Think of this as having a super-organized checklist that ensures you never forget what you packed, making future packing faster.
  • Tree Shaking: This feature is like removing unnecessary items from your bag to make it lighter and more efficient.
  • Asset Modules: Simplifies the handling of asset files (like images and fonts), making sure everything has its place.
  • Web Assembly Support: Improved support for WebAssembly, like having a special compartment for high-tech camping gadgets.

What is a Standalone Component?

Imagine you’re building with LEGO bricks. Each LEGO brick is a standalone piece that you can use independently or combine with other bricks to create something bigger. You don’t need to worry about which set the brick came from; it just works wherever you place it.

In Angular, standalone components are like these versatile LEGO bricks. They are Angular components that are not bound to any particular NgModule and can be used across multiple parts of an application without the need for explicit imports and exports within NgModule declarations.

  • Highly Reusable: Like LEGO bricks, standalone components can be used across different modules and projects.
  • Simplifies Relationships: They eliminate complex import/export relationships, simplifying the component hierarchy.
  • Works with Lazy-Loaded Modules: Standalone components integrate seamlessly with lazy-loaded modules.
  • Integrates with Third-Party Libraries: You can use them with third-party libraries without modifying NgModule declarations.
  • Reduces Complexity: Managing dependencies becomes easier, just like organizing a LEGO collection.
  • Encourages Cleaner Code Separation: Your codebase remains clean and modular, much like building distinct sections of a LEGO model.
  • More Portable: Standalone components are easy to move around, refactor, or migrate, just like rearranging LEGO pieces.

Thank you for reading, and happy coding! :)

Related Links:

--

--

Hürkan UĞUR

Software Engineer @TÜBİTAK (Scientific and Technological Research Council of Türkiye)