feat(core): allow to add precompiled tokens via a provider
Introduces the new `ANALYZE_FOR_PRECOMPILE` token. This token can be used to create a virtual provider that will populate the `precompile` fields of components and app modules based on its `useValue`. All components that are referenced in the `useValue` value (either directly or in a nested array or map) will be added to the `precompile` property. closes #9874 related to #9726
This commit is contained in:
@ -11,13 +11,16 @@ import {BrowserModule} from '@angular/platform-browser';
|
||||
|
||||
import {AnimateCmp} from './animate';
|
||||
import {BasicComp} from './basic';
|
||||
import {CompWithPrecompile} from './precompile';
|
||||
import {CompWithAnalyzePrecompileProvider, CompWithPrecompile} from './precompile';
|
||||
import {ProjectingComp} from './projection';
|
||||
import {CompWithChildQuery} from './queries';
|
||||
|
||||
@AppModule({
|
||||
modules: [BrowserModule],
|
||||
precompile: [AnimateCmp, BasicComp, CompWithPrecompile, ProjectingComp, CompWithChildQuery]
|
||||
precompile: [
|
||||
AnimateCmp, BasicComp, CompWithPrecompile, CompWithAnalyzePrecompileProvider, ProjectingComp,
|
||||
CompWithChildQuery
|
||||
]
|
||||
})
|
||||
export class MainModule {
|
||||
constructor(public appRef: ApplicationRef) {}
|
||||
|
@ -7,7 +7,7 @@
|
||||
*/
|
||||
|
||||
import {LowerCasePipe, NgIf} from '@angular/common';
|
||||
import {AppModule, Component, ComponentFactoryResolver, Injectable} from '@angular/core';
|
||||
import {ANALYZE_FOR_PRECOMPILE, AppModule, Component, ComponentFactoryResolver, Inject, Injectable, OpaqueToken} from '@angular/core';
|
||||
import {BrowserModule} from '@angular/platform-browser';
|
||||
|
||||
@Injectable()
|
||||
@ -53,3 +53,17 @@ export class SomeModule {
|
||||
})
|
||||
export class SomeModuleUsingParentComp {
|
||||
}
|
||||
|
||||
export const SOME_TOKEN = new OpaqueToken('someToken');
|
||||
|
||||
export function provideValueWithPrecompile(value: any) {
|
||||
return [
|
||||
{provide: SOME_TOKEN, useValue: value},
|
||||
{provide: ANALYZE_FOR_PRECOMPILE, useValue: value, multi: true},
|
||||
];
|
||||
}
|
||||
|
||||
@AppModule({providers: [provideValueWithPrecompile([{a: 'b', component: SomeComp}])]})
|
||||
export class SomeModuleWithAnalyzePrecompileProvider {
|
||||
constructor(@Inject(SOME_TOKEN) public providedValue: any) {}
|
||||
}
|
||||
|
@ -6,10 +6,30 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Component, ComponentFactoryResolver, Inject, OpaqueToken} from '@angular/core';
|
||||
import {ANALYZE_FOR_PRECOMPILE, Component, ComponentFactoryResolver, Inject, OpaqueToken} from '@angular/core';
|
||||
|
||||
import {BasicComp} from './basic';
|
||||
|
||||
@Component({selector: 'cmp-precompile', template: '', precompile: [BasicComp]})
|
||||
export class CompWithPrecompile {
|
||||
constructor(public cfr: ComponentFactoryResolver) {}
|
||||
}
|
||||
|
||||
export const SOME_TOKEN = new OpaqueToken('someToken');
|
||||
|
||||
export function provideValueWithPrecompile(value: any) {
|
||||
return [
|
||||
{provide: SOME_TOKEN, useValue: value},
|
||||
{provide: ANALYZE_FOR_PRECOMPILE, useValue: value, multi: true},
|
||||
];
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'comp-precompile-provider',
|
||||
template: '',
|
||||
providers: [provideValueWithPrecompile([{a: 'b', component: BasicComp}])]
|
||||
})
|
||||
export class CompWithAnalyzePrecompileProvider {
|
||||
constructor(public cfr: ComponentFactoryResolver, @Inject(SOME_TOKEN) public providedValue: any) {
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
*/
|
||||
import './init';
|
||||
import {NestedModule, NestedService, ParentComp, SomeComp, SomeModule, SomeService} from '../src/module_fixtures';
|
||||
import {SomeModuleNgFactory, SomeModuleUsingParentCompNgFactory} from '../src/module_fixtures.ngfactory';
|
||||
import {SomeModuleNgFactory, SomeModuleUsingParentCompNgFactory, SomeModuleWithAnalyzePrecompileProviderNgFactory} from '../src/module_fixtures.ngfactory';
|
||||
import {createComponent, createModule} from './util';
|
||||
|
||||
describe('AppModule', () => {
|
||||
@ -26,6 +26,15 @@ describe('AppModule', () => {
|
||||
expect(compRef.instance instanceof SomeComp).toBe(true);
|
||||
});
|
||||
|
||||
it('should support precompile via the ANALYZE_FOR_PRECOMPILE provider and function providers in components',
|
||||
() => {
|
||||
const moduleRef = createModule(SomeModuleWithAnalyzePrecompileProviderNgFactory);
|
||||
const cf = moduleRef.componentFactoryResolver.resolveComponentFactory(SomeComp);
|
||||
expect(cf.componentType).toBe(SomeComp);
|
||||
// check that the function call that created the provider for ANALYZE_FOR_PRECOMPILE worked.
|
||||
expect(moduleRef.instance.providedValue).toEqual([{a: 'b', component: SomeComp}]);
|
||||
});
|
||||
|
||||
it('should support module directives and pipes', () => {
|
||||
var compFixture = createComponent(SomeComp, SomeModuleNgFactory);
|
||||
var debugElement = compFixture.debugElement;
|
||||
|
@ -7,14 +7,27 @@
|
||||
*/
|
||||
|
||||
import './init';
|
||||
|
||||
import {BasicComp} from '../src/basic';
|
||||
import {CompWithPrecompile} from '../src/precompile';
|
||||
import {CompWithAnalyzePrecompileProvider, CompWithPrecompile} from '../src/precompile';
|
||||
|
||||
import {createComponent} from './util';
|
||||
|
||||
describe('content projection', () => {
|
||||
it('should support basic content projection', () => {
|
||||
it('should support precompile in components', () => {
|
||||
var compFixture = createComponent(CompWithPrecompile);
|
||||
var cf = compFixture.componentInstance.cfr.resolveComponentFactory(BasicComp);
|
||||
expect(cf.componentType).toBe(BasicComp);
|
||||
});
|
||||
|
||||
it('should support precompile via the ANALYZE_FOR_PRECOMPILE provider and function providers in components',
|
||||
() => {
|
||||
const compFixture = createComponent(CompWithAnalyzePrecompileProvider);
|
||||
const cf = compFixture.componentInstance.cfr.resolveComponentFactory(BasicComp);
|
||||
expect(cf.componentType).toBe(BasicComp);
|
||||
// check that the function call that created the provider for ANALYZE_FOR_PRECOMPILE worked.
|
||||
expect(compFixture.componentInstance.providedValue).toEqual([
|
||||
{a: 'b', component: BasicComp}
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user