Skip to content

Commit b7862af

Browse files
committed
docs: add FederationConstructor
1 parent 790302a commit b7862af

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

apps/website-new/docs/en/guide/basic/runtime/runtime-api.mdx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,4 +416,34 @@ registerPlugins([
416416
]);
417417
```
418418
419+
## FederationConstructor
419420
421+
In addition to exposing APIs, the Federation Runtime also provides a FederationConstructor class, which you can use to create Federation instance.
422+
423+
* What is a `Federation` instance?
424+
425+
A `Federation` instance is an instance of the `FederationConstructor` class, which contains all the functionality of the `Federation` runtime.
426+
427+
> You can enter `__FEDERATION__.__INSTANCES__` in the console to view the created instances.
428+
429+
* When to use `FederationConstructor`?
430+
431+
To ensure the uniqueness of the FederationHost instance, after the build plugin creates an instance, it will be stored in memory. The exported APIs all first obtain the FederationHost instance from memory and then call the APIs of the FederationHost instance. This is also why APIs like loadRemote can be used directly from the `module-federation/runtime` package and inherently understand what application container they are attached to.
432+
433+
However, this singleton pattern also limits the ability to create multiple instances, as it assumes that there is only one instance per bundle. Therefore, if you need to create a new instance , you can use the FederationHost class to create a new one.
434+
435+
```ts
436+
import { FederationConstructor } from '@vmok/kit/runtime';
437+
438+
const host = new FederationConstructor({
439+
name: '@demo/host',
440+
remotes: [
441+
{
442+
name: '@demo/sub1',
443+
entry: 'http://localhost:8080/vmok-manifest.json'
444+
}
445+
]
446+
});
447+
448+
host.loadRemote('@demo/sub1/util').then((m) => m.add(1, 2, 3));
449+
```

apps/website-new/docs/zh/guide/basic/runtime/runtime-api.mdx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,4 +416,34 @@ registerPlugins([
416416
]);
417417
```
418418
419+
## FederationConstructor
419420
421+
`Federation Runtime` 除了暴露出 API 外,还提供了 `FederationConstructor` 类,你可以使用它来创建一个 `Federation` 实例。
422+
423+
* 什么是 `Federation` 实例 ?
424+
425+
`Federation` 实例是 `FederationConstructor` 类的实例,它包含了 `Federation` 运行时的所有功能。
426+
427+
> 你可以在控制台输入 `__FEDERATION__.__INSTANCES__` 来查看已经创建好的实例。
428+
429+
* 什么时候使用 `FederationConstructor`
430+
431+
为了保证 `FederationConstructor` 实例的唯一性,我们在构建插件创建实例后,会将其存储到内存中,导出的 API 都是先从内存中获取 `FederationConstructor` 实例,然后再调用 `FederationConstructor` 实例的 API。这也是为什么 `loadRemote` 等 API 可以直接使用的原因。
432+
433+
但是这种单例模式同时也限制无法创建多份实例,因此如果你需要**创建新的 FederationConstructor 实例**,那么你可以使用 `FederationConstructor` 类来创建一个新的实例。
434+
435+
```ts
436+
import { FederationConstructor } from '@module-federation/enhanced/runtime';
437+
438+
const host = new FederationConstructor({
439+
name: 'host',
440+
remotes: [
441+
{
442+
name: 'sub1',
443+
entry: 'http://localhost:8080/mf-manifest.json'
444+
}
445+
]
446+
});
447+
448+
host.loadRemote('@demo/sub1/util').then((m) => m.add(1, 2, 3));
449+
```

0 commit comments

Comments
 (0)