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
Please find the example [here (branch: nf-solution)](https://github.com/manfredsteyer/module-federation-plugin-example/tree/nf-solution):
53
+
Please find the example [here (branch: nf-standalone-solution)](https://github.com/manfredsteyer/module-federation-plugin-example/tree/nf-standalone-solution):
Please note, that the current **experimental** version does **not** support `ng serve`. Hence, you need to build it and serve it from the `dist` folder (this is what npm run build && npm run start in the above shown example do).
65
+
```
66
+
ng serve mfe1 -o
67
+
```
68
+
69
+
Wait until the Micro Frontend is started.
70
+
71
+
Open another console and start the shell:
72
+
73
+
```
74
+
ng serve shell -o
75
+
```
68
76
69
77
## About the Mental Model
70
78
@@ -77,41 +85,43 @@ For this, the mental model introduces several concepts:
77
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.
78
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.
79
87
80
-
## Usage
88
+
## Usage/ Tutorial
81
89
82
-
> You can checkout the [nf-starter branch](https://github.com/manfredsteyer/module-federation-plugin-example/tree/nf-solution) to try out Native Federation.
83
-
84
-
### Adding Native Federation
90
+
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:
> Our `init` schematic shown above generates all of this if you pass `--type host`.
178
-
179
-
You can directly pass a mapping between remote names and their `remoteEntry.json`. The `remoteEntry.json` contains the necessary metadata. It is generated when compiling the remote.
186
+
> Our `init` schematic shown above generates this file for you.
180
187
181
-
Please note that in Native Federation, the remote entry is just a `.json` file while its a `.js` file in Module Federation.
188
+
### Initializing the Host
182
189
183
-
However, you don't need to hardcode this mapping. Feel free to point to the file name of a federation manifest:
190
+
When bootstrapping the host (shell), Native Federation (`projects\shell\src\main.ts`) is initialized:
This manifest can be exchanged when deploying the solution. Hence, you can adopt the solution to the current environment.
201
+
> This file is generated by the schematic described above.
197
202
198
-
> Our `init` schematic shown above generates this variation if you pass `--type dynamic-host`.
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.
199
204
200
-
Credits: The Nx team originally came up with the idea for the manifest.
205
+
**Credits:** The Nx team originally came up with the idea for the manifest.
201
206
202
-
This is what the (also generated) federation manifest looks like:
207
+
This is what the (also generated) federation manifest (`projects\shell\src\assets\federation.manifest.json`) looks like:
203
208
204
209
```json
205
210
{
206
-
"mfe1": "http://localhost:3001/remoteEntry.json"
211
+
"mfe1": "http://localhost:4201/remoteEntry.json"
207
212
}
208
213
```
209
214
215
+
Native Federation generates the `remoteEntry.json`. It contains metadata about the individual remote.
216
+
217
+
If you follow this tutorial, ensure this entry points to port `4201` (!).
218
+
210
219
### Initializing the Remote
211
220
212
-
Also, the remote needs to be initialized. If a remote doesn't load further remotes, you don't need to pass any mappings to `initFederation`:
221
+
When bootstrapping your remote (`projects\mfe1\src\main.ts`), Native Federation is initialized too:
After the initialization, it loads the file `bootstrap.ts` starting your Angular application.
245
235
246
-
This can be used with and without routing; with `NgModule`s but also with **standalone** building blocks. Just use it instead of dynamic imports.
236
+
### Loading a Remote
247
237
248
-
For the sake of compatibility with our Module Federation API, you can also use the `remoteEntry` to identify the remote in question:
238
+
For loading a component (or any other building block) exposed by a remote into the host, use Native Federation's `loadRemoteModule` function together with lazy loading (`projects\shell\src\app\app.routes.ts`):
This library uses Import Maps. As of today, not all browsers support this emerging browser feature, we need a polyfill. We recommend the polyfill `es-module-shims` which has been developed for production use cases. Our schematics install it via npm and add it to your `polyfills.ts`.
262
+
{
263
+
path: '**',
264
+
component: NotFoundComponent,
265
+
},
275
266
276
-
Also, the schematics add the following to your `index.html`:
267
+
// DO NOT insert routes after this one.
268
+
// { path:'**', ...} needs to be the LAST one.
269
+
];
270
+
```
277
271
278
-
```html
279
-
<scripttype="esms-options">
280
-
{
281
-
"shimMode": true,
282
-
"mapOverrides": true
283
-
}
284
-
</script>
272
+
### Starting your example
285
273
286
-
<scripttype="module"src="polyfills.js"></script>
274
+
Start the remote:
287
275
288
-
<scripttype="module-shim"src="main.js"></script>
276
+
```
277
+
ng serve mfe1 -o
289
278
```
290
279
291
-
The script with the type `esms-options` configures the polyfill. This library was built for shim mode. In this mode, the polyfill provides some additional features beyond the proposal for Import Maps. These features, for instance, allow for dynamically creating an import map after loading the first EcmaScript module. Native Federation uses this possibility.
280
+
Once, the remote is started, start the shell:
292
281
293
-
To make the polyfill to load your EcmaScript modules (bundles) in shim mode, assign the type `module-shim`. However, please just use module for the polyfill bundle itself to prevent an hen/egg-issue.
0 commit comments