2018-05-09 08:35:25 -07:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2019-01-11 16:07:01 -08:00
|
|
|
import {R3DirectiveMetadataFacade, getCompilerFacade} from '../../compiler/compiler_facade';
|
2019-04-21 17:37:15 +02:00
|
|
|
import {R3BaseMetadataFacade, R3ComponentMetadataFacade, R3QueryMetadataFacade} from '../../compiler/compiler_facade_interface';
|
2018-12-18 13:36:19 -08:00
|
|
|
import {resolveForwardRef} from '../../di/forward_ref';
|
2019-04-16 16:58:44 -07:00
|
|
|
import {compileInjectable} from '../../di/jit/injectable';
|
2019-01-11 16:07:01 -08:00
|
|
|
import {getReflect, reflectDependencies} from '../../di/jit/util';
|
2019-01-09 13:49:16 -08:00
|
|
|
import {Type} from '../../interface/type';
|
2018-10-29 10:07:40 +01:00
|
|
|
import {Query} from '../../metadata/di';
|
2019-01-28 20:45:41 -08:00
|
|
|
import {Component, Directive, Input} from '../../metadata/directives';
|
2018-06-22 19:05:31 -07:00
|
|
|
import {componentNeedsResolution, maybeQueueResolutionOfComponentResources} from '../../metadata/resource_loading';
|
2018-07-31 11:14:06 -07:00
|
|
|
import {ViewEncapsulation} from '../../metadata/view';
|
2019-04-21 17:37:15 +02:00
|
|
|
import {getBaseDef, getComponentDef, getDirectiveDef} from '../definition';
|
2018-12-13 15:51:47 -08:00
|
|
|
import {EMPTY_ARRAY, EMPTY_OBJ} from '../empty';
|
2019-04-21 17:37:15 +02:00
|
|
|
import {NG_BASE_DEF, NG_COMPONENT_DEF, NG_DIRECTIVE_DEF} from '../fields';
|
2019-04-01 15:36:43 -07:00
|
|
|
import {ComponentType} from '../interfaces/definition';
|
2019-02-20 14:21:20 -08:00
|
|
|
import {renderStringify} from '../util/misc_utils';
|
2018-05-09 08:35:25 -07:00
|
|
|
|
|
|
|
import {angularCoreEnv} from './environment';
|
2018-12-03 13:58:07 -08:00
|
|
|
import {flushModuleScopingQueueAsMuchAsPossible, patchComponentDefWithScope, transitiveScopesFor} from './module';
|
2018-05-09 08:35:25 -07:00
|
|
|
|
2018-10-24 16:02:25 -07:00
|
|
|
|
2018-06-12 16:58:09 -07:00
|
|
|
|
2018-05-09 08:35:25 -07:00
|
|
|
/**
|
|
|
|
* Compile an Angular component according to its decorator metadata, and patch the resulting
|
|
|
|
* ngComponentDef onto the component type.
|
|
|
|
*
|
|
|
|
* Compilation may be asynchronous (due to the need to resolve URLs for the component template or
|
|
|
|
* other resources, for example). In the event that compilation is not immediate, `compileComponent`
|
2018-06-22 19:05:31 -07:00
|
|
|
* will enqueue resource resolution into a global queue and will fail to return the `ngComponentDef`
|
|
|
|
* until the global queue has been resolved with a call to `resolveComponentResources`.
|
2018-05-09 08:35:25 -07:00
|
|
|
*/
|
2018-06-22 19:05:31 -07:00
|
|
|
export function compileComponent(type: Type<any>, metadata: Component): void {
|
2018-08-06 14:09:38 -07:00
|
|
|
let ngComponentDef: any = null;
|
2018-06-22 19:05:31 -07:00
|
|
|
// Metadata may have resources which need to be resolved.
|
2019-03-11 10:35:25 -07:00
|
|
|
maybeQueueResolutionOfComponentResources(type, metadata);
|
2018-06-06 11:23:38 -07:00
|
|
|
Object.defineProperty(type, NG_COMPONENT_DEF, {
|
2018-05-21 08:15:19 -07:00
|
|
|
get: () => {
|
2018-10-24 16:02:25 -07:00
|
|
|
const compiler = getCompilerFacade();
|
2018-08-06 14:09:38 -07:00
|
|
|
if (ngComponentDef === null) {
|
2018-06-22 19:05:31 -07:00
|
|
|
if (componentNeedsResolution(metadata)) {
|
2019-01-12 00:59:48 -08:00
|
|
|
const error = [`Component '${renderStringify(type)}' is not resolved:`];
|
2018-06-22 19:05:31 -07:00
|
|
|
if (metadata.templateUrl) {
|
2019-01-12 00:59:48 -08:00
|
|
|
error.push(` - templateUrl: ${renderStringify(metadata.templateUrl)}`);
|
2018-06-22 19:05:31 -07:00
|
|
|
}
|
|
|
|
if (metadata.styleUrls && metadata.styleUrls.length) {
|
|
|
|
error.push(` - styleUrls: ${JSON.stringify(metadata.styleUrls)}`);
|
|
|
|
}
|
|
|
|
error.push(`Did you run and wait for 'resolveComponentResources()'?`);
|
|
|
|
throw new Error(error.join('\n'));
|
|
|
|
}
|
2018-12-03 13:58:07 -08:00
|
|
|
|
2019-02-08 22:10:20 +00:00
|
|
|
const templateUrl = metadata.templateUrl || `ng:///${renderStringify(type)}/template.html`;
|
2018-10-24 16:02:25 -07:00
|
|
|
const meta: R3ComponentMetadataFacade = {
|
|
|
|
...directiveMetadata(type, metadata),
|
2019-01-24 17:25:46 -08:00
|
|
|
typeSourceSpan:
|
2019-02-08 22:10:20 +00:00
|
|
|
compiler.createParseSourceSpan('Component', renderStringify(type), templateUrl),
|
2018-10-24 16:02:25 -07:00
|
|
|
template: metadata.template || '',
|
|
|
|
preserveWhitespaces: metadata.preserveWhitespaces || false,
|
|
|
|
styles: metadata.styles || EMPTY_ARRAY,
|
|
|
|
animations: metadata.animations,
|
2018-11-20 17:20:16 +01:00
|
|
|
directives: [],
|
2018-12-20 09:49:24 +01:00
|
|
|
changeDetection: metadata.changeDetection,
|
2018-10-24 16:02:25 -07:00
|
|
|
pipes: new Map(),
|
|
|
|
encapsulation: metadata.encapsulation || ViewEncapsulation.Emulated,
|
2018-11-29 16:21:16 -08:00
|
|
|
interpolation: metadata.interpolation,
|
2018-10-24 16:02:25 -07:00
|
|
|
viewProviders: metadata.viewProviders || null,
|
|
|
|
};
|
2019-04-21 17:37:15 +02:00
|
|
|
if (meta.usesInheritance) {
|
|
|
|
addBaseDefToUndecoratedParents(type);
|
|
|
|
}
|
2019-02-08 22:10:20 +00:00
|
|
|
ngComponentDef = compiler.compileComponent(angularCoreEnv, templateUrl, meta);
|
2018-06-06 11:23:38 -07:00
|
|
|
|
2018-12-03 13:58:07 -08:00
|
|
|
// When NgModule decorator executed, we enqueued the module definition such that
|
|
|
|
// it would only dequeue and add itself as module scope to all of its declarations,
|
|
|
|
// but only if if all of its declarations had resolved. This call runs the check
|
|
|
|
// to see if any modules that are in the queue can be dequeued and add scope to
|
|
|
|
// their declarations.
|
|
|
|
flushModuleScopingQueueAsMuchAsPossible();
|
|
|
|
|
2018-06-06 11:23:38 -07:00
|
|
|
// If component compilation is async, then the @NgModule annotation which declares the
|
|
|
|
// component may execute and set an ngSelectorScope property on the component type. This
|
2018-08-20 15:20:12 +02:00
|
|
|
// allows the component to patch itself with directiveDefs from the module after it
|
|
|
|
// finishes compiling.
|
2018-06-06 11:23:38 -07:00
|
|
|
if (hasSelectorScope(type)) {
|
2018-08-06 14:09:38 -07:00
|
|
|
const scopes = transitiveScopesFor(type.ngSelectorScope);
|
|
|
|
patchComponentDefWithScope(ngComponentDef, scopes);
|
2018-06-06 11:23:38 -07:00
|
|
|
}
|
2018-05-21 08:15:19 -07:00
|
|
|
}
|
2018-08-06 14:09:38 -07:00
|
|
|
return ngComponentDef;
|
2018-05-21 08:15:19 -07:00
|
|
|
},
|
2018-08-06 14:09:38 -07:00
|
|
|
// Make the property configurable in dev mode to allow overriding in tests
|
|
|
|
configurable: !!ngDevMode,
|
2018-05-21 08:15:19 -07:00
|
|
|
});
|
2019-04-16 16:58:44 -07:00
|
|
|
|
|
|
|
|
|
|
|
// Add ngInjectableDef so components are reachable through the module injector by default
|
|
|
|
// This is mostly to support injecting components in tests. In real application code,
|
|
|
|
// components should be retrieved through the node injector, so this isn't a problem.
|
|
|
|
compileInjectable(type);
|
2018-05-21 08:15:19 -07:00
|
|
|
}
|
|
|
|
|
2018-06-06 11:23:38 -07:00
|
|
|
function hasSelectorScope<T>(component: Type<T>): component is Type<T>&
|
|
|
|
{ngSelectorScope: Type<any>} {
|
|
|
|
return (component as{ngSelectorScope?: any}).ngSelectorScope !== undefined;
|
|
|
|
}
|
|
|
|
|
2018-05-21 08:15:19 -07:00
|
|
|
/**
|
|
|
|
* Compile an Angular directive according to its decorator metadata, and patch the resulting
|
|
|
|
* ngDirectiveDef onto the component type.
|
|
|
|
*
|
|
|
|
* In the event that compilation is not immediate, `compileDirective` will return a `Promise` which
|
|
|
|
* will resolve when compilation completes and the directive becomes usable.
|
|
|
|
*/
|
2018-06-22 19:05:31 -07:00
|
|
|
export function compileDirective(type: Type<any>, directive: Directive): void {
|
2018-08-06 14:09:38 -07:00
|
|
|
let ngDirectiveDef: any = null;
|
2018-06-06 11:23:38 -07:00
|
|
|
Object.defineProperty(type, NG_DIRECTIVE_DEF, {
|
2018-05-21 08:15:19 -07:00
|
|
|
get: () => {
|
2018-08-06 14:09:38 -07:00
|
|
|
if (ngDirectiveDef === null) {
|
2019-01-24 17:25:46 -08:00
|
|
|
const name = type && type.name;
|
|
|
|
const sourceMapUrl = `ng://${name}/ngDirectiveDef.js`;
|
|
|
|
const compiler = getCompilerFacade();
|
2018-12-11 10:43:02 -08:00
|
|
|
const facade = directiveMetadata(type as ComponentType<any>, directive);
|
2019-01-24 17:25:46 -08:00
|
|
|
facade.typeSourceSpan =
|
|
|
|
compiler.createParseSourceSpan('Directive', renderStringify(type), sourceMapUrl);
|
2019-04-21 17:37:15 +02:00
|
|
|
if (facade.usesInheritance) {
|
|
|
|
addBaseDefToUndecoratedParents(type);
|
|
|
|
}
|
2019-01-24 17:25:46 -08:00
|
|
|
ngDirectiveDef = compiler.compileDirective(angularCoreEnv, sourceMapUrl, facade);
|
2018-05-21 08:15:19 -07:00
|
|
|
}
|
2018-08-06 14:09:38 -07:00
|
|
|
return ngDirectiveDef;
|
2018-05-21 08:15:19 -07:00
|
|
|
},
|
2018-08-06 14:09:38 -07:00
|
|
|
// Make the property configurable in dev mode to allow overriding in tests
|
|
|
|
configurable: !!ngDevMode,
|
2018-05-21 08:15:19 -07:00
|
|
|
});
|
2019-04-16 16:58:44 -07:00
|
|
|
|
|
|
|
// Add ngInjectableDef so directives are reachable through the module injector by default
|
|
|
|
// This is mostly to support injecting directives in tests. In real application code,
|
|
|
|
// directives should be retrieved through the node injector, so this isn't a problem.
|
|
|
|
compileInjectable(type);
|
2018-05-09 08:35:25 -07:00
|
|
|
}
|
|
|
|
|
2018-06-18 08:05:06 -07:00
|
|
|
export function extendsDirectlyFromObject(type: Type<any>): boolean {
|
|
|
|
return Object.getPrototypeOf(type.prototype) === Object.prototype;
|
|
|
|
}
|
|
|
|
|
2018-05-21 08:15:19 -07:00
|
|
|
/**
|
|
|
|
* Extract the `R3DirectiveMetadata` for a particular directive (either a `Directive` or a
|
|
|
|
* `Component`).
|
|
|
|
*/
|
2019-03-08 18:21:15 +01:00
|
|
|
export function directiveMetadata(type: Type<any>, metadata: Directive): R3DirectiveMetadataFacade {
|
2018-05-21 08:15:19 -07:00
|
|
|
// Reflect inputs and outputs.
|
2019-02-21 22:45:37 +01:00
|
|
|
const propMetadata = getReflect().ownPropMetadata(type);
|
2018-06-12 16:58:09 -07:00
|
|
|
|
2018-05-21 08:15:19 -07:00
|
|
|
return {
|
|
|
|
name: type.name,
|
2018-10-24 16:02:25 -07:00
|
|
|
type: type,
|
2018-07-13 14:49:01 -07:00
|
|
|
typeArgumentCount: 0,
|
2018-05-21 08:15:19 -07:00
|
|
|
selector: metadata.selector !,
|
2018-10-24 16:02:25 -07:00
|
|
|
deps: reflectDependencies(type),
|
|
|
|
host: metadata.host || EMPTY_OBJ,
|
|
|
|
propMetadata: propMetadata,
|
|
|
|
inputs: metadata.inputs || EMPTY_ARRAY,
|
|
|
|
outputs: metadata.outputs || EMPTY_ARRAY,
|
2018-12-11 10:43:02 -08:00
|
|
|
queries: extractQueriesMetadata(type, propMetadata, isContentQuery),
|
2019-02-06 15:03:42 +01:00
|
|
|
lifecycle: {usesOnChanges: type.prototype.hasOwnProperty('ngOnChanges')},
|
2018-05-21 08:15:19 -07:00
|
|
|
typeSourceSpan: null !,
|
2018-06-18 08:05:06 -07:00
|
|
|
usesInheritance: !extendsDirectlyFromObject(type),
|
2019-01-08 16:30:57 -08:00
|
|
|
exportAs: extractExportAs(metadata.exportAs),
|
2018-10-24 16:02:25 -07:00
|
|
|
providers: metadata.providers || null,
|
2019-03-13 19:30:38 +01:00
|
|
|
viewQueries: extractQueriesMetadata(type, propMetadata, isViewQuery),
|
2018-05-21 08:15:19 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-21 17:37:15 +02:00
|
|
|
/**
|
|
|
|
* Adds an `ngBaseDef` to all parent classes of a type that don't have an Angular decorator.
|
|
|
|
*/
|
|
|
|
function addBaseDefToUndecoratedParents(type: Type<any>) {
|
|
|
|
const objPrototype = Object.prototype;
|
|
|
|
let parent = Object.getPrototypeOf(type);
|
|
|
|
|
|
|
|
// Go up the prototype until we hit `Object`.
|
|
|
|
while (parent && parent !== objPrototype) {
|
|
|
|
// Since inheritance works if the class was annotated already, we only need to add
|
|
|
|
// the base def if there are no annotations and the base def hasn't been created already.
|
|
|
|
if (!getDirectiveDef(parent) && !getComponentDef(parent) && !getBaseDef(parent)) {
|
|
|
|
const facade = extractBaseDefMetadata(parent);
|
|
|
|
facade && compileBase(parent, facade);
|
|
|
|
}
|
|
|
|
parent = Object.getPrototypeOf(parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Compiles the base metadata into a base definition. */
|
|
|
|
function compileBase(type: Type<any>, facade: R3BaseMetadataFacade): void {
|
|
|
|
let ngBaseDef: any = null;
|
|
|
|
Object.defineProperty(type, NG_BASE_DEF, {
|
|
|
|
get: () => {
|
|
|
|
if (ngBaseDef === null) {
|
|
|
|
const name = type && type.name;
|
|
|
|
const sourceMapUrl = `ng://${name}/ngBaseDef.js`;
|
|
|
|
const compiler = getCompilerFacade();
|
|
|
|
ngBaseDef = compiler.compileBase(angularCoreEnv, sourceMapUrl, facade);
|
|
|
|
}
|
|
|
|
return ngBaseDef;
|
|
|
|
},
|
|
|
|
// Make the property configurable in dev mode to allow overriding in tests
|
|
|
|
configurable: !!ngDevMode,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Extracts the metadata necessary to construct an `ngBaseDef` from a class. */
|
|
|
|
function extractBaseDefMetadata(type: Type<any>): R3BaseMetadataFacade|null {
|
|
|
|
const propMetadata = getReflect().ownPropMetadata(type);
|
|
|
|
const viewQueries = extractQueriesMetadata(type, propMetadata, isViewQuery);
|
|
|
|
const queries = extractQueriesMetadata(type, propMetadata, isContentQuery);
|
|
|
|
let inputs: {[key: string]: string | [string, string]}|undefined;
|
|
|
|
let outputs: {[key: string]: string}|undefined;
|
|
|
|
|
|
|
|
for (const field in propMetadata) {
|
|
|
|
propMetadata[field].forEach(ann => {
|
|
|
|
if (ann.ngMetadataName === 'Input') {
|
|
|
|
inputs = inputs || {};
|
|
|
|
inputs[field] = ann.bindingPropertyName ? [ann.bindingPropertyName, field] : field;
|
|
|
|
} else if (ann.ngMetadataName === 'Output') {
|
|
|
|
outputs = outputs || {};
|
|
|
|
outputs[field] = ann.bindingPropertyName || field;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only generate the base def if there's any info inside it.
|
|
|
|
if (inputs || outputs || viewQueries.length || queries.length) {
|
|
|
|
return {inputs, outputs, viewQueries, queries};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-10-24 16:02:25 -07:00
|
|
|
function convertToR3QueryPredicate(selector: any): any|string[] {
|
2018-12-18 13:36:19 -08:00
|
|
|
return typeof selector === 'string' ? splitByComma(selector) : resolveForwardRef(selector);
|
2018-10-29 10:07:40 +01:00
|
|
|
}
|
|
|
|
|
2018-10-24 16:02:25 -07:00
|
|
|
export function convertToR3QueryMetadata(propertyName: string, ann: Query): R3QueryMetadataFacade {
|
2018-10-29 10:07:40 +01:00
|
|
|
return {
|
|
|
|
propertyName: propertyName,
|
|
|
|
predicate: convertToR3QueryPredicate(ann.selector),
|
|
|
|
descendants: ann.descendants,
|
|
|
|
first: ann.first,
|
2019-02-18 17:33:59 -08:00
|
|
|
read: ann.read ? ann.read : null,
|
|
|
|
static: !!ann.static
|
2018-10-29 10:07:40 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
function extractQueriesMetadata(
|
2018-12-11 10:43:02 -08:00
|
|
|
type: Type<any>, propMetadata: {[key: string]: any[]},
|
2018-10-24 16:02:25 -07:00
|
|
|
isQueryAnn: (ann: any) => ann is Query): R3QueryMetadataFacade[] {
|
|
|
|
const queriesMeta: R3QueryMetadataFacade[] = [];
|
2018-10-29 10:07:40 +01:00
|
|
|
for (const field in propMetadata) {
|
|
|
|
if (propMetadata.hasOwnProperty(field)) {
|
2019-01-28 20:45:41 -08:00
|
|
|
const annotations = propMetadata[field];
|
|
|
|
annotations.forEach(ann => {
|
2018-10-29 10:07:40 +01:00
|
|
|
if (isQueryAnn(ann)) {
|
2018-12-11 10:43:02 -08:00
|
|
|
if (!ann.selector) {
|
|
|
|
throw new Error(
|
|
|
|
`Can't construct a query for the property "${field}" of ` +
|
2019-01-12 00:59:48 -08:00
|
|
|
`"${renderStringify(type)}" since the query selector wasn't defined.`);
|
2018-12-11 10:43:02 -08:00
|
|
|
}
|
2019-01-28 20:45:41 -08:00
|
|
|
if (annotations.some(isInputAnn)) {
|
|
|
|
throw new Error(`Cannot combine @Input decorators with query decorators`);
|
|
|
|
}
|
2018-10-29 10:07:40 +01:00
|
|
|
queriesMeta.push(convertToR3QueryMetadata(field, ann));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return queriesMeta;
|
|
|
|
}
|
|
|
|
|
2019-01-08 16:30:57 -08:00
|
|
|
function extractExportAs(exportAs: string | undefined): string[]|null {
|
|
|
|
if (exportAs === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return exportAs.split(',').map(part => part.trim());
|
|
|
|
}
|
|
|
|
|
2018-10-29 10:07:40 +01:00
|
|
|
function isContentQuery(value: any): value is Query {
|
|
|
|
const name = value.ngMetadataName;
|
|
|
|
return name === 'ContentChild' || name === 'ContentChildren';
|
|
|
|
}
|
|
|
|
|
|
|
|
function isViewQuery(value: any): value is Query {
|
|
|
|
const name = value.ngMetadataName;
|
|
|
|
return name === 'ViewChild' || name === 'ViewChildren';
|
|
|
|
}
|
|
|
|
|
2019-01-28 20:45:41 -08:00
|
|
|
function isInputAnn(value: any): value is Input {
|
|
|
|
return value.ngMetadataName === 'Input';
|
|
|
|
}
|
|
|
|
|
2018-10-29 10:07:40 +01:00
|
|
|
function splitByComma(value: string): string[] {
|
|
|
|
return value.split(',').map(piece => piece.trim());
|
|
|
|
}
|