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:
Tobias Bosch
2016-07-18 03:50:31 -07:00
parent ca16fc29a6
commit 46b212706b
129 changed files with 3580 additions and 3366 deletions

View File

@ -11,3 +11,4 @@ import {__core_private__ as r, __core_private_types__ as t} from '@angular/core'
export var reflector: typeof t.reflector = r.reflector;
export var ReflectionCapabilities: typeof t.ReflectionCapabilities = r.ReflectionCapabilities;
export var wtfInit: typeof t.wtfInit = r.wtfInit;
export var Console: typeof t.Console = r.Console;

View File

@ -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);
});
}

View File

@ -6,44 +6,67 @@
* found in the LICENSE file at https://angular.io/license
*/
import {AppModule, CompilerFactory, OpaqueToken, PLATFORM_COMMON_PROVIDERS, PLATFORM_INITIALIZER, PlatformRef, ReflectiveInjector, assertPlatform, createPlatform, createPlatformFactory, getPlatform} from '@angular/core';
import {BROWSER_DYNAMIC_TEST_COMPILER_FACTORY, BrowserDynamicTestModule, TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from '@angular/platform-browser-dynamic/testing';
import {analyzeAppProvidersForDeprecatedConfiguration} from '@angular/compiler';
import {coreDynamicTestingPlatform} from '@angular/compiler/testing';
import {CompilerFactory, CompilerOptions, NgModule, OpaqueToken, PLATFORM_COMMON_PROVIDERS, PLATFORM_INITIALIZER, PlatformRef, ReflectiveInjector, assertPlatform, createPlatform, createPlatformFactory, getPlatform} from '@angular/core';
import {initTestEnvironment} from '@angular/core/testing';
import {BrowserDynamicTestingModule, TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS, browserDynamicTestingPlatform} from '@angular/platform-browser-dynamic/testing';
import {Console} from '../core_private';
import {serverPlatform} from '../index';
import {Parse5DomAdapter} from '../src/parse5_adapter';
function initServerTests() {
Parse5DomAdapter.makeCurrent();
}
/**
* @deprecated Use initTestEnvironment with serverTestPlatform instead.
*/
export const TEST_SERVER_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> =
/*@ts2dart_const*/[
PLATFORM_COMMON_PROVIDERS,
/*@ts2dart_Provider*/ {provide: PLATFORM_INITIALIZER, useValue: initServerTests, multi: true},
{provide: CompilerFactory, useValue: BROWSER_DYNAMIC_TEST_COMPILER_FACTORY},
];
import {INTERNAL_SERVER_PLATFORM_PROVIDERS} from '../src/server';
/**
* Platform for testing
*
* @experimental API related to bootstrapping are still under review.
*/
export const serverTestPlatform =
createPlatformFactory('serverTest', TEST_SERVER_PLATFORM_PROVIDERS);
export const serverTestingPlatform = createPlatformFactory(
coreDynamicTestingPlatform, 'serverTesting', INTERNAL_SERVER_PLATFORM_PROVIDERS);
/**
* AppModule for testing.
* NgModule for testing.
*
* @stable
* @experimental API related to bootstrapping are still under review.
*/
@AppModule({modules: [BrowserDynamicTestModule]})
export class ServerTestModule {
@NgModule({exports: [BrowserDynamicTestingModule]})
export class ServerTestingModule {
}
/**
* @deprecated Use initTestEnvironment with ServerTestModule instead.
* Providers of the `serverTestingPlatform` to be used for creating own platform based on this.
*
* @deprecated Use `serverTestingPlatform()` or create a custom platform factory via
* `createPlatformFactory(serverTestingPlatform, ...)`
*/
export const TEST_SERVER_APPLICATION_PROVIDERS: Array<any /*Type | Provider | any[]*/> =
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS;
export const TEST_SERVER_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> =
// Note: This is not a real provider but a hack to still support the deprecated
// `setBaseTestProviders` method!
[(appProviders: any[]) => {
const deprecatedConfiguration = analyzeAppProvidersForDeprecatedConfiguration(appProviders);
const platformRef = createPlatformFactory(serverTestingPlatform, 'serverTestingDeprecated', [{
provide: CompilerOptions,
useValue: deprecatedConfiguration.compilerOptions,
multi: true
}])();
@NgModule({
exports: [ServerTestingModule],
declarations: [deprecatedConfiguration.moduleDeclarations]
})
class DynamicTestModule {
}
const testInjector = initTestEnvironment(DynamicTestModule, platformRef);
const console: Console = testInjector.get(Console);
deprecatedConfiguration.deprecationMessages.forEach((msg) => console.warn(msg));
}];
/**
* @deprecated Use initTestEnvironment with ServerTestModule instead. This is empty for backwards
* compatibility,
* as all of our bootstrap methods add a module implicitly, i.e. keeping this filled would add the
* providers 2x.
*/
export const TEST_SERVER_APPLICATION_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [];