Skip to content

Commit

Permalink
docs(nf): improve readme
Browse files Browse the repository at this point in the history
  • Loading branch information
manfredsteyer committed Aug 6, 2023
1 parent 61414f7 commit 12b4330
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 60 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# @angular-architects/module-federation

- See [readme here](./libs/mf/README.md)
- [Readme for Module Federation](./libs/mf/README.md)
- [Readme for Native Federation](./libs/native-federation/README.md)
Binary file added error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions libs/native-federation-core/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@softarc/native-federation",
"version": "1.1.1",
"version": "1.1.2",
"type": "commonjs",
"dependencies": {
"json5": "^2.2.0",
"npmlog": "^6.0.2",
"@softarc/native-federation-runtime": "1.1.1"
"@softarc/native-federation-runtime": "1.1.2"
}
}
2 changes: 1 addition & 1 deletion libs/native-federation-esbuild/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@softarc/native-federation-esbuild",
"version": "1.1.1",
"version": "1.1.2",
"type": "commonjs",
"dependencies": {
"@rollup/plugin-commonjs": "^22.0.2",
Expand Down
2 changes: 1 addition & 1 deletion libs/native-federation-runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@softarc/native-federation-runtime",
"version": "1.1.1",
"version": "1.1.2",
"peerDependencies": {},
"dependencies": {
"tslib": "^2.3.0"
Expand Down
111 changes: 70 additions & 41 deletions libs/native-federation/README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,37 @@
# Native Federation
# Native Federation for Angular

Native Federation is a "browser-native" implementation of the successful mental model behind wepback Module Federation for building Micro Frontends.
Native Federation is a "browser-native" implementation of the successful mental model behind webpack Module Federation for building Micro Frontends (Plugins, etc.).

## Features
## Features 🔥

- ✅ Mental Model of Module Federation
- ✅ Future Proof: Independent of build tools like webpack
- ✅ Embraces Import Maps - an emerging web standard
- ✅ Easy to configure: We use the same API and Schematics as for our Module Federation plugin
- ✅ Blazing Fast: The reference implementation not only uses the fast esbuild; it also caches already built shared dependencies (like Angular itself).
- ✅ Blazing Fast: The reference implementation not only uses the fast esbuild; it also caches already built shared dependencies.

## Today and Tomorrow
## Prerequisite

### Bundler
Angular & Angular CLI 16.1 or higher

The current version uses **esbuild**. Future versions will allow to easily **switch out the build tool**.
This package was successfully tested with Angular CLI projects and with Nx projects.

### Frameworks
## Versions

**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**.

### Design

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.

## Current Limitations

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!

Limitations:

- 🔷 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.
- 🔷 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.
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.

## Credits

Big thanks to:

- [Zack Jackson](https://twitter.com/ScriptedAlchemy) for originally coming up with the great idea of Module Federation and its successful mental model
- [Zack [Jackson](https://twitter.com/ScriptedAlchemy) for initially coming up with the great idea of Module Federation and its successful mental model
- [Tobias Koppers](https://twitter.com/wSokra) for helping to make Module Federation a first class citizen of webpack
- [Florian Rappl](https://twitter.com/FlorianRappl) for an good discussion about these topics during a speakers dinner in Nuremberg
- [Florian [Rappl](https://twitter.com/FlorianRappl) for a good discussion about these topics during a speakers dinner in Nuremberg
- [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 === ❤️)
- [Michael Egger-Zikes](https://twitter.com/MikeZks) for contributing to our Module Federation efforts and brining in valuable feedback
- 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
- 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

## Example
## Example 🛠️

We migrated our webpack Module Federation example to Native Federation:

Expand All @@ -66,6 +53,8 @@ Start the Micro Frontend:
ng serve mfe1 -o
```

_(In the case of an error, see this [information below](#error-file-srcmaints-is-missing-from-the-typescript-compilation-plugin-angular-compiler"))_

Wait until the Micro Frontend is started.

Open another console and start the shell:
Expand All @@ -74,18 +63,26 @@ Open another console and start the shell:
ng serve shell -o
```

## About the Mental Model
The example loads a Micro Frontends into a shell:

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.
![Microfrontend Loaded into Shell](https://github.com/angular-architects/module-federation-plugin/raw/main/libs/mf/tutorial/result.png)

## Relationship to @angular-architects/module-federation

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.

## About the Mental Model 🧠

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.

For this, the mental model introduces several concepts:

- **Remote:** The remote is a separately built and deployed application. It can **expose EcmaScript** modules that can be loaded into other applications.
- **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.
- **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.
- **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.
- **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.
- **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.
- **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.

## Usage/ Tutorial
## Usage/ Tutorial 🧪

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:

Expand All @@ -97,6 +94,10 @@ cd module-federation-plugin-example
npm i
```

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`:

![Microfrontend Loaded into Shell](https://github.com/angular-architects/module-federation-plugin/raw/main/libs/mf/tutorial/result.png)

### Adding Native Federation

```
Expand All @@ -117,6 +118,8 @@ ng g @angular-architects/native-federation:init --project shell --port 4200 --ty

A dynamic host reads the configuration data at runtime from a `.json` file.

> 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).
### Configuring the Host

The host configuration (`projects/shell/federation.config.js`) looks like what you know from our Module Federation plugin:
Expand Down Expand Up @@ -200,7 +203,7 @@ initFederation('/assets/federation.manifest.json')

> This file is generated by the schematic described above.
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.
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.

**Credits:** The Nx team originally came up with the idea for the manifest.

Expand All @@ -214,7 +217,7 @@ This is what the (also generated) federation manifest (`projects\shell\src\asset

Native Federation generates the `remoteEntry.json`. It contains metadata about the individual remote.

If you follow this tutorial, ensure this entry points to port `4201` (!).
If you follow this tutorial, **ensure** this entry points to port `4201` (!).

### Initializing the Remote

Expand Down Expand Up @@ -277,27 +280,53 @@ Start the remote:
ng serve mfe1 -o
```

Once, the remote is started, start the shell:
_(In the case of an error, see this [information below](#error-file-srcmaints-is-missing-from-the-typescript-compilation-plugin-angular-compiler"))_

Once the remote is started, start the shell:

```
ng serve shell -o
```

Now, by clicking at the 2nd menu item, you can load the remote directly into the host.

## FAQ

### Should we Already use Native Federation in Production?
### When to use this package?

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.

### Error: File 'src\main.ts' is missing from the TypeScript compilation. [plugin angular-compiler]

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.

### I get an error when preparing shared packages. What to do?

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.

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.
If the preparation of one of these packages fails, you get an error like this one:

We will evolve Native Federation but also our Module Federation support and keep you posted.
![error when preparing shared packages](https://github.com/angular-architects/module-federation-plugin/blob/main/error.png?raw=true)

For this, there are several reasons:

- 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`.

- 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.

### How to speed up package preparation during the build process

The already prepared packages are cached in `node_modules/.cache`. Make sure, this folder is reused across subsequent build process runs.

### How does Native Federation Work under the Covers?

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.
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.

## Documentation 📰

## More: Blog Articles
Please have a look at this [article series](https://www.angulararchitects.io/en/aktuelles/the-microfrontend-revolution-part-2-module-federation-with-angular/).

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/).
Even though these articles were written for Module Federation, thanks to the same API, they also apply to Native Federation.

## More: Angular Architecture Workshop (100% online, interactive)

Expand Down
6 changes: 3 additions & 3 deletions libs/native-federation/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@angular-architects/native-federation",
"version": "1.1.1",
"version": "16.1.2",
"main": "src/index.js",
"generators": "./collection.json",
"builders": "./builders.json",
Expand All @@ -16,8 +16,8 @@
},
"dependencies": {
"@babel/core": "^7.19.0",
"@softarc/native-federation": "1.1.1",
"@softarc/native-federation-runtime": "1.1.1",
"@softarc/native-federation": "1.1.2",
"@softarc/native-federation-runtime": "1.1.2",
"@types/browser-sync": "^2.26.3",
"browser-sync": "^2.29.3",
"esbuild": "^0.18.12",
Expand Down
15 changes: 8 additions & 7 deletions libs/native-federation/src/builders/build/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ export async function runBuilder(

options.externalDependencies = externals.filter((e) => e !== 'tslib');

// const builderRun = await context.scheduleBuilder(
// '@angular-devkit/build-angular:browser-esbuild',
// options as any,
// { target }
// );

const builderRun = await context.scheduleTarget(target, options as any);
const builderRun = await context.scheduleBuilder(
'@angular-devkit/build-angular:browser-esbuild',
options as any,
{ target }
);

// TODO: Allow more flexibility?
// const builderRun = await context.scheduleTarget(target, options as any);

let first = true;
builderRun.output.subscribe(async (output) => {
Expand Down
8 changes: 4 additions & 4 deletions update-local.bat
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
call npm unpublish @softarc/native-federation@1.1.1 --registry http://localhost:4873
call npm unpublish @softarc/native-federation-runtime@1.1.1 --registry http://localhost:4873
call npm unpublish @softarc/native-federation-esbuild@1.1.1 --registry http://localhost:4873
call npm unpublish @angular-architects/native-federation@1.1.1 --registry http://localhost:4873
call npm unpublish @softarc/native-federation@1.1.2 --registry http://localhost:4873
call npm unpublish @softarc/native-federation-runtime@1.1.2 --registry http://localhost:4873
call npm unpublish @softarc/native-federation-esbuild@1.1.2 --registry http://localhost:4873
call npm unpublish @angular-architects/native-federation@16.1.0 --registry http://localhost:4873

call nx build native-federation
call nx build native-federation-core
Expand Down

0 comments on commit 12b4330

Please sign in to comment.