Skip to content

Commit f13bac2

Browse files
authored
docs: add runtime api and runtime hooks part (#4658)
1 parent 954f06a commit f13bac2

4 files changed

Lines changed: 308 additions & 20 deletions

File tree

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Used to create ModuleFederation instance.
2020
- Multiple ModuleFederation instances need to be created with different configurations for each instance
2121
- You want to use ModuleFederation's partitioning feature to encapsulate corresponding APIs for use in other projects
2222

23-
* `createInstance` will not overwrite an existing instance. Each call will create a new instance.
23+
* `createInstance` always creates a fresh `ModuleFederation` instance and registers it in the global instance registry. It does not look up an existing instance by `name` / `version`, and it does not merge options into an existing instance.
2424

2525
```ts
2626
import { createInstance } from '@module-federation/enhanced/runtime';
@@ -38,17 +38,19 @@ const mf = createInstance({
3838
mf.loadRemote('sub1/util').then((m) => m.add(1, 2, 3));
3939
```
4040

41-
## init <Badge type='danger'>Deprecated</Badge>
41+
## init <Badge type='warning'>Use with caution</Badge>
4242

4343
:::warning Warning
4444

45-
This API will be deprecated. If you need to get an existing instance, you can use [getInstance](#getinstance) to retrieve the created instance.
45+
`init` is still supported, but choose it based on how you want instances to be managed.
4646

47-
If you need to create a new instance, please use the [createInstance](#createinstance).
47+
Unlike [createInstance](#createinstance), `init` does not always create a fresh instance. It first looks up an existing instance by `name` and `version`. If a matching instance is found, it reuses that instance and merges the new options by calling `instance.initOptions(options)`. If no matching instance exists, it falls back to creating one.
48+
49+
Use `init` when you want repeated calls for the same host to reuse and extend a single runtime instance. Use [createInstance](#createinstance) when you need guaranteed isolation and a brand-new instance every time. If you only need the instance created by the build plugin, use [getInstance](#getinstance).
4850

4951
:::
5052

51-
- Type: `init(options: InitOptions): void`
53+
- Type: `init(options: InitOptions): ModuleFederation`
5254
- Used for dynamically registering `Vmok` modules at runtime.
5355
- InitOptions:
5456

@@ -137,7 +139,7 @@ init({
137139
```
138140
</Collapse>
139141

140-
### How to Migrate
142+
### Recommended alternatives
141143

142144
#### Build Plugin(Use build plugin)
143145

@@ -226,7 +228,7 @@ plugins: [mfRuntimePlugin()]
226228
});
227229
```
228230

229-
If you are not using the build plugin, you can replace `init` with [createInstance](#createinstance), which has exactly the same parameters.
231+
If you are not using the build plugin, you can switch from `init` to [createInstance](#createinstance) because the option shape is the same. However, the instance semantics are different: `createInstance` always creates a fresh instance, while `init` may reuse an existing one with the same `name` / `version` and merge the new options into it.
230232

231233
## getInstance
232234

apps/website-new/docs/en/guide/runtime/runtime-hooks.mdx

Lines changed: 141 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ interface RemoteInfo {
130130

131131
`AsyncWaterfallHook`
132132

133-
Triggered before the host calls `remoteEntry.init(...)`. Useful for dynamically rewriting the share-scope alignment for this initialization (shareScopeKeys/shareScopeMap) and the `remoteEntryInitOptions` passed to the remote.
133+
Triggered before the host calls `remoteEntry.init(...)`. Useful for dynamically rewriting the `shareScope` / `initScope` used in this initialization and the `remoteEntryInitOptions` passed to the remote.
134134

135135
* type
136136

@@ -146,6 +146,29 @@ type BeforeInitContainerOptions ={
146146
}
147147
```
148148
149+
## initContainer
150+
151+
`AsyncWaterfallHook`
152+
153+
Triggered after `remoteEntry.init(...)` completes successfully. Useful for metrics, observability, or post-initialization customization.
154+
155+
* type
156+
157+
```ts
158+
async function initContainer(args: InitContainerOptions): Promise<InitContainerOptions>
159+
160+
type InitContainerOptions ={
161+
shareScope: ShareScopeMap[string];
162+
initScope: InitScope;
163+
remoteEntryInitOptions: RemoteEntryInitOptions;
164+
remoteInfo: RemoteInfo;
165+
remoteEntryExports: RemoteEntryExports;
166+
origin: ModuleFederation;
167+
id?: string;
168+
remoteSnapshot?: ModuleInfo;
169+
}
170+
```
171+
149172
## handlePreloadModule
150173
151174
`SyncHook`
@@ -160,8 +183,10 @@ function handlePreloadModule(args: HandlePreloadModuleOptions): void
160183
type HandlePreloadModuleOptions ={
161184
id: string;
162185
name: string;
186+
remote: Remote;
163187
remoteSnapshot: ModuleInfo;
164188
preloadConfig: PreloadRemoteArgs;
189+
origin: ModuleFederation;
165190
}
166191
```
167192
@@ -394,7 +419,7 @@ Called before the preload handler executes any preload logic.
394419
* type
395420

396421
```ts
397-
async function beforePreloadRemote(args: BeforePreloadRemoteOptions): BeforePreloadRemoteOptions
422+
async function beforePreloadRemote(args: BeforePreloadRemoteOptions): Promise<void>
398423

399424
type BeforePreloadRemoteOptions ={
400425
preloadOps: Array<PreloadRemoteArgs>;
@@ -430,6 +455,10 @@ interface PreloadAssets {
430455
}
431456
```
432457

458+
## loaderHook
459+
460+
`loaderHook` is used to intercept resource loading and module-factory resolution.
461+
433462
## createScript
434463

435464
`SyncHook`
@@ -439,10 +468,11 @@ Used to modify the script when loading resources.
439468
* type
440469

441470
```ts
442-
function createScript(args: CreateScriptOptions): HTMLScriptElement | void
471+
function createScript(args: CreateScriptOptions): CreateScriptHookReturn
443472

444473
type CreateScriptOptions ={
445474
url: string;
475+
attrs?: Record<string, any>;
446476
}
447477
```
448478
@@ -498,6 +528,62 @@ export default function (): FederationRuntimePlugin {
498528
};
499529
```
500530

531+
## createLink
532+
533+
`SyncHook`
534+
535+
Used to customize the link element created for preload/style loading.
536+
537+
* type
538+
539+
```ts
540+
function createLink(args: CreateLinkOptions): HTMLLinkElement | void
541+
542+
type CreateLinkOptions ={
543+
url: string;
544+
attrs?: Record<string, any>;
545+
}
546+
```
547+
548+
## loadEntryError
549+
550+
`AsyncHook`
551+
552+
Triggered when loading remoteEntry fails (typically script loading errors). Useful for retries or custom fallback strategies.
553+
554+
* type
555+
556+
```ts
557+
async function loadEntryError(args: LoadEntryErrorOptions): Promise<Promise<RemoteEntryExports | undefined> | undefined>
558+
559+
type LoadEntryErrorOptions ={
560+
getRemoteEntry: typeof getRemoteEntry;
561+
origin: ModuleFederation;
562+
remoteInfo: RemoteInfo;
563+
remoteEntryExports?: RemoteEntryExports;
564+
globalLoading: Record<string, Promise<void | RemoteEntryExports> | undefined>;
565+
uniqueKey: string;
566+
}
567+
```
568+
569+
## getModuleFactory
570+
571+
`AsyncHook`
572+
573+
Triggered before calling `remoteEntry.get(expose)`, allowing custom module-factory resolution.
574+
575+
* type
576+
577+
```ts
578+
async function getModuleFactory(args: GetModuleFactoryOptions): Promise<(() => Promise<Module>) | undefined>
579+
580+
type GetModuleFactoryOptions ={
581+
remoteEntryExports: RemoteEntryExports;
582+
expose: string;
583+
moduleInfo: RemoteInfo;
584+
}
585+
```
586+
501587
## loadEntry
502588
The `loadEntry` function allows for full customization of remotes, enabling you to extend and create new remote types. The following two simple examples demonstrate loading JSON data and module delegation.
503589
@@ -631,3 +717,55 @@ import test2 from "delegateModulesA/test2"
631717
test1 // "test1 value"
632718
test2 // "test2 value"
633719
```
720+
721+
## bridgeHook
722+
723+
`bridgeHook` is defined in `runtime-core/src/core.ts` and is used to extend context across bridge render/destroy stages (for example, React/Vue bridge).
724+
725+
## beforeBridgeRender
726+
727+
`SyncHook`
728+
729+
Triggered before bridge rendering. You can return an object to augment render arguments (for example, `extraProps`).
730+
731+
* type
732+
733+
```ts
734+
function beforeBridgeRender(args: Record<string, any>): void | Record<string, any>
735+
```
736+
737+
## afterBridgeRender
738+
739+
`SyncHook`
740+
741+
Triggered after bridge rendering.
742+
743+
* type
744+
745+
```ts
746+
function afterBridgeRender(args: Record<string, any>): void | Record<string, any>
747+
```
748+
749+
## beforeBridgeDestroy
750+
751+
`SyncHook`
752+
753+
Triggered before bridge teardown.
754+
755+
* type
756+
757+
```ts
758+
function beforeBridgeDestroy(args: Record<string, any>): void | Record<string, any>
759+
```
760+
761+
## afterBridgeDestroy
762+
763+
`SyncHook`
764+
765+
Triggered after bridge teardown.
766+
767+
* type
768+
769+
```ts
770+
function afterBridgeDestroy(args: Record<string, any>): void | Record<string, any>
771+
```

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Runtime from '@components/zh/runtime/index';
2121
- 希望使用 ModuleFederation 分治特性,封装相应的 API 提供给其他项目使用
2222

2323

24-
* `createInstance` 不会对已存在的实例进行覆盖,每次调用都会创建一个新的实例
24+
* `createInstance` 每次都会创建一个全新的 `ModuleFederation` 实例,并将其注册到全局实例列表中。它不会根据 `name` / `version` 查找已有实例,也不会把新配置合并到已有实例上
2525

2626
```ts
2727
import { createInstance } from '@module-federation/enhanced/runtime';
@@ -39,17 +39,19 @@ const mf = createInstance({
3939
mf.loadRemote('sub1/util').then((m) => m.add(1, 2, 3));
4040
```
4141

42-
## init <Badge type='danger'>废弃</Badge>
42+
## init <Badge type='warning'>谨慎使用</Badge>
4343

4444
:::warning Warning
4545

46-
此 API 将被废弃。如果需要获取已创建的实例,可以使用 [getInstance](#getinstance) 获取已创建的实例
46+
`init` 仍然受支持,但需要根据你希望如何管理实例来选择是否使用它
4747

48-
如果需要创建新的实例,请使用 [createInstance](#createinstance)
48+
它与 [createInstance](#createinstance) 的核心区别在于:`init` 不会每次都创建新实例,而是会先根据 `name``version` 查找已有实例。如果找到了匹配实例,就复用该实例,并通过 `instance.initOptions(options)` 合并新的配置;只有在找不到匹配实例时,才会退回到创建新实例。
49+
50+
当你希望同一个 host 的多次调用复用并扩展同一个运行时实例时,适合使用 `init`。当你需要严格隔离、确保每次都拿到全新实例时,请使用 [createInstance](#createinstance)。如果你只是想获取构建插件创建的实例,请使用 [getInstance](#getinstance)
4951

5052
:::
5153

52-
- Type: `init(options: InitOptions): void`
54+
- Type: `init(options: InitOptions): ModuleFederation`
5355
- 用于运行时动态注册 `Vmok` 模块
5456
- InitOptions:
5557

@@ -138,7 +140,7 @@ init({
138140
```
139141
</Collapse>
140142

141-
### 如何迁移
143+
### 推荐替代方式
142144

143145
#### Build Plugin(使用构建插件)
144146

@@ -229,7 +231,7 @@ plugins: [mfRuntimePlugin()]
229231
```
230232

231233

232-
如果没有使用构建插件,你可以使用 [createInstance](#createinstance)替换 `init` ,其参数完全一致
234+
如果没有使用构建插件,你可以从 `init` 切换到 [createInstance](#createinstance),因为两者接收的配置形状是一致的。但要注意实例语义不同:`createInstance` 总是创建新实例,而 `init` 可能会复用同一个 `name` / `version` 对应的已有实例,并把新配置合并进去
233235

234236
## getInstance
235237

0 commit comments

Comments
 (0)