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
|
|
|
|
*/
|
|
|
|
|
2018-12-11 10:43:02 -08:00
|
|
|
import {ComponentType} from '..';
|
2018-12-18 13:36:19 -08:00
|
|
|
import {resolveForwardRef} from '../../di/forward_ref';
|
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';
|
2018-10-24 16:02:25 -07:00
|
|
|
import {Component, Directive} 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';
|
2018-12-13 15:51:47 -08:00
|
|
|
import {EMPTY_ARRAY, EMPTY_OBJ} from '../empty';
|
2018-08-29 16:34:44 -07:00
|
|
|
import {NG_COMPONENT_DEF, NG_DIRECTIVE_DEF} from '../fields';
|
2019-01-12 00:59:48 -08:00
|
|
|
import {renderStringify} from '../util';
|
2018-05-09 08:35:25 -07:00
|
|
|
|
2018-10-24 16:02:25 -07:00
|
|
|
import {R3DirectiveMetadataFacade, getCompilerFacade} from './compiler_facade';
|
|
|
|
import {R3ComponentMetadataFacade, R3QueryMetadataFacade} from './compiler_facade_interface';
|
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-21 08:15:19 -07:00
|
|
|
import {getReflect, reflectDependencies} from './util';
|
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.
|
|
|
|
maybeQueueResolutionOfComponentResources(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
|
|
|
|
2018-10-24 16:02:25 -07:00
|
|
|
const meta: R3ComponentMetadataFacade = {
|
|
|
|
...directiveMetadata(type, metadata),
|
|
|
|
template: metadata.template || '',
|
|
|
|
preserveWhitespaces: metadata.preserveWhitespaces || false,
|
|
|
|
styles: metadata.styles || EMPTY_ARRAY,
|
|
|
|
animations: metadata.animations,
|
2018-12-11 10:43:02 -08:00
|
|
|
viewQueries: extractQueriesMetadata(type, getReflect().propMetadata(type), isViewQuery),
|
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,
|
|
|
|
};
|
|
|
|
ngComponentDef = compiler.compileComponent(
|
2019-01-12 00:59:48 -08:00
|
|
|
angularCoreEnv, `ng://${renderStringify(type)}/template.html`, 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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-12-11 10:43:02 -08:00
|
|
|
const facade = directiveMetadata(type as ComponentType<any>, directive);
|
2018-10-24 16:02:25 -07:00
|
|
|
ngDirectiveDef = getCompilerFacade().compileDirective(
|
|
|
|
angularCoreEnv, `ng://${type && type.name}/ngDirectiveDef.js`, 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
|
|
|
});
|
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`).
|
|
|
|
*/
|
2018-10-24 16:02:25 -07:00
|
|
|
function directiveMetadata(type: Type<any>, metadata: Directive): R3DirectiveMetadataFacade {
|
2018-05-21 08:15:19 -07:00
|
|
|
// Reflect inputs and outputs.
|
2018-06-12 16:58:09 -07:00
|
|
|
const propMetadata = getReflect().propMetadata(type);
|
|
|
|
|
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),
|
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,
|
2018-05-21 08:15:19 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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,
|
2018-10-24 16:02:25 -07:00
|
|
|
read: ann.read ? ann.read : null
|
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)) {
|
|
|
|
propMetadata[field].forEach(ann => {
|
|
|
|
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
|
|
|
}
|
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';
|
|
|
|
}
|
|
|
|
|
|
|
|
function splitByComma(value: string): string[] {
|
|
|
|
return value.split(',').map(piece => piece.trim());
|
|
|
|
}
|