Skip to content

Commit fbdb463

Browse files
committed
docs(mf): update readme and tutorial for v14.3
1 parent b765515 commit fbdb463

File tree

15 files changed

+568
-334
lines changed

15 files changed

+568
-334
lines changed

libs/mf-runtime/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "@angular-architects/module-federation-runtime",
33
"license": "MIT",
4-
"version": "14.3.0-beta.4",
4+
"version": "14.3.0",
55
"peerDependencies": {
6-
"@angular/common": ">=14.0.0-next.15",
7-
"@angular/core": ">=14.0.0-next.15"
6+
"@angular/common": ">=14.0.0",
7+
"@angular/core": ">=14.0.0"
88
},
99
"dependencies": {
1010
"tslib": "^2.0.0"

libs/mf-tools/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "@angular-architects/module-federation-tools",
3-
"version": "14.3.0-beta.4",
3+
"version": "14.3.0",
44
"license": "MIT",
55
"peerDependencies": {
6-
"@angular/common": ">=14.0.0-next.15",
7-
"@angular/core": ">=14.0.0-next.15",
8-
"@angular/router": ">=14.0.0-next.15",
9-
"@angular-architects/module-federation": "^14.3.0-beta.4",
10-
"@angular/platform-browser": ">=14.0.0-next.15",
6+
"@angular/common": ">=14.0.0",
7+
"@angular/core": ">=14.0.0",
8+
"@angular/router": ">=14.0.0",
9+
"@angular-architects/module-federation": "^14.3.0",
10+
"@angular/platform-browser": ">=14.0.0",
1111
"rxjs": ">= 6.0.0"
1212
},
1313
"dependencies": {

libs/mf/README.md

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ Big thanks to the following people who helped to make this possible:
88

99
- [Tobias Koppers](https://twitter.com/wSokra), Founder of Webpack
1010
- [Dmitriy Shekhovtsov](https://twitter.com/valorkin), Angular GDE
11+
- [Michael Egger-Zikes](https://twitter.com/MikeZks), Angular Architects
1112

1213
## Prequisites
1314

14-
- Angular CLI 12
15+
- Angular CLI 12 or higher (13, 14)
1516

1617
## Motivation 💥
1718

@@ -36,10 +37,21 @@ Since Version 1.2, we also provide some advanced features like:
3637
## Which Version to use?
3738

3839
- Angular 12: @angular-architects/module-federation: ^12.0.0
39-
- Angular 13: @angular-architects/module-federation: ^14.0.0
40+
- Angular 13: @angular-architects/module-federation: ~14.2.0
41+
- Angular 14: @angular-architects/module-federation: ^14.3.0
4042

4143
Beginning with Angular 13, we had to add some changes to adjust to the Angular CLI. Please see the next section for this.
4244

45+
## Update
46+
47+
This library supports ``ng update``:
48+
49+
```
50+
ng update @angular-architects/module-federation
51+
```
52+
53+
If you update by hand (e. g. via ``npm install``), make sure you also install a respective version of ngx-build-plus (version 14 for Angular 14, version 13 for Angular 13, etc.)
54+
4355
## Upgrade from Angular 12 or lower
4456

4557
Beginning with Angular 13, the CLI generates EcmaScript modules instead of script files. This affects how we work with Module Federation a bit.
@@ -50,28 +62,6 @@ Please find information on migrating here:
5062

5163
If you start from the scratch, ``ng add`` will take care of these settings.
5264

53-
## Upgrade from Version 1.x
54-
55-
After updating the libs, you need to adjust the ``webpack.conf.js`` a bit:
56-
57-
58-
```diff
59-
module.exports = {
60-
output: {
61-
uniqueName: "delme3",
62-
+ publicPath: "auto"
63-
},
64-
optimization: {
65-
runtimeChunk: false
66-
},
67-
+ resolve: {
68-
+ alias: {
69-
+ ...sharedMappings.getAliases(),
70-
+ }
71-
+ },
72-
[...]
73-
}
74-
```
7565

7666
## Usage 🛠️
7767

@@ -81,13 +71,23 @@ module.exports = {
8171
2. Adjust the generated ``webpack.config.js`` file
8272
3. Repeat this for further projects in your workspace (if needed)
8373

84-
## Nx
74+
### Nx
8575

8676
1. ``npm install @angular-architects/module-federation``
8777
2. ``ng g @angular-architects/module-federation:init``
8878
3. Adjust the generated ``webpack.config.js`` file
8979
4. Repeat this for further projects in your workspace (if needed)
9080

81+
### 🆕🔥 Version 14+: Use the --type switch to get the new streamlined configuration
82+
83+
With version 14, we've introduced a --type switch for ``ng add`` and the ``init`` schematic. Set it to one of the following values to get a more streamlined configuration file:
84+
85+
- ``host``
86+
- ``dynamic-host``
87+
- ``remote``
88+
89+
A dynamic host reads the micro frontend's URLs from a configuration file at runtime.
90+
9191
## Getting Started 🧪
9292

9393
Please find here a [tutorial](https://github.com/angular-architects/module-federation-plugin/blob/12.0.0/libs/mf/tutorial/tutorial.md) that shows how to use this plugin.
@@ -105,11 +105,8 @@ Please have a look at this [article series about Module Federation](https://www.
105105
This [example](https://github.com/manfredsteyer/module-federation-plugin-example)
106106
loads a microfrontend into a shell:
107107

108-
![Microfrontend Loaded into Shell](https://github.com/angular-architects/module-federation-plugin/raw/main/packages/mf/tutorial/result.png)
109-
110108
Please have a look into the example's **readme**. It points you to the important aspects of using Module Federation.
111109

112-
113110
## Advanced Features
114111

115112
While the above-mentioned tutorial and blog articles guide you through using Module Federation, this section draws your attention to some advanced aspects of this plugin and Module Federation in general.
@@ -190,7 +187,30 @@ Let's assume, you have an Angular CLI Monorepo or an Nx Monorepo using path mapp
190187

191188
You can now share such a library across all your micro frontends (apps) in your mono repo. This means, this library will be only loaded once.
192189

193-
To accomplish this, just register this lib name with the ``SharedMappings`` instance in your webpack config:
190+
#### New streamlined configuration in version 14+
191+
192+
Beginning with version 14, we use a more steamlined configuration, when using the above mentioned --type switch with one of the following options: ``remote``, ``host``, ``dynamic-host``.
193+
194+
This new configuration automatically shares all local libararies. Hence, you don't need to do a thing.
195+
196+
However, if you want to control, which local libraries to share, you can use the the ``sharedMappings`` array:
197+
198+
```javascript
199+
module.exports = withModuleFederationPlugin({
200+
201+
shared: {
202+
...shareAll({ singleton: true, strictVersion: true, requiredVersion: 'auto' }),
203+
},
204+
205+
sharedMappings: ['shared-lib'],
206+
});
207+
```
208+
209+
Please don't forget that sharing in Module Federation is always an opt-in: You need to add this setting to each micro frontend that should share it.
210+
211+
#### Legacy-Syntax and version 12-13
212+
213+
In previous versions, you registered the lib name with the ``SharedMappings`` instance in your webpack config:
194214

195215
```javascript
196216
const mf = require("@angular-architects/module-federation/webpack");
@@ -207,7 +227,7 @@ sharedMappings.register(
207227

208228
Beginning with version 1.2, the boilerplate for using ``SharedMappings`` is generated for you. You only need to add your lib's name here.
209229

210-
This generated code includes providing the metadata for these libraries for the ``ModuleFederationPlugin`` and adding a plugin making sure that even source code generated by the Angular Compiler uses the shared version of the library.
230+
This generated code includes providing metadata for these libraries for the ``ModuleFederationPlugin`` and adding a plugin making sure that even source code generated by the Angular Compiler uses the shared version of the library.
211231

212232
```javascript
213233
plugins: [
@@ -297,6 +317,28 @@ The options passed to shareAll are applied to all dependencies found in your ``p
297317

298318
This might come in handy in an mono repo scenario and when doing some experiments/ trouble shooting.
299319

320+
#### Eager and Pinned
321+
322+
> Big thanks to [Michael Egger-Zickes](https://twitter.com/MikeZks), who came up with these solutions.
323+
324+
Module Federation allows to directly bundle shared dependencies into your app's bundles. Hence, you don't need to load an additional bundle per shared dependency. This can be interesting to improve an application's startup performance, when there are lots of shared dependencies.
325+
326+
One possible usage for improving the startup times is to set ``eager`` to ``true`` **just** for the host. The remotes loaded later can reuse these eager dependencies alothough they've been shipped via the host's bundle (e. g. its ``main.js``). This works best, if the host always has the highest compatible versions of the shared dependencies. Also, in this case, you don't need to load the remote entry points upfront.
327+
328+
While the ``eager`` flag is an out of the box feature provided by module federation since its very first days, we need to adjust the webpack configuration used by the Angular CLI a bit to avoid code duplication in the generated bundles. The new ``withModuleFederationPlugin`` helper that has been introduces with this plugin's version 14 and is the basis for the new streamlined configuration, does this by default. The config just needs to set eager to ``true``.
329+
330+
```javascript
331+
module.exports = withModuleFederationPlugin({
332+
333+
shared: {
334+
...shareAll({ singleton: true, eager: true, pinned: true, strictVersion: true, requiredVersion: 'auto' }),
335+
},
336+
337+
});
338+
```
339+
340+
As shown in the last example, we also added another property: pinned. This makes sure, the shared dependency in put into the application's (e. g. the host's) bundle, even though it's not used there. This allows to preload dependencies that are needed later but subsequently loaded micro frontends via one bundle.
341+
300342
### Nx Integration
301343

302344
If the plugin detects that you are using Nx (it basically looks for a ``nx.json``), it uses the builders provided by Nx.
@@ -425,8 +467,6 @@ shared: share({
425467
})
426468
```
427469

428-
429-
430470
#### Not exported Components
431471

432472
If you use a shared component without exporting it via your library's barrel (``index.ts`` or ``public-api.ts``), you get the following error at runtime:

libs/mf/collection.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
"factory": "./src/schematics/mf/schematic#add",
88
"schema": "./src/schematics/mf/schema.json",
99
"description": "Initialize an angular project for webpack module federation",
10-
"aliases": ["init"]
10+
},
11+
"init": {
12+
"factory": "./src/schematics/mf/schematic#add",
13+
"schema": "./src/schematics/mf/schema.json",
14+
"description": "Initialize an angular project for webpack module federation",
1115
},
1216
"config": {
1317
"factory": "./src/schematics/mf/schematic",

libs/mf/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@angular-architects/module-federation",
3-
"version": "14.3.0-beta.4",
3+
"version": "14.3.0",
44
"license": "MIT",
55
"repository": {
66
"type": "GitHub",
@@ -17,7 +17,7 @@
1717
"schematics": "./collection.json",
1818
"builders": "./builders.json",
1919
"dependencies": {
20-
"@angular-architects/module-federation-runtime": "14.3.0-beta.4",
20+
"@angular-architects/module-federation-runtime": "14.3.0",
2121
"word-wrap": "^1.2.3",
2222
"callsite": "^1.0.0",
2323
"node-fetch": "^2.6.7",

libs/mf/src/schematics/mf/files/webpack.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@ module.exports = withModuleFederationPlugin({
1616
...shareAll({ singleton: true, strictVersion: true, requiredVersion: 'auto' }),
1717
},
1818

19-
sharedMappings: [],
2019
});

libs/mf/src/schematics/mf/schematic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,11 @@ export default function config(options: MfSchematicSchema): Rule {
301301

302302
const dep = getPackageJsonDependency(tree, "ngx-build-plus");
303303

304-
if (!dep || !semver.satisfies(dep.version, '>=14.0.0-beta.0')) {
304+
if (!dep || !semver.satisfies(dep.version, '>=14.0.0')) {
305305
addPackageJsonDependency(tree, {
306306
name: 'ngx-build-plus',
307307
type: NodeDependencyType.Dev,
308-
version: '>=14.0.0-beta.0',
308+
version: '^14.0.0',
309309
overwrite: true
310310
});
311311

libs/mf/src/schematics/migrate-to-14-3/schematic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function index(): Rule {
99
addPackageJsonDependency(tree, {
1010
name: 'ngx-build-plus',
1111
type: NodeDependencyType.Dev,
12-
version: '>=14.0.0-beta.0',
12+
version: '^14.0.0',
1313
overwrite: true
1414
});
1515

libs/mf/tutorial/mfe1.png

7.88 KB
Loading

libs/mf/tutorial/result.png

-19.8 KB
Loading

0 commit comments

Comments
 (0)