2

Future-proof Module Federation With Browser Technologies

 1 year ago
source link: https://devm.io/angular/module-federation
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Evolutionary progress with import maps

Future-proof Module Federation With Browser Technologies

21. Oct 2022


Module federation greatly simplifies the implementation of microfrontends. However, it is — to some extent — linked to webpack. This is not a major issue in the short term, and possibly also in the medium term, because webpack is currently the most popular build tool for web technologies, with over 20 million downloads.

However, we all know that technologies come and go. The Angular team, for example, is currently working on CLI support for the bundler esbuild [1], which looks very promising, particularly in terms of build performance.

This raises the question of how to implement the tried-and-tested mental model of module federation independently of webpack in order to future-proof your own microfrontend architecture. This article provides an answer based on import maps that now work with all major browsers — either natively, as in Chrome or Edge, or by adding a polyfill. The examples shown can be found in [2].

Mental model behind module federation

Before we get into import maps, I would like to talk about the mental model of module federation, which we will try to emulate later with import maps (Fig. 1).

Overview of module federation

Fig. 1: Overview of module federation

This mental model, among other things, defines host applications that load modules of separately built and separately deployed remote applications. These modules can export any JavaScript construct: Examples include functions, components, and data structures such as Angular modules or routes. The host in the world of microfrontends is a shell, and the remote is the microfrontend itself.

The host loads the provided modules with a dynamic import or — if the remotes are not known in advance — via a low-level API provided by webpack at runtime. The latter is indicated by the auxiliary function loadRemoteModule. This function points to both the name of the published module (exposedModule) and a remote entry (remoteEntry).

The remote entry is a JavaScript file that webpack generates during bundling that contains metadata about the remote. This metadata, among other things, informs the remotes and the host about the dependencies to be shared at runtime. These dependencies must be specified in the configuration under shared.

For example, in Figure 1, it is specified that @angular/core should be shared. This means that even if @angular/core is used in multiple remotes or the host, it is only loaded once.

When sharing dependencies, version conflicts can, of course, occur. Long-time Windows users may recall this issue being referred to as DLL Hell. Fortunately, module federation comes with several strategies for avoiding conflicts (Box: "Module Federation and version conflicts").

Module federation and version conflicts

To avoid version conflicts, module federation offers various configuration settings:

  • The highest compatible version: Module federation loads only the highest compatible version of a shared dependency by default. For example, if the host uses version 10.0 and the remote uses version 10.1, module federation may decide to load only version 10.1 because it should be backward compatible with version 10.0.
  • Load multiple versions: If two or more applications require mutually incompatible versions, module federation loads both versions. This is the case, for example, when the host references version 10.0 and the remote version 11.0.
  • Prevent multiple versions from being loaded: module federation can be instructed to treat a shared dependency as a singleton. In this case, only the highest version is loaded, regardless of whether it is backward compatible or not. If a version conflict is possible, module federation displays a warning on the JavaScript console. If desired, module federation can also throw an exception instead, so that integration tests, for example, quickly become aware of the problem at hand.

Whether two versions are compatible with each other is determined by the settings in package.json. For example, ^10.1.7 means that a higher version within major version 10 is also allowed. At runtime, this information can be found in the metadata of the remote entry. This metadata is therefore critical in resolving version...


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK