Skip to content

Commit 4fe1f60

Browse files
author
Ryan Clark
committed
chore(controller): change the webpack watch logic
webpack v5 requires a callback passed to the compiler instead of using .watch()
1 parent 8d7366f commit 4fe1f60

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

lib/KarmaWebpackController.js

+18-19
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class KarmaSyncPlugin {
2626
}`;
2727
this.controller.bundlesContent[webpackFileObj.name] = fs.readFileSync(
2828
filePath,
29-
'utf-8'
29+
'utf-8',
3030
);
3131
});
3232

@@ -88,13 +88,8 @@ class KarmaWebpackController {
8888
new KarmaSyncPlugin({
8989
karmaEmitter: emitter,
9090
controller: this,
91-
})
91+
}),
9292
);
93-
94-
emitter.on('exit', (done) => {
95-
this.onKarmaExit();
96-
done();
97-
});
9893
}
9994

10095
get karmaEmitter() {
@@ -112,6 +107,15 @@ class KarmaWebpackController {
112107
this.webpackOptions = defaultWebpackOptions;
113108
}
114109

110+
setupExitHandler(compiler) {
111+
this.karmaEmitter.once('exit', (done) => {
112+
compiler.close(() => {
113+
console.log('Webpack stopped watching.');
114+
done();
115+
});
116+
});
117+
}
118+
115119
updateWebpackOptions(newOptions) {
116120
this.webpackOptions = merge(this.webpackOptions, newOptions);
117121
}
@@ -126,16 +130,18 @@ class KarmaWebpackController {
126130

127131
async _bundle() {
128132
this.isActive = true;
129-
this.compiler = webpack(this.webpackOptions);
133+
130134
return new Promise((resolve) => {
131135
if (this.webpackOptions.watch === true) {
132136
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),
135139
);
140+
141+
this.setupExitHandler(this.compiler);
136142
} 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),
139145
);
140146
}
141147
});
@@ -164,13 +170,6 @@ class KarmaWebpackController {
164170
console.log(stats.toString(this.webpackOptions.stats));
165171
resolve();
166172
}
167-
168-
onKarmaExit() {
169-
if (this.webpackFileWatcher) {
170-
this.webpackFileWatcher.close();
171-
console.log('Webpack stopped watching.');
172-
}
173-
}
174173
}
175174

176175
module.exports = {

0 commit comments

Comments
 (0)