Skip to content

Commit 86e031d

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): use istanbul-lib-instrument directly for karma code coverage
The `istanbul-lib-instrument` package provides the required functionality needed to instrument code for test coverage within the context of the Angular CLI. Since the build pipeline already contains a customized babel preset, this package can be integrated directly into the pipeline. This reduces the number of dependencies required for `@angular-devkit/build-angular` including the deprecated `inflight` package. (cherry picked from commit fb2981d)
1 parent bdd168f commit 86e031d

File tree

7 files changed

+271
-60
lines changed

7 files changed

+271
-60
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@
122122
"ansi-colors": "4.1.3",
123123
"autoprefixer": "10.4.19",
124124
"babel-loader": "9.1.3",
125-
"babel-plugin-istanbul": "6.1.1",
126125
"browser-sync": "3.0.2",
127126
"browserslist": "^4.21.5",
128127
"buffer": "6.0.3",
@@ -145,6 +144,7 @@
145144
"husky": "9.0.11",
146145
"ini": "4.1.2",
147146
"inquirer": "9.2.22",
147+
"istanbul-lib-instrument": "6.0.2",
148148
"jasmine": "^5.0.0",
149149
"jasmine-core": "~5.1.0",
150150
"jasmine-spec-reporter": "~7.0.0",

packages/angular_devkit/build_angular/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ ts_library(
162162
"@npm//ansi-colors",
163163
"@npm//autoprefixer",
164164
"@npm//babel-loader",
165-
"@npm//babel-plugin-istanbul",
166165
"@npm//browserslist",
167166
"@npm//copy-webpack-plugin",
168167
"@npm//critters",
@@ -173,6 +172,7 @@ ts_library(
173172
"@npm//http-proxy-middleware",
174173
"@npm//https-proxy-agent",
175174
"@npm//inquirer",
175+
"@npm//istanbul-lib-instrument",
176176
"@npm//jsonc-parser",
177177
"@npm//karma",
178178
"@npm//karma-source-map-support",

packages/angular_devkit/build_angular/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"ansi-colors": "4.1.3",
2727
"autoprefixer": "10.4.19",
2828
"babel-loader": "9.1.3",
29-
"babel-plugin-istanbul": "6.1.1",
3029
"browserslist": "^4.21.5",
3130
"copy-webpack-plugin": "11.0.0",
3231
"critters": "0.0.22",
@@ -36,6 +35,7 @@
3635
"https-proxy-agent": "7.0.4",
3736
"http-proxy-middleware": "3.0.0",
3837
"inquirer": "9.2.22",
38+
"istanbul-lib-instrument": "6.0.2",
3939
"jsonc-parser": "3.2.1",
4040
"karma-source-map-support": "1.4.0",
4141
"less": "4.2.0",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { NodePath, PluginObj, types } from '@babel/core';
10+
import { Visitor, programVisitor } from 'istanbul-lib-instrument';
11+
import assert from 'node:assert';
12+
13+
/**
14+
* A babel plugin factory function for adding istanbul instrumentation.
15+
*
16+
* @returns A babel plugin object instance.
17+
*/
18+
export default function (): PluginObj {
19+
const visitors = new WeakMap<NodePath, Visitor>();
20+
21+
return {
22+
visitor: {
23+
Program: {
24+
enter(path, state) {
25+
const visitor = programVisitor(types, state.filename, {
26+
// Babel returns a Converter object from the `convert-source-map` package
27+
inputSourceMap: (state.file.inputMap as undefined | { toObject(): object })?.toObject(),
28+
});
29+
visitors.set(path, visitor);
30+
31+
visitor.enter(path);
32+
},
33+
exit(path) {
34+
const visitor = visitors.get(path);
35+
assert(visitor, 'Instrumentation visitor should always be present for program path.');
36+
37+
visitor.exit(path);
38+
visitors.delete(path);
39+
},
40+
},
41+
},
42+
};
43+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
declare module 'istanbul-lib-instrument' {
10+
export interface Visitor {
11+
enter(path: import('@babel/core').NodePath<types.Program>): void;
12+
exit(path: import('@babel/core').NodePath<types.Program>): void;
13+
}
14+
15+
export function programVisitor(
16+
types: typeof import('@babel/core').types,
17+
filePath?: string,
18+
options?: { inputSourceMap?: object | null },
19+
): Visitor;
20+
}

packages/angular_devkit/build_angular/src/tools/babel/presets/application.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,7 @@ export default function (api: unknown, options: ApplicationPresetOptions) {
265265
}
266266

267267
if (options.instrumentCode) {
268-
plugins.push([
269-
require('babel-plugin-istanbul').default,
270-
{
271-
inputSourceMap: options.instrumentCode.inputSourceMap ?? false,
272-
cwd: options.instrumentCode.includedBasePath,
273-
},
274-
]);
268+
plugins.push(require('../plugins/add-code-coverage').default);
275269
}
276270

277271
if (needRuntimeTransform) {

0 commit comments

Comments
 (0)