@@ -26,7 +26,7 @@ class KarmaSyncPlugin {
26
26
} `;
27
27
this . controller . bundlesContent [ webpackFileObj . name ] = fs . readFileSync (
28
28
filePath ,
29
- 'utf-8'
29
+ 'utf-8' ,
30
30
) ;
31
31
} ) ;
32
32
@@ -88,13 +88,8 @@ class KarmaWebpackController {
88
88
new KarmaSyncPlugin ( {
89
89
karmaEmitter : emitter ,
90
90
controller : this ,
91
- } )
91
+ } ) ,
92
92
) ;
93
-
94
- emitter . on ( 'exit' , ( done ) => {
95
- this . onKarmaExit ( ) ;
96
- done ( ) ;
97
- } ) ;
98
93
}
99
94
100
95
get karmaEmitter ( ) {
@@ -112,6 +107,15 @@ class KarmaWebpackController {
112
107
this . webpackOptions = defaultWebpackOptions ;
113
108
}
114
109
110
+ setupExitHandler ( compiler ) {
111
+ this . karmaEmitter . once ( 'exit' , ( done ) => {
112
+ compiler . close ( ( ) => {
113
+ console . log ( 'Webpack stopped watching.' ) ;
114
+ done ( ) ;
115
+ } ) ;
116
+ } ) ;
117
+ }
118
+
115
119
updateWebpackOptions ( newOptions ) {
116
120
this . webpackOptions = merge ( this . webpackOptions , newOptions ) ;
117
121
}
@@ -126,16 +130,18 @@ class KarmaWebpackController {
126
130
127
131
async _bundle ( ) {
128
132
this . isActive = true ;
129
- this . compiler = webpack ( this . webpackOptions ) ;
133
+
130
134
return new Promise ( ( resolve ) => {
131
135
if ( this . webpackOptions . watch === true ) {
132
136
console . log ( 'Webpack starts watching...' ) ;
133
- this . webpackFileWatcher = this . compiler . watch ( { } , ( err , stats ) =>
134
- this . handleBuildResult ( err , stats , resolve )
137
+ this . compiler = webpack ( this . webpackOptions , ( err , stats ) =>
138
+ this . handleBuildResult ( err , stats , resolve ) ,
135
139
) ;
140
+
141
+ this . setupExitHandler ( this . compiler ) ;
136
142
} else {
137
- this . compiler . run ( ( err , stats ) =>
138
- this . handleBuildResult ( err , stats , resolve )
143
+ this . compiler = webpack ( this . webpackOptions ) . run ( ( err , stats ) =>
144
+ this . handleBuildResult ( err , stats , resolve ) ,
139
145
) ;
140
146
}
141
147
} ) ;
@@ -164,13 +170,6 @@ class KarmaWebpackController {
164
170
console . log ( stats . toString ( this . webpackOptions . stats ) ) ;
165
171
resolve ( ) ;
166
172
}
167
-
168
- onKarmaExit ( ) {
169
- if ( this . webpackFileWatcher ) {
170
- this . webpackFileWatcher . close ( ) ;
171
- console . log ( 'Webpack stopped watching.' ) ;
172
- }
173
- }
174
173
}
175
174
176
175
module . exports = {
0 commit comments