Skip to content

Commit d5f5b01

Browse files
clydinhybrist
authored andcommitted
refactor(@angular-devkit/build-angular): decouple karma builder from webpack plugin callbacks
Decouples the Karma builder from the Webpack plugin by removing the custom `successCb` and `failureCb` callback mechanism. The builder now idiomaticallly listens to the standard Karma `run_complete` event on the server instance to emit builder results.
1 parent 00eb7c3 commit d5f5b01

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

packages/angular_devkit/build_angular/src/builders/karma/browser_builder.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { purgeStaleBuildCache } from '@angular/build/private';
1010
import type { BuilderContext, BuilderOutput } from '@angular-devkit/architect';
11-
import type { Config, ConfigOptions, Server } from 'karma';
11+
import type { ConfigOptions, Server } from 'karma';
1212
import * as path from 'node:path';
1313
import type { Configuration } from 'webpack';
1414
import { getCommonConfig, getStylesConfig } from '../../tools/webpack/configs';
@@ -83,11 +83,11 @@ export function execute(
8383
logger: context.logger,
8484
};
8585

86-
const parsedKarmaConfig = (await karma.config.parseConfig(
86+
const parsedKarmaConfig = await karma.config.parseConfig(
8787
options.karmaConfig && path.resolve(context.workspaceRoot, options.karmaConfig),
8888
transforms.karmaOptions ? transforms.karmaOptions(karmaOptions) : karmaOptions,
8989
{ promiseConfig: true, throwErrors: true },
90-
)) as KarmaConfigOptions;
90+
);
9191

9292
if (isCancelled) {
9393
return;
@@ -109,21 +109,16 @@ export function execute(
109109
}
110110
};
111111

112-
// Pass onto Karma to emit BuildEvents.
113-
parsedKarmaConfig.buildWebpack ??= {};
114-
if (typeof parsedKarmaConfig.buildWebpack === 'object') {
115-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
116-
(parsedKarmaConfig.buildWebpack as any).failureCb ??= () => enqueue({ success: false });
117-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
118-
(parsedKarmaConfig.buildWebpack as any).successCb ??= () => enqueue({ success: true });
119-
}
120-
121112
// Close the stream once the Karma server returns.
122-
karmaServer = new karma.Server(parsedKarmaConfig as Config, (exitCode) => {
113+
karmaServer = new karma.Server(parsedKarmaConfig, (exitCode) => {
123114
enqueue({ success: exitCode === 0 });
124115
close();
125116
});
126117

118+
karmaServer.on('run_complete', (_, results) => {
119+
enqueue({ success: results.exitCode === 0 });
120+
});
121+
127122
await karmaServer.start();
128123
},
129124
async cancel() {

packages/angular_devkit/build_angular/src/tools/webpack/plugins/karma/karma.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ const KARMA_APPLICATION_PATH = '_karma_webpack_';
2525
let blocked: any[] = [];
2626
let isBlocked = false;
2727
let webpackMiddleware: any;
28-
let successCb: () => void;
29-
let failureCb: () => void;
3028

3129
const init: any = (config: any, emitter: any) => {
3230
if (!config.buildWebpack) {
@@ -37,8 +35,6 @@ const init: any = (config: any, emitter: any) => {
3735
}
3836
const options = config.buildWebpack.options as BuildOptions;
3937
const logger: logging.Logger = config.buildWebpack.logger || createConsoleLogger();
40-
successCb = config.buildWebpack.successCb;
41-
failureCb = config.buildWebpack.failureCb;
4238

4339
// Add a reporter that fixes sourcemap urls.
4440
if (normalizeSourceMaps(options.sourceMap).scripts) {
@@ -132,9 +128,6 @@ const init: any = (config: any, emitter: any) => {
132128

133129
// Finish Karma run early in case of compilation error.
134130
emitter.emit('run_complete', [], { exitCode: 1 });
135-
136-
// Emit a failure build event if there are compilation errors.
137-
failureCb();
138131
}
139132
});
140133

@@ -224,14 +217,6 @@ const eventReporter: any = function (this: any, baseReporterDecorator: any, conf
224217

225218
muteDuplicateReporterLogging(this, config);
226219

227-
this.onRunComplete = function (_browsers: any, results: any) {
228-
if (results.exitCode === 0) {
229-
successCb();
230-
} else {
231-
failureCb();
232-
}
233-
};
234-
235220
// avoid duplicate failure message
236221
this.specFailure = () => {};
237222
};

0 commit comments

Comments
 (0)