/
githubmirror
/
angular
Обзор
Документация
Войти
/
githubmirror
/
angular
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/platform-browser/src/browser.ts
316 строк
10 KB
SkyZeroZx
docs(docs-infra): Add build-time validation for API and guide links using route manifest
08 июл 2026, 20:24
08 июл 2026, 20:24
c1829f6
Код
Авторство
О чём код?
/** * @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 {CommonModule, DOCUMENT, ɵPLATFORM_BROWSER_ID as PLATFORM_BROWSER_ID} from '@angular/common'; import { ApplicationConfig, ApplicationModule, ApplicationRef, createPlatformFactory, ErrorHandler, inject, InjectionToken, ɵINJECTOR_SCOPE as INJECTOR_SCOPE, ɵinternalCreateApplication as internalCreateApplication, NgModule, PLATFORM_ID, PLATFORM_INITIALIZER, platformCore, PlatformRef, Provider, RendererFactory2, ɵresolveComponentResources as resolveComponentResources, ɵRuntimeError as RuntimeError, ɵSHARED_STYLES_HOST as SHARED_STYLES_HOST, StaticProvider, NgZone, Testability, TestabilityRegistry, ɵTESTABILITY as TESTABILITY, ɵTESTABILITY_GETTER as TESTABILITY_GETTER, ɵUSE_PENDING_TASKS, Type, ɵsetDocument, } from '@angular/core'; import {BrowserDomAdapter} from './browser/browser_adapter'; import {BrowserGetTestability} from './browser/testability'; import {DomRendererFactory2} from './dom/dom_renderer'; import {DomEventsPlugin} from './dom/events/dom_events'; import {EVENT_MANAGER_PLUGINS, EventManager} from './dom/events/event_manager'; import {KeyEventsPlugin} from './dom/events/key_events'; import {SharedStylesHost} from './dom/shared_styles_host'; import {RuntimeErrorCode} from './errors'; /** * A context object that can be passed to `bootstrapApplication` to provide a pre-existing platform * injector. * * @publicApi */ export interface BootstrapContext { /** * A reference to a platform. */ platformRef: PlatformRef; } /** * Bootstraps an instance of an Angular application and renders a standalone component as the * application's root component. More information about standalone components can be found in [this * guide](guide/components). * * @usageNotes * The root component passed into this function **must** be a standalone one * * ```angular-ts * @Component({ * template: 'Hello world!' * }) * class Root {} * * const appRef: ApplicationRef = await bootstrapApplication(Root); * ``` * * You can add the list of providers that should be available in the application injector by * specifying the `providers` field in an object passed as the second argument: * * ```ts * await bootstrapApplication(Root, { * providers: [ * {provide: BACKEND_URL, useValue: 'https://yourdomain.com/api'} * ] * }); * ``` * * The `importProvidersFrom` helper method can be used to collect all providers from any * existing NgModule (and transitively from all NgModules that it imports): * * ```ts * await bootstrapApplication(Root, { * providers: [ * importProvidersFrom(SomeNgModule) * ] * }); * ``` * * Note: the `bootstrapApplication` method doesn't include [Testability](api/core/Testability) by * default. You can add [Testability](api/core/Testability) by getting the list of necessary * providers using `provideProtractorTestingSupport()` function and adding them into the `providers` * array, for example: * * ```ts * import {provideProtractorTestingSupport} from '@angular/platform-browser'; * * await bootstrapApplication(Root, {providers: [provideProtractorTestingSupport()]}); * ``` * * @param rootComponent A reference to a standalone component that should be rendered. * @param options Extra configuration for the bootstrap operation, see `ApplicationConfig` for * additional info. * @param context Optional context object that can be used to provide a pre-existing * platform injector. This is useful for advanced use-cases, for example, server-side * rendering, where the platform is created for each request. * @returns A promise that returns an `ApplicationRef` instance once resolved. * * @publicApi */ export async function bootstrapApplication( rootComponent: Type<unknown>, options?: ApplicationConfig, context?: BootstrapContext, ): Promise<ApplicationRef> { const config = { rootComponent, ...createProvidersConfig(options, context), }; if ((typeof ngJitMode === 'undefined' || ngJitMode) && typeof fetch === 'function') { await resolveJitResources(); } return internalCreateApplication(config); } /** * Create an instance of an Angular application without bootstrapping any components. This is useful * for the situation where one wants to decouple application environment creation (a platform and * associated injectors) from rendering components on a screen. Components can be subsequently * bootstrapped on the returned `ApplicationRef`. * * @param options Extra configuration for the application environment, see `ApplicationConfig` for * additional info. * @param context Optional context object that can be used to provide a pre-existing * platform injector. This is useful for advanced use-cases, for example, server-side * rendering, where the platform is created for each request. * @returns A promise that returns an `ApplicationRef` instance once resolved. * * @publicApi */ export async function createApplication( options?: ApplicationConfig, context?: BootstrapContext, ): Promise<ApplicationRef> { if ((typeof ngJitMode === 'undefined' || ngJitMode) && typeof fetch === 'function') { await resolveJitResources(); } return internalCreateApplication(createProvidersConfig(options, context)); } function createProvidersConfig(options?: ApplicationConfig, context?: BootstrapContext) { return { platformRef: context?.platformRef, appProviders: [...BROWSER_MODULE_PROVIDERS, ...(options?.providers ?? [])], platformProviders: INTERNAL_BROWSER_PLATFORM_PROVIDERS, }; } /** Attempt to resolve component resources before bootstrapping in JIT mode. */ async function resolveJitResources(): Promise<void> { try { return await resolveComponentResources(fetch); } catch (error) { // Log, but don't block bootstrapping on error. // tslint:disable-next-line:no-console console.error(error); } } /** * Returns a set of providers required to setup [Testability](api/core/Testability) for an * application bootstrapped using the `bootstrapApplication` function. The set of providers is * needed to support testing an application with Protractor (which relies on the Testability APIs * to be present). * * @returns An array of providers required to setup Testability for an application and make it * available for testing using Protractor. * * @publicApi */ export function provideProtractorTestingSupport( options: {usePendingTasksForStability?: boolean} = {}, ): Provider[] { // Return a copy to prevent changes to the original array in case any in-place // alterations are performed to the `provideProtractorTestingSupport` call results in app // code. return [ ...TESTABILITY_PROVIDERS, options?.usePendingTasksForStability !== undefined ? {provide: ɵUSE_PENDING_TASKS, useValue: options.usePendingTasksForStability ?? false} : [], ]; } export function initDomAdapter() { BrowserDomAdapter.makeCurrent(); } export function errorHandler(): ErrorHandler { return new ErrorHandler(); } export function _document(): any { // Tell ivy about the global document ɵsetDocument(document); return document; } const INTERNAL_BROWSER_PLATFORM_PROVIDERS: StaticProvider[] = [ {provide: PLATFORM_ID, useValue: PLATFORM_BROWSER_ID}, {provide: PLATFORM_INITIALIZER, useValue: initDomAdapter, multi: true}, {provide: DOCUMENT, useFactory: _document}, ]; /** * A factory function that returns a `PlatformRef` instance associated with browser service * providers. * * @publicApi */ export const platformBrowser: (extraProviders?: StaticProvider[]) => PlatformRef = createPlatformFactory(platformCore, 'browser', INTERNAL_BROWSER_PLATFORM_PROVIDERS); /** * Internal marker to signal whether providers from the `BrowserModule` are already present in DI. * This is needed to avoid loading `BrowserModule` providers twice. We can't rely on the * `BrowserModule` presence itself, since the standalone-based bootstrap just imports * `BrowserModule` providers without referencing the module itself. */ const BROWSER_MODULE_PROVIDERS_MARKER = new InjectionToken( typeof ngDevMode === 'undefined' || ngDevMode ? 'BrowserModule Providers Marker' : '', ); const TESTABILITY_PROVIDERS = [ { provide: TESTABILITY_GETTER, useClass: BrowserGetTestability, }, { provide: TESTABILITY, useClass: Testability, deps: [NgZone, TestabilityRegistry, TESTABILITY_GETTER], }, { provide: Testability, // Also provide as `Testability` for backwards-compatibility. useClass: Testability, deps: [NgZone, TestabilityRegistry, TESTABILITY_GETTER], }, ]; const BROWSER_MODULE_PROVIDERS: Provider[] = [ {provide: INJECTOR_SCOPE, useValue: 'root'}, {provide: ErrorHandler, useFactory: errorHandler}, { provide: EVENT_MANAGER_PLUGINS, useClass: DomEventsPlugin, multi: true, }, {provide: EVENT_MANAGER_PLUGINS, useClass: KeyEventsPlugin, multi: true}, DomRendererFactory2, {provide: SHARED_STYLES_HOST, useClass: SharedStylesHost}, // Only remains for backwards compatibility, should be removed once g3 no longer needs it. {provide: SharedStylesHost, useExisting: SHARED_STYLES_HOST}, EventManager, {provide: RendererFactory2, useExisting: DomRendererFactory2}, typeof ngDevMode === 'undefined' || ngDevMode ? {provide: BROWSER_MODULE_PROVIDERS_MARKER, useValue: true} : [], ]; /** * Exports required infrastructure for all Angular apps. * Included by default in all Angular apps created with the CLI * `new` command. * Re-exports `CommonModule` and `ApplicationModule`, making their * exports and providers available to all apps. * * @publicApi */ @NgModule({ providers: [...BROWSER_MODULE_PROVIDERS, ...TESTABILITY_PROVIDERS], exports: [CommonModule, ApplicationModule], }) export class BrowserModule { constructor() { if (typeof ngDevMode === 'undefined' || ngDevMode) { const providersAlreadyPresent = inject(BROWSER_MODULE_PROVIDERS_MARKER, { optional: true, skipSelf: true, }); if (providersAlreadyPresent) { throw new RuntimeError( RuntimeErrorCode.BROWSER_MODULE_ALREADY_LOADED, `Providers from the \`BrowserModule\` have already been loaded. If you need access ` + `to common directives such as NgIf and NgFor, import the \`CommonModule\` instead.`, ); } } } }