Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ export function elideImports(
return;
}

// Pure type declarations and type-only exports are erased at runtime.
if (
ts.isInterfaceDeclaration(node) ||
ts.isTypeAliasDeclaration(node) ||
ts.isTypeParameterDeclaration(node) ||
(ts.isExportDeclaration(node) && node.isTypeOnly) ||
(ts.isExportSpecifier(node) && node.isTypeOnly)
) {
return;
}

let symbol: ts.Symbol | undefined;
switch (node.kind) {
case ts.SyntaxKind.Identifier:
Expand Down
11 changes: 11 additions & 0 deletions packages/ngtools/webpack/src/transformers/elide_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ export function elideImports(
return;
}

// Pure type declarations and type-only exports are erased at runtime.
if (
ts.isInterfaceDeclaration(node) ||
ts.isTypeAliasDeclaration(node) ||
ts.isTypeParameterDeclaration(node) ||
(ts.isExportDeclaration(node) && node.isTypeOnly) ||
(ts.isExportSpecifier(node) && node.isTypeOnly)
) {
return;
}

let symbol: ts.Symbol | undefined;
switch (node.kind) {
case ts.SyntaxKind.Identifier:
Expand Down
41 changes: 41 additions & 0 deletions packages/ngtools/webpack/src/transformers/elide_imports_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,47 @@ describe('elide_imports', () => {

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});

it('should remove import when specifiers are only used in type annotations, type-only re-exports, or interface extends', () => {
const input = tags.stripIndent`
import { Decorator } from './decorator';
import { OnChanges, SimpleChanges } from './type';
import { Service, Service2 } from './service';

export type { SimpleChanges };
export { type Service2 };
export interface ExtendedChanges extends OnChanges {
extra: string;
}

@Decorator()
export class Foo {
constructor(param: Service) {
}
}

${dummyNode}
`;

const output = tags.stripIndent`
import { __decorate } from "tslib";
import { Decorator } from './decorator';

let Foo = class Foo { constructor(param) { } };
Foo = __decorate([ Decorator() ], Foo);
export { Foo };
`;

const { program, compilerHost } = createTypescriptContext(
input,
additionalFiles,
true,
extraCompilerOptions,
);
const result = transformTypescript(undefined, [transformer(program)], program, compilerHost);

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});
});

it('keeps jsxFactory imports when configured', () => {
Expand Down
Loading