Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(angular): add exposed methods for dynamic federation
- Loading branch information
Showing
5 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { setRemoteUrlResolver, loadRemoteModule } from './mfe'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
export type ResolveRemoteUrlFunction = ( | ||
remoteName: string | ||
) => string | Promise<string>; | ||
|
||
declare const __webpack_init_sharing__: (scope: string) => Promise<void>; | ||
declare const __webpack_share_scopes__: { default: unknown }; | ||
|
||
let resolveRemoteUrl: ResolveRemoteUrlFunction; | ||
export function setRemoteUrlResolver( | ||
_resolveRemoteUrl: ResolveRemoteUrlFunction | ||
) { | ||
resolveRemoteUrl = _resolveRemoteUrl; | ||
} | ||
|
||
let remoteModuleMap = new Map<string, unknown>(); | ||
let remoteContainerMap = new Map<string, unknown>(); | ||
export async function loadRemoteModule(remoteName: string, moduleName: string) { | ||
const remoteModuleKey = `${remoteName}:${moduleName}`; | ||
if (remoteModuleMap.has(remoteModuleKey)) { | ||
return remoteModuleMap.get(remoteModuleKey); | ||
} | ||
|
||
const container = remoteContainerMap.has(remoteName) | ||
? remoteContainerMap.get(remoteName) | ||
: await loadRemoteContainer(remoteName); | ||
|
||
const factory = await container.get(moduleName); | ||
const Module = factory(); | ||
|
||
remoteModuleMap.set(remoteModuleKey, Module); | ||
|
||
return Module; | ||
} | ||
|
||
function loadModule(url: string) { | ||
return import(/* webpackIgnore:true */ url); | ||
} | ||
|
||
let initialSharingScopeCreated = false; | ||
async function loadRemoteContainer(remoteName: string) { | ||
if (!resolveRemoteUrl) { | ||
throw new Error('Call setLoadContainerFunction'); | ||
} | ||
|
||
if (!initialSharingScopeCreated) { | ||
initialSharingScopeCreated = true; | ||
await __webpack_init_sharing__('default'); | ||
} | ||
|
||
const remoteUrl = await resolveRemoteUrl(remoteName); | ||
const containerUrl = `${remoteUrl}${ | ||
remoteUrl.endsWith('/') ? '' : '/' | ||
}remoteEntry.mjs`; | ||
|
||
const container = await loadModule(containerUrl); | ||
await container.init(__webpack_share_scopes__.default); | ||
|
||
remoteContainerMap.set(remoteName, container); | ||
return container; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json", | ||
"lib": { | ||
"entryFile": "index.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { withModuleFederation } from '../src/utils/mfe/with-module-federation'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters