Skip to content

Commit 8ce35de

Browse files
committed
add blocker
1 parent 2a89e25 commit 8ce35de

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

packages/@angular/cli/plugins/karma.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import { KarmaWebpackThrowError } from './karma-webpack-throw-error';
1010

1111
const getAppFromConfig = require('../utilities/app-utils').getAppFromConfig;
1212

13+
let blocked: any[] = [];
14+
let isBlocked = false;
15+
1316
function isDirectory(path: string) {
1417
try {
1518
return fs.statSync(path).isDirectory();
@@ -91,16 +94,7 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
9194
const webpackMiddlewareConfig = {
9295
noInfo: true, // Hide webpack output because its noisy.
9396
watchOptions: { poll: testConfig.poll },
94-
publicPath: '/_karma_webpack_/',
95-
stats: { // Also prevent chunk and module display output, cleaner look. Only emit errors.
96-
assets: false,
97-
colors: true,
98-
version: false,
99-
hash: false,
100-
timings: false,
101-
chunks: false,
102-
chunkModules: false
103-
},
97+
publicPath: '/_karma_webpack_/'
10498
};
10599

106100
// If Karma is being ran in single run mode, throw errors.
@@ -129,6 +123,9 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
129123
});
130124

131125

126+
// Add the blocker.
127+
config.beforeMiddleware = config.beforeMiddleware || [];
128+
config.beforeMiddleware.push('angularCliBlocker');
132129

133130
// code from karma-webpack
134131

@@ -148,7 +145,22 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
148145
throw e;
149146
}
150147

151-
compiler.plugin('done', () => emitter.refreshFiles());
148+
['invalid', 'watch-run', 'run'].forEach(function (name) {
149+
compiler.plugin(name, function (_: any, callback: () => void) {
150+
isBlocked = true;
151+
152+
if (typeof callback === 'function') {
153+
callback();
154+
}
155+
});
156+
});
157+
158+
compiler.plugin('done', () => {
159+
emitter.refreshFiles();
160+
isBlocked = false;
161+
blocked.forEach((cb) => cb());
162+
blocked = [];
163+
});
152164

153165
const middleware = new webpackDevMiddleware(compiler, webpackMiddlewareConfig);
154166

@@ -174,9 +186,21 @@ init.$inject = ['config', 'emitter', 'customFileHandlers'];
174186
const preprocessor: any = () => (content: any, _file: string, done: any) => done(null, content);
175187
preprocessor.$inject = [];
176188

189+
// Block requests until the Webpack compilation is done.
190+
function requestBlocker() {
191+
return function (_request: any, _response: any, next: () => void) {
192+
if (isBlocked) {
193+
blocked.push(next);
194+
} else {
195+
next();
196+
}
197+
};
198+
}
199+
177200
// Also export karma-webpack and karma-sourcemap-loader.
178201
module.exports = Object.assign({
179202
'framework:@angular/cli': ['factory', init],
180-
'preprocessor:@angular/cli': ['factory', preprocessor]
203+
'preprocessor:@angular/cli': ['factory', preprocessor],
204+
'middleware:angularCliBlocker': ['factory', requestBlocker]
181205
// }, require('./karma-webpack-custom'), require('karma-sourcemap-loader'));
182206
});

0 commit comments

Comments
 (0)