refactor(core): change module semantics
This contains major changes to the compiler, bootstrap of the platforms and test environment initialization. Main part of #10043 Closes #10164 BREAKING CHANGE: - Semantics and name of `@AppModule` (now `@NgModule`) changed quite a bit. This is actually not breaking as `@AppModules` were not part of rc.4. We will have detailed docs on `@NgModule` separately. - `coreLoadAndBootstrap` and `coreBootstrap` can't be used any more (without migration support). Use `bootstrapModule` / `bootstrapModuleFactory` instead. - All Components listed in routes have to be part of the `declarations` of an NgModule. Either directly on the bootstrap module / lazy loaded module, or in an NgModule imported by them.
This commit is contained in:
@ -7,11 +7,13 @@
|
||||
*/
|
||||
|
||||
import {PlatformLocation} from '@angular/common';
|
||||
import {CompilerFactory, ComponentRef, OpaqueToken, PLATFORM_COMMON_PROVIDERS, PLATFORM_INITIALIZER, PlatformRef, ReflectiveInjector, Type, assertPlatform, coreLoadAndBootstrap, createPlatform, createPlatformFactory, getPlatform} from '@angular/core';
|
||||
import {BROWSER_DYNAMIC_TEST_COMPILER_FACTORY} from '@angular/platform-browser-dynamic/testing';
|
||||
import {analyzeAppProvidersForDeprecatedConfiguration, coreDynamicPlatform} from '@angular/compiler';
|
||||
import {ApplicationRef, CompilerFactory, ComponentRef, NgModule, OpaqueToken, PLATFORM_COMMON_PROVIDERS, PLATFORM_INITIALIZER, PlatformRef, ReflectiveInjector, Type, assertPlatform, bootstrapModule, corePlatform, createPlatform, createPlatformFactory, getPlatform} from '@angular/core';
|
||||
import {BrowserModule} from '@angular/platform-browser';
|
||||
|
||||
import {ReflectionCapabilities, reflector, wtfInit} from '../core_private';
|
||||
import {Console, ReflectionCapabilities, reflector, wtfInit} from '../core_private';
|
||||
|
||||
import {ConcreteType} from './facade/lang';
|
||||
import {Parse5DomAdapter} from './parse5_adapter';
|
||||
|
||||
function notSupported(feature: string): Error {
|
||||
@ -31,23 +33,21 @@ class ServerPlatformLocation extends PlatformLocation {
|
||||
back(): void { notSupported('back'); };
|
||||
}
|
||||
|
||||
/**
|
||||
* A set of providers to initialize the Angular platform in a server.
|
||||
*
|
||||
* Used automatically by `serverBootstrap`, or can be passed to `platform`.
|
||||
* @experimental
|
||||
*/
|
||||
export const SERVER_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
|
||||
PLATFORM_COMMON_PROVIDERS,
|
||||
export const INTERNAL_SERVER_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
|
||||
{provide: PLATFORM_INITIALIZER, useValue: initParse5Adapter, multi: true},
|
||||
{provide: PlatformLocation, useClass: ServerPlatformLocation},
|
||||
];
|
||||
|
||||
const SERVER_DYNAMIC_PROVIDERS: any[] = [
|
||||
SERVER_PLATFORM_PROVIDERS,
|
||||
{provide: CompilerFactory, useValue: BROWSER_DYNAMIC_TEST_COMPILER_FACTORY},
|
||||
];
|
||||
|
||||
/**
|
||||
* A set of providers to initialize the Angular platform in a server.
|
||||
*
|
||||
* Used automatically by `serverBootstrap`, or can be passed to `platform`.
|
||||
* @deprecated Use `serverPlatform()` or create a custom platform factory via
|
||||
* `createPlatformFactory(serverPlatform, ...)`
|
||||
*/
|
||||
export const SERVER_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> =
|
||||
[PLATFORM_COMMON_PROVIDERS, INTERNAL_SERVER_PLATFORM_PROVIDERS];
|
||||
|
||||
function initParse5Adapter() {
|
||||
Parse5DomAdapter.makeCurrent();
|
||||
@ -57,7 +57,8 @@ function initParse5Adapter() {
|
||||
/**
|
||||
* @experimental
|
||||
*/
|
||||
export const serverPlatform = createPlatformFactory('server', SERVER_PLATFORM_PROVIDERS);
|
||||
export const serverPlatform =
|
||||
createPlatformFactory(corePlatform, 'server', INTERNAL_SERVER_PLATFORM_PROVIDERS);
|
||||
|
||||
/**
|
||||
* The server platform that supports the runtime compiler.
|
||||
@ -65,7 +66,7 @@ export const serverPlatform = createPlatformFactory('server', SERVER_PLATFORM_PR
|
||||
* @experimental
|
||||
*/
|
||||
export const serverDynamicPlatform =
|
||||
createPlatformFactory('serverDynamic', SERVER_DYNAMIC_PROVIDERS);
|
||||
createPlatformFactory(coreDynamicPlatform, 'serverDynamic', INTERNAL_SERVER_PLATFORM_PROVIDERS);
|
||||
|
||||
/**
|
||||
* Used to bootstrap Angular in server environment (such as node).
|
||||
@ -81,16 +82,35 @@ export const serverDynamicPlatform =
|
||||
* serverBootstrap(..., [BROWSER_APP_PROVIDERS, BROWSER_APP_COMPILER_PROVIDERS])
|
||||
* ```
|
||||
*
|
||||
* @deprecated create an {@link AppModule} and use {@link bootstrapModule} with the {@link
|
||||
* @deprecated create an {@link NgModule} and use {@link bootstrapModule} with the {@link
|
||||
* serverDynamicPlatform}()
|
||||
* instead.
|
||||
*/
|
||||
export function serverBootstrap(
|
||||
appComponentType: Type,
|
||||
providers: Array<any /*Type | Provider | any[]*/>): Promise<ComponentRef<any>> {
|
||||
export function serverBootstrap<T>(
|
||||
appComponentType: ConcreteType<T>,
|
||||
customProviders: Array<any /*Type | Provider | any[]*/>): Promise<ComponentRef<T>> {
|
||||
console.warn(
|
||||
'serverBootstrap is deprecated. Create an @AppModule and use `bootstrapModule` with the `serverDynamicPlatform()` instead.');
|
||||
'serverBootstrap is deprecated. Create an @NgModule and use `bootstrapModule` with the `serverDynamicPlatform()` instead.');
|
||||
reflector.reflectionCapabilities = new ReflectionCapabilities();
|
||||
var appInjector = ReflectiveInjector.resolveAndCreate(providers, serverPlatform().injector);
|
||||
return coreLoadAndBootstrap(appComponentType, appInjector);
|
||||
|
||||
const deprecatedConfiguration = analyzeAppProvidersForDeprecatedConfiguration(customProviders);
|
||||
const declarations = [deprecatedConfiguration.moduleDeclarations.concat([appComponentType])];
|
||||
|
||||
@NgModule({
|
||||
providers: customProviders,
|
||||
declarations: declarations,
|
||||
imports: [BrowserModule],
|
||||
precompile: [appComponentType]
|
||||
})
|
||||
class DynamicModule {
|
||||
}
|
||||
|
||||
return bootstrapModule(
|
||||
DynamicModule, serverDynamicPlatform(), deprecatedConfiguration.compilerOptions)
|
||||
.then((moduleRef) => {
|
||||
const console = moduleRef.injector.get(Console);
|
||||
deprecatedConfiguration.deprecationMessages.forEach((msg) => console.warn(msg));
|
||||
const appRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
|
||||
return appRef.bootstrap(appComponentType);
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user