Use classes for webpack plugins.

This commit is contained in:
Jason Miller
2018-03-27 20:39:13 -04:00
parent 465d043502
commit ccf71556a6

View File

@@ -1,18 +1,17 @@
let fs = require('fs'); const fs = require('fs');
module.exports = WatchTimestampsPlugin; module.exports = class WatchTimestampsPlugin {
constructor(patterns) {
function WatchTimestampsPlugin(patterns) {
this.patterns = patterns; this.patterns = patterns;
} }
WatchTimestampsPlugin.prototype.apply = function (compiler) { apply(compiler) {
compiler.plugin('watch-run', (watch, callback) => { compiler.plugin('watch-run', (watch, callback) => {
let patterns = this.patterns; let patterns = this.patterns;
let timestamps = watch.fileTimestamps || watch.compiler.fileTimestamps; let timestamps = watch.fileTimestamps || watch.compiler.fileTimestamps;
Object.keys(timestamps).forEach( filepath => { Object.keys(timestamps).forEach(filepath => {
if (patterns.some( pat => pat instanceof RegExp ? pat.test(filepath) : filepath.indexOf(pat)===0 )) { if (patterns.some(pat => pat instanceof RegExp ? pat.test(filepath) : filepath.indexOf(pat) === 0)) {
let time = fs.statSync(filepath).mtime; let time = fs.statSync(filepath).mtime;
if (timestamps instanceof Map) timestamps.set(filepath, time); if (timestamps instanceof Map) timestamps.set(filepath, time);
else timestamps[filepath] = time; else timestamps[filepath] = time;
@@ -20,4 +19,5 @@ WatchTimestampsPlugin.prototype.apply = function (compiler) {
}); });
callback(); callback();
}); });
}
}; };