Skip to content

Commit 12ab759

Browse files
committed
refactor(@angular-devkit/build-angular): optimize karma plugin hooks
Consolidates the compiler hooks in the Karma plugin. Merges separate `compiler.hooks.done` taps into a single unified handler that manages error reporting, file refreshing, and blocking logic.
1 parent 98f3b93 commit 12ab759

File tree

1 file changed

+26
-30
lines changed
  • packages/angular_devkit/build_angular/src/tools/webpack/plugins/karma

1 file changed

+26
-30
lines changed

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

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,6 @@ const init: any = (config: any, emitter: any) => {
8585
config.middleware = config.middleware || [];
8686
config.middleware.push('@angular-devkit/build-angular--fallback');
8787

88-
compiler.hooks.done.tap('karma', (stats) => {
89-
if (stats.hasErrors()) {
90-
// Only generate needed JSON stats and when needed.
91-
const statsJson = stats.toJson({
92-
all: false,
93-
children: true,
94-
errors: true,
95-
warnings: true,
96-
});
97-
98-
logger.error(statsErrorsToString(statsJson, { colors: true }));
99-
100-
if (config.singleRun) {
101-
// Notify potential listeners of the compile error.
102-
emitter.emit('load_error');
103-
}
104-
105-
// Finish Karma run early in case of compilation error.
106-
emitter.emit('run_complete', [], { exitCode: 1 });
107-
}
108-
});
109-
11088
function handler(callback?: () => void): void {
11189
isBlocked = true;
11290
callback?.();
@@ -133,6 +111,32 @@ const init: any = (config: any, emitter: any) => {
133111

134112
return new Promise<void>((resolve) => {
135113
compiler.hooks.done.tap('karma', (stats) => {
114+
if (stats.hasErrors()) {
115+
lastCompilationHash = undefined;
116+
117+
// Only generate needed JSON stats and when needed.
118+
const statsJson = stats.toJson({
119+
all: false,
120+
children: true,
121+
errors: true,
122+
warnings: true,
123+
});
124+
125+
logger.error(statsErrorsToString(statsJson, { colors: true }));
126+
127+
if (config.singleRun) {
128+
// Notify potential listeners of the compile error.
129+
emitter.emit('load_error');
130+
}
131+
132+
// Finish Karma run early in case of compilation error.
133+
emitter.emit('run_complete', [], { exitCode: 1 });
134+
} else if (stats.hash != lastCompilationHash) {
135+
// Refresh karma only when there are no webpack errors, and if the compilation changed.
136+
lastCompilationHash = stats.hash;
137+
emitter.refreshFiles();
138+
}
139+
136140
if (isFirstRun) {
137141
// This is needed to block Karma from launching browsers before Webpack writes the assets in memory.
138142
// See the below:
@@ -142,14 +146,6 @@ const init: any = (config: any, emitter: any) => {
142146
resolve();
143147
}
144148

145-
if (stats.hasErrors()) {
146-
lastCompilationHash = undefined;
147-
} else if (stats.hash != lastCompilationHash) {
148-
// Refresh karma only when there are no webpack errors, and if the compilation changed.
149-
lastCompilationHash = stats.hash;
150-
emitter.refreshFiles();
151-
}
152-
153149
unblock();
154150
});
155151
});

0 commit comments

Comments
 (0)