diff --git a/apps/website-new/docs/en/guide/basic/runtime.mdx b/apps/website-new/docs/en/guide/basic/runtime.mdx index 291e150343..96da199915 100644 --- a/apps/website-new/docs/en/guide/basic/runtime.mdx +++ b/apps/website-new/docs/en/guide/basic/runtime.mdx @@ -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. + +* When to use `FederationHost` ? + +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. + +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. + +```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 diff --git a/apps/website-new/docs/zh/guide/basic/runtime.mdx b/apps/website-new/docs/zh/guide/basic/runtime.mdx index 9c3e747067..196fc8feb7 100644 --- a/apps/website-new/docs/zh/guide/basic/runtime.mdx +++ b/apps/website-new/docs/zh/guide/basic/runtime.mdx @@ -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 差异