Skip to content

Commit e11c0c4

Browse files
devversionthePunderWoman
authored andcommitted
fix(compiler-cli): run JIT transforms on @NgModule classes with jit: true (angular#57212)
This commit is similar to 98ed5b6, and makes use of the preparation work implemented there. Similar to directives and components marked via `jit: true`, we also need to do the same for JIT marked `@NgModule` classes. This is mostly important for downleveling of decorators to support dependency injection of such classes. Inside Google3, migrating from `ts_library` to `ng_module` turns of decorator downleveling, so the `jit: true` for NgModule's is implicitly requesting/reliant on this transform— as expected. PR Close angular#57212
1 parent 3afd7f0 commit e11c0c4

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

packages/compiler-cli/src/ngtsc/annotations/ng_module/src/handler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ import {
100100
getValidConstructorDependencies,
101101
InjectableClassRegistry,
102102
isExpressionForwardReference,
103+
JitDeclarationRegistry,
103104
ReferencesRegistry,
104105
resolveProvidersRequiringFactory,
105106
toR3Reference,
@@ -285,6 +286,7 @@ export class NgModuleDecoratorHandler
285286
private includeSelectorScope: boolean,
286287
private readonly compilationMode: CompilationMode,
287288
private readonly localCompilationExtraImportsTracker: LocalCompilationExtraImportsTracker | null,
289+
private readonly jitDeclarationRegistry: JitDeclarationRegistry,
288290
) {}
289291

290292
readonly precedence = HandlerPrecedence.PRIMARY;
@@ -341,6 +343,7 @@ export class NgModuleDecoratorHandler
341343
const ngModule = reflectObjectLiteral(meta);
342344

343345
if (ngModule.has('jit')) {
346+
this.jitDeclarationRegistry.jitDeclarations.add(node);
344347
// The only allowed value is true, so there's no need to expand further.
345348
return {};
346349
}

packages/compiler-cli/src/ngtsc/annotations/ng_module/test/ng_module_spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ import {
2828
import {LocalModuleScopeRegistry, MetadataDtsModuleScopeResolver} from '../../../scope';
2929
import {getDeclaration, makeProgram} from '../../../testing';
3030
import {CompilationMode} from '../../../transform';
31-
import {InjectableClassRegistry, NoopReferencesRegistry} from '../../common';
31+
import {
32+
InjectableClassRegistry,
33+
JitDeclarationRegistry,
34+
NoopReferencesRegistry,
35+
} from '../../common';
3236
import {NgModuleDecoratorHandler} from '../src/handler';
3337

3438
function setup(program: ts.Program, compilationMode = CompilationMode.FULL) {
@@ -52,6 +56,7 @@ function setup(program: ts.Program, compilationMode = CompilationMode.FULL) {
5256
const refEmitter = new ReferenceEmitter([new LocalIdentifierStrategy()]);
5357
const injectableRegistry = new InjectableClassRegistry(reflectionHost, /* isCore */ false);
5458
const exportedProviderStatusResolver = new ExportedProviderStatusResolver(metaReader);
59+
const jitDeclarationRegistry = new JitDeclarationRegistry();
5560

5661
const handler = new NgModuleDecoratorHandler(
5762
reflectionHost,
@@ -72,6 +77,7 @@ function setup(program: ts.Program, compilationMode = CompilationMode.FULL) {
7277
true,
7378
compilationMode,
7479
/* localCompilationExtraImportsTracker */ null,
80+
jitDeclarationRegistry,
7581
);
7682

7783
return {handler, reflectionHost};

packages/compiler-cli/src/ngtsc/core/src/compiler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,7 @@ export class NgCompiler {
15121512
supportJitMode,
15131513
compilationMode,
15141514
localCompilationExtraImportsTracker,
1515+
jitDeclarationRegistry,
15151516
),
15161517
];
15171518

packages/compiler-cli/test/ngtsc/ngtsc_spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,37 @@ runInEachFileSystem((os: string) => {
645645
expect(env.getContents('test.js')).not.toContain('NonJitComponent.propDecorators');
646646
});
647647

648+
it('should run JIT transform for `NgModule` marked as `jit: true`', () => {
649+
env.write(
650+
'test.ts',
651+
`
652+
import {NgModule, Injector} from '@angular/core';
653+
654+
@NgModule({
655+
jit: true
656+
})
657+
class MyJitModule {
658+
constructor(dep: Injector) {}
659+
}
660+
661+
@NgModule({})
662+
class NonJitModule {
663+
constructor(dep: Injector) {}
664+
}
665+
`,
666+
);
667+
668+
env.driveMain();
669+
670+
expect(trim(env.getContents('test.js'))).toContain(
671+
trim(`
672+
MyJitModule.ctorParameters = () => [
673+
{ type: Injector }
674+
];`),
675+
);
676+
expect(env.getContents('test.js')).not.toContain('NonJitModule.ctorParameters');
677+
});
678+
648679
// This test triggers the Tsickle compiler which asserts that the file-paths
649680
// are valid for the real OS. When on non-Windows systems it doesn't like paths
650681
// that start with `C:`.

0 commit comments

Comments
 (0)