Skip to content

Commit 8eaf55a

Browse files
committed
chore: Added docs for sharedMapping feature
1 parent f240548 commit 8eaf55a

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

libs/native-federation/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,34 @@ module.exports = withNativeFederation({
233233

234234
> Our `init` schematic shown above generates this file for you.
235235
236+
### Sharing Mapped Paths and Mapping Versions
237+
238+
In monorepo setups, mapped paths from your `tsconfig` are shared by default. You can restrict this behavior via `sharedMappings`.
239+
240+
If you additionally want to provide version metadata for mapped paths, enable `features.mappingVersion`.
241+
242+
```javascript
243+
const { withNativeFederation, shareAll } = require('@angular-architects/native-federation/config');
244+
245+
module.exports = withNativeFederation({
246+
shared: {
247+
...shareAll({
248+
singleton: true,
249+
strictVersion: true,
250+
requiredVersion: 'auto',
251+
}),
252+
},
253+
sharedMappings: ['@my-org/auth-lib', '@my-org/ui/*'],
254+
features: {
255+
mappingVersion: true,
256+
},
257+
});
258+
```
259+
260+
If `sharedMappings` is omitted, all discovered mapped paths are shared. For more details, see [FAQ for Sharing Libraries](./docs/share-faq.md).
261+
262+
`sharedMappings` reads mapped paths from the workspace root tsconfig file: `tsconfig.base.json` if it exists, otherwise `tsconfig.json`.
263+
236264
### Initializing the Host
237265

238266
When bootstrapping the host (shell), Native Federation (`projects\shell\src\main.ts`) is initialized:

libs/native-federation/docs/share-faq.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,59 @@ skip: [
1414

1515
This speeds up your build and the initial page load. Also, it gives you automatic page reloads within your application when changing the source code.
1616

17+
## Sharing Selected Mapped Paths with `sharedMappings`
18+
19+
In Nx/monorepo setups, Native Federation shares all libraries from your `tsconfig` path mappings by default.
20+
21+
If you only want to share selected mapped paths, you can use `sharedMappings` in your `federation.config.js`:
22+
23+
```js
24+
module.exports = withNativeFederation({
25+
shared: {
26+
...shareAll({
27+
singleton: true,
28+
strictVersion: true,
29+
requiredVersion: 'auto',
30+
}),
31+
},
32+
sharedMappings: ['@my-org/auth-lib', '@my-org/ui/*'],
33+
});
34+
```
35+
36+
Notes:
37+
38+
- `sharedMappings` is optional. If you omit it, all mapped paths are shared.
39+
- You can use wildcard suffixes (for example, `@my-org/ui/*`) to include multiple mapped paths.
40+
- `skip` still applies and can be used to exclude mapped paths even if they were selected via `sharedMappings`.
41+
- Mapped paths are read from the workspace root tsconfig file: `tsconfig.base.json` if present, otherwise `tsconfig.json`.
42+
- The workspace root is detected by searching upward from the current working directory until a `package.json` is found.
43+
44+
## Mapping Versions via `features.mappingVersion`
45+
46+
If your mapped paths point to libraries that have a `package.json` with a `version`, you can include this version in federation metadata by enabling `features.mappingVersion`.
47+
48+
```js
49+
module.exports = withNativeFederation({
50+
shared: {
51+
...shareAll({
52+
singleton: true,
53+
strictVersion: true,
54+
requiredVersion: 'auto',
55+
}),
56+
},
57+
sharedMappings: ['@my-org/auth-lib', '@my-org/ui/*'],
58+
features: {
59+
mappingVersion: true,
60+
},
61+
});
62+
```
63+
64+
Notes:
65+
66+
- Default is `false`.
67+
- If enabled, Native Federation tries to read the version from the mapped library's nearest `package.json`.
68+
- If no `package.json` version is found for a mapping, the version remains empty.
69+
1770
## Using Multiple Framework Versions
1871

1972
After compiling an Angular application, the compilation is accessing Angular's private API. As private APIs do not align with semver, there is no guarantee that your compiled application works with a different version of Angular. Even having a different minor or patch version at runtime can lead to issues.

0 commit comments

Comments
 (0)