Skip to content

Commit b59f20b

Browse files
committed
fix(@ngtools/webpack): elide type only named imports when using emitDecoratorMetadata
With this change we fix an issue where type only named imports were being emitted. As a result webpack failed to resolve such symbols as they don't exist in JavaScript. Closes angular#23667
1 parent b9c6169 commit b59f20b

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export function elideImports(
153153
clausesCount += namedBindings.elements.length;
154154

155155
for (const specifier of namedBindings.elements) {
156-
if (isUnused(specifier.name)) {
156+
if (specifier.isTypeOnly || isUnused(specifier.name)) {
157157
removedClausesCount++;
158158
// in case we don't have any more namedImports we should remove the parent ie the {}
159159
const nodeToRemove =

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

+38
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,44 @@ describe('@ngtools/webpack transformers', () => {
440440
experimentalDecorators: true,
441441
};
442442

443+
it('should elide type only named imports', () => {
444+
const input = tags.stripIndent`
445+
import { Decorator } from './decorator';
446+
import { type OnChanges, type SimpleChanges } from './type';
447+
448+
@Decorator()
449+
export class Foo implements OnChanges {
450+
ngOnChanges(changes: SimpleChanges) { }
451+
}
452+
453+
${dummyNode}
454+
`;
455+
456+
const output = tags.stripIndent`
457+
import { __decorate } from "tslib";
458+
import { Decorator } from './decorator';
459+
460+
let Foo = class Foo { ngOnChanges(changes) { } };
461+
Foo = __decorate([ Decorator() ], Foo);
462+
export { Foo };
463+
`;
464+
465+
const { program, compilerHost } = createTypescriptContext(
466+
input,
467+
additionalFiles,
468+
true,
469+
extraCompilerOptions,
470+
);
471+
const result = transformTypescript(
472+
undefined,
473+
[transformer(program)],
474+
program,
475+
compilerHost,
476+
);
477+
478+
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
479+
});
480+
443481
it('should not remove ctor parameter type reference', () => {
444482
const input = tags.stripIndent`
445483
import { Decorator } from './decorator';

0 commit comments

Comments
 (0)