Skip to content

Commit ba21615

Browse files
authored
Merge pull request #212 from webpack/bugfix/context-time-info
report time info for directories correctly
2 parents b903dd3 + d401815 commit ba21615

3 files changed

Lines changed: 54 additions & 5 deletions

File tree

lib/DirectoryWatcher.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,7 @@ class DirectoryWatcher extends EventEmitter {
714714
safeTime,
715715
w.directoryWatcher.collectTimeInfoEntries(
716716
fileTimestamps,
717-
directoryTimestamps,
718-
safeTime
717+
directoryTimestamps
719718
)
720719
);
721720
}
@@ -726,7 +725,10 @@ class DirectoryWatcher extends EventEmitter {
726725
} else {
727726
for (const dir of this.directories.keys()) {
728727
// No additional info about this directory
729-
directoryTimestamps.set(dir, EXISTANCE_ONLY_TIME_ENTRY);
728+
// but maybe another DirectoryWatcher has info
729+
fileTimestamps.set(dir, EXISTANCE_ONLY_TIME_ENTRY);
730+
if (!directoryTimestamps.has(dir))
731+
directoryTimestamps.set(dir, EXISTANCE_ONLY_TIME_ENTRY);
730732
}
731733
fileTimestamps.set(this.path, EXISTANCE_ONLY_TIME_ENTRY);
732734
directoryTimestamps.set(this.path, EXISTANCE_ONLY_TIME_ENTRY);

lib/watchpack.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ const cachedNormalizeOptions = options => {
6666

6767
class WatchpackFileWatcher {
6868
constructor(watchpack, watcher, files) {
69-
if (!watcher) throw new Error();
7069
this.files = Array.isArray(files) ? files : [files];
7170
this.watcher = watcher;
7271
watcher.on("initial-missing", type => {
@@ -106,7 +105,6 @@ class WatchpackFileWatcher {
106105

107106
class WatchpackDirectoryWatcher {
108107
constructor(watchpack, watcher, directories) {
109-
if (!watcher) throw new Error();
110108
this.directories = Array.isArray(directories) ? directories : [directories];
111109
this.watcher = watcher;
112110
watcher.on("initial-missing", type => {

test/Watchpack.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,55 @@ describe("Watchpack", function() {
599599
});
600600
});
601601

602+
it("should watch directory as file and directory", function(done) {
603+
var w = new Watchpack({
604+
aggregateTimeout: 1000
605+
});
606+
w.on("aggregated", function(changes) {
607+
const files = new Map();
608+
const directories = new Map();
609+
w.collectTimeInfoEntries(files, directories);
610+
// fixtures should exist
611+
const fixturesAsFile = files.get(path.join(fixtures));
612+
fixturesAsFile.should.be.type("object");
613+
// dir should exist
614+
const dirAsFile = files.get(path.join(fixtures, "dir"));
615+
dirAsFile.should.be.type("object");
616+
dirAsFile.should.not.have.property("safeTime");
617+
// a should have timestamp
618+
const a = files.get(path.join(fixtures, "dir", "sub", "a"));
619+
a.should.be.type("object");
620+
a.should.have.property("safeTime");
621+
a.should.have.property("timestamp");
622+
// sub should have timestamp
623+
const sub = directories.get(path.join(fixtures, "dir", "sub"));
624+
sub.should.be.type("object");
625+
sub.should.have.property("safeTime");
626+
// sub should exist as file
627+
const subAsFile = files.get(path.join(fixtures, "dir", "sub"));
628+
subAsFile.should.be.type("object");
629+
subAsFile.should.not.have.property("safeTime");
630+
w.close();
631+
done();
632+
});
633+
testHelper.dir("dir");
634+
testHelper.dir(path.join("dir", "sub"));
635+
testHelper.dir(path.join("dir", "sub2"));
636+
testHelper.tick(function() {
637+
w.watch(
638+
[
639+
path.join(fixtures, "dir", "sub", "a"),
640+
path.join(fixtures, "dir", "sub"),
641+
path.join(fixtures)
642+
],
643+
[path.join(fixtures, "dir", "sub")]
644+
);
645+
testHelper.tick(function() {
646+
testHelper.file(path.join("dir", "sub", "a"));
647+
});
648+
});
649+
});
650+
602651
it("should watch 2 files in a not-existing directory", function(done) {
603652
var w = new Watchpack({
604653
aggregateTimeout: 1000

0 commit comments

Comments
 (0)