diff --git a/.changeset/fast-sheep-melt.md b/.changeset/fast-sheep-melt.md new file mode 100644 index 00000000000..c07cc4027ed --- /dev/null +++ b/.changeset/fast-sheep-melt.md @@ -0,0 +1,5 @@ +--- +'@module-federation/runtime': patch +--- + +onLoad hook will allow you to return a custom module factory or proxy diff --git a/packages/runtime/src/core.ts b/packages/runtime/src/core.ts index 27e340a1b4f..2e2b7d666e7 100644 --- a/packages/runtime/src/core.ts +++ b/packages/runtime/src/core.ts @@ -571,7 +571,7 @@ export class FederationHost { const { pkgNameOrAlias, remote, expose, id: idRes } = remoteMatchInfo; const moduleOrFactory = (await module.get(expose, options)) as T; - await this.hooks.lifecycle.onLoad.emit({ + const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({ id: idRes, pkgNameOrAlias, expose, @@ -583,6 +583,10 @@ export class FederationHost { origin: this, }); + if (typeof moduleWrapper === 'function') { + return moduleWrapper as T; + } + return moduleOrFactory; } catch (error) { const { from = 'runtime' } = options || { from: 'runtime' };