You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: libs/native-federation/README.md
+70-41Lines changed: 70 additions & 41 deletions
Original file line number
Diff line number
Diff line change
@@ -1,50 +1,37 @@
1
-
# Native Federation
1
+
# Native Federation for Angular
2
2
3
-
Native Federation is a "browser-native" implementation of the successful mental model behind wepback Module Federation for building Micro Frontends.
3
+
Native Federation is a "browser-native" implementation of the successful mental model behind webpack Module Federation for building Micro Frontends (Plugins, etc.).
4
4
5
-
## Features
5
+
## Features 🔥
6
6
7
7
- ✅ Mental Model of Module Federation
8
8
- ✅ Future Proof: Independent of build tools like webpack
9
9
- ✅ Embraces Import Maps - an emerging web standard
10
10
- ✅ Easy to configure: We use the same API and Schematics as for our Module Federation plugin
11
-
- ✅ Blazing Fast: The reference implementation not only uses the fast esbuild; it also caches already built shared dependencies (like Angular itself).
11
+
- ✅ Blazing Fast: The reference implementation not only uses the fast esbuild; it also caches already built shared dependencies.
12
12
13
-
## Today and Tomorrow
13
+
## Prerequisite
14
14
15
-
### Bundler
15
+
Angular & Angular CLI 16.1 or higher
16
16
17
-
The current version uses **esbuild**. Future versions will allow to easily **switch out the build tool**.
17
+
This package was successfully tested with Angular CLI projects and with Nx projects.
18
18
19
-
### Frameworks
19
+
##Versions
20
20
21
-
**Angular** is a first-class citizen: The package ships with **Schematics** for the Angular CLI and a **builder** (that delegates to the experimental esbuild builder the CLI team is current working on). Future versions will also make it easy to use the implementation **with other frameworks**.
22
-
23
-
### Design
24
-
25
-
This is possible, because by design, most of the implementation runs outside of the bundler und independently of CLI mechanisms. Hence, we will expose 2-3 helper functions everyone can call in their build process regardless of the framework or build tool used.
26
-
27
-
## Current Limitations
28
-
29
-
This is a first experimental version. The results look very promising, however it's not intended to be used in production. Feel free to try it out and to provide feedback!
30
-
31
-
Limitations:
32
-
33
-
- 🔷 As we use a fork of the experimental esbuild builder the CLI team is current working on, there is currently **only a builder for ng build**. ng serve or ng test are currently not supported. This support will be added with a future version. Also, as the forked esbuile builder is still experimental, you cannot expect to get all the features you are used to. This will also change over time.
34
-
- 🔷 Libraries are currently only shared if two or more remotes (Micro Frontends) request the very same version. This is also what works best with Angular. In a future version, we will add optional "version negotiation" for the sake of feature parity with Module Federation. This allows Native Federation to decide for a "higher compatible version" (e. g. a higher minor version provided by another Micro Frontend) at runtime.
21
+
We will at least provide a new version of this package per Angular major. If necessary, we will also provide packages to adapt to Angular minors. To make the relationship between Angular versions and versions of this package easy for all of us, **we follow Angular's version numbers**. E. g., `@angular-architects/native-federation` 16.1 is intended for Angular 16.1 and upwards.
35
22
36
23
## Credits
37
24
38
25
Big thanks to:
39
26
40
-
-[Zack Jackson](https://twitter.com/ScriptedAlchemy) for originally coming up with the great idea of Module Federation and its successful mental model
27
+
-[Zack [Jackson](https://twitter.com/ScriptedAlchemy) for initially coming up with the great idea of Module Federation and its successful mental model
41
28
-[Tobias Koppers](https://twitter.com/wSokra) for helping to make Module Federation a first class citizen of webpack
42
-
-[Florian Rappl](https://twitter.com/FlorianRappl) for an good discussion about these topics during a speakers dinner in Nuremberg
29
+
-[Florian [Rappl](https://twitter.com/FlorianRappl) for a good discussion about these topics during a speakers dinner in Nuremberg
43
30
-[The Nx Team](https://twitter.com/NxDevTools), esp. [Colum Ferry](https://twitter.com/FerryColum), who seamlessly integrated webpack Module Federation into Nx and hence helped to spread the word about it (Nx + Module Federation === ❤️)
44
31
-[Michael Egger-Zikes](https://twitter.com/MikeZks) for contributing to our Module Federation efforts and brining in valuable feedback
45
-
- The Angular CLI-Team, esp. [Alan Agius](https://twitter.com/AlanAgius4) and [Charles Lyding](https://twitter.com/charleslyding), for working on the experimental esbuild builder for Angular
32
+
- The Angular CLI-Team, esp. [Alan Agius](https://twitter.com/AlanAgius4) and [Charles Lyding](https://twitter.com/charleslyding), for their fantastic work on the esbuild builder for Angular
46
33
47
-
## Example
34
+
## Example 🛠️
48
35
49
36
We migrated our webpack Module Federation example to Native Federation:
50
37
@@ -66,6 +53,8 @@ Start the Micro Frontend:
66
53
ng serve mfe1 -o
67
54
```
68
55
56
+
_(In the case of an error, see this [information below](#error-file-srcmaints-is-missing-from-the-typescript-compilation-plugin-angular-compiler))_
57
+
69
58
Wait until the Micro Frontend is started.
70
59
71
60
Open another console and start the shell:
@@ -74,18 +63,26 @@ Open another console and start the shell:
74
63
ng serve shell -o
75
64
```
76
65
77
-
## About the Mental Model
66
+
The example loads a Micro Frontends into a shell:
78
67
79
-
The underlying mental model allows for runtime integration: Loading a part of a separately built and deployed application into your's. This is needed for Micro Frontend architectures but also for plugin-based solutions.
68
+

69
+
70
+
## Relationship to @angular-architects/module-federation
71
+
72
+
This package, `@angular-architects/native-federation`, uses the same API as `@angular-architects/module-federation`. To switch over, just make sure you import everything from the former package. Don't mix these packages.
73
+
74
+
## About the Mental Model 🧠
75
+
76
+
The underlying mental model allows for runtime integration: Loading a part of a separately built and deployed application into yours. This is needed for Micro Frontend architectures but also for plugin-based solutions.
80
77
81
78
For this, the mental model introduces several concepts:
82
79
83
80
-**Remote:** The remote is a separately built and deployed application. It can **expose EcmaScript** modules that can be loaded into other applications.
84
-
-**Host:** The host loads one or several remotes on demand. For your framework's perspective, this looks like traditional lazy loading. The big difference is that the host doesn't know the remotes at compilation time.
85
-
-**Shared Dependencies:** If a several remotes and the host use the same library, you might not want to download it several times. Instead, you might want to just download it once and share it at runtime. For this use case, the mental model allows for defining such shared dependencies.
86
-
-**Version Mismatch:** If two or more applications use a different version of the same shared library, we need to prevent a version mismatch. To deal with it, the mental model defines several strategies, like falling back to another version that fits the application, using a different compatible one (according to semantic versioning) or throwing an error.
81
+
-**Host:** The host loads one or several remotes on demand. From your framework's perspective, this looks like traditional lazy loading. The big difference is that the host doesn't know the remotes at compilation time.
82
+
-**Shared Dependencies**:\*\* If several remotes and the host use the same library, you might not want to download it several times. Instead, you might want to download it once and share it at runtime. For this use case, the mental model allows for defining such shared dependencies.
83
+
-**Version Mismatch:** If two or more applications use a different version of the same shared library, we need to prevent a version mismatch. The mental model defines several strategies to deal with it, like falling back to another version that fits the application, using a different compatible one (according to semantic versioning), or throwing an error.
87
84
88
-
## Usage/ Tutorial
85
+
## Usage/ Tutorial 🧪
89
86
90
87
You can checkout the [nf-standalone-starter branch](https://github.com/manfredsteyer/module-federation-plugin-example/tree/nf-standalone-starter) to try out Native Federation:
91
88
@@ -97,6 +94,10 @@ cd module-federation-plugin-example
97
94
npm i
98
95
```
99
96
97
+
This repository consists of two Angular applications: a `shell` and a Micro Frontend called `mfe1`. During this tutorial, you will load `mfe1` into the `shell`:
98
+
99
+

100
+
100
101
### Adding Native Federation
101
102
102
103
```
@@ -117,6 +118,8 @@ ng g @angular-architects/native-federation:init --project shell --port 4200 --ty
117
118
118
119
A dynamic host reads the configuration data at runtime from a `.json` file.
119
120
121
+
> The schematics called here automate most steps of this tutorial, esp. adding configuration files and bootstrapping Native Federation. Hence, the following sections primarily discuss these changes. You just need to add a lazy route (see below) and make sure the correct ports are configured in the federation manifest (see below too).
122
+
120
123
### Configuring the Host
121
124
122
125
The host configuration (`projects/shell/federation.config.js`) looks like what you know from our Module Federation plugin:
> This file is generated by the schematic described above.
202
205
203
-
The function points to a federation manifest. This manifest points to the individual Micro Frontends. It can be exchanged when deploying the solution. Hence, you can adopt the solution to the current environment.
206
+
The function points to a federation manifest. This manifest lists the individual remotes. It can be exchanged when deploying the solution. Hence, you can adapt the build to the respective environment.
204
207
205
208
**Credits:** The Nx team originally came up with the idea for the manifest.
206
209
@@ -214,7 +217,7 @@ This is what the (also generated) federation manifest (`projects\shell\src\asset
214
217
215
218
Native Federation generates the `remoteEntry.json`. It contains metadata about the individual remote.
216
219
217
-
If you follow this tutorial, ensure this entry points to port `4201` (!).
220
+
If you follow this tutorial, **ensure** this entry points to port `4201` (!).
218
221
219
222
### Initializing the Remote
220
223
@@ -277,27 +280,53 @@ Start the remote:
277
280
ng serve mfe1 -o
278
281
```
279
282
280
-
Once, the remote is started, start the shell:
283
+
_(In the case of an error, see this [information below](#error-file-srcmaints-is-missing-from-the-typescript-compilation-plugin-angular-compiler))_
284
+
285
+
Once the remote is started, start the shell:
281
286
282
287
```
283
288
ng serve shell -o
284
289
```
285
290
291
+
Now, by clicking at the 2nd menu item, you can load the remote directly into the host.
292
+
286
293
## FAQ
287
294
288
-
### Should we Already use Native Federation in Production?
295
+
### When to use this package?
296
+
297
+
If you like the idea of webpack Module Federation but want to switch over to Angular's new esbuild builder (currently in developer preview), you can use this package.
298
+
299
+
### Error: File 'src\main.ts' is missing from the TypeScript compilation. [plugin angular-compiler]
300
+
301
+
It seems like the current version of Angular's esbuild builder has an issue with paths on Windows when using the traditional command prompt. For the time being, try to ng serve and ng build your application via PowerShell, the git bash, or WSL.
302
+
303
+
### I get an error when preparing shared packages. What to do?
304
+
305
+
Native Federation needs to prepare all your shared packages so that it can load them on demand as EcmaScript modules. This only happens once for development and once for production builds. The result of this is cached.
289
306
290
-
For production, we would stick with Module Federation for the time being. Native Federation, however, shows that you don't need to fear that you are left alone, once you (or the community) wants to move over to other build tools.
307
+
If the preparation of one of these packages fails, you get an error like this one:
291
308
292
-
We will evolve Native Federation but also our Module Federation support and keep you posted.
309
+

310
+
311
+
For this, there are several reasons:
312
+
313
+
- Perhaps you try to share a package intended for NodeJS/ a package that cannot be converted to EcmaScript modules. This happens if you use `shareAll` in the `federation.config.js` and when the package in question is part of your dependencies in `package.json`. If you don't need (to share) this package at runtime, move it to `devDependencies` or add it to the `skip` section of your `federation.config.js`.
314
+
315
+
- Perhaps your shared packages contain some code esbuild cannot transfer to EcmaScript modules. This should not be the case for packages, built with the Angular CLI or Nx and the underlying package ng-packagr. If this happens, please let us know about the package causing troubles.
316
+
317
+
### How to speed up package preparation during the build process
318
+
319
+
The already prepared packages are cached in `node_modules/.cache`. Make sure, this folder is reused across subsequent build process runs.
293
320
294
321
### How does Native Federation Work under the Covers?
295
322
296
-
We use Import Maps at runtime. As they are currently not supported in every browser, our `init` schematic installs the `es-module-shims` polyfill. In addition to Import Maps, we use some code at build time and at runtime to provide the Mental Model of Module Federation.
323
+
We use Import Maps at runtime. In addition to Import Maps, we use some code at build time and at runtime to provide the Mental Model of Module Federation.
324
+
325
+
## Documentation 📰
297
326
298
-
## More: Blog Articles
327
+
Please have a look at this [article series](https://www.angulararchitects.io/en/aktuelles/the-microfrontend-revolution-part-2-module-federation-with-angular/).
299
328
300
-
Find out more about our work including Micro Frontends and Module Federation but also about alternatives to these approaches in our [blog](https://www.angulararchitects.io/en/aktuelles/the-microfrontend-revolution-part-2-module-federation-with-angular/).
329
+
Even though these articles were written for Module Federation, thanks to the same API, they also apply to Native Federation.
0 commit comments