Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

Expose internal "instrumentForCoverage" option

未關閉
#30,742 6 則留言 61 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
58/100
Issue 類型
功能
描述清晰度
描述清楚
活躍度
冷清
技術堆疊
angular, typescript

研究方向

從 @angular/build/src/builders/application/schema.json 和 @angular/build/src/tools/esbuild/angular/compiler-plugin.js 開始,接著追蹤 application builder 選項如何傳遞到 compiler plugin。在讓該選項可用之前,檢查現有的 builder 和 compiler-plugin 測試;當該選項被接受、對預期的應用程式程式碼進行插樁,並在收集覆蓋率時排除 node_modules 後,即可視為工作完成。

由索引模型根據 Issue 內容生成。

描述

angular/build:application area: @angular/build feature feature: under consideration
Command

build

Description

E2E test runners like Cypress, in order to collect code coverage data, require the code served by the dev-server to be instrumented. I'd like the built-in builder to let me do exactly that, considering the fact that it can.

Describe the solution you'd like

I'd like to be able to specify "instrumentForCoverage": true in my @angular/build:application options and be done with it. I've patched my node modules to see what that would look like and it works like a charm: @cypress/code-coverage is generating the reports, the node_modules are excluded, and I'm able to use the same builder for E2E and prod builds.

In the next section I've collected some of the hoops I had to jump through to get close (but fail to attain) this level of integration. By the end of the read, I hope you'll agree with me that a built-in solution (considering it's already coded!) would be a step-up in terms of developer experience.

In the meantime, for reference, here's the quick-n-dirty patch.

diff --git a/node_modules/@angular/build/src/builders/application/schema.json b/node_modules/@angular/build/src/builders/application/schema.json
index 3ee8699..0b893a0 100755
--- a/node_modules/@angular/build/src/builders/application/schema.json
+++ b/node_modules/@angular/build/src/builders/application/schema.json
@@ -47,6 +47,10 @@
       "type": "string",
       "description": "Customize the base path for the URLs of resources in 'index.html' and component stylesheets. This option is only necessary for specific deployment scenarios, such as with Angular Elements or when utilizing different CDN locations."
     },
+    "instrumentForCoverage": {
+      "type": "boolean",
+      "description": "Enables instrumentation to collect code coverage data for specific files."
+    },
     "security": {
       "description": "Security features to protect against XSS and other common attacks",
       "type": "object",
diff --git a/node_modules/@angular/build/src/tools/esbuild/angular/compiler-plugin.js b/node_modules/@angular/build/src/tools/esbuild/angular/compiler-plugin.js
index 675ae15..ab42aab 100755
--- a/node_modules/@angular/build/src/tools/esbuild/angular/compiler-plugin.js
+++ b/node_modules/@angular/build/src/tools/esbuild/angular/compiler-plugin.js
@@ -354,7 +354,7 @@ function createCompilerPlugin(pluginOptions, compilationOrFactory, stylesheetBun
                     // A string indicates untransformed output from the TS/NG compiler.
                     // This step is unneeded when using esbuild transpilation.
                     const sideEffects = await hasSideEffects(request);
-                    const instrumentForCoverage = pluginOptions.instrumentForCoverage?.(request);
+                    const instrumentForCoverage = pluginOptions.instrumentForCoverage; //?.(request);
                     contents = await javascriptTransformer.transformData(request, contents, true /* skipLinker */, sideEffects, instrumentForCoverage);
                     // Store as the returned Uint8Array to allow caching the fully transformed code
                     typeScriptFileCache.set(request, contents);
Describe alternatives you've considered

In my search, for Angular 20, I've found only one semi-working[1] solution that requires a 3rd-party builder (@angular-builder/custom-webpack) based on Webpack. That might sound like I haven't done any due diligence, when in fact I have.

  • @angular-builder/custom-esbuild with esbuild-plugin-istanbul doesn't work because the onLoad callback of an esbuild plugin (such as the one defined by the istanbul plugin) is called only as long as no previous plugin handled the resource that's being loaded, and the angular-compiler handles them all.

    • A possible solution could be a custom plugin that overrides the default namespace of each resource we may want to instrument (with onResolve), which would effectively sidestep the Angular compiler BUT force the plugin author to reimplement the compilation. Very high effort.
  • A Vite plugin (rather than an esbuild one) could work, but the Angular builder is not based on Vite. @analogjs/vite-plugin-angular is, but it doesn't have a builder, only an executor for @nx, which is another can of worms. In short, the migration path isn't straightforward at all, and let's remember: this is all to add instrumentation!

  • I've considered trying Angular Rspack, but it's still experimental. I want my E2E tests to be as close to my production environment as they can get, and using two different builders (one for prod and one for e2e) is not an option.

  • Finally, of course, the @angular/build package doesn't do instrumentation. Not through the builder schema anyway.

Apart from my lack of Webpack knowledge (see footnote), it appears to me that the setup I found is still not ideal. Some reasons:

  1. Angular moved away from Webpack. The happy path--aka, the officially supported path--is esbuild, not Webpack.
  2. Webpack is slower than esbuild in most cases. Not a big deal but I'm sure there are some peeps who'd be very impacted by that.
  3. No third-party esbuild-based builder that makes use of the official devkit/extends the @angular/build builder will ever be able to offer a different experience. Any plugin passed to the builder will always be appended to the end of the list of plugins, after the angular compiler. Ergo, no onLoad callbacks.

[1] I say semi-working 'cause I don't know Webpack enough--but who does 😆--and didn't manage to add the instrumentation to the un-chunked code, which regrettably leads to instrumented node_modules

主要語言
TypeScript
星號
27k
分支
11.8k
平均合併
17 小時 25 分鐘
30 天內合併 PR
183

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

angular/angular-cli 的其他 Issue

查看 angular/angular-cli 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。