Skip to content

Commit 7185e2c

Browse files
authored
Merge pull request webpack#6229 from webpack/feature/times-on-invalidate
get timestamps from watcher on invalidate
2 parents 048121a + 2b33f64 commit 7185e2c

4 files changed

Lines changed: 41 additions & 73 deletions

File tree

lib/WatchIgnorePlugin.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class IgnoringWatchFileSystem {
3636
const ignoredFiles = files.filter(ignored);
3737
const ignoredDirs = dirs.filter(ignored);
3838

39-
this.wfs.watch(files.filter(notIgnored), dirs.filter(notIgnored), missing, startTime, options, (err, filesModified, dirsModified, missingModified, fileTimestamps, dirTimestamps) => {
39+
const watcher = this.wfs.watch(files.filter(notIgnored), dirs.filter(notIgnored), missing, startTime, options, (err, filesModified, dirsModified, missingModified, fileTimestamps, dirTimestamps) => {
4040
if(err) return callback(err);
4141

4242
ignoredFiles.forEach(path => {
@@ -49,5 +49,24 @@ class IgnoringWatchFileSystem {
4949

5050
callback(err, filesModified, dirsModified, missingModified, fileTimestamps, dirTimestamps);
5151
}, callbackUndelayed);
52+
53+
return {
54+
close: () => watcher.close(),
55+
pause: () => watcher.pause(),
56+
getContextTimestamps: () => {
57+
const dirTimestamps = watcher.getContextTimestamps();
58+
ignoredDirs.forEach(path => {
59+
dirTimestamps[path] = 1;
60+
});
61+
return dirTimestamps;
62+
},
63+
getFileTimestamps: () => {
64+
const fileTimestamps = watcher.getFileTimestamps();
65+
ignoredFiles.forEach(path => {
66+
fileTimestamps[path] = 1;
67+
});
68+
return fileTimestamps;
69+
}
70+
};
5271
}
5372
}

lib/Watching.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class Watching {
111111

112112
this.compiler.fileTimestamps = fileTimestamps;
113113
this.compiler.contextTimestamps = contextTimestamps;
114-
this.invalidate();
114+
this._invalidate();
115115
}, (fileName, changeTime) => {
116116
this.compiler.hooks.invalid.call(fileName, changeTime);
117117
});
@@ -121,6 +121,14 @@ class Watching {
121121
if(callback) {
122122
this.callbacks.push(callback);
123123
}
124+
if(this.watcher) {
125+
this.compiler.fileTimestamps = this.watcher.getFileTimestamps();
126+
this.compiler.contextTimestamps = this.watcher.getContextTimestamps();
127+
}
128+
return this._invalidate();
129+
}
130+
131+
_invalidate() {
124132
if(this.watcher) {
125133
this.pausedWatcher = this.watcher;
126134
this.watcher.pause();

lib/node/NodeWatchFileSystem.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ class NodeWatchFileSystem {
6464
if(this.watcher) {
6565
this.watcher.pause();
6666
}
67+
},
68+
getFileTimestamps: () => {
69+
if(this.watcher)
70+
return this.watcher.getTimes();
71+
else
72+
return {};
73+
},
74+
getContextTimestamps: () => {
75+
if(this.watcher)
76+
return this.watcher.getTimes();
77+
else
78+
return {};
6779
}
6880
};
6981
}

test/Compiler.test.js

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -279,75 +279,4 @@ describe("Compiler", () => {
279279
done();
280280
});
281281
});
282-
describe("Watching", () => {
283-
let compiler;
284-
beforeEach(() => {
285-
compiler = webpack({
286-
entry: "./c",
287-
context: path.join(__dirname, "fixtures"),
288-
output: {
289-
path: "/",
290-
pathinfo: true,
291-
}
292-
});
293-
});
294-
describe("constructor", () => {
295-
it("constructs Watching.watchOptions correctly when passed a number, string, or object for watchOptions", (done) => {
296-
const Watching1 = compiler.watch(1000, err => err);
297-
const Watching2 = compiler.watch({
298-
aggregateTimeout: 1000
299-
}, err => err);
300-
const Watching3 = compiler.watch("I am a string", err => err);
301-
Watching1.watchOptions.aggregateTimeout.should.equal(Watching2.watchOptions.aggregateTimeout);
302-
Watching3.watchOptions.aggregateTimeout.should.equal(200);
303-
done();
304-
});
305-
it("invokes compiler.readRecords", (done) => {
306-
compiler.readRecords = sinon.spy();
307-
compiler.watch(1000, err => err);
308-
compiler.readRecords.callCount.should.be.exactly(1);
309-
done();
310-
});
311-
});
312-
describe("_done", () => {
313-
it("invokes this.handler and turns this.running boolean to false when passed an error", (done) => {
314-
const mockHandler = sinon.spy();
315-
const Watching1 = compiler.watch(1000, mockHandler);
316-
Watching1.running.should.be.exactly(true);
317-
Watching1._done(Watching1.handler, false);
318-
mockHandler.callCount.should.be.exactly(1);
319-
Watching1.running.should.be.exactly(false);
320-
done();
321-
});
322-
});
323-
describe("invalidate", () => {
324-
it("pauses this.watcher and sets this.watcher to null if this.watcher is true", (done) => {
325-
const mockPause = sinon.spy();
326-
const Watching1 = compiler.watch(1000, err => err);
327-
Watching1.watcher = {
328-
pause: mockPause
329-
};
330-
Watching1.invalidate();
331-
mockPause.callCount.should.be.exactly(1);
332-
should(Watching1.watcher).be.exactly(null);
333-
done();
334-
});
335-
it("sets this.invalid to true if this.running is true, else this.invalid = false", (done) => {
336-
const Watching1 = compiler.watch(1000, err => err);
337-
Watching1.invalid = false;
338-
const response = Watching1.invalidate();
339-
Watching1.invalid.should.be.exactly(true);
340-
response.should.be.exactly(false);
341-
done();
342-
});
343-
it("invokes this._go() if !this.running", (done) => {
344-
const Watching1 = compiler.watch(1000, err => err);
345-
Watching1.running = false;
346-
Watching1._go = sinon.spy();
347-
Watching1.invalidate();
348-
Watching1._go.callCount.should.be.exactly(1);
349-
done();
350-
});
351-
});
352-
});
353282
});

0 commit comments

Comments
 (0)