@@ -10,6 +10,9 @@ import { KarmaWebpackThrowError } from './karma-webpack-throw-error';
10
10
11
11
const getAppFromConfig = require ( '../utilities/app-utils' ) . getAppFromConfig ;
12
12
13
+ let blocked : any [ ] = [ ] ;
14
+ let isBlocked = false ;
15
+
13
16
function isDirectory ( path : string ) {
14
17
try {
15
18
return fs . statSync ( path ) . isDirectory ( ) ;
@@ -91,16 +94,7 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
91
94
const webpackMiddlewareConfig = {
92
95
noInfo : true , // Hide webpack output because its noisy.
93
96
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_/'
104
98
} ;
105
99
106
100
// If Karma is being ran in single run mode, throw errors.
@@ -129,6 +123,9 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
129
123
} ) ;
130
124
131
125
126
+ // Add the blocker.
127
+ config . beforeMiddleware = config . beforeMiddleware || [ ] ;
128
+ config . beforeMiddleware . push ( 'angularCliBlocker' ) ;
132
129
133
130
// code from karma-webpack
134
131
@@ -148,7 +145,22 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
148
145
throw e ;
149
146
}
150
147
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
+ } ) ;
152
164
153
165
const middleware = new webpackDevMiddleware ( compiler , webpackMiddlewareConfig ) ;
154
166
@@ -174,9 +186,21 @@ init.$inject = ['config', 'emitter', 'customFileHandlers'];
174
186
const preprocessor : any = ( ) => ( content : any , _file : string , done : any ) => done ( null , content ) ;
175
187
preprocessor . $inject = [ ] ;
176
188
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
+
177
200
// Also export karma-webpack and karma-sourcemap-loader.
178
201
module . exports = Object . assign ( {
179
202
'framework:@angular/cli' : [ 'factory' , init ] ,
180
- 'preprocessor:@angular/cli' : [ 'factory' , preprocessor ]
203
+ 'preprocessor:@angular/cli' : [ 'factory' , preprocessor ] ,
204
+ 'middleware:angularCliBlocker' : [ 'factory' , requestBlocker ]
181
205
// }, require('./karma-webpack-custom'), require('karma-sourcemap-loader'));
182
206
} ) ;
0 commit comments