Bug: [ngtools] Replace resource transformer issue with conditional `templateUrl`

未关闭
#27,611 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
冷清
技术栈
angular, typescript, webpack

调研方向

首先运行 packages/ngtools/webpack/src/transformers/replace_resources_spec.ts,并阅读相关的 replace_resources.ts transformer。使用失败的 conditional templateUrl reproduction,并比较生成的 ESM 和 CommonJS 输出。当两种形式都能正确转换 conditional resource 且测试通过时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

area: @angular/build area: @ngtools/webpack freq1: low severity2: inconvenient type: bug/fix
Command

test

Is this a regression?
  • Yes, this behavior used to work in the previous version
The previous version in which this bug was not present was

No response

Description

When having conditional templateUrl like this

  templateUrl:
    true === true
        ? './dashboard.component.html'
        : './dashboard.component.copy.html',

replace_resources.ts couldn't transform into proper codes (either require or import)

Minimal Reproduction
// esm

//...
AppComponent = __decorate([
    Component({
        selector: 'app-root',
        templateUrl: true === true
            ? './app.component.html'
            : './app.component.copy.html',
        styles: [__NG_CLI_RESOURCE__0, __NG_CLI_RESOURCE__1]
    })
], AppComponent);
// commonjs

//...
exports.AppComponent = AppComponent = tslib_1.__decorate([
    (0, core_1.Component)({
        selector: 'app-root',
        templateUrl: true === true
            ? './app.component.html'
            : './app.component.copy.html',
        styles: [require("./app.component.css?ngResource"), require("./app.component.2.css?ngResource")]
    })
], AppComponent);
Exception or Error
Component 'AppComponent' is not resolved:
     - templateUrl: ./app.component.html
    Did you run and wait for 'resolveComponentResources()'?


### Your Environment

```text
Angular CLI: 17.3.6
Node: 20.12.2
Package Manager: yarn 1.22.17
OS: darwin x64

Angular: 18.0.0-rc.0
... animations, cdk, common, compiler, compiler-cli, core, forms
... localize, material, platform-browser
... platform-browser-dynamic, platform-server, router
... service-worker

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.0.0-EXPERIMENTAL-PLACEHOLDER
@angular-devkit/core         0.0.0-PLACEHOLDER
@angular-devkit/schematics   0.0.0-PLACEHOLDER
@angular/bazel               18.1.0-next.0+sha-1872fcd
@angular/build-tooling       0.0.0-e6e98d32d7d4d350ecf4ec10ad9c7e75198daabd
@angular/ng-dev              <error>
@schematics/angular          0.0.0-PLACEHOLDER
ng-packagr                   18.0.0-rc.0
rxjs                         7.8.1
typescript                   5.4.5
webpack                      5.91.0
zone.js                      0.14.4
Anything else relevant?

Related to this bug https://github.com/thymikee/jest-preset-angular/issues/1866

主要语言
TypeScript
星标
27k
派生
11.8k
平均合并
16 小时 35 分钟
30 天内合并 PR
176

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

angular/angular-cli 的其他 Issue

查看 angular/angular-cli 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。