Skip to content

Commit fb78141

Browse files
committed
feat(@angular/build): move istanbul-lib-instrument to optional peer dependency
Move `istanbul-lib-instrument` from dependencies to optional peer dependencies. This helps reduce the transitive dependency count for new projects. BREAKING CHANGE: `istanbul-lib-instrument` is now an optional peer dependency. Projects using karma with code coverage enabled will need to ensure that istanbul-lib-instrument is installed. Note: `ng update` will automatically add this dependency during the update process.
1 parent 7fbc715 commit fb78141

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

packages/angular/build/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"browserslist": "^4.26.0",
3030
"esbuild": "0.27.3",
3131
"https-proxy-agent": "8.0.0",
32-
"istanbul-lib-instrument": "6.0.3",
3332
"jsonc-parser": "3.3.1",
3433
"listr2": "10.2.1",
3534
"magic-string": "0.30.21",
@@ -67,6 +66,7 @@
6766
"@angular/platform-server": "0.0.0-ANGULAR-FW-PEER-DEP",
6867
"@angular/service-worker": "0.0.0-ANGULAR-FW-PEER-DEP",
6968
"@angular/ssr": "^0.0.0-PLACEHOLDER",
69+
"istanbul-lib-instrument": "^6.0.0",
7070
"karma": "^6.4.0",
7171
"less": "^4.2.0",
7272
"ng-packagr": "0.0.0-NG-PACKAGR-PEER-DEP",
@@ -95,6 +95,9 @@
9595
"@angular/ssr": {
9696
"optional": true
9797
},
98+
"istanbul-lib-instrument": {
99+
"optional": true
100+
},
98101
"karma": {
99102
"optional": true
100103
},

packages/angular/build/src/tools/babel/plugins/add-code-coverage.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
*/
88

99
import { NodePath, PluginObj, types } from '@babel/core';
10-
import { Visitor, programVisitor } from 'istanbul-lib-instrument';
10+
import type { Visitor } from 'istanbul-lib-instrument';
1111
import assert from 'node:assert';
1212

1313
/**
1414
* A babel plugin factory function for adding istanbul instrumentation.
1515
*
1616
* @returns A babel plugin object instance.
1717
*/
18-
export default function (): PluginObj {
18+
export default function (
19+
programVisitor: typeof import('istanbul-lib-instrument').programVisitor,
20+
): PluginObj {
1921
const visitors = new WeakMap<NodePath, Visitor>();
2022

2123
return {

packages/angular/build/src/tools/esbuild/javascript-transformer-worker.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,18 @@ async function transformWithBabel(
6666
const plugins: PluginItem[] = [];
6767

6868
if (options.instrumentForCoverage) {
69-
const { default: coveragePlugin } = await import('../babel/plugins/add-code-coverage.js');
70-
plugins.push(coveragePlugin);
69+
try {
70+
const { programVisitor } = await import('istanbul-lib-instrument');
71+
const { default: coveragePluginFactory } = await import(
72+
'../babel/plugins/add-code-coverage.js'
73+
);
74+
plugins.push(coveragePluginFactory(programVisitor));
75+
} catch {
76+
throw new Error(
77+
"The 'istanbul-lib-instrument' package is required for code coverage but was not found. " +
78+
'Please install it.',
79+
);
80+
}
7181
}
7282

7383
if (shouldLink) {

0 commit comments

Comments
 (0)