@@ -20,6 +20,14 @@ var escapeRegExp = function(str) {
20
20
return str . replace ( / [ - [ \] / { } ( ) * + ? . \\ ^ $ | ] / g, '\\$&' )
21
21
}
22
22
23
+ function invalidate ( middleware ) {
24
+ if ( middleware . context . watching ) {
25
+ return middleware . context . watching . invalidate ( )
26
+ }
27
+
28
+ return middleware . invalidate ( )
29
+ }
30
+
23
31
function Plugin (
24
32
/* config.webpack */ webpackOptions ,
25
33
/* config.webpackServer */ webpackServerOptions ,
@@ -75,6 +83,7 @@ function Plugin(
75
83
this . files = [ ]
76
84
this . basePath = basePath
77
85
this . waiting = [ ]
86
+ this . plugin = { name : 'KarmaWebpack' }
78
87
79
88
var compiler
80
89
@@ -91,23 +100,56 @@ function Plugin(
91
100
var applyPlugins = compiler . compilers || [ compiler ]
92
101
93
102
applyPlugins . forEach ( function ( compiler ) {
94
- compiler . plugin ( 'this-compilation' , function ( compilation , params ) {
95
- compilation . dependencyFactories . set ( SingleEntryDependency , params . normalModuleFactory )
96
- } )
97
- compiler . plugin ( 'make' , this . make . bind ( this ) )
98
- } , this ) ;
103
+ if ( compiler . hooks ) {
104
+ compiler . hooks
105
+ . thisCompilation
106
+ . tap ( this . plugin , ( compilation , params ) => {
107
+ compilation . dependencyFactories . set ( SingleEntryDependency , params . normalModuleFactory )
108
+ } )
109
+ compiler . hooks
110
+ . make
111
+ . tapAsync ( this . plugin , this . make . bind ( this ) )
112
+ } else {
113
+ compiler . plugin ( 'this-compilation' , function ( compilation , params ) {
114
+ compilation . dependencyFactories . set ( SingleEntryDependency , params . normalModuleFactory )
115
+ } )
116
+ compiler . plugin ( 'make' , this . make . bind ( this ) )
117
+ }
118
+ } , this )
119
+
120
+ function handler ( callback ) {
121
+ isBlocked = true
122
+
123
+ if ( typeof callback === 'function' ) {
124
+ callback ( null )
125
+ }
126
+ }
99
127
100
- [ 'invalid' , 'watch-run' , 'run' ] . forEach ( function ( name ) {
101
- compiler . plugin ( name , function ( _ , callback ) {
102
- isBlocked = true
128
+ var hooks = [ 'invalid' , 'watch-run' , 'run' ]
103
129
104
- if ( typeof callback === 'function' ) {
105
- callback ( )
130
+ if ( compiler . hooks ) {
131
+ hooks = [
132
+ { method : 'sync' , name : 'invalid' } ,
133
+ { method : 'async' , name : 'watchRun' } ,
134
+ { method : 'async' , name : 'run' }
135
+ ]
136
+ }
137
+
138
+ hooks . forEach ( function ( hook ) {
139
+ if ( compiler . hooks ) {
140
+ if ( hook . method === 'sync' ) {
141
+ compiler . hooks [ hook . name ] . tap ( this . plugin , ( ) => handler ( ) )
142
+ } else {
143
+ compiler . hooks [ hook . name ] . tapAsync ( this . plugin , ( _ , callback ) => handler ( callback ) )
106
144
}
107
- } )
108
- } )
145
+ } else {
146
+ compiler . plugin ( hook , function ( _ , callback ) {
147
+ handler ( callback )
148
+ } )
149
+ }
150
+ } , this )
109
151
110
- compiler . plugin ( 'done' , function ( stats ) {
152
+ function done ( stats ) {
111
153
var applyStats = Array . isArray ( stats . stats ) ? stats . stats : [ stats ]
112
154
var assets = [ ]
113
155
var noAssets = false
@@ -139,12 +181,21 @@ function Plugin(
139
181
blocked [ i ] ( )
140
182
}
141
183
blocked = [ ]
142
- } . bind ( this ) )
143
- compiler . plugin ( 'invalid' , function ( ) {
184
+ }
185
+
186
+ function invalid ( ) {
144
187
if ( ! this . waiting ) {
145
188
this . waiting = [ ]
146
189
}
147
- } . bind ( this ) )
190
+ }
191
+
192
+ if ( compiler . hooks ) {
193
+ compiler . hooks . done . tap ( this . plugin , done . bind ( this ) )
194
+ compiler . hooks . invalid . tap ( this . plugin , invalid . bind ( this ) )
195
+ } else {
196
+ compiler . plugin ( 'done' , done . bind ( this ) )
197
+ compiler . plugin ( 'invalid' , invalid . bind ( this ) )
198
+ }
148
199
149
200
webpackMiddlewareOptions . publicPath = path . join ( os . tmpdir ( ) , '_karma_webpack_' , '/' )
150
201
var middleware = this . middleware = new webpackDevMiddleware ( compiler , webpackMiddlewareOptions )
@@ -197,7 +248,7 @@ Plugin.prototype.make = function(compilation, callback) {
197
248
this . files = this . files . filter ( function ( f ) {
198
249
return file !== f
199
250
} )
200
- this . middleware . invalidate ( )
251
+ invalidate ( this . middleware )
201
252
}
202
253
callback ( err )
203
254
} . bind ( this ) )
@@ -259,7 +310,7 @@ function createPreprocesor(/* config.basePath */ basePath, webpackPlugin) {
259
310
return function ( content , file , done ) {
260
311
if ( webpackPlugin . addFile ( file . originalPath ) ) {
261
312
// recompile as we have an asset that we have not seen before
262
- webpackPlugin . middleware . invalidate ( )
313
+ invalidate ( webpackPlugin . middleware )
263
314
}
264
315
265
316
// read blocks until bundle is done
0 commit comments