Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,29 +169,42 @@ const init: any = (config: any, emitter: any) => {
compiler.hooks.watchRun.tapAsync('karma', (_: any, callback: () => void) => handler(callback));
compiler.hooks.run.tapAsync('karma', (_: any, callback: () => void) => handler(callback));

webpackMiddleware = webpackDevMiddleware(compiler, webpackMiddlewareConfig);
emitter.on('exit', (done: any) => {
webpackMiddleware.close();
done();
});

function unblock() {
isBlocked = false;
blocked.forEach((cb) => cb());
blocked = [];
}

let lastCompilationHash: string | undefined;
compiler.hooks.done.tap('karma', (stats) => {
if (stats.hasErrors()) {
lastCompilationHash = undefined;
} else if (stats.hash != lastCompilationHash) {
// Refresh karma only when there are no webpack errors, and if the compilation changed.
lastCompilationHash = stats.hash;
emitter.refreshFiles();
}
unblock();
});
let isFirstRun = true;

return new Promise<void>((resolve) => {
compiler.hooks.done.tap('karma', (stats) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if Karma fails? Will this callback still be executed or do we need a separate error handler to reject the Promise?

Copy link
Collaborator Author

@alan-agius4 alan-agius4 Feb 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant if webpack fails, yeah this will always be executed.

We don’t want to reject this promise because otherwise Karma will fail to start.

if (isFirstRun) {
// This is needed to block Karma from launching browsers before Webpack writes the assets in memory.
// See the below:
// https://github.com/karma-runner/karma-chrome-launcher/issues/154#issuecomment-986661937
// https://github.com/angular/angular-cli/issues/22495
isFirstRun = false;
resolve();
}

webpackMiddleware = webpackDevMiddleware(compiler, webpackMiddlewareConfig);
if (stats.hasErrors()) {
lastCompilationHash = undefined;
} else if (stats.hash != lastCompilationHash) {
// Refresh karma only when there are no webpack errors, and if the compilation changed.
lastCompilationHash = stats.hash;
emitter.refreshFiles();
}

emitter.on('exit', (done: any) => {
webpackMiddleware.close();
done();
unblock();
});
});
};

Expand Down