Skip to content

Commit ecd0475

Browse files
JoostKkirjs
authored andcommitted
fix(compiler): account for NgModule dependencies in JIT-compiled partial declarations
When partial declarations are not preprocessed to AOT by the linker, the `ngDeclareComponent` call causes them to be compiled ad-hoc. In this mode, NgModule imports in standalone components would be dropped, deviating from the linker. This commit changes the ad-hoc compilation of component declarations to pass the NgModule imports along just like the linker does. Fixes angular#69451
1 parent f9c4b71 commit ecd0475

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

packages/compiler/src/jit_compiler_facade.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
R3DeclareFactoryFacade,
2222
R3DeclareInjectableFacade,
2323
R3DeclareInjectorFacade,
24+
R3DeclareNgModuleDependencyFacade,
2425
R3DeclareNgModuleFacade,
2526
R3DeclarePipeDependencyFacade,
2627
R3DeclarePipeFacade,
@@ -87,6 +88,7 @@ import {
8788
R3DirectiveMetadata,
8889
R3HostMetadata,
8990
R3InputMetadata,
91+
R3NgModuleDependencyMetadata,
9092
R3PipeDependencyMetadata,
9193
R3QueryMetadata,
9294
R3TemplateDependency,
@@ -678,6 +680,9 @@ function convertDeclareComponentFacadeToMetadata(
678680
case 'pipe':
679681
declarations.push(convertPipeDeclarationToMetadata(innerDep));
680682
break;
683+
case 'ngmodule':
684+
declarations.push(convertNgModuleDeclarationToMetadata(innerDep));
685+
break;
681686
}
682687
}
683688
} else if (decl.components || decl.directives || decl.pipes) {
@@ -772,6 +777,15 @@ function convertPipeDeclarationToMetadata(
772777
};
773778
}
774779

780+
function convertNgModuleDeclarationToMetadata(
781+
ngModule: R3DeclareNgModuleDependencyFacade,
782+
): R3NgModuleDependencyMetadata {
783+
return {
784+
kind: R3TemplateDependencyKind.NgModule,
785+
type: new WrappedNodeExpr(ngModule.type),
786+
};
787+
}
788+
775789
function parseJitTemplate(
776790
template: string,
777791
typeName: string,

packages/core/test/render3/ivy/jit_spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,32 @@ describe('render3 jit', () => {
116116
expect(directiveDefs[0].type).toBe(InnerCmp);
117117
});
118118

119+
it('compiles a partially compiled component with ngmodule dependency', () => {
120+
@NgModule()
121+
class InnerMod {}
122+
123+
class OuterCmp {
124+
static ɵcmp = ngDeclareComponent({
125+
template: '<inner-cmp></inner-cmp>',
126+
type: OuterCmp,
127+
version: '21.0.0',
128+
dependencies: [
129+
{
130+
kind: 'ngmodule',
131+
type: InnerMod,
132+
},
133+
],
134+
});
135+
}
136+
137+
const rawDependencies = (OuterCmp.ɵcmp as ComponentDef<OuterCmp>).dependencies;
138+
expect(rawDependencies).not.toBeNull();
139+
const dependencies =
140+
rawDependencies! instanceof Function ? rawDependencies!() : rawDependencies!;
141+
expect(dependencies.length).toBe(1);
142+
expect(dependencies[0]).toBe(InnerMod);
143+
});
144+
119145
it('compiles an injectable with a type provider', () => {
120146
@Injectable({providedIn: 'root'})
121147
class Service {}

0 commit comments

Comments
 (0)