@ngtools/webpack keeps a `const enum` import when it rewrites the import declaration ("export ... was not found")
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 新手友好度
- 72/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- angular, typescript, webpack
- 領域
- backend, build-system
研究方向
Start by reading elide_imports.ts and the related fix in #34594; the issue identifies the import rewrite as the likely failure point. Reproduce with the linked project using npm ci && npm run build, then trace how the combined Period and PeriodFormatter import is rewritten. Done when the build succeeds for that reproduction and the const enum import is elided without breaking the runtime value.
由索引模型根據 Issue 內容生成。
描述
Which @angular/* package(s) are the source of the bug?
@ngtools/webpack (via @angular-devkit/build-angular, browser builder)
Is this a regression?
Yes. The last version that works is @angular-devkit/build-angular 22.1.9. It fails from 22.2.0 through 22.3.0 (the latest at the time of writing), whatever the @angular/* framework version: build-angular 22.1.9 with Angular 22.2.0 works, and build-angular 22.2.0 with Angular 22.1.8 fails.
Description
When one import statement brings in a const enum together with a runtime value that is used only in type positions (for example a class that is only a constructor parameter type), the webpack build fails:
./src/app/app.component.ts:1:0-34 - Error: export 'Period' (imported as 'Period') was not found in './period' (possible exports: PeriodFormatter)
// period.ts
import { Injectable } from '@angular/core';
export const enum Period {
Month = 'month',
Quarter = 'quarter',
}
@Injectable({ providedIn: 'root' })
export class PeriodFormatter {
format(period: string): string {
return `Period: ${period}`;
}
}
// app.component.ts
import { Component } from '@angular/core';
import { Period, PeriodFormatter } from './period';
@Component({ selector: 'app-root', template: '<p>{{ label }}</p>' })
export class AppComponent {
protected readonly label: string;
constructor(formatter: PeriodFormatter) {
this.label = formatter.format(Period.Month);
}
}
What seems to happen, judging from elide_imports.ts: PeriodFormatter is only referenced from a type node, so its specifier is classed as an unused runtime value and the declaration is rewritten without it. The comment there notes that TypeScript skips its own import elision for a declaration a transformer replaced. Period counts as used (Period.Month is an identifier reference), so it is kept, but TypeScript has inlined the const enum and would normally drop the specifier. The rewritten declaration still imports Period, which has no runtime export.
Notes:
- The constructor parameter is not essential: any runtime value used only in type positions in the same statement triggers it, for example a field typed
PeriodFormatter | undefined. - Not needed: a barrel, tsconfig
paths, or any non-default compiler option.ngcalone emits the file correctly. - Possibly related: #34594 (22.2.6), which fixed type-only specifiers but not this case.
- Workarounds: a regular
enum, or importing the const enum in its own import statement.
| @angular/* | @angular-devkit/build-angular | result |
|---|---|---|
| 22.1.8 | 22.1.9 | builds |
| 22.2.0 | 22.1.9 | builds |
| 22.1.8 | 22.2.0 | fails |
| 22.2.0 | 22.2.0 | fails |
| 22.2.4 – 22.2.6 | 22.2.4 – 22.2.7 | fails |
| 22.2.7 | 22.2.8 | fails |
| 22.3.0 | 22.3.0 | fails |
Please provide a link to a minimal reproduction of the bug
https://github.com/IT-CASADO/const-enum-elision-repro
npm ci && npm run build
Please provide the exception or error you saw
./src/app/app.component.ts:1:0-34 - Error: export 'Period' (imported as 'Period') was not found in './period' (possible exports: PeriodFormatter)
Please provide the environment you discovered this bug in
Angular CLI : 22.2.7
Angular : 22.2.6
@angular-devkit/build-angular : 22.2.7
TypeScript : 6.0.3
Node : 24.22.3
OS : Windows 11
- 主要語言
- TypeScript
- 星號
- 27k
- 分支
- 11.8k
- 平均合併
- 1 天 2 小時
- 30 天內合併 PR
- 225
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
angular/angular-cli 的其他 Issue
-
`@angular/[email protected]` depends on vulnerable Vite 7.3.7 - GHSA-p4f5-76q4-ffq7 (Critical RCE)未關閉area: @angular/build
難度 2/5 1-3 小時 新手友好度 65/100
angular/angular-cli#34764 ·
維護者通常 1 天內回覆
-
ng add @angular/ssr fails with TypeError when an NgModule in the app module file has no bootstrap array可能已有人在做 @0z5a 今天認領。 未關閉area: @schematics/angular
難度 2/5 1-3 小時 新手友好度 90/100
angular/angular-cli#34761 ·
維護者通常 1 天內回覆
-
Angular SSR can crash `AngularAppEngine` servers with a single aborted request (22.2.6 regression)可能已有人在做 @SkyZeroZx 於 1 天前認領。 未關閉area: @angular/ssr
難度 4/5 3-5 天 新手友好度 20/100
angular/angular-cli#34754 ·
維護者通常 1 天內回覆
-
area: @schematics/angular
難度 3/5 1-2 天 新手友好度 74/100
angular/angular-cli#34730 ·
維護者通常 1 天內回覆
-
area: @angular/build
難度 4/5 3-5 天 新手友好度 48/100
angular/angular-cli#34728 · 2 則留言 ·
維護者通常 1 天內回覆
查看 angular/angular-cli 的全部 Issue
相似的 Issue
-
status/need-triage
難度 2/5 1-3 小時 新手友好度 76/100
google-gemini/gemini-cli#30384 · 1 則留言 ·
維護者通常 1 天內回覆
-
P2
難度 1/5 1 小時以內 新手友好度 84/100
維護者通常 1 天內回覆
-
bug ready-for-human
難度 2/5 1-3 小時 新手友好度 60/100
LucasSantana-Dev/Lucky#3150 ·
維護者通常 1 天內回覆
-
enhancement good first issue priority: low size: XS
難度 2/5 1-3 小時 新手友好度 88/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
vercel-labs/scriptc#3979 ·
維護者通常 1 天內回覆