|
9 | 9 | import {runInEachFileSystem} from '../../src/ngtsc/file_system/testing'; |
10 | 10 | import {loadStandardTestFiles} from '../../src/ngtsc/testing'; |
11 | 11 | import {NgtscTestEnvironment} from './env'; |
| 12 | +import {CompilerOptions} from '@angular/compiler-cli/src/transformers/api'; |
| 13 | +import {createCompilerHost} from '@angular/compiler-cli/src/transformers/compiler_host'; |
| 14 | +import {NgtscProgram} from '@angular/compiler-cli/src/ngtsc/program'; |
| 15 | +import ts from 'typescript'; |
12 | 16 |
|
13 | 17 | runInEachFileSystem(() => { |
14 | 18 | describe('HMR code generation', () => { |
@@ -901,5 +905,71 @@ runInEachFileSystem(() => { |
901 | 905 | 'export default function Cmp_UpdateMetadata(Cmp, ɵɵnamespaces, token, Foo, Component) {', |
902 | 906 | ); |
903 | 907 | }); |
| 908 | + |
| 909 | + it('should generate HMR code for a transformed class', () => { |
| 910 | + env.write( |
| 911 | + 'test.ts', |
| 912 | + ` |
| 913 | + import {Component} from '@angular/core'; |
| 914 | +
|
| 915 | + @Component({template: ''}) |
| 916 | + export class Cmp {} |
| 917 | + `, |
| 918 | + ); |
| 919 | + |
| 920 | + const options: CompilerOptions = { |
| 921 | + target: ts.ScriptTarget.Latest, |
| 922 | + module: ts.ModuleKind.ESNext, |
| 923 | + _enableHmr: true, |
| 924 | + }; |
| 925 | + |
| 926 | + const program = new NgtscProgram(['/test.ts'], options, createCompilerHost({options})); |
| 927 | + const transformers = program.compiler.prepareEmit().transformers; |
| 928 | + |
| 929 | + transformers.before!.unshift((ctx) => (sourceFile) => { |
| 930 | + const visitor = (node: ts.Node) => { |
| 931 | + if (ts.isClassDeclaration(node) && node.name?.getText() === 'Cmp') { |
| 932 | + const newMember = ctx.factory.createPropertyDeclaration( |
| 933 | + undefined, |
| 934 | + 'newProp', |
| 935 | + undefined, |
| 936 | + undefined, |
| 937 | + ctx.factory.createNumericLiteral(123), |
| 938 | + ); |
| 939 | + |
| 940 | + return ctx.factory.updateClassDeclaration( |
| 941 | + node, |
| 942 | + node.modifiers, |
| 943 | + node.name, |
| 944 | + node.typeParameters, |
| 945 | + node.heritageClauses, |
| 946 | + [newMember, ...node.members], |
| 947 | + ); |
| 948 | + } |
| 949 | + return ts.visitEachChild(node, visitor, ctx); |
| 950 | + }; |
| 951 | + return ts.visitEachChild(sourceFile, visitor, ctx); |
| 952 | + }); |
| 953 | + |
| 954 | + const {diagnostics, emitSkipped} = program |
| 955 | + .getTsProgram() |
| 956 | + .emit(undefined, undefined, undefined, undefined, transformers); |
| 957 | + const declaration = program |
| 958 | + .getTsProgram() |
| 959 | + .getSourceFile('/test.ts') |
| 960 | + ?.statements.find( |
| 961 | + (stmt) => ts.isClassDeclaration(stmt) && stmt.name?.getText() === 'Cmp', |
| 962 | + ) as ts.ClassDeclaration; |
| 963 | + |
| 964 | + const jsContents = env.getContents('/test.js'); |
| 965 | + const hmrContents = program.compiler.emitHmrUpdateModule(declaration); |
| 966 | + |
| 967 | + expect(diagnostics.length).toBe(0); |
| 968 | + expect(emitSkipped).toBe(false); |
| 969 | + |
| 970 | + expect(jsContents).toContain('ɵreplaceMetadata(Cmp'); |
| 971 | + expect(jsContents).toContain('newProp = 123'); |
| 972 | + expect(hmrContents).toContain('export default function Cmp_UpdateMetadata'); |
| 973 | + }); |
904 | 974 | }); |
905 | 975 | }); |
0 commit comments