-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathbootstrap-utils.ts
More file actions
224 lines (191 loc) · 5.58 KB
/
bootstrap-utils.ts
File metadata and controls
224 lines (191 loc) · 5.58 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import {
CompilerOptions,
enableProdMode,
Injector,
NgModuleRef,
NgZone,
PlatformRef,
Type,
Version,
VERSION,
} from '@angular/core';
import { platformBrowser } from '@angular/platform-browser';
import { APP_BASE_HREF } from '@angular/common';
import {
getGlobalStateSlice,
setGlobalStateSlice,
} from '../utils/global-state';
import { Router } from '@angular/router';
import { connectRouter } from './router-utils';
export type AppType = 'shell' | 'microfrontend';
export type Options = {
production: boolean;
platformFactory?: () => PlatformRef;
compilerOptions?: CompilerOptions & BootstrapOptions;
version?: () => string | Version;
appType?: AppType;
/**
* Opt-out of ngZone sharing.
* Not recommanded.
* Default value true.
*/
ngZoneSharing?: boolean;
/**
* Opt-out of platformSharing sharing.
* Possible, if dependencies are not shared or each bootstrapped
* remote app uses a different version.
* Default value true.
*/
platformSharing?: boolean;
/**
* Deactivate support for legacy mode.
* Only recommanded if all used implementations depend on
* @angular-architects/module-federation-tools > 13.0.1.
* Default value true.
*/
activeLegacyMode?: boolean;
};
declare interface BootstrapOptions {
ngZone?: NgZone | 'zone.js' | 'noop';
ngZoneEventCoalescing?: boolean;
ngZoneRunCoalescing?: boolean;
}
let ngZoneSharing = true;
let platformSharing = true;
let legacyMode = true;
export function getMajor(version: string): string {
const pre = version.match(/\d+/)[0];
const post = version.match(/-.*/);
if (!pre) {
throw new Error('Cound not identify major version: ' + version);
}
if (post) {
return pre + post[0];
}
return pre;
}
/**
* LEGACY IMPLEMENTATIONS START
*
* Can be deprecated in later major releases.
*
* To increase backwards compatibility legacy and current namespaces
* within the window object are used.
*/
export type LegacyPlatformCache = {
platform: Record<string, PlatformRef>;
};
function getLegacyPlatformCache(): LegacyPlatformCache {
const platformCache = window as unknown as LegacyPlatformCache;
platformCache.platform = platformCache.platform || {};
return platformCache;
}
function getLegacyPlatform(key: string): PlatformRef {
const platform = getLegacyPlatformCache().platform[key];
/**
* If dependencies are not shared, platform with same version is different
* and shared platform will not be returned.
*/
return platform instanceof PlatformRef ? platform : null;
}
function setLegacyPlatform(key: string, platform: PlatformRef): void {
getLegacyPlatformCache().platform[key] = platform;
}
function getLegacyNgZone(): NgZone {
return window['ngZone'];
}
function setLegacyNgZone(zone: NgZone): void {
window['ngZone'] = zone;
}
/**
* LEGACY IMPLEMENTATIONS END
*/
function getPlatformCache(): Map<Version, PlatformRef> {
return (
getGlobalStateSlice(
(state: { platformCache: Map<Version, PlatformRef> }) =>
state.platformCache
) ||
setGlobalStateSlice({
platformCache: new Map<Version, PlatformRef>(),
}).platformCache
);
}
function setPlatform(version: Version, platform: PlatformRef): void {
if (platformSharing) {
legacyMode && setLegacyPlatform(version.full, platform);
getPlatformCache().set(version, platform);
}
}
function getPlatform(options: Options): PlatformRef {
if (!platformSharing) {
return options.platformFactory();
}
const versionResult = options.version();
const version = versionResult === VERSION.full ? VERSION : versionResult;
const versionKey = typeof version === 'string' ? version : version.full;
let platform =
getPlatformCache().get(version as Version) ||
(legacyMode && getLegacyPlatform(versionKey));
if (!platform) {
platform = options.platformFactory();
setPlatform(VERSION, platform);
options.production && enableProdMode();
}
return platform;
}
function getNgZone(): NgZone {
return (
getGlobalStateSlice((state: { ngZone: NgZone }) => state.ngZone) ||
getLegacyNgZone()
);
}
export function shareNgZone(zone: NgZone): void {
if (ngZoneSharing) {
legacyMode && setLegacyNgZone(zone);
setGlobalStateSlice({ ngZone: zone });
}
}
export function bootstrap<M>(
module: Type<M>,
options: Options
): Promise<NgModuleRef<M>> {
ngZoneSharing = options.ngZoneSharing !== false;
platformSharing = options.platformSharing !== false;
legacyMode = options.activeLegacyMode !== false;
options.platformFactory =
options.platformFactory || (() => platformBrowser());
options.version = options.version || (() => VERSION);
if (ngZoneSharing && !options.compilerOptions?.ngZone) {
options.compilerOptions = options.compilerOptions || {};
options.compilerOptions.ngZone = getNgZone();
}
return getPlatform(options)
.bootstrapModule(module, options.compilerOptions)
.then((ref) => {
if (options.appType === 'shell') {
shareShellZone(ref.injector);
} else if (options.appType === 'microfrontend') {
connectMicroFrontendRouter(ref.injector);
}
return ref;
});
}
function shareShellZone(injector: Injector) {
const ngZone = injector.get(NgZone, null);
if (!ngZone) {
console.warn('No NgZone to share found');
return;
}
shareNgZone(ngZone);
}
function connectMicroFrontendRouter(injector: Injector) {
const router = injector.get(Router);
const baseHref = injector.get(APP_BASE_HREF, '');
const useHash = location.href.includes('#');
if (!router) {
console.warn('No router to connect found');
return;
}
connectRouter(router, useHash, baseHref);
}