Skip to content

fix(@ngtools/webpack): elide const enum imports when rewriting an import declaration - #34771

Open
0z5a wants to merge 1 commit into
angular:mainfrom
0z5a:ngtools-const-enum-elision
Open

0z5a wants to merge 1 commit into
angular:mainfrom
0z5a:ngtools-const-enum-elision

Conversation

@0z5a

@0z5a 0z5a commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

How I found it

I started from #34760 and checked it against the code. In elide_imports.ts, once a declaration has to be rewritten because a value specifier is unused (here a class used only as a constructor parameter type), the remaining specifiers are no longer left to TypeScript's own elision. isValueAlias treated the const enum as a runtime value, and because its property access counts as a use, the specifier was kept in the rewritten import. With default options the const enum is inlined and the module has no such export, so webpack reports export 'Period' (imported as 'Period') was not found.

Where this can show up in practice

PR Checklist

PR Type

  • Bugfix

What is the current behavior?

When an import declaration is rewritten, a const enum specifier whose references are inlined is still kept, so the emitted import names an export that does not exist at runtime and the webpack build fails.

Issue Number: Closes #34760

What is the new behavior?

isValueAlias now treats an alias that resolves to a const enum as a non-value alias, unless preserveConstEnums, isolatedModules or verbatimModuleSyntax keeps const enum references (the same options TypeScript itself checks). The specifier is then dropped like a type-only one.

New test in elide_imports_spec.ts (emitDecoratorMetadata is false): a declaration that imports a const enum and a type-only class, where the class specifier is removed. The expected output has no import and the inlined value 1 /* MixedEnum.A */.

Testing

  • I could not run the bazel target on the machine I used (macOS). Instead I ran elide_imports_spec.ts with jasmine and TypeScript 6.0.3 together with copies of elide_imports.ts and spec_helpers.ts:
    • without the fix, the new test fails (the output still contains import { MixedEnum } from './mixed') and the other 38 pass;
    • with the fix, all 39 pass.
  • As a reference I compared plain tsc emit for the same source with the transformer output:
    • with default options both drop the import;
    • with isolatedModules both keep it and the reference is not inlined;
    • with preserveConstEnums alone tsc drops the import and the transformer keeps it. The enum object is emitted in that mode, so the import resolves.
  • End to end, I used the repro project from @ngtools/webpack keeps a const enum import when it rewrites the import declaration ("export ... was not found") #34760 (Angular 22.2.6, @ngtools/webpack 22.2.7). ng build fails with the error above. After applying the same change to the installed elide_imports.js, the build succeeds and the bundle contains "month" /* Period.Month */.
  • prettier --check passes on both files. CI here is the real check for the bazel target.

Does this PR introduce a breaking change?

  • Yes
  • No

…ort 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 angular#34760

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the elideImports transformer to correctly elide const enum imports when their references are inlined, unless compiler options like preserveConstEnums, isolatedModules, or verbatimModuleSyntax are enabled. A test case is also added to verify this behavior. The review feedback points out a potential issue where a const enum merged with another value-carrying declaration (such as a namespace) could be incorrectly elided, potentially causing a runtime ReferenceError. A code suggestion is provided to ensure the elision logic is only applied when no other value flags are present on the symbol.

Comment on lines +249 to +255
if ((current.flags & ts.SymbolFlags.ConstEnum) !== 0) {
return !!(
compilerOptions.preserveConstEnums ||
compilerOptions.isolatedModules ||
compilerOptions.verbatimModuleSyntax
);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If a const enum is merged with another value-carrying declaration (such as a namespace), the symbol will have both ts.SymbolFlags.ConstEnum and other value flags (e.g., ts.SymbolFlags.ValueModule).\n\nIn the current implementation, if ts.SymbolFlags.ConstEnum is present, the function immediately returns based on the compiler options, ignoring any other merged value flags. This will cause the import to be elided even if the merged value component (like the namespace) is used at runtime, leading to a runtime ReferenceError.\n\nTo prevent this, we should only apply the const enum elision logic if the symbol does not contain any other value flags.

  if (\n    (current.flags & ts.SymbolFlags.ConstEnum) !== 0 &&\n    (current.flags & (ts.SymbolFlags.Value & ~ts.SymbolFlags.ConstEnum)) === 0\n  ) {\n    return !!(\n      compilerOptions.preserveConstEnums ||\n      compilerOptions.isolatedModules ||\n      compilerOptions.verbatimModuleSyntax\n    );\n  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@ngtools/webpack keeps a const enum import when it rewrites the import declaration ("export ... was not found")

1 participant