From 81de9ea5c1a642622a4ec12e000341d93a6446a5 Mon Sep 17 00:00:00 2001 From: rk Date: Wed, 25 Nov 2026 03:23:10 +0500 Subject: [PATCH] fix(@ngtools/webpack): elide inlined const enum imports from rewritten import declarations TypeScript inlines const enum usages and elides their imports. When an unused runtime value specifier was removed from the same import declaration, the declaration was rewritten, TypeScript skipped its own elision, and the const enum import was kept even though the enum has no runtime export. --- .../webpack/src/transformers/elide_imports.ts | 22 ++++- .../src/transformers/elide_imports_spec.ts | 94 +++++++++++++++++++ 2 files changed, 113 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..d0a860c811ce 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, as their usages are + * inlined, unless const enums are preserved. */ -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..31791af7e4fe 100644 --- a/packages/ngtools/webpack/src/transformers/elide_imports_spec.ts +++ b/packages/ngtools/webpack/src/transformers/elide_imports_spec.ts @@ -545,6 +545,76 @@ describe('elide_imports', () => { expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); }); + + it('should not retain inlined const enum imports when a value specifier is removed from a declaration', () => { + const input = tags.stripIndent` + import { MixedEnum, MixedService } from './mixed'; + + export const value = MixedEnum.A; + + const remove = MixedService; + `; + + const output = tags.stripIndent` + export const value = 1 /* MixedEnum.A */; + `; + + 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('should retain const enum imports when a value specifier is removed and isolatedModules is enabled', () => { + const input = tags.stripIndent` + import { MixedEnum, MixedService } from './mixed'; + + export const value = MixedEnum.A; + + const remove = MixedService; + `; + + const output = tags.stripIndent` + import { MixedEnum } from './mixed'; + export const value = MixedEnum.A; + `; + + const { program, compilerHost } = createTypescriptContext(input, additionalFiles, true, { + ...extraCompilerOptions, + isolatedModules: true, + }); + const result = transformTypescript(undefined, [transformer(program)], program, compilerHost); + + expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); + }); + + it('should retain const enum imports when a value specifier is removed and preserveConstEnums is enabled', () => { + const input = tags.stripIndent` + import { MixedEnum, MixedService } from './mixed'; + + export const value = MixedEnum.A; + + const remove = MixedService; + `; + + const output = tags.stripIndent` + import { MixedEnum } from './mixed'; + export const value = 1 /* MixedEnum.A */; + `; + + const { program, compilerHost } = createTypescriptContext(input, additionalFiles, true, { + ...extraCompilerOptions, + preserveConstEnums: true, + }); + const result = transformTypescript(undefined, [transformer(program)], program, compilerHost); + + expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); + }); }); it('keeps jsxFactory imports when configured', () => { @@ -1072,6 +1142,30 @@ describe('elide_imports', () => { expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); }); + it('should not retain inlined const enum imports when a value specifier is removed from a declaration', () => { + const input = tags.stripIndent` + import { MixedEnum, MixedService } from './mixed'; + + export const value = MixedEnum.A; + + const remove = MixedService; + `; + + const output = tags.stripIndent` + export const value = 1 /* MixedEnum.A */; + `; + + 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('should elide imports that resolve through type-only re-exports', () => { const input = tags.stripIndent` import { Decorator } from './decorator';