From 82a192f2cd00df55c79b9a5d6d520914294b7f9f Mon Sep 17 00:00:00 2001 From: 0z5a Date: Sun, 22 Nov 2026 12:56:46 +0100 Subject: [PATCH] fix(@ngtools/webpack): elide const enum imports when rewriting an import declaration When an import declaration has to be rewritten because a runtime value specifier is no longer referenced (for example a class used only as a type), TypeScript no longer elides the remaining specifiers itself. A const enum specifier was treated as a referenced runtime value and kept, although its references are inlined and the module has no such export, so webpack failed with "export ... was not found". Const enum aliases are now handled like type-only aliases unless `preserveConstEnums`, `isolatedModules` or `verbatimModuleSyntax` keeps them. With default options the output now matches what TypeScript emits for the same declaration. Closes #34760 --- .../webpack/src/transformers/elide_imports.ts | 22 +++++++++++++--- .../src/transformers/elide_imports_spec.ts | 26 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/packages/ngtools/webpack/src/transformers/elide_imports.ts b/packages/ngtools/webpack/src/transformers/elide_imports.ts index 91b2f648f685..6d0754b7bc56 100644 --- a/packages/ngtools/webpack/src/transformers/elide_imports.ts +++ b/packages/ngtools/webpack/src/transformers/elide_imports.ts @@ -133,7 +133,10 @@ export function elideImports( // Note: with `verbatimModuleSyntax`, TypeScript reports an error for imports of non-value aliases // that do not use a `type` modifier (TS1484/TS1485), so no special handling is needed for it. - if ((symbol.flags & ts.SymbolFlags.Alias) !== 0 && !isValueAlias(symbol, typeChecker)) { + if ( + (symbol.flags & ts.SymbolFlags.Alias) !== 0 && + !isValueAlias(symbol, typeChecker, compilerOptions) + ) { return 'type'; } @@ -210,9 +213,14 @@ export function elideImports( /** * Determines whether an import alias resolves to a runtime value. Aliases that only resolve to * types, or that pass through a type-only import or export declaration anywhere along the alias - * chain, are always elided by TypeScript. + * chain, are always elided by TypeScript. The same applies to const enums, whose references are + * inlined, unless `preserveConstEnums`, `isolatedModules` or `verbatimModuleSyntax` keeps them. */ -function isValueAlias(symbol: ts.Symbol, typeChecker: ts.TypeChecker): boolean { +function isValueAlias( + symbol: ts.Symbol, + typeChecker: ts.TypeChecker, + compilerOptions: ts.CompilerOptions, +): boolean { const seen = new Set(); let current = symbol; @@ -238,5 +246,13 @@ function isValueAlias(symbol: ts.Symbol, typeChecker: ts.TypeChecker): boolean { current = next; } + if ((current.flags & ts.SymbolFlags.ConstEnum) !== 0) { + return !!( + compilerOptions.preserveConstEnums || + compilerOptions.isolatedModules || + compilerOptions.verbatimModuleSyntax + ); + } + return (current.flags & ts.SymbolFlags.Value) !== 0; } diff --git a/packages/ngtools/webpack/src/transformers/elide_imports_spec.ts b/packages/ngtools/webpack/src/transformers/elide_imports_spec.ts index 6fe212135a9b..77ebbe034bce 100644 --- a/packages/ngtools/webpack/src/transformers/elide_imports_spec.ts +++ b/packages/ngtools/webpack/src/transformers/elide_imports_spec.ts @@ -545,6 +545,32 @@ describe('elide_imports', () => { expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); }); + + it('should not retain inlined const enum imports when a declaration is rewritten to remove an unused value import', () => { + const input = tags.stripIndent` + import { MixedEnum, MixedService } from './mixed'; + + export const value = MixedEnum.A; + export let service: MixedService | undefined; + + ${dummyNode} + `; + + const output = tags.stripIndent` + export const value = 1 /* MixedEnum.A */; + export let service; + `; + + 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', () => {