feat(ivy): JIT support for compilation of @Pipes (#24703)

Adds support for compiling @Pipe in JIT mode, along with tests
to verify that certain aspects of compilation are correct.

PR Close #24703
This commit is contained in:
Alex Rickabaugh
2018-06-26 10:44:05 -07:00
committed by Miško Hevery
parent dbdcfed2bd
commit 3d52174bf1
6 changed files with 71 additions and 4 deletions

View File

@ -11,3 +11,4 @@ export const R3_COMPILE_COMPONENT: ((type: any, meta: any) => void)|null = null;
export const R3_COMPILE_DIRECTIVE: ((type: any, meta: any) => void)|null = null;
export const R3_COMPILE_INJECTABLE: ((type: any, meta: any) => void)|null = null;
export const R3_COMPILE_NGMODULE: ((type: any, meta: any) => void)|null = null;
export const R3_COMPILE_PIPE: ((type: any, meta: any) => void)|null = null;

View File

@ -9,9 +9,11 @@
import {compileComponent, compileDirective} from './render3/jit/directive';
import {compileInjectable} from './render3/jit/injectable';
import {compileNgModule} from './render3/jit/module';
import {compilePipe} from './render3/jit/pipe';
export const ivyEnabled = true;
export const R3_COMPILE_COMPONENT = compileComponent;
export const R3_COMPILE_DIRECTIVE = compileDirective;
export const R3_COMPILE_INJECTABLE = compileInjectable;
export const R3_COMPILE_NGMODULE = compileNgModule;
export const R3_COMPILE_PIPE = compilePipe;

View File

@ -8,7 +8,7 @@
import {ChangeDetectionStrategy} from '../change_detection/constants';
import {Provider} from '../di';
import {R3_COMPILE_COMPONENT, R3_COMPILE_DIRECTIVE} from '../ivy_switch';
import {R3_COMPILE_COMPONENT, R3_COMPILE_DIRECTIVE, R3_COMPILE_PIPE} from '../ivy_switch';
import {Type} from '../type';
import {TypeDecorator, makeDecorator, makePropDecorator} from '../util/decorators';
import {ViewEncapsulation} from './view';
@ -810,7 +810,9 @@ export interface Pipe {
*
* @Annotation
*/
export const Pipe: PipeDecorator = makeDecorator('Pipe', (p: Pipe) => ({pure: true, ...p}));
export const Pipe: PipeDecorator = makeDecorator(
'Pipe', (p: Pipe) => ({pure: true, ...p}), undefined, undefined,
(type: Type<any>, meta: Pipe) => (R3_COMPILE_PIPE || (() => {}))(type, meta));
/**

View File

@ -0,0 +1,40 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Expression, WrappedNodeExpr, compilePipeFromMetadata, jitExpression} from '@angular/compiler';
import {Pipe} from '../../metadata/directives';
import {Type} from '../../type';
import {stringify} from '../util';
import {angularCoreEnv} from './environment';
import {NG_PIPE_DEF} from './fields';
import {reflectDependencies} from './util';
export function compilePipe(type: Type<any>, meta: Pipe): void {
let def: any = null;
Object.defineProperty(type, NG_PIPE_DEF, {
get: () => {
if (def === null) {
const sourceMapUrl = `ng://${stringify(type)}/ngPipeDef.js`;
const name = type.name;
const res = compilePipeFromMetadata({
name,
type: new WrappedNodeExpr(type),
deps: reflectDependencies(type),
pipeName: meta.name,
pure: meta.pure !== undefined ? meta.pure : true,
});
def = jitExpression(res.expression, angularCoreEnv, sourceMapUrl);
}
return def;
}
});
}