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

@ -6,90 +6,70 @@
* found in the LICENSE file at https://angular.io/license
*/
import {COMMON_DIRECTIVES, COMMON_PIPES} from '@angular/common';
import {CompilerConfig, DirectiveResolver, ViewResolver} from '@angular/compiler';
import {MockDirectiveResolver, MockViewResolver, OverridingTestComponentBuilder} from '@angular/compiler/testing';
import {AppModule, Compiler, CompilerFactory, PLATFORM_DIRECTIVES, PLATFORM_PIPES, PlatformRef, Provider, ReflectiveInjector, Type, createPlatformFactory} from '@angular/core';
import {TestComponentBuilder, TestComponentRenderer} from '@angular/core/testing';
import {BrowserTestModule, TEST_BROWSER_APPLICATION_PROVIDERS, TEST_BROWSER_PLATFORM_PROVIDERS} from '@angular/platform-browser/testing';
import {CompilerConfig, DirectiveResolver, NgModuleResolver, ViewResolver, analyzeAppProvidersForDeprecatedConfiguration} from '@angular/compiler';
import {OverridingTestComponentBuilder, coreDynamicTestingPlatform} from '@angular/compiler/testing';
import {Compiler, CompilerFactory, CompilerOptions, NgModule, PlatformRef, Provider, ReflectiveInjector, Type, createPlatform, createPlatformFactory} from '@angular/core';
import {TestComponentBuilder, TestComponentRenderer, initTestEnvironment} from '@angular/core/testing';
import {BrowserTestingModule, browserTestingPlatform} from '@angular/platform-browser/testing';
import {BROWSER_APP_COMPILER_PROVIDERS, BROWSER_DYNAMIC_COMPILER_FACTORY, BROWSER_DYNAMIC_PLATFORM_PROVIDERS} from './index';
import {Console} from './core_private';
import {browserDynamicPlatform} from './index';
import {INTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS} from './src/platform_providers';
import {DOMTestComponentRenderer} from './testing/dom_test_component_renderer';
export * from './private_export_testing'
/**
* CompilerFactory for browser dynamic test platform
*
* @experimental
*/
export const BROWSER_DYNAMIC_TEST_COMPILER_FACTORY = BROWSER_DYNAMIC_COMPILER_FACTORY.withDefaults({
providers: [
{provide: DirectiveResolver, useClass: MockDirectiveResolver},
{provide: ViewResolver, useClass: MockViewResolver}
]
});
/**
* Providers for the browser dynamic platform
*
* @experimental
*/
const BROWSER_DYNAMIC_TEST_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
TEST_BROWSER_PLATFORM_PROVIDERS,
BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
{provide: CompilerFactory, useValue: BROWSER_DYNAMIC_TEST_COMPILER_FACTORY},
];
/**
* @experimental API related to bootstrapping are still under review.
*/
export const browserDynamicTestPlatform =
createPlatformFactory('browserDynamicTest', BROWSER_DYNAMIC_TEST_PLATFORM_PROVIDERS);
export const browserDynamicTestingPlatform = createPlatformFactory(
coreDynamicTestingPlatform, 'browserDynamicTesting',
INTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS);
/**
* AppModule for testing.
* NgModule for testing.
*
* @stable
*/
@AppModule({
modules: [BrowserTestModule],
@NgModule({
exports: [BrowserTestingModule],
providers: [
{provide: TestComponentBuilder, useClass: OverridingTestComponentBuilder},
{provide: TestComponentRenderer, useClass: DOMTestComponentRenderer},
]
})
export class BrowserDynamicTestModule {
export class BrowserDynamicTestingModule {
}
// Used only as a shim until TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS is deprecated.
const BROWSER_DYNAMIC_TEST_COMPILER_FACTORY_OLD = BROWSER_DYNAMIC_COMPILER_FACTORY.withDefaults({
providers: [
{provide: DirectiveResolver, useClass: MockDirectiveResolver},
{provide: ViewResolver, useClass: MockViewResolver}
],
deprecatedAppProviders: [
{provide: PLATFORM_DIRECTIVES, useValue: COMMON_DIRECTIVES, multi: true},
{provide: PLATFORM_PIPES, useValue: COMMON_PIPES, multi: true}
]
});
/**
* @deprecated Use initTestEnvironment with browserDynamicTestingPlatform instead.
*/
export const TEST_BROWSER_DYNAMIC_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(browserDynamicTestingPlatform, 'browserDynamicTestingDeprecated', [{
provide: CompilerOptions,
useValue: deprecatedConfiguration.compilerOptions,
multi: true
}])();
@NgModule({
exports: [BrowserDynamicTestingModule],
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 browserDynamicTestPlatform instead.
* @deprecated Use initTestEnvironment with BrowserDynamicTestingModule instead.
*/
export const TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
TEST_BROWSER_PLATFORM_PROVIDERS,
BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
{provide: CompilerFactory, useValue: BROWSER_DYNAMIC_TEST_COMPILER_FACTORY_OLD},
];
/**
* @deprecated Use initTestEnvironment with BrowserDynamicTestModule instead.
*/
export const TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
TEST_BROWSER_APPLICATION_PROVIDERS,
{provide: TestComponentBuilder, useClass: OverridingTestComponentBuilder},
{provide: TestComponentRenderer, useClass: DOMTestComponentRenderer},
];
export const TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS: Array<any /*Type | Provider | any[]*/> =
[];