refactor(core): clean up platform bootstrap and initTestEnvironment
- Introduces `CompilerFactory` which can be part of a `PlatformRef`. - Introduces `WorkerAppModule`, `WorkerUiModule`, `ServerModule` - Introduces `serverDynamicPlatform` for applications using runtime compilation on the server. - Changes browser bootstrap for runtime and offline compilation (see below for an example). * introduces `bootstrapModule` and `bootstrapModuleFactory` in `@angular/core` * introduces new `browserDynamicPlatform` in `@angular/platform-browser-dynamic - Changes `initTestEnvironment` (which used to be `setBaseTestProviders`) to not take a compiler factory any more (see below for an example). BREAKING CHANGE: ## Migration from `setBaseTestProviders` to `initTestEnvironment`: - For the browser platform: BEFORE: ``` import {setBaseTestProviders} from ‘@angular/core/testing’; import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’; setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS); ``` AFTER: ``` import {initTestEnvironment} from ‘@angular/core/testing’; import {browserDynamicTestPlatform, BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’; initTestEnvironment( BrowserDynamicTestModule, browserDynamicTestPlatform()); ``` - For the server platform: BEFORE: ``` import {setBaseTestProviders} from ‘@angular/core/testing’; import {TEST_SERVER_PLATFORM_PROVIDERS, TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’; setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS, TEST_SERVER_APPLICATION_PROVIDERS); ``` AFTER: ``` import {initTestEnvironment} from ‘@angular/core/testing’; import {serverTestPlatform, ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’; initTestEnvironment( ServerTestModule, serverTestPlatform()); ``` ## Bootstrap changes ``` @AppModule({ modules: [BrowserModule], precompile: [MainComponent], providers: […], // additional providers directives: […], // additional platform directives pipes: […] // additional platform pipes }) class MyModule { constructor(appRef: ApplicationRef) { appRef.bootstrap(MainComponent); } } // offline compile import {browserPlatform} from ‘@angular/platform-browser’; import {bootstrapModuleFactory} from ‘@angular/core’; bootstrapModuleFactory(MyModuleNgFactory, browserPlatform()); // runtime compile long form import {browserDynamicPlatform} from ‘@angular/platform-browser-dynamic’; import {bootstrapModule} from ‘@angular/core’; bootstrapModule(MyModule, browserDynamicPlatform()); ``` Closes #9922 Part of #9726
This commit is contained in:
@ -7,7 +7,7 @@
|
||||
*/
|
||||
|
||||
import {COMMON_DIRECTIVES, COMMON_PIPES, PlatformLocation} from '@angular/common';
|
||||
import {APPLICATION_COMMON_PROVIDERS, AppModule, AppModuleFactory, AppModuleRef, ExceptionHandler, NgZone, OpaqueToken, PLATFORM_COMMON_PROVIDERS, PLATFORM_INITIALIZER, PlatformRef, ReflectiveInjector, RootRenderer, SanitizationService, Testability, assertPlatform, createPlatform, getPlatform, isDevMode} from '@angular/core';
|
||||
import {APPLICATION_COMMON_PROVIDERS, AppModule, AppModuleFactory, AppModuleRef, ExceptionHandler, NgZone, OpaqueToken, PLATFORM_COMMON_PROVIDERS, PLATFORM_INITIALIZER, PlatformRef, ReflectiveInjector, RootRenderer, SanitizationService, Testability, assertPlatform, createPlatform, createPlatformFactory, getPlatform, isDevMode} from '@angular/core';
|
||||
|
||||
import {wtfInit} from '../core_private';
|
||||
import {AnimationDriver} from '../src/dom/animation_driver';
|
||||
@ -28,7 +28,6 @@ import {DomSharedStylesHost, SharedStylesHost} from './dom/shared_styles_host';
|
||||
import {isBlank} from './facade/lang';
|
||||
import {DomSanitizationService, DomSanitizationServiceImpl} from './security/dom_sanitization_service';
|
||||
|
||||
const BROWSER_PLATFORM_MARKER = new OpaqueToken('BrowserPlatformMarker');
|
||||
|
||||
/**
|
||||
* A set of providers to initialize the Angular platform in a web browser.
|
||||
@ -38,8 +37,7 @@ const BROWSER_PLATFORM_MARKER = new OpaqueToken('BrowserPlatformMarker');
|
||||
* @experimental API related to bootstrapping are still under review.
|
||||
*/
|
||||
export const BROWSER_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
|
||||
{provide: BROWSER_PLATFORM_MARKER, useValue: true}, PLATFORM_COMMON_PROVIDERS,
|
||||
{provide: PLATFORM_INITIALIZER, useValue: initDomAdapter, multi: true},
|
||||
PLATFORM_COMMON_PROVIDERS, {provide: PLATFORM_INITIALIZER, useValue: initDomAdapter, multi: true},
|
||||
{provide: PlatformLocation, useClass: BrowserPlatformLocation}
|
||||
];
|
||||
|
||||
@ -80,12 +78,7 @@ export const BROWSER_APP_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
|
||||
/**
|
||||
* @experimental API related to bootstrapping are still under review.
|
||||
*/
|
||||
export function browserPlatform(): PlatformRef {
|
||||
if (isBlank(getPlatform())) {
|
||||
createPlatform(ReflectiveInjector.resolveAndCreate(BROWSER_PLATFORM_PROVIDERS));
|
||||
}
|
||||
return assertPlatform(BROWSER_PLATFORM_MARKER);
|
||||
}
|
||||
export const browserPlatform = createPlatformFactory('browser', BROWSER_PLATFORM_PROVIDERS);
|
||||
|
||||
export function initDomAdapter() {
|
||||
BrowserDomAdapter.makeCurrent();
|
||||
@ -110,7 +103,8 @@ export function _resolveDefaultAnimationDriver(): AnimationDriver {
|
||||
|
||||
/**
|
||||
* The app module for the browser.
|
||||
* @stable
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
@AppModule({
|
||||
providers: [
|
||||
@ -121,37 +115,3 @@ export function _resolveDefaultAnimationDriver(): AnimationDriver {
|
||||
})
|
||||
export class BrowserModule {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance of an `@AppModule` for the browser platform
|
||||
* for offline compilation.
|
||||
*
|
||||
* ## Simple Example
|
||||
*
|
||||
* ```typescript
|
||||
* my_module.ts:
|
||||
*
|
||||
* @AppModule({
|
||||
* modules: [BrowserModule]
|
||||
* })
|
||||
* class MyModule {}
|
||||
*
|
||||
* main.ts:
|
||||
* import {MyModuleNgFactory} from './my_module.ngfactory';
|
||||
* import {bootstrapModuleFactory} from '@angular/platform-browser';
|
||||
*
|
||||
* let moduleRef = bootstrapModuleFactory(MyModuleNgFactory);
|
||||
* ```
|
||||
* @stable
|
||||
*/
|
||||
export function bootstrapModuleFactory<M>(moduleFactory: AppModuleFactory<M>): AppModuleRef<M> {
|
||||
let platformInjector = browserPlatform().injector;
|
||||
// Note: We need to create the NgZone _before_ we instantiate the module,
|
||||
// as instantiating the module creates some providers eagerly.
|
||||
// So we create a mini parent injector that just contains the new NgZone and
|
||||
// pass that as parent to the AppModuleFactory.
|
||||
let ngZone = new NgZone({enableLongStackTrace: isDevMode()});
|
||||
let ngZoneInjector =
|
||||
ReflectiveInjector.resolveAndCreate([{provide: NgZone, useValue: ngZone}], platformInjector);
|
||||
return ngZone.run(() => { return moduleFactory.create(ngZoneInjector); });
|
||||
}
|
||||
|
@ -6,8 +6,8 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {FORM_PROVIDERS} from '@angular/common';
|
||||
import {APPLICATION_COMMON_PROVIDERS, APP_INITIALIZER, ExceptionHandler, NgZone, OpaqueToken, PLATFORM_COMMON_PROVIDERS, PlatformRef, ReflectiveInjector, RootRenderer, assertPlatform, createPlatform, getPlatform} from '@angular/core';
|
||||
import {COMMON_DIRECTIVES, COMMON_PIPES, FORM_PROVIDERS} from '@angular/common';
|
||||
import {APPLICATION_COMMON_PROVIDERS, APP_INITIALIZER, AppModule, ExceptionHandler, NgZone, OpaqueToken, PLATFORM_COMMON_PROVIDERS, PlatformRef, ReflectiveInjector, RootRenderer, assertPlatform, createPlatform, createPlatformFactory, getPlatform} from '@angular/core';
|
||||
|
||||
import {BROWSER_SANITIZATION_PROVIDERS} from './browser';
|
||||
import {isBlank, print} from './facade/lang';
|
||||
@ -28,13 +28,11 @@ class PrintLogger {
|
||||
logGroupEnd() {}
|
||||
}
|
||||
|
||||
const WORKER_APP_PLATFORM_MARKER = new OpaqueToken('WorkerAppPlatformMarker');
|
||||
|
||||
/**
|
||||
* @experimental
|
||||
*/
|
||||
export const WORKER_APP_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> =
|
||||
[PLATFORM_COMMON_PROVIDERS, {provide: WORKER_APP_PLATFORM_MARKER, useValue: true}];
|
||||
PLATFORM_COMMON_PROVIDERS;
|
||||
|
||||
/**
|
||||
* @experimental
|
||||
@ -53,12 +51,7 @@ export const WORKER_APP_APPLICATION_PROVIDERS: Array<any /*Type | Provider | any
|
||||
/**
|
||||
* @experimental
|
||||
*/
|
||||
export function workerAppPlatform(): PlatformRef {
|
||||
if (isBlank(getPlatform())) {
|
||||
createPlatform(ReflectiveInjector.resolveAndCreate(WORKER_APP_PLATFORM_PROVIDERS));
|
||||
}
|
||||
return assertPlatform(WORKER_APP_PLATFORM_MARKER);
|
||||
}
|
||||
export const workerAppPlatform = createPlatformFactory('workerApp', WORKER_APP_PLATFORM_PROVIDERS);
|
||||
|
||||
function _exceptionHandler(): ExceptionHandler {
|
||||
return new ExceptionHandler(new PrintLogger());
|
||||
@ -82,3 +75,16 @@ function createMessageBus(zone: NgZone): MessageBus {
|
||||
function setupWebWorker(): void {
|
||||
WorkerDomAdapter.makeCurrent();
|
||||
}
|
||||
|
||||
/**
|
||||
* The app module for the worker app side.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
@AppModule({
|
||||
providers: WORKER_APP_APPLICATION_PROVIDERS,
|
||||
directives: COMMON_DIRECTIVES,
|
||||
pipes: COMMON_PIPES
|
||||
})
|
||||
export class WorkerAppModule {
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {APPLICATION_COMMON_PROVIDERS, APP_INITIALIZER, ExceptionHandler, Injectable, Injector, NgZone, OpaqueToken, PLATFORM_COMMON_PROVIDERS, PLATFORM_INITIALIZER, PlatformRef, ReflectiveInjector, RootRenderer, Testability, assertPlatform, createPlatform, getPlatform} from '@angular/core';
|
||||
import {APPLICATION_COMMON_PROVIDERS, APP_INITIALIZER, AppModule, ExceptionHandler, Injectable, Injector, NgZone, OpaqueToken, PLATFORM_COMMON_PROVIDERS, PLATFORM_INITIALIZER, PlatformRef, ReflectiveInjector, RootRenderer, Testability, assertPlatform, createPlatform, createPlatformFactory, getPlatform} from '@angular/core';
|
||||
|
||||
import {wtfInit} from '../core_private';
|
||||
|
||||
@ -33,7 +33,6 @@ import {Serializer} from './web_workers/shared/serializer';
|
||||
import {ServiceMessageBrokerFactory, ServiceMessageBrokerFactory_} from './web_workers/shared/service_message_broker';
|
||||
import {MessageBasedRenderer} from './web_workers/ui/renderer';
|
||||
|
||||
const WORKER_RENDER_PLATFORM_MARKER = new OpaqueToken('WorkerRenderPlatformMarker');
|
||||
|
||||
/**
|
||||
* Wrapper class that exposes the Worker
|
||||
@ -72,7 +71,7 @@ export const WORKER_UI_STARTABLE_MESSAGING_SERVICE =
|
||||
* @experimental WebWorker support is currently experimental.
|
||||
*/
|
||||
export const WORKER_UI_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
|
||||
PLATFORM_COMMON_PROVIDERS, {provide: WORKER_RENDER_PLATFORM_MARKER, useValue: true},
|
||||
PLATFORM_COMMON_PROVIDERS,
|
||||
{provide: PLATFORM_INITIALIZER, useValue: initWebWorkerRenderPlatform, multi: true}
|
||||
];
|
||||
|
||||
@ -132,12 +131,7 @@ function initWebWorkerRenderPlatform(): void {
|
||||
/**
|
||||
* @experimental WebWorker support is currently experimental.
|
||||
*/
|
||||
export function workerUiPlatform(): PlatformRef {
|
||||
if (isBlank(getPlatform())) {
|
||||
createPlatform(ReflectiveInjector.resolveAndCreate(WORKER_UI_PLATFORM_PROVIDERS));
|
||||
}
|
||||
return assertPlatform(WORKER_RENDER_PLATFORM_MARKER);
|
||||
}
|
||||
export const workerUiPlatform = createPlatformFactory('workerUi', WORKER_UI_PLATFORM_PROVIDERS);
|
||||
|
||||
function _exceptionHandler(): ExceptionHandler {
|
||||
return new ExceptionHandler(getDOM());
|
||||
@ -181,3 +175,14 @@ function _resolveDefaultAnimationDriver(): AnimationDriver {
|
||||
// work with animations just yet...
|
||||
return AnimationDriver.NOOP;
|
||||
}
|
||||
|
||||
/**
|
||||
* The app module for the worker ui side.
|
||||
* To use this, you need to create an own module that includes this module
|
||||
* and provides the `WORKER_SCRIPT` token.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
@AppModule({providers: WORKER_UI_APPLICATION_PROVIDERS})
|
||||
export class WorkerUiModule {
|
||||
}
|
||||
|
Reference in New Issue
Block a user