-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathbootstrap-utils.ts
More file actions
72 lines (70 loc) · 3.09 KB
/
bootstrap-utils.ts
File metadata and controls
72 lines (70 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { ApplicationConfig, ApplicationRef, NgZone, Type, reflectComponentType } from '@angular/core';
import { bootstrapApplication, createApplication } from '@angular/platform-browser';
import { Router } from '@angular/router';
import { NgElementConstructor, createCustomElement } from '@angular/elements';
import { connectRouter } from './router-utils';
import { BootstrapFederatedApplicationOptions } from './model/bootstrap-federated-application-options';
import { BootstrapFederatedWebComponentOptions } from './model/bootstrap-federated-webcomponent-options';
export function bootstrapFederatedApplication<TRootComponent>(
component: Type<TRootComponent>,
appConfig: ApplicationConfig,
options: BootstrapFederatedApplicationOptions,
): Promise<ApplicationRef> {
let { appType, enableNgZoneSharing } = options;
enableNgZoneSharing = enableNgZoneSharing !== false;
if (enableNgZoneSharing && appType === 'microfrontend') {
const ngZoneProvider = (globalThis as any).ngZone ? { provide: NgZone, useValue: (globalThis as any).ngZone } : [];
appConfig = { ...appConfig };
appConfig.providers = [ngZoneProvider, ...appConfig.providers];
}
return bootstrapApplication(component, appConfig).then((appRef: ApplicationRef) => {
if (appType === 'shell') {
if (enableNgZoneSharing) {
const ngZone = appRef.injector.get(NgZone);
if (ngZone) {
(globalThis as any).ngZone = ngZone;
}
}
} else if (appType === 'microfrontend') {
const router = appRef.injector.get(Router);
if (router) {
const useHash = location.href.includes('#');
connectRouter(router, useHash);
}
}
return appRef;
});
}
export function bootstrapFederatedWebComponent<TRootComponent>(
component: Type<TRootComponent>,
appConfig: ApplicationConfig,
options: BootstrapFederatedWebComponentOptions,
): Promise<{ appRef: ApplicationRef; elementCtr: NgElementConstructor<TRootComponent> }> {
let { appType, enableNgZoneSharing } = options;
enableNgZoneSharing = enableNgZoneSharing !== false;
if (enableNgZoneSharing && appType === 'microfrontend') {
const ngZoneProvider = (globalThis as any).ngZone ? { provide: NgZone, useValue: (globalThis as any).ngZone } : [];
appConfig = { ...appConfig };
appConfig.providers = [ngZoneProvider, ...appConfig.providers];
}
return createApplication(appConfig).then((appRef: ApplicationRef) => {
if (appType === 'shell') {
if (enableNgZoneSharing) {
const ngZone = appRef.injector.get(NgZone);
if (ngZone) {
(globalThis as any).ngZone = ngZone;
}
}
} else if (appType === 'microfrontend') {
const router = appRef.injector.get(Router);
if (router) {
const useHash = location.href.includes('#');
connectRouter(router, useHash);
}
}
const elementCtr = createCustomElement<TRootComponent>(component, { injector: appRef.injector });
const componentType = reflectComponentType(component);
customElements.define(componentType?.selector || options.elementName, elementCtr);
return { appRef, elementCtr };
});
}