-
Notifications
You must be signed in to change notification settings - Fork 26.2k
/
Copy pathbootstrap.ts
228 lines (205 loc) Β· 8.28 KB
/
bootstrap.ts
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
225
226
227
228
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {Subscription} from 'rxjs';
import {PROVIDED_NG_ZONE} from '../change_detection/scheduling/ng_zone_scheduling';
import {R3Injector} from '../di/r3_injector';
import {INTERNAL_APPLICATION_ERROR_HANDLER} from '../error_handler';
import {RuntimeError, RuntimeErrorCode} from '../errors';
import {DEFAULT_LOCALE_ID} from '../i18n/localization';
import {LOCALE_ID} from '../i18n/tokens';
import {ImagePerformanceWarning} from '../image_performance_warning';
import {Type} from '../interface/type';
import {PLATFORM_DESTROY_LISTENERS} from './platform_destroy_listeners';
import {setLocaleId} from '../render3/i18n/i18n_locale_id';
import {NgZone} from '../zone/ng_zone';
import {ApplicationInitStatus} from '../application/application_init';
import {ApplicationRef, remove} from '../application/application_ref';
import {PROVIDED_ZONELESS} from '../change_detection/scheduling/zoneless_scheduling';
import {InjectionToken, Injector} from '../di';
import {InternalNgModuleRef, NgModuleRef} from '../linker/ng_module_factory';
import {stringify} from '../util/stringify';
import {isPromise} from '../util/lang';
/**
* InjectionToken to control root component bootstrap behavior.
*
* This token is primarily used in Angular's server-side rendering (SSR) scenarios,
* particularly by the `@angular/ssr` package, to manage whether the root component
* should be bootstrapped during the application initialization process.
*
* ## Purpose:
* During SSR route extraction, setting this token to `false` prevents Angular from
* bootstrapping the root component. This avoids unnecessary component rendering,
* enabling route extraction without requiring additional APIs or triggering
* component logic.
*
* ## Behavior:
* - **`false`**: Prevents the root component from being bootstrapped.
* - **`true`** (default): Proceeds with the normal root component bootstrap process.
*
* This mechanism ensures SSR can efficiently separate route extraction logic
* from component rendering.
*/
export const ENABLE_ROOT_COMPONENT_BOOTSTRAP = new InjectionToken<boolean>(
ngDevMode ? 'ENABLE_ROOT_COMPONENT_BOOTSTRAP' : '',
);
export interface BootstrapConfig {
platformInjector: Injector;
}
export interface ModuleBootstrapConfig<M> extends BootstrapConfig {
moduleRef: InternalNgModuleRef<M>;
allPlatformModules: NgModuleRef<unknown>[];
}
export interface ApplicationBootstrapConfig extends BootstrapConfig {
r3Injector: R3Injector;
rootComponent: Type<unknown> | undefined;
}
function isApplicationBootstrapConfig(
config: ApplicationBootstrapConfig | ModuleBootstrapConfig<unknown>,
): config is ApplicationBootstrapConfig {
return !(config as ModuleBootstrapConfig<unknown>).moduleRef;
}
export function bootstrap<M>(
moduleBootstrapConfig: ModuleBootstrapConfig<M>,
): Promise<NgModuleRef<M>>;
export function bootstrap(
applicationBootstrapConfig: ApplicationBootstrapConfig,
): Promise<ApplicationRef>;
export function bootstrap<M>(
config: ModuleBootstrapConfig<M> | ApplicationBootstrapConfig,
): Promise<ApplicationRef> | Promise<NgModuleRef<M>> {
const envInjector = isApplicationBootstrapConfig(config)
? config.r3Injector
: config.moduleRef.injector;
const ngZone = envInjector.get(NgZone);
return ngZone.run(() => {
if (isApplicationBootstrapConfig(config)) {
config.r3Injector.resolveInjectorInitializers();
} else {
config.moduleRef.resolveInjectorInitializers();
}
const exceptionHandler = envInjector.get(INTERNAL_APPLICATION_ERROR_HANDLER);
if (typeof ngDevMode === 'undefined' || ngDevMode) {
if (envInjector.get(PROVIDED_ZONELESS) && envInjector.get(PROVIDED_NG_ZONE)) {
throw new RuntimeError(
RuntimeErrorCode.PROVIDED_BOTH_ZONE_AND_ZONELESS,
'Invalid change detection configuration: ' +
'provideZoneChangeDetection and provideZonelessChangeDetection cannot be used together.',
);
}
}
let onErrorSubscription: Subscription;
ngZone.runOutsideAngular(() => {
onErrorSubscription = ngZone.onError.subscribe({
next: exceptionHandler,
});
});
// If the whole platform is destroyed, invoke the `destroy` method
// for all bootstrapped applications as well.
if (isApplicationBootstrapConfig(config)) {
const destroyListener = () => envInjector.destroy();
const onPlatformDestroyListeners = config.platformInjector.get(PLATFORM_DESTROY_LISTENERS);
onPlatformDestroyListeners.add(destroyListener);
envInjector.onDestroy(() => {
onErrorSubscription.unsubscribe();
onPlatformDestroyListeners.delete(destroyListener);
});
} else {
const destroyListener = () => config.moduleRef.destroy();
const onPlatformDestroyListeners = config.platformInjector.get(PLATFORM_DESTROY_LISTENERS);
onPlatformDestroyListeners.add(destroyListener);
config.moduleRef.onDestroy(() => {
remove(config.allPlatformModules, config.moduleRef);
onErrorSubscription.unsubscribe();
onPlatformDestroyListeners.delete(destroyListener);
});
}
return _callAndReportToErrorHandler(exceptionHandler, ngZone, () => {
const initStatus = envInjector.get(ApplicationInitStatus);
initStatus.runInitializers();
return initStatus.donePromise.then(() => {
// If the `LOCALE_ID` provider is defined at bootstrap then we set the value for ivy
const localeId = envInjector.get(LOCALE_ID, DEFAULT_LOCALE_ID);
setLocaleId(localeId || DEFAULT_LOCALE_ID);
const enableRootComponentBoostrap = envInjector.get(ENABLE_ROOT_COMPONENT_BOOTSTRAP, true);
if (!enableRootComponentBoostrap) {
if (isApplicationBootstrapConfig(config)) {
return envInjector.get(ApplicationRef);
}
config.allPlatformModules.push(config.moduleRef);
return config.moduleRef;
}
if (typeof ngDevMode === 'undefined' || ngDevMode) {
const imagePerformanceService = envInjector.get(ImagePerformanceWarning);
imagePerformanceService.start();
}
if (isApplicationBootstrapConfig(config)) {
const appRef = envInjector.get(ApplicationRef);
if (config.rootComponent !== undefined) {
appRef.bootstrap(config.rootComponent);
}
return appRef;
} else {
moduleBootstrapImpl?.(config.moduleRef, config.allPlatformModules);
return config.moduleRef;
}
});
});
});
}
/**
* Having a separate symbol for the module boostrap implementation allows us to
* tree shake the module based boostrap implementation in standalone apps.
*/
let moduleBootstrapImpl: undefined | typeof _moduleDoBootstrap;
/**
* Set the implementation of the module based bootstrap.
*/
export function setModuleBootstrapImpl() {
moduleBootstrapImpl = _moduleDoBootstrap;
}
function _moduleDoBootstrap(
moduleRef: InternalNgModuleRef<any>,
allPlatformModules: NgModuleRef<unknown>[],
): void {
const appRef = moduleRef.injector.get(ApplicationRef);
if (moduleRef._bootstrapComponents.length > 0) {
moduleRef._bootstrapComponents.forEach((f) => appRef.bootstrap(f));
} else if (moduleRef.instance.ngDoBootstrap) {
moduleRef.instance.ngDoBootstrap(appRef);
} else {
throw new RuntimeError(
RuntimeErrorCode.BOOTSTRAP_COMPONENTS_NOT_FOUND,
ngDevMode &&
`The module ${stringify(moduleRef.instance.constructor)} was bootstrapped, ` +
`but it does not declare "@NgModule.bootstrap" components nor a "ngDoBootstrap" method. ` +
`Please define one of these.`,
);
}
allPlatformModules.push(moduleRef);
}
function _callAndReportToErrorHandler(
errorHandler: (e: unknown) => void,
ngZone: NgZone,
callback: () => any,
): any {
try {
const result = callback();
if (isPromise(result)) {
return result.catch((e: any) => {
ngZone.runOutsideAngular(() => errorHandler(e));
// rethrow as the exception handler might not do it
throw e;
});
}
return result;
} catch (e) {
ngZone.runOutsideAngular(() => errorHandler(e));
// rethrow as the exception handler might not do it
throw e;
}
}