refactor(compiler): split compiler and core (#18683)
After this, neither @angular/compiler nor @angular/comnpiler-cli depend on @angular/core. This add a duplication of some interfaces and enums which is stored in @angular/compiler/src/core.ts BREAKING CHANGE: - `@angular/platform-server` now additionally depends on `@angular/platform-browser-dynamic` as a peer dependency. PR Close #18683
This commit is contained in:

committed by
Miško Hevery

parent
a0ca01d580
commit
0cc77b4a69
@ -6,13 +6,16 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Component, Directive, HostBinding, HostListener, Input, Output, Query, Type, resolveForwardRef, ɵstringify as stringify} from '@angular/core';
|
||||
|
||||
import {CompileReflector} from './compile_reflector';
|
||||
import {CompilerInjectable} from './injectable';
|
||||
import {splitAtColon} from './util';
|
||||
|
||||
import {Component, Directive, Type, createComponent, createContentChild, createContentChildren, createDirective, createHostBinding, createHostListener, createInput, createOutput, createViewChild, createViewChildren} from './core';
|
||||
import {resolveForwardRef, splitAtColon, stringify} from './util';
|
||||
|
||||
const QUERY_METADATA_IDENTIFIERS = [
|
||||
createViewChild,
|
||||
createViewChildren,
|
||||
createContentChild,
|
||||
createContentChildren,
|
||||
];
|
||||
|
||||
/*
|
||||
* Resolve a `Type` for {@link Directive}.
|
||||
@ -21,11 +24,10 @@ import {splitAtColon} from './util';
|
||||
*
|
||||
* See {@link Compiler}
|
||||
*/
|
||||
@CompilerInjectable()
|
||||
export class DirectiveResolver {
|
||||
constructor(private _reflector: CompileReflector) {}
|
||||
|
||||
isDirective(type: Type<any>) {
|
||||
isDirective(type: Type) {
|
||||
const typeMetadata = this._reflector.annotations(resolveForwardRef(type));
|
||||
return typeMetadata && typeMetadata.some(isDirectiveMetadata);
|
||||
}
|
||||
@ -33,10 +35,10 @@ export class DirectiveResolver {
|
||||
/**
|
||||
* Return {@link Directive} for a given `Type`.
|
||||
*/
|
||||
resolve(type: Type<any>): Directive;
|
||||
resolve(type: Type<any>, throwIfNotFound: true): Directive;
|
||||
resolve(type: Type<any>, throwIfNotFound: boolean): Directive|null;
|
||||
resolve(type: Type<any>, throwIfNotFound = true): Directive|null {
|
||||
resolve(type: Type): Directive;
|
||||
resolve(type: Type, throwIfNotFound: true): Directive;
|
||||
resolve(type: Type, throwIfNotFound: boolean): Directive|null;
|
||||
resolve(type: Type, throwIfNotFound = true): Directive|null {
|
||||
const typeMetadata = this._reflector.annotations(resolveForwardRef(type));
|
||||
if (typeMetadata) {
|
||||
const metadata = findLast(typeMetadata, isDirectiveMetadata);
|
||||
@ -54,15 +56,14 @@ export class DirectiveResolver {
|
||||
}
|
||||
|
||||
private _mergeWithPropertyMetadata(
|
||||
dm: Directive, propertyMetadata: {[key: string]: any[]},
|
||||
directiveType: Type<any>): Directive {
|
||||
dm: Directive, propertyMetadata: {[key: string]: any[]}, directiveType: Type): Directive {
|
||||
const inputs: string[] = [];
|
||||
const outputs: string[] = [];
|
||||
const host: {[key: string]: string} = {};
|
||||
const queries: {[key: string]: any} = {};
|
||||
|
||||
Object.keys(propertyMetadata).forEach((propName: string) => {
|
||||
const input = findLast(propertyMetadata[propName], (a) => a instanceof Input);
|
||||
const input = findLast(propertyMetadata[propName], (a) => createInput.isTypeOf(a));
|
||||
if (input) {
|
||||
if (input.bindingPropertyName) {
|
||||
inputs.push(`${propName}: ${input.bindingPropertyName}`);
|
||||
@ -70,7 +71,7 @@ export class DirectiveResolver {
|
||||
inputs.push(propName);
|
||||
}
|
||||
}
|
||||
const output = findLast(propertyMetadata[propName], (a) => a instanceof Output);
|
||||
const output = findLast(propertyMetadata[propName], (a) => createOutput.isTypeOf(a));
|
||||
if (output) {
|
||||
if (output.bindingPropertyName) {
|
||||
outputs.push(`${propName}: ${output.bindingPropertyName}`);
|
||||
@ -78,7 +79,7 @@ export class DirectiveResolver {
|
||||
outputs.push(propName);
|
||||
}
|
||||
}
|
||||
const hostBindings = propertyMetadata[propName].filter(a => a && a instanceof HostBinding);
|
||||
const hostBindings = propertyMetadata[propName].filter(a => createHostBinding.isTypeOf(a));
|
||||
hostBindings.forEach(hostBinding => {
|
||||
if (hostBinding.hostPropertyName) {
|
||||
const startWith = hostBinding.hostPropertyName[0];
|
||||
@ -93,12 +94,13 @@ export class DirectiveResolver {
|
||||
host[`[${propName}]`] = propName;
|
||||
}
|
||||
});
|
||||
const hostListeners = propertyMetadata[propName].filter(a => a && a instanceof HostListener);
|
||||
const hostListeners = propertyMetadata[propName].filter(a => createHostListener.isTypeOf(a));
|
||||
hostListeners.forEach(hostListener => {
|
||||
const args = hostListener.args || [];
|
||||
host[`(${hostListener.eventName})`] = `${propName}(${args.join(',')})`;
|
||||
});
|
||||
const query = findLast(propertyMetadata[propName], (a) => a instanceof Query);
|
||||
const query = findLast(
|
||||
propertyMetadata[propName], (a) => QUERY_METADATA_IDENTIFIERS.some(i => i.isTypeOf(a)));
|
||||
if (query) {
|
||||
queries[propName] = query;
|
||||
}
|
||||
@ -125,7 +127,7 @@ export class DirectiveResolver {
|
||||
|
||||
private _merge(
|
||||
directive: Directive, inputs: string[], outputs: string[], host: {[key: string]: string},
|
||||
queries: {[key: string]: any}, directiveType: Type<any>): Directive {
|
||||
queries: {[key: string]: any}, directiveType: Type): Directive {
|
||||
const mergedInputs =
|
||||
this._dedupeBindings(directive.inputs ? directive.inputs.concat(inputs) : inputs);
|
||||
const mergedOutputs =
|
||||
@ -133,30 +135,31 @@ export class DirectiveResolver {
|
||||
const mergedHost = directive.host ? {...directive.host, ...host} : host;
|
||||
const mergedQueries = directive.queries ? {...directive.queries, ...queries} : queries;
|
||||
|
||||
if (directive instanceof Component) {
|
||||
return new Component({
|
||||
selector: directive.selector,
|
||||
if (createComponent.isTypeOf(directive)) {
|
||||
const comp = directive as Component;
|
||||
return createComponent({
|
||||
selector: comp.selector,
|
||||
inputs: mergedInputs,
|
||||
outputs: mergedOutputs,
|
||||
host: mergedHost,
|
||||
exportAs: directive.exportAs,
|
||||
moduleId: directive.moduleId,
|
||||
exportAs: comp.exportAs,
|
||||
moduleId: comp.moduleId,
|
||||
queries: mergedQueries,
|
||||
changeDetection: directive.changeDetection,
|
||||
providers: directive.providers,
|
||||
viewProviders: directive.viewProviders,
|
||||
entryComponents: directive.entryComponents,
|
||||
template: directive.template,
|
||||
templateUrl: directive.templateUrl,
|
||||
styles: directive.styles,
|
||||
styleUrls: directive.styleUrls,
|
||||
encapsulation: directive.encapsulation,
|
||||
animations: directive.animations,
|
||||
interpolation: directive.interpolation,
|
||||
changeDetection: comp.changeDetection,
|
||||
providers: comp.providers,
|
||||
viewProviders: comp.viewProviders,
|
||||
entryComponents: comp.entryComponents,
|
||||
template: comp.template,
|
||||
templateUrl: comp.templateUrl,
|
||||
styles: comp.styles,
|
||||
styleUrls: comp.styleUrls,
|
||||
encapsulation: comp.encapsulation,
|
||||
animations: comp.animations,
|
||||
interpolation: comp.interpolation,
|
||||
preserveWhitespaces: directive.preserveWhitespaces,
|
||||
});
|
||||
} else {
|
||||
return new Directive({
|
||||
return createDirective({
|
||||
selector: directive.selector,
|
||||
inputs: mergedInputs,
|
||||
outputs: mergedOutputs,
|
||||
@ -170,7 +173,7 @@ export class DirectiveResolver {
|
||||
}
|
||||
|
||||
function isDirectiveMetadata(type: any): type is Directive {
|
||||
return type instanceof Directive;
|
||||
return createDirective.isTypeOf(type) || createComponent.isTypeOf(type);
|
||||
}
|
||||
|
||||
export function findLast<T>(arr: T[], condition: (value: T) => boolean): T|null {
|
||||
|
Reference in New Issue
Block a user