Skip to content

Commit 18f3595

Browse files
committed
allow to pass watch options
deprecate old API
1 parent 9d51138 commit 18f3595

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

lib/Compiler.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@ var Resolver = require("enhanced-resolve/lib/Resolver");
1212
var NormalModuleFactory = require("./NormalModuleFactory");
1313
var ContextModuleFactory = require("./ContextModuleFactory");
1414

15-
function Watching(compiler, handler, watchDelay) {
15+
function Watching(compiler, watchOptions, handler) {
1616
this.startTime = null;
1717
this.invalid = false;
1818
this.error = null;
1919
this.stats = null;
2020
this.handler = handler;
21-
this.watchDelay = watchDelay;
21+
if(typeof watchOptions === "number") {
22+
this.watchOptions = {
23+
aggregateTimeout: watchOptions
24+
};
25+
} else if(watchOptions && typeof watchOptions === "object") {
26+
this.watchOptions = Object.create(watchOptions);
27+
} else {
28+
this.watchOptions = {};
29+
}
30+
this.watchOptions.aggregateTimeout = this.watchOptions.aggregateTimeout || 200;
2231
this.compiler = compiler;
2332
this.running = true;
2433
this.compiler.readRecords(function(err) {
@@ -75,7 +84,7 @@ Watching.prototype._done = function(err, compilation) {
7584
};
7685

7786
Watching.prototype.watch = function(files, dirs, missing) {
78-
this.watcher = this.compiler.watchFileSystem.watch(files, dirs, missing, this.startTime, this.watchDelay, function(err, filesModified, contextModified, missingModified, fileTimestamps, contextTimestamps) {
87+
this.watcher = this.compiler.watchFileSystem.watch(files, dirs, missing, this.startTime, this.watchOptions, function(err, filesModified, contextModified, missingModified, fileTimestamps, contextTimestamps) {
7988
this.watcher = null;
8089
if(err) return this.handler(err);
8190

@@ -145,10 +154,10 @@ module.exports = Compiler;
145154
Compiler.prototype = Object.create(Tapable.prototype);
146155

147156
Compiler.Watching = Watching;
148-
Compiler.prototype.watch = function(watchDelay, handler) {
157+
Compiler.prototype.watch = function(watchOptions, handler) {
149158
this.fileTimestamps = {};
150159
this.contextTimestamps = {};
151-
var watching = new Watching(this, handler, watchDelay || 0);
160+
var watching = new Watching(this, watchOptions, handler);
152161
return watching;
153162
};
154163

lib/node/NodeWatchFileSystem.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function NodeWatchFileSystem(inputFileSystem) {
1414

1515
module.exports = NodeWatchFileSystem;
1616

17-
NodeWatchFileSystem.prototype.watch = function watch(files, dirs, missing, startTime, delay, callback, callbackUndelayed) {
17+
NodeWatchFileSystem.prototype.watch = function watch(files, dirs, missing, startTime, options, callback, callbackUndelayed) {
1818
if(!Array.isArray(files))
1919
throw new Error("Invalid arguments: 'files'");
2020
if(!Array.isArray(dirs))
@@ -25,14 +25,12 @@ NodeWatchFileSystem.prototype.watch = function watch(files, dirs, missing, start
2525
throw new Error("Invalid arguments: 'callback'");
2626
if(typeof startTime !== "number" && startTime)
2727
throw new Error("Invalid arguments: 'startTime'");
28-
if(typeof delay !== "number")
29-
throw new Error("Invalid arguments: 'delay'");
28+
if(typeof options !== "object")
29+
throw new Error("Invalid arguments: 'options'");
3030
if(typeof callbackUndelayed !== "function" && callbackUndelayed)
3131
throw new Error("Invalid arguments: 'callbackUndelayed'");
3232
var oldWatcher = this.watcher;
33-
this.watcher = new Watchpack({
34-
aggregateTimeout: delay
35-
});
33+
this.watcher = new Watchpack(options);
3634

3735
if(callbackUndelayed)
3836
this.watcher.once("change", callbackUndelayed);

lib/webpack.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ function webpack(options, callback) {
2828
}
2929
if(callback) {
3030
if(typeof callback !== "function") throw new Error("Invalid argument: callback");
31-
var watchOptions = options.watch || !Array.isArray(options) ? options : options[0];
32-
if(watchOptions.watch) {
33-
return compiler.watch(watchOptions.watchDelay, callback);
34-
} else {
35-
compiler.run(callback);
31+
if(options.watch === true) {
32+
console.warn("options.watch and options.watchDelay is deprecated: use webpack(options).watch instead");
33+
var watchOptions = options.watch || !Array.isArray(options) ? options : options[0];
34+
watchOptions = {
35+
aggregateTimeout: watchOptions.watchDelay
36+
};
37+
return compiler.watch(watchOptions, callback);
3638
}
39+
compiler.run(callback);
3740
}
3841
return compiler;
3942
}

test/NodeWatchFileSystem.test.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
/* globals describe it */
2+
13
if(process.env.NO_WATCH_TESTS) {
24
describe("NodeWatchFileSystem", function() {
35
it("tests excluded");
46
});
57
return;
68
}
79

8-
var should = require("should");
10+
require("should");
911
var path = require("path");
1012
var fs = require("fs");
1113

@@ -15,18 +17,12 @@ var fixtures = path.join(__dirname, "fixtures");
1517
var fileDirect = path.join(fixtures, "watched-file.txt");
1618
var fileSubdir = path.join(fixtures, "subdir", "watched-file.txt");
1719

18-
function simpleObject(key, value) {
19-
var obj = {};
20-
obj[key] = value;
21-
return obj;
22-
}
23-
2420
describe("NodeWatchFileSystem", function() {
2521
this.timeout(10000);
2622
it("should register a file change (change delayed)", function(done) {
2723
var startTime = new Date().getTime();
2824
var wfs = new NodeWatchFileSystem();
29-
var watcher = wfs.watch([fileDirect], [], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) {
25+
var watcher = wfs.watch([fileDirect], [], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps /*, dirTimestamps */) {
3026
if(err) throw err;
3127
filesModified.should.be.eql([fileDirect]);
3228
dirsModified.should.be.eql([]);
@@ -43,7 +39,7 @@ describe("NodeWatchFileSystem", function() {
4339
var startTime = new Date().getTime();
4440
setTimeout(function() {
4541
var wfs = new NodeWatchFileSystem();
46-
var watcher = wfs.watch([fileDirect], [], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) {
42+
var watcher = wfs.watch([fileDirect], [], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps /*, dirTimestamps */) {
4743
if(err) throw err;
4844
filesModified.should.be.eql([fileDirect]);
4945
dirsModified.should.be.eql([]);
@@ -58,7 +54,7 @@ describe("NodeWatchFileSystem", function() {
5854
it("should register a context change (change delayed)", function(done) {
5955
var startTime = new Date().getTime();
6056
var wfs = new NodeWatchFileSystem();
61-
var watcher = wfs.watch([], [fixtures], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) {
57+
var watcher = wfs.watch([], [fixtures], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) {
6258
if(err) throw err;
6359
filesModified.should.be.eql([]);
6460
dirsModified.should.be.eql([fixtures]);
@@ -75,7 +71,7 @@ describe("NodeWatchFileSystem", function() {
7571
var startTime = new Date().getTime();
7672
setTimeout(function() {
7773
var wfs = new NodeWatchFileSystem();
78-
var watcher = wfs.watch([], [fixtures], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) {
74+
var watcher = wfs.watch([], [fixtures], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) {
7975
if(err) throw err;
8076
filesModified.should.be.eql([]);
8177
dirsModified.should.be.eql([fixtures]);
@@ -90,7 +86,7 @@ describe("NodeWatchFileSystem", function() {
9086
it("should register a context change (change delayed, subdirectory)", function(done) {
9187
var startTime = new Date().getTime();
9288
var wfs = new NodeWatchFileSystem();
93-
var watcher = wfs.watch([], [fixtures], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) {
89+
var watcher = wfs.watch([], [fixtures], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) {
9490
if(err) throw err;
9591
filesModified.should.be.eql([]);
9692
dirsModified.should.be.eql([fixtures]);
@@ -107,7 +103,7 @@ describe("NodeWatchFileSystem", function() {
107103
var startTime = new Date().getTime();
108104
setTimeout(function() {
109105
var wfs = new NodeWatchFileSystem();
110-
var watcher = wfs.watch([], [fixtures], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) {
106+
var watcher = wfs.watch([], [fixtures], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) {
111107
if(err) throw err;
112108
filesModified.should.be.eql([]);
113109
dirsModified.should.be.eql([fixtures]);
@@ -123,7 +119,7 @@ describe("NodeWatchFileSystem", function() {
123119
var startTime = new Date().getTime();
124120
setTimeout(function() {
125121
var wfs = new NodeWatchFileSystem();
126-
var watcher = wfs.watch([fileDirect, fileSubdir], [fixtures], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) {
122+
var watcher = wfs.watch([fileDirect, fileSubdir], [fixtures], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) {
127123
if(err) throw err;
128124
filesModified.should.be.eql([fileSubdir, fileDirect]);
129125
dirsModified.should.be.eql([fixtures]);
@@ -141,7 +137,7 @@ describe("NodeWatchFileSystem", function() {
141137
it("should sum up multiple changes", function(done) {
142138
var startTime = new Date().getTime();
143139
var wfs = new NodeWatchFileSystem();
144-
var watcher = wfs.watch([fileDirect, fileSubdir], [fixtures], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) {
140+
var watcher = wfs.watch([fileDirect, fileSubdir], [fixtures], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) {
145141
if(err) throw err;
146142
filesModified.should.be.eql([fileSubdir, fileDirect]);
147143
dirsModified.should.be.eql([fixtures]);

0 commit comments

Comments
 (0)