Skip to content

docs: add FederationHost class usage description #3827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions apps/website-new/docs/en/guide/basic/runtime.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,31 @@ registerPlugins([runtimePlugin()]);

If you want to develop Module Federation plugin, you can read [Module Federation Plugin System](../../plugin/dev/index) for more info.

## FederationHost

In addition to exposing APIs, Federation Runtime also provides the FederationHost class, which you can use to create a FederationHost instance.
Copy link
Member

@ScriptedAlchemy ScriptedAlchemy Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In addition to exposing APIs, Federation Runtime also provides the FederationHost class, which you can use to create a FederationHost instance.
In addition to exposing APIs, the Federation Runtime also provides a FederationHost class, which you can use to create FederationHost instances.


* When to use `FederationHost` ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be a good idea to have small section on "What is FederationHost"

How i explained it to people before was that in the FEDERATION.INSTANCES - each runtime or application who uses module federation creates a FederationHost instance, it represents an both consumer and provider instances of the system. Or something so users understand what federation host actually is for


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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


However, this singleton pattern also limits the inability to create multiple instances. Therefore, if you need to create a new instance , you can use the FederationHost class to create a new one.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
However, this singleton pattern also limits the inability to create multiple instances. Therefore, if you need to create a new instance , you can use the FederationHost class to create a new one.
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.


```ts
import { FederationHost } from '@module-federation/enhanced/runtime';

const host = new FederationHost({
name: '@demo/host',
remotes: [
{
name: '@demo/sub1',
entry: 'http://localhost:8080/mf-manifest.json'
}
]
});

host.loadRemote('@demo/sub1/util').then((m) => m.add(1, 2, 3));
```

## FAQ

Expand Down
26 changes: 26 additions & 0 deletions apps/website-new/docs/zh/guide/basic/runtime.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,32 @@ registerPlugins([runtimePlugin()]);

如果你需要开发 Module Federation 插件,可以阅读 [Module Federation 插件系统](../../plugin/dev/index) 获取更多信息。

## FederationHost

`Federation Runtime` 除了暴露出 API 外,还提供了 `FederationHost` 类,你可以使用它来创建一个 `FederationHost` 实例。

* 什么时候使用 `FederationHost` ?

为了保证 `FederationHost` 实例的唯一性,我们在构建插件创建实例后,会将其存储到内存中,导出的 API 都是先从内存中获取 `FederationHost` 实例,然后再调用 `FederationHost` 实例的 API。这也是为什么 `loadRemote` 等 API 可以直接使用的原因。

但是这种单例模式同时也限制无法创建多份实例,因此如果你需要**创建新的实例**,那么你可以使用 `FederationHost` 类来创建一个新的实例。

```ts
import { FederationHost } from '@module-federation/enhanced/runtime';

const host = new FederationHost({
name: '@demo/host',
remotes: [
{
name: '@demo/sub1',
entry: 'http://localhost:8080/mf-manifest.json'
}
]
});

host.loadRemote('@demo/sub1/util').then((m) => m.add(1, 2, 3));
```

## FAQ

### 构建插件和 Runtime 差异
Expand Down