Skip to content

Commit

Permalink
fix(compiler): include used components during JIT compilation of part…
Browse files Browse the repository at this point in the history
…ial component declaration

In angular#41104 the list of used directives was split into two arrays of used
directives and components, but the JIT side was not updated. This commit
fixes the JIT integration by including the list of used components.

Fixes angular#41318
  • Loading branch information
JoostK committed Mar 26, 2021
1 parent b61c009 commit d0c8669
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 18 deletions.
17 changes: 10 additions & 7 deletions packages/compiler/src/compiler_facade_interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,8 @@ export interface R3DeclareComponentFacade extends R3DeclareDirectiveFacade {
template: string;
isInline?: boolean;
styles?: string[];
directives?: {
selector: string; type: OpaqueValue | (() => OpaqueValue);
inputs?: string[];
outputs?: string[];
exportAs?: string[];
}[];
components?: R3DeclareUsedDirectiveFacade[];
directives?: R3DeclareUsedDirectiveFacade[];
pipes?: {[pipeName: string]: OpaqueValue|(() => OpaqueValue)};
viewProviders?: OpaqueValue;
animations?: OpaqueValue;
Expand All @@ -216,6 +212,14 @@ export interface R3DeclareComponentFacade extends R3DeclareDirectiveFacade {
preserveWhitespaces?: boolean;
}

export interface R3DeclareUsedDirectiveFacade {
selector: string;
type: OpaqueValue|(() => OpaqueValue);
inputs?: string[];
outputs?: string[];
exportAs?: string[];
}

export interface R3UsedDirectiveMetadata {
selector: string;
inputs: string[];
Expand Down Expand Up @@ -266,7 +270,6 @@ export interface R3DeclareInjectorFacade {
type: Function;
imports?: OpaqueValue[];
providers?: OpaqueValue[];
deps: R3DependencyMetadataFacade[]|null;
}

export interface R3DeclareNgModuleFacade {
Expand Down
9 changes: 5 additions & 4 deletions packages/compiler/src/jit_compiler_facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/


import {CompilerFacade, CoreEnvironment, ExportedCompilerFacade, OpaqueValue, R3ComponentMetadataFacade, R3DeclareComponentFacade, R3DeclareDirectiveFacade, R3DeclareInjectorFacade, R3DeclareNgModuleFacade, R3DeclarePipeFacade, R3DeclareQueryMetadataFacade, R3DependencyMetadataFacade, R3DirectiveMetadataFacade, R3FactoryDefMetadataFacade, R3InjectableMetadataFacade, R3InjectorMetadataFacade, R3NgModuleMetadataFacade, R3PipeMetadataFacade, R3QueryMetadataFacade, StringMap, StringMapWithRename} from './compiler_facade_interface';
import {CompilerFacade, CoreEnvironment, ExportedCompilerFacade, OpaqueValue, R3ComponentMetadataFacade, R3DeclareComponentFacade, R3DeclareDirectiveFacade, R3DeclareInjectorFacade, R3DeclareNgModuleFacade, R3DeclarePipeFacade, R3DeclareQueryMetadataFacade, R3DeclareUsedDirectiveFacade, R3DependencyMetadataFacade, R3DirectiveMetadataFacade, R3FactoryDefMetadataFacade, R3InjectableMetadataFacade, R3InjectorMetadataFacade, R3NgModuleMetadataFacade, R3PipeMetadataFacade, R3QueryMetadataFacade, StringMap, StringMapWithRename} from './compiler_facade_interface';
import {ConstantPool} from './constant_pool';
import {ChangeDetectionStrategy, HostBinding, HostListener, Input, Output, Type, ViewEncapsulation} from './core';
import {Identifiers} from './identifiers';
Expand Down Expand Up @@ -372,7 +372,9 @@ function convertDeclareComponentFacadeToMetadata(
...convertDeclareDirectiveFacadeToMetadata(declaration, typeSourceSpan),
template,
styles: declaration.styles ?? [],
directives: (declaration.directives ?? []).map(convertUsedDirectiveDeclarationToMetadata),
directives: (declaration.components ?? [])
.concat(declaration.directives ?? [])
.map(convertUsedDirectiveDeclarationToMetadata),
pipes: convertUsedPipesToMetadata(declaration.pipes),
viewProviders: declaration.viewProviders !== undefined ?
new WrappedNodeExpr(declaration.viewProviders) :
Expand All @@ -388,8 +390,7 @@ function convertDeclareComponentFacadeToMetadata(
};
}

function convertUsedDirectiveDeclarationToMetadata(
declaration: NonNullable<R3DeclareComponentFacade['directives']>[number]):
function convertUsedDirectiveDeclarationToMetadata(declaration: R3DeclareUsedDirectiveFacade):
R3UsedDirectiveMetadata {
return {
selector: declaration.selector,
Expand Down
5 changes: 5 additions & 0 deletions packages/compiler/test/compiler_facade_interface_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ const coreR3DeclareComponentFacade: core.R3DeclareComponentFacade =
const compilerR3DeclareComponentFacade: compiler.R3DeclareComponentFacade =
null! as core.R3DeclareComponentFacade;

const coreR3DeclareUsedDirectiveFacade: core.R3DeclareUsedDirectiveFacade =
null! as compiler.R3DeclareUsedDirectiveFacade;
const compilerR3DeclareUsedDirectiveFacade: compiler.R3DeclareUsedDirectiveFacade =
null! as core.R3DeclareUsedDirectiveFacade;

const coreR3UsedDirectiveMetadata: core.R3UsedDirectiveMetadata =
null! as compiler.R3UsedDirectiveMetadata;
const compilerR3UsedDirectiveMetadata: compiler.R3UsedDirectiveMetadata =
Expand Down
16 changes: 10 additions & 6 deletions packages/core/src/compiler/compiler_facade_interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,8 @@ export interface R3DeclareComponentFacade extends R3DeclareDirectiveFacade {
template: string;
isInline?: boolean;
styles?: string[];
directives?: {
selector: string; type: OpaqueValue | (() => OpaqueValue);
inputs?: string[];
outputs?: string[];
exportAs?: string[];
}[];
components?: R3DeclareUsedDirectiveFacade[];
directives?: R3DeclareUsedDirectiveFacade[];
pipes?: {[pipeName: string]: OpaqueValue|(() => OpaqueValue)};
viewProviders?: OpaqueValue;
animations?: OpaqueValue;
Expand All @@ -216,6 +212,14 @@ export interface R3DeclareComponentFacade extends R3DeclareDirectiveFacade {
preserveWhitespaces?: boolean;
}

export interface R3DeclareUsedDirectiveFacade {
selector: string;
type: OpaqueValue|(() => OpaqueValue);
inputs?: string[];
outputs?: string[];
exportAs?: string[];
}

export interface R3UsedDirectiveMetadata {
selector: string;
inputs: string[];
Expand Down
40 changes: 39 additions & 1 deletion packages/core/test/render3/jit/declare_component_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ChangeDetectionStrategy, Directive, ElementRef, forwardRef, Pipe, Type, ViewEncapsulation, ɵɵngDeclareComponent} from '@angular/core';
import {ChangeDetectionStrategy, Component, Directive, ElementRef, forwardRef, Pipe, Type, ViewEncapsulation, ɵɵngDeclareComponent} from '@angular/core';
import {AttributeMarker, ComponentDef} from '../../../src/render3';
import {functionContaining} from './matcher';

Expand Down Expand Up @@ -338,6 +338,21 @@ describe('component declaration jit compilation', () => {
});
});

it('should compile used components', () => {
const def = ɵɵngDeclareComponent({
type: TestClass,
template: '<cmp></cmp>',
components: [{
type: TestCmp,
selector: 'cmp',
}],
}) as ComponentDef<TestClass>;

expectComponentDef(def, {
directives: [TestCmp],
});
});

it('should compile used directives', () => {
const def = ɵɵngDeclareComponent({
type: TestClass,
Expand All @@ -353,6 +368,25 @@ describe('component declaration jit compilation', () => {
});
});

it('should compile used directives together with used components', () => {
const def = ɵɵngDeclareComponent({
type: TestClass,
template: '<cmp dir></cmp>',
components: [{
type: TestCmp,
selector: 'cmp',
}],
directives: [{
type: TestDir,
selector: '[dir]',
}],
}) as ComponentDef<TestClass>;

expectComponentDef(def, {
directives: [TestCmp, TestDir],
});
});

it('should compile forward declared directives', () => {
const def = ɵɵngDeclareComponent({
type: TestClass,
Expand Down Expand Up @@ -534,6 +568,10 @@ class TestClass {}
class TestDir {
}

@Component({selector: 'cmp', template: ''})
class TestCmp {
}

@Pipe({name: 'test'})
class TestPipe {
}

0 comments on commit d0c8669

Please sign in to comment.