Skip to content
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

feat(angular): add exposed methods for dynamic federation #9437

Merged
merged 1 commit into from Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/angular/mfe/index.ts
@@ -0,0 +1 @@
export { setRemoteUrlResolver, loadRemoteModule } from './mfe';
70 changes: 70 additions & 0 deletions packages/angular/mfe/mfe.ts
@@ -0,0 +1,70 @@
export type ResolveRemoteUrlFunction = (
remoteName: string
) => string | Promise<string>;

declare const __webpack_init_sharing__: (scope: 'default') => Promise<void>;
declare const __webpack_share_scopes__: { default: unknown };

let resolveRemoteUrl: ResolveRemoteUrlFunction;
export function setRemoteUrlResolver(
_resolveRemoteUrl: ResolveRemoteUrlFunction
) {
resolveRemoteUrl = _resolveRemoteUrl;
}

let remoteUrlDefinitions: Record<string, string>;
export function setRemoteDefinitions(definitions: Record<string, string>) {
remoteUrlDefinitions = definitions;
}

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 && !remoteUrlDefinitions) {
throw new Error(
'Call setRemoteDefinitions or setRemoteUrlResolver to allow Dynamic Federation to find the remote apps correctly.'
);
}

if (!initialSharingScopeCreated) {
initialSharingScopeCreated = true;
await __webpack_init_sharing__('default');
}

const remoteUrl = remoteUrlDefinitions
? remoteUrlDefinitions[remoteName]
: 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;
}
6 changes: 6 additions & 0 deletions packages/angular/mfe/ng-package.json
@@ -0,0 +1,6 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
"lib": {
"entryFile": "index.ts"
}
}
1 change: 1 addition & 0 deletions packages/angular/module-federation/index.ts
@@ -0,0 +1 @@
export { withModuleFederation } from '../src/utils/mfe/with-module-federation';
2 changes: 1 addition & 1 deletion packages/angular/package.json
Expand Up @@ -22,7 +22,7 @@
"./executors": "./executors.js",
"./tailwind": "./tailwind.js",
"./src/generators/utils": "./src/generators/utils/index.js",
"./module-federation": "./src/utils/mfe/with-module-federation.js"
"./module-federation": "./module-federation/index.js"
},
"author": "Victor Savkin",
"license": "MIT",
Expand Down