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

オープン
#34,760 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
angular, typescript, webpack

調査の方向性

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. ngc alone 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時間
マージ済み PR(30日)
225

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

angular/angular-cli のほかの issue

angular/angular-cli の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。