Skip to content

Commit

Permalink
chore(controller): change the webpack watch logic
Browse files Browse the repository at this point in the history
webpack v5 requires a callback passed to the compiler instead of
using .watch()
  • Loading branch information
Ryan Clark committed Dec 6, 2020
1 parent 8d7366f commit 4fe1f60
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions lib/KarmaWebpackController.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class KarmaSyncPlugin {
}`;
this.controller.bundlesContent[webpackFileObj.name] = fs.readFileSync(
filePath,
'utf-8'
'utf-8',
);
});

Expand Down Expand Up @@ -88,13 +88,8 @@ class KarmaWebpackController {
new KarmaSyncPlugin({
karmaEmitter: emitter,
controller: this,
})
}),
);

emitter.on('exit', (done) => {
this.onKarmaExit();
done();
});
}

get karmaEmitter() {
Expand All @@ -112,6 +107,15 @@ class KarmaWebpackController {
this.webpackOptions = defaultWebpackOptions;
}

setupExitHandler(compiler) {
this.karmaEmitter.once('exit', (done) => {
compiler.close(() => {
console.log('Webpack stopped watching.');
done();
});
});
}

updateWebpackOptions(newOptions) {
this.webpackOptions = merge(this.webpackOptions, newOptions);
}
Expand All @@ -126,16 +130,18 @@ class KarmaWebpackController {

async _bundle() {
this.isActive = true;
this.compiler = webpack(this.webpackOptions);

return new Promise((resolve) => {
if (this.webpackOptions.watch === true) {
console.log('Webpack starts watching...');
this.webpackFileWatcher = this.compiler.watch({}, (err, stats) =>
this.handleBuildResult(err, stats, resolve)
this.compiler = webpack(this.webpackOptions, (err, stats) =>
this.handleBuildResult(err, stats, resolve),
);

this.setupExitHandler(this.compiler);
} else {
this.compiler.run((err, stats) =>
this.handleBuildResult(err, stats, resolve)
this.compiler = webpack(this.webpackOptions).run((err, stats) =>
this.handleBuildResult(err, stats, resolve),
);
}
});
Expand Down Expand Up @@ -164,13 +170,6 @@ class KarmaWebpackController {
console.log(stats.toString(this.webpackOptions.stats));
resolve();
}

onKarmaExit() {
if (this.webpackFileWatcher) {
this.webpackFileWatcher.close();
console.log('Webpack stopped watching.');
}
}
}

module.exports = {
Expand Down

0 comments on commit 4fe1f60

Please sign in to comment.