Microfrontends using NX in Angular

Manoharmano Taneti
4 min readNov 22, 2023

--

Microfrontends: An architectural style where independently deliverable frontend applications are composed into a single unit.

Advantages of Microfrontends:

Team scalability:
Different teams can work on different Micro Frontends, allowing for parallel development and faster time-to-market.

Strategic Design of the Web Page:
Host –Host will load all microfrontends Dynamically.

Host can also check the users role and can load the microfrontends based on the role of the user.

Remotes –All Remotes are independent of each other and can be deployed seperately.

Code Reusability

Framework agnostic:
Remotes can be built using different frameworks(Angular,React…) and can be integrated into the same shell (Host).

Module Federation:

Module Federation is a concept that enables developers to share code and resources among multiple JavaScript applications, known as micro-frontends. Rather than having all the code for a single page within a monolithic codebase, Module Federation allows for smaller, independently deployable modules that can be loaded on demand.

By leveraging the Webpack module bundler, Module Federation enables the remote loading of JavaScript modules. Instead of loading all the code at once, modules can be split into smaller units that are loaded when required. This approach promotes independent development and deployment of micro-frontends, enhancing coordination and speeding up development cycles.

To implement Module Federation, developers configure their Webpack builds to expose specific modules to other applications and consume modules from other applications. This is achieved through the use of Webpack’s exposes and remotes options in the configuration.

The exposes option exposes selected modules from an application to be accessed by other applications when loaded remotely. On the other hand, the remotes option allows an application to consume modules from other applications by loading them remotely when needed.

Once the Webpack configurations are set up for each application, they can be deployed independently. When a user visits a page containing multiple micro-frontends, these micro-frontends dynamically load the required code from other micro-frontends using the exposes and remotes options.
Overall, Module Federation simplifies the development and deployment of micro-frontends by enabling code sharing and on-demand loading of modules, leading to more manageable and scalable applications.

NX:

Nx is a powerful open-source build system that provides tools and techniques for enhancing developer productivity, optimizing CI performance, and maintaining code quality.

Nx is a smart, fast and extensible and open source build system with first class monorepo support and powerful integrations.

Nx provides the same dev experience for many frameworks You can build Angular apps, Next.js apps, share code between them.

With Nx Where your code lives and how it is divided should be based on your organizational structure, not on what frameworks you use.

Why NX?

To Thrive “Angular Community” has to stay up-to-date with the broader javascript community.

  • Nx therefore built “OUT OF THE BOX” support for those tools into Nx. They include:
  • Jest -UNIT TESTING (replaces KARMA).
  • Cypress -E2E TESTING (replaces Protractor).
  • ESLint -LINTING(replaces TSLint).
  • Storybook -COMPONENT DESIGN.
  • COMPUTATION CACHING.
  • Dependency Graph.

NX Setup:

  • Set up a new workspace
  • What is workspace?
  • A workspace is a folder created using Nx. The folder consists of a single git repository, with folders for apps (applications) and libs (libraries); along with some scaffolding to help with building, linting, and testing.
  • Folder Structure — — Example
  • myorg/
    ├── apps/ — -Applications
    ├── libs/ — -Libraries
    ├── workspace.json
    ├── nx.json
    ├── package.json
    └── tsconfig.base.json
  • Dependency Graph:The Nx dependency graph is the way to always get an instant view of the dependencies between projects in your Nx workspace.

What is an app?
An app produces a binary. It contains the minimal amount of code required to package many libs to create an artifact that is deployed.

Apps are meant only to organize other libs into a deployable artifact — there is not a lot of code present in the applications outside of the module file and maybe a some basic routing. All of the application’s code is organized into libs.

What is a lib?

A lib is a set of files packaged together that is consumed by apps. Libs are similar to node modules or nuget packages. They can be published to NPM or bundled with a deployed application as-is.

Conclusion:

Nx is a versatile tool that supports the development of micro-frontends using Angular. By leveraging Nx, developers can efficiently manage multiple Angular applications within a single workspace.

Nx offers various features and best practices tailored for building scalable and modular applications, making it an ideal choice for implementing micro-frontends.
With Nx, developers can easily share code between micro-frontends, enforce consistent architectural patterns, and streamline the development and deployment workflows.

By harnessing the power of Nx, developers can effectively leverage Angular to create micro-frontends with improved productivity and maintainability.

--

--