feat(compiler): support sync runtime compile
Adds new abstraction `Compiler` with methods `compileComponentAsync` and `compileComponentSync`. This is in preparation of deprecating `ComponentResolver`. `compileComponentSync` is able to compile components synchronously given all components either have an inline template or they have been compiled before. Also changes `TestComponentBuilder.createSync` to take a `Type` and use the new `compileComponentSync` method. Also supports overriding the component metadata even if the component has already been compiled. Also fixes #7084 in a better way. BREAKING CHANGE: `TestComponentBuilder.createSync` now takes a component type and throws if not all templates are either inlined are compiled before via `createAsync`. Closes #9594
This commit is contained in:
@ -6,13 +6,14 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ComponentMetadata, DirectiveMetadata, Injectable} from '@angular/core';
|
||||
import {Compiler, ComponentMetadata, DirectiveMetadata, Injectable, Injector} from '@angular/core';
|
||||
|
||||
import {DirectiveResolver} from '../src/directive_resolver';
|
||||
import {Map} from '../src/facade/collection';
|
||||
import {Type, isPresent} from '../src/facade/lang';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* An implementation of {@link DirectiveResolver} that allows overriding
|
||||
* various properties of directives.
|
||||
@ -22,6 +23,10 @@ export class MockDirectiveResolver extends DirectiveResolver {
|
||||
private _providerOverrides = new Map<Type, any[]>();
|
||||
private viewProviderOverrides = new Map<Type, any[]>();
|
||||
|
||||
constructor(private _injector: Injector) { super(); }
|
||||
|
||||
private get _compiler(): Compiler { return this._injector.get(Compiler); }
|
||||
|
||||
resolve(type: Type): DirectiveMetadata {
|
||||
var dm = super.resolve(type);
|
||||
|
||||
@ -69,9 +74,11 @@ export class MockDirectiveResolver extends DirectiveResolver {
|
||||
|
||||
setProvidersOverride(type: Type, providers: any[]): void {
|
||||
this._providerOverrides.set(type, providers);
|
||||
this._compiler.clearCacheFor(type);
|
||||
}
|
||||
|
||||
setViewProvidersOverride(type: Type, viewProviders: any[]): void {
|
||||
this.viewProviderOverrides.set(type, viewProviders);
|
||||
this._compiler.clearCacheFor(type);
|
||||
}
|
||||
}
|
||||
|
@ -6,39 +6,36 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {AnimationEntryMetadata, ComponentFactory, ComponentResolver, Injectable, Injector, NgZone, ViewMetadata} from '@angular/core';
|
||||
import {AnimationEntryMetadata, Compiler, ComponentFactory, Injectable, Injector, NgZone, ViewMetadata} from '@angular/core';
|
||||
import {ComponentFixture, ComponentFixtureNoNgZone, TestComponentBuilder} from '@angular/core/testing';
|
||||
|
||||
import {DirectiveResolver, ViewResolver} from '../index';
|
||||
import {MapWrapper} from '../src/facade/collection';
|
||||
import {IS_DART, Type, isPresent} from '../src/facade/lang';
|
||||
import {ConcreteType, IS_DART, Type, isPresent} from '../src/facade/lang';
|
||||
|
||||
/**
|
||||
* @deprecated Import TestComponentRenderer from @angular/core/testing
|
||||
*/
|
||||
export {TestComponentRenderer} from '@angular/core/testing';
|
||||
|
||||
/**
|
||||
* @deprecated Import TestComponentBuilder from @angular/core/testing
|
||||
*/
|
||||
export {TestComponentBuilder} from '@angular/core/testing';
|
||||
|
||||
/**
|
||||
* @deprecated Import ComponentFixture from @angular/core/testing
|
||||
*/
|
||||
export {ComponentFixture} from '@angular/core/testing';
|
||||
|
||||
/**
|
||||
* @deprecated Import ComponentFixtureNoNgZone from @angular/core/testing
|
||||
*/
|
||||
export {ComponentFixtureNoNgZone} from '@angular/core/testing';
|
||||
|
||||
/**
|
||||
* @deprecated Import ComponentFixtureAutoDetect from @angular/core/testing
|
||||
*/
|
||||
export {ComponentFixtureAutoDetect} from '@angular/core/testing';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A TestComponentBuilder that allows overriding based on the compiler.
|
||||
*/
|
||||
@ -114,32 +111,31 @@ export class OverridingTestComponentBuilder extends TestComponentBuilder {
|
||||
return clone;
|
||||
}
|
||||
|
||||
createAsync(rootComponentType: Type): Promise<ComponentFixture<any>> {
|
||||
let noNgZone = IS_DART || this._injector.get(ComponentFixtureNoNgZone, false);
|
||||
let ngZone: NgZone = noNgZone ? null : this._injector.get(NgZone, null);
|
||||
createAsync<T>(rootComponentType: ConcreteType<T>): Promise<ComponentFixture<T>> {
|
||||
this._applyMetadataOverrides();
|
||||
return super.createAsync(rootComponentType);
|
||||
}
|
||||
|
||||
let initComponent = () => {
|
||||
let mockDirectiveResolver = this._injector.get(DirectiveResolver);
|
||||
let mockViewResolver = this._injector.get(ViewResolver);
|
||||
this._viewOverrides.forEach((view, type) => mockViewResolver.setView(type, view));
|
||||
this._templateOverrides.forEach(
|
||||
(template, type) => mockViewResolver.setInlineTemplate(type, template));
|
||||
this._animationOverrides.forEach(
|
||||
(animationsEntry, type) => mockViewResolver.setAnimations(type, animationsEntry));
|
||||
this._directiveOverrides.forEach((overrides, component) => {
|
||||
overrides.forEach(
|
||||
(to, from) => { mockViewResolver.overrideViewDirective(component, from, to); });
|
||||
});
|
||||
this._bindingsOverrides.forEach(
|
||||
(bindings, type) => mockDirectiveResolver.setProvidersOverride(type, bindings));
|
||||
this._viewBindingsOverrides.forEach(
|
||||
(bindings, type) => mockDirectiveResolver.setViewProvidersOverride(type, bindings));
|
||||
createSync<T>(rootComponentType: ConcreteType<T>): ComponentFixture<T> {
|
||||
this._applyMetadataOverrides();
|
||||
return super.createSync(rootComponentType);
|
||||
}
|
||||
|
||||
let promise: Promise<ComponentFactory<any>> =
|
||||
this._injector.get(ComponentResolver).resolveComponent(rootComponentType);
|
||||
return promise.then(componentFactory => this.createFromFactory(ngZone, componentFactory));
|
||||
};
|
||||
|
||||
return ngZone == null ? initComponent() : ngZone.run(initComponent);
|
||||
private _applyMetadataOverrides() {
|
||||
let mockDirectiveResolver = this._injector.get(DirectiveResolver);
|
||||
let mockViewResolver = this._injector.get(ViewResolver);
|
||||
this._viewOverrides.forEach((view, type) => { mockViewResolver.setView(type, view); });
|
||||
this._templateOverrides.forEach(
|
||||
(template, type) => mockViewResolver.setInlineTemplate(type, template));
|
||||
this._animationOverrides.forEach(
|
||||
(animationsEntry, type) => mockViewResolver.setAnimations(type, animationsEntry));
|
||||
this._directiveOverrides.forEach((overrides, component) => {
|
||||
overrides.forEach(
|
||||
(to, from) => { mockViewResolver.overrideViewDirective(component, from, to); });
|
||||
});
|
||||
this._bindingsOverrides.forEach(
|
||||
(bindings, type) => mockDirectiveResolver.setProvidersOverride(type, bindings));
|
||||
this._viewBindingsOverrides.forEach(
|
||||
(bindings, type) => mockDirectiveResolver.setViewProvidersOverride(type, bindings));
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {AnimationEntryMetadata, BaseException, Injectable, Type, ViewMetadata, resolveForwardRef} from '@angular/core';
|
||||
import {AnimationEntryMetadata, BaseException, Compiler, Injectable, Injector, Type, ViewMetadata, resolveForwardRef} from '@angular/core';
|
||||
|
||||
import {ViewResolver} from '../index';
|
||||
import {Map} from '../src/facade/collection';
|
||||
@ -21,38 +21,38 @@ export class MockViewResolver extends ViewResolver {
|
||||
/** @internal */
|
||||
_animations = new Map<Type, AnimationEntryMetadata[]>();
|
||||
/** @internal */
|
||||
_viewCache = new Map<Type, ViewMetadata>();
|
||||
/** @internal */
|
||||
_directiveOverrides = new Map<Type, Map<Type, Type>>();
|
||||
|
||||
constructor() { super(); }
|
||||
constructor(private _injector: Injector) { super(); }
|
||||
|
||||
private get _compiler(): Compiler { return this._injector.get(Compiler); }
|
||||
|
||||
private _clearCacheFor(component: Type) { this._compiler.clearCacheFor(component); }
|
||||
|
||||
/**
|
||||
* Overrides the {@link ViewMetadata} for a component.
|
||||
*/
|
||||
setView(component: Type, view: ViewMetadata): void {
|
||||
this._checkOverrideable(component);
|
||||
this._views.set(component, view);
|
||||
this._clearCacheFor(component);
|
||||
}
|
||||
/**
|
||||
* Overrides the inline template for a component - other configuration remains unchanged.
|
||||
*/
|
||||
setInlineTemplate(component: Type, template: string): void {
|
||||
this._checkOverrideable(component);
|
||||
this._inlineTemplates.set(component, template);
|
||||
this._clearCacheFor(component);
|
||||
}
|
||||
|
||||
setAnimations(component: Type, animations: AnimationEntryMetadata[]): void {
|
||||
this._checkOverrideable(component);
|
||||
this._animations.set(component, animations);
|
||||
this._clearCacheFor(component);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides a directive from the component {@link ViewMetadata}.
|
||||
*/
|
||||
overrideViewDirective(component: Type, from: Type, to: Type): void {
|
||||
this._checkOverrideable(component);
|
||||
|
||||
var overrides = this._directiveOverrides.get(component);
|
||||
|
||||
if (isBlank(overrides)) {
|
||||
@ -61,6 +61,7 @@ export class MockViewResolver extends ViewResolver {
|
||||
}
|
||||
|
||||
overrides.set(from, to);
|
||||
this._clearCacheFor(component);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -72,10 +73,7 @@ export class MockViewResolver extends ViewResolver {
|
||||
* - Override the @View definition, see `setInlineTemplate`.
|
||||
*/
|
||||
resolve(component: Type): ViewMetadata {
|
||||
var view = this._viewCache.get(component);
|
||||
if (isPresent(view)) return view;
|
||||
|
||||
view = this._views.get(component);
|
||||
var view = this._views.get(component);
|
||||
if (isBlank(view)) {
|
||||
view = super.resolve(component);
|
||||
}
|
||||
@ -123,26 +121,8 @@ export class MockViewResolver extends ViewResolver {
|
||||
interpolation: view.interpolation
|
||||
});
|
||||
|
||||
this._viewCache.set(component, view);
|
||||
return view;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* Once a component has been compiled, the AppProtoView is stored in the compiler cache.
|
||||
*
|
||||
* Then it should not be possible to override the component configuration after the component
|
||||
* has been compiled.
|
||||
*/
|
||||
_checkOverrideable(component: Type): void {
|
||||
var cached = this._viewCache.get(component);
|
||||
|
||||
if (isPresent(cached)) {
|
||||
throw new BaseException(
|
||||
`The component ${stringify(component)} has already been compiled, its configuration can not be changed`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function flattenArray(tree: any[], out: Array<Type|any[]>): void {
|
||||
|
Reference in New Issue
Block a user