Skip to content

Commit 3b02644

Browse files
committed
added experimental NewWatchingPlugin webpack#125
1 parent 272d721 commit 3b02644

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

‎lib/NewWatchingPlugin.js‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var Watchpack = require("watchpack");
6+
7+
function NewWatchingPlugin() {
8+
}
9+
module.exports = NewWatchingPlugin;
10+
11+
NewWatchingPlugin.prototype.apply = function(compiler) {
12+
compiler.plugin("environment", function() {
13+
compiler.watchFileSystem = new NodeWatchFileSystem(compiler.inputFileSystem);
14+
});
15+
};
16+
17+
function NodeWatchFileSystem(inputFileSystem) {
18+
this.inputFileSystem = inputFileSystem;
19+
this.watcherOptions = {
20+
aggregateTimeout: 0
21+
};
22+
this.watcher = new Watchpack(this.watcherOptions);
23+
}
24+
25+
NodeWatchFileSystem.prototype.watch = function watch(files, dirs, startTime, delay, callback, callbackUndelayed) {
26+
var oldWatcher = this.watcher;
27+
this.watcher = new Watchpack({
28+
aggregateTimeout: delay
29+
});
30+
31+
var onChange = function() {
32+
callbackUndelayed();
33+
}.bind(this);
34+
this.watcher.once("change", onChange);
35+
this.watcher.once("aggregated", function(changes) {
36+
if(this.inputFileSystem && this.inputFileSystem.purge) {
37+
this.inputFileSystem.purge(changes);
38+
}
39+
var times = this.watcher.getTimes();
40+
callback(null, changes.filter(function(file) {
41+
return files.indexOf(file) >= 0;
42+
}).sort(), changes.filter(function(file) {
43+
return dirs.indexOf(file) >= 0;
44+
}).sort(), times, times);
45+
}.bind(this));
46+
47+
this.watcher.watch(files, dirs, startTime);
48+
49+
if(oldWatcher) {
50+
oldWatcher.close();
51+
}
52+
return {
53+
close: function() {
54+
this.watcher.pause();
55+
}.bind(this)
56+
}
57+
};

‎lib/webpack.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function webpack(options, callback) {
2121
compiler.options = options;
2222
compiler.options = new WebpackOptionsApply().process(options, compiler);
2323
new NodeEnvironmentPlugin().apply(compiler);
24+
compiler.applyPlugins("environment");
25+
compiler.applyPlugins("after-environment");
2426
}
2527
if(callback) {
2628
if(options.watch) {
@@ -74,6 +76,7 @@ exportPlugins(exports, ".", [
7476
"SetVarMainTemplatePlugin",
7577
"UmdMainTemplatePlugin",
7678
"NoErrorsPlugin",
79+
"NewWatchingPlugin",
7780
]);
7881
exportPlugins(exports.optimize = {}, "./optimize", [
7982
"AggressiveMergingPlugin",

0 commit comments

Comments
 (0)