diff --git a/lib/Compiler.js b/lib/Compiler.js index 3c4d40379..a245c4d71 100644 --- a/lib/Compiler.js +++ b/lib/Compiler.js @@ -12,13 +12,22 @@ var Resolver = require("enhanced-resolve/lib/Resolver"); var NormalModuleFactory = require("./NormalModuleFactory"); var ContextModuleFactory = require("./ContextModuleFactory"); -function Watching(compiler, handler, watchDelay) { +function Watching(compiler, watchOptions, handler) { this.startTime = null; this.invalid = false; this.error = null; this.stats = null; this.handler = handler; - this.watchDelay = watchDelay; + if(typeof watchOptions === "number") { + this.watchOptions = { + aggregateTimeout: watchOptions + }; + } else if(watchOptions && typeof watchOptions === "object") { + this.watchOptions = Object.create(watchOptions); + } else { + this.watchOptions = {}; + } + this.watchOptions.aggregateTimeout = this.watchOptions.aggregateTimeout || 200; this.compiler = compiler; this.running = true; this.compiler.readRecords(function(err) { @@ -75,7 +84,7 @@ Watching.prototype._done = function(err, compilation) { }; Watching.prototype.watch = function(files, dirs, missing) { - this.watcher = this.compiler.watchFileSystem.watch(files, dirs, missing, this.startTime, this.watchDelay, function(err, filesModified, contextModified, missingModified, fileTimestamps, contextTimestamps) { + this.watcher = this.compiler.watchFileSystem.watch(files, dirs, missing, this.startTime, this.watchOptions, function(err, filesModified, contextModified, missingModified, fileTimestamps, contextTimestamps) { this.watcher = null; if(err) return this.handler(err); @@ -145,10 +154,10 @@ module.exports = Compiler; Compiler.prototype = Object.create(Tapable.prototype); Compiler.Watching = Watching; -Compiler.prototype.watch = function(watchDelay, handler) { +Compiler.prototype.watch = function(watchOptions, handler) { this.fileTimestamps = {}; this.contextTimestamps = {}; - var watching = new Watching(this, handler, watchDelay || 0); + var watching = new Watching(this, watchOptions, handler); return watching; }; diff --git a/lib/node/NodeWatchFileSystem.js b/lib/node/NodeWatchFileSystem.js index 052a28c6d..832fda6bc 100644 --- a/lib/node/NodeWatchFileSystem.js +++ b/lib/node/NodeWatchFileSystem.js @@ -14,7 +14,7 @@ function NodeWatchFileSystem(inputFileSystem) { module.exports = NodeWatchFileSystem; -NodeWatchFileSystem.prototype.watch = function watch(files, dirs, missing, startTime, delay, callback, callbackUndelayed) { +NodeWatchFileSystem.prototype.watch = function watch(files, dirs, missing, startTime, options, callback, callbackUndelayed) { if(!Array.isArray(files)) throw new Error("Invalid arguments: 'files'"); if(!Array.isArray(dirs)) @@ -25,14 +25,12 @@ NodeWatchFileSystem.prototype.watch = function watch(files, dirs, missing, start throw new Error("Invalid arguments: 'callback'"); if(typeof startTime !== "number" && startTime) throw new Error("Invalid arguments: 'startTime'"); - if(typeof delay !== "number") - throw new Error("Invalid arguments: 'delay'"); + if(typeof options !== "object") + throw new Error("Invalid arguments: 'options'"); if(typeof callbackUndelayed !== "function" && callbackUndelayed) throw new Error("Invalid arguments: 'callbackUndelayed'"); var oldWatcher = this.watcher; - this.watcher = new Watchpack({ - aggregateTimeout: delay - }); + this.watcher = new Watchpack(options); if(callbackUndelayed) this.watcher.once("change", callbackUndelayed); diff --git a/lib/webpack.js b/lib/webpack.js index 20f78c8f0..42a82aab4 100644 --- a/lib/webpack.js +++ b/lib/webpack.js @@ -28,12 +28,15 @@ function webpack(options, callback) { } if(callback) { if(typeof callback !== "function") throw new Error("Invalid argument: callback"); - var watchOptions = options.watch || !Array.isArray(options) ? options : options[0]; - if(watchOptions.watch) { - return compiler.watch(watchOptions.watchDelay, callback); - } else { - compiler.run(callback); + if(options.watch === true) { + console.warn("options.watch and options.watchDelay is deprecated: use webpack(options).watch instead"); + var watchOptions = options.watch || !Array.isArray(options) ? options : options[0]; + watchOptions = { + aggregateTimeout: watchOptions.watchDelay + }; + return compiler.watch(watchOptions, callback); } + compiler.run(callback); } return compiler; } diff --git a/test/NodeWatchFileSystem.test.js b/test/NodeWatchFileSystem.test.js index 1120f4906..a83d7c502 100644 --- a/test/NodeWatchFileSystem.test.js +++ b/test/NodeWatchFileSystem.test.js @@ -1,3 +1,5 @@ +/* globals describe it */ + if(process.env.NO_WATCH_TESTS) { describe("NodeWatchFileSystem", function() { it("tests excluded"); @@ -5,7 +7,7 @@ if(process.env.NO_WATCH_TESTS) { return; } -var should = require("should"); +require("should"); var path = require("path"); var fs = require("fs"); @@ -15,18 +17,12 @@ var fixtures = path.join(__dirname, "fixtures"); var fileDirect = path.join(fixtures, "watched-file.txt"); var fileSubdir = path.join(fixtures, "subdir", "watched-file.txt"); -function simpleObject(key, value) { - var obj = {}; - obj[key] = value; - return obj; -} - describe("NodeWatchFileSystem", function() { this.timeout(10000); it("should register a file change (change delayed)", function(done) { var startTime = new Date().getTime(); var wfs = new NodeWatchFileSystem(); - var watcher = wfs.watch([fileDirect], [], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { + var watcher = wfs.watch([fileDirect], [], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps /*, dirTimestamps */) { if(err) throw err; filesModified.should.be.eql([fileDirect]); dirsModified.should.be.eql([]); @@ -43,7 +39,7 @@ describe("NodeWatchFileSystem", function() { var startTime = new Date().getTime(); setTimeout(function() { var wfs = new NodeWatchFileSystem(); - var watcher = wfs.watch([fileDirect], [], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { + var watcher = wfs.watch([fileDirect], [], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps /*, dirTimestamps */) { if(err) throw err; filesModified.should.be.eql([fileDirect]); dirsModified.should.be.eql([]); @@ -58,7 +54,7 @@ describe("NodeWatchFileSystem", function() { it("should register a context change (change delayed)", function(done) { var startTime = new Date().getTime(); var wfs = new NodeWatchFileSystem(); - var watcher = wfs.watch([], [fixtures], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { + var watcher = wfs.watch([], [fixtures], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { if(err) throw err; filesModified.should.be.eql([]); dirsModified.should.be.eql([fixtures]); @@ -75,7 +71,7 @@ describe("NodeWatchFileSystem", function() { var startTime = new Date().getTime(); setTimeout(function() { var wfs = new NodeWatchFileSystem(); - var watcher = wfs.watch([], [fixtures], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { + var watcher = wfs.watch([], [fixtures], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { if(err) throw err; filesModified.should.be.eql([]); dirsModified.should.be.eql([fixtures]); @@ -90,7 +86,7 @@ describe("NodeWatchFileSystem", function() { it("should register a context change (change delayed, subdirectory)", function(done) { var startTime = new Date().getTime(); var wfs = new NodeWatchFileSystem(); - var watcher = wfs.watch([], [fixtures], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { + var watcher = wfs.watch([], [fixtures], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { if(err) throw err; filesModified.should.be.eql([]); dirsModified.should.be.eql([fixtures]); @@ -107,7 +103,7 @@ describe("NodeWatchFileSystem", function() { var startTime = new Date().getTime(); setTimeout(function() { var wfs = new NodeWatchFileSystem(); - var watcher = wfs.watch([], [fixtures], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { + var watcher = wfs.watch([], [fixtures], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { if(err) throw err; filesModified.should.be.eql([]); dirsModified.should.be.eql([fixtures]); @@ -123,7 +119,7 @@ describe("NodeWatchFileSystem", function() { var startTime = new Date().getTime(); setTimeout(function() { var wfs = new NodeWatchFileSystem(); - var watcher = wfs.watch([fileDirect, fileSubdir], [fixtures], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { + var watcher = wfs.watch([fileDirect, fileSubdir], [fixtures], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { if(err) throw err; filesModified.should.be.eql([fileSubdir, fileDirect]); dirsModified.should.be.eql([fixtures]); @@ -141,7 +137,7 @@ describe("NodeWatchFileSystem", function() { it("should sum up multiple changes", function(done) { var startTime = new Date().getTime(); var wfs = new NodeWatchFileSystem(); - var watcher = wfs.watch([fileDirect, fileSubdir], [fixtures], [], startTime, 1000, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { + var watcher = wfs.watch([fileDirect, fileSubdir], [fixtures], [], startTime, { aggregateTimeout: 1000 }, function(err, filesModified, dirsModified, missingCreated, fileTimestamps, dirTimestamps) { if(err) throw err; filesModified.should.be.eql([fileSubdir, fileDirect]); dirsModified.should.be.eql([fixtures]);