Skip to content

Commit 0e06cf9

Browse files
alan-agius4clydin
authored andcommitted
fix(@ngtools/webpack): elide type-only imports when using emitDecoratorMetadata
Closes #19234 (cherry picked from commit 8e11ad9)
1 parent 83f1b58 commit 0e06cf9

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

packages/ngtools/webpack/src/transformers/elide_imports.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export function elideImports(
4848

4949
// Record import and skip
5050
if (ts.isImportDeclaration(node)) {
51-
imports.push(node);
51+
if (!node.importClause?.isTypeOnly) {
52+
imports.push(node);
53+
}
5254

5355
return;
5456
}

packages/ngtools/webpack/src/transformers/elide_imports_spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ describe('@ngtools/webpack transformers', () => {
4141
'service.ts': `
4242
export class Service { }
4343
`,
44+
'type.ts': `
45+
export interface OnChanges {
46+
ngOnChanges(changes: SimpleChanges): void;
47+
}
48+
49+
export interface SimpleChanges {
50+
[propName: string]: unknown;
51+
}
52+
`,
4453
};
4554

4655
it('should remove unused imports', () => {
@@ -530,6 +539,40 @@ describe('@ngtools/webpack transformers', () => {
530539
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
531540
});
532541

542+
it('should remove type-only imports', () => {
543+
const input = tags.stripIndent`
544+
import { Decorator } from './decorator';
545+
import { Service } from './service';
546+
import type { OnChanges, SimpleChanges } from './type';
547+
548+
@Decorator()
549+
class Foo implements OnChanges {
550+
constructor(param: Service) { }
551+
ngOnChanges(changes: SimpleChanges) { }
552+
}
553+
554+
${dummyNode}
555+
`;
556+
557+
const output = tags.stripIndent`
558+
import { __decorate, __metadata } from "tslib";
559+
import { Decorator } from './decorator';
560+
import { Service } from './service';
561+
562+
let Foo = class Foo {
563+
constructor(param) { }
564+
ngOnChanges(changes) { }
565+
};
566+
567+
Foo = __decorate([ Decorator(), __metadata("design:paramtypes", [Service]) ], Foo);
568+
`;
569+
570+
const { program, compilerHost } = createTypescriptContext(input, additionalFiles, true, extraCompilerOptions);
571+
const result = transformTypescript(undefined, [transformer(program)], program, compilerHost);
572+
573+
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
574+
});
575+
533576
describe('NGTSC - ShorthandPropertyAssignment to PropertyAssignment', () => {
534577
const transformShorthandPropertyAssignment = (context: ts.TransformationContext): ts.Transformer<ts.SourceFile> => {
535578
const visit: ts.Visitor = node => {

0 commit comments

Comments
 (0)