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', () => {