Skip to content

Commit 08f6a1a

Browse files
author
Mark Molinaro
authored
Allow function in watchOptions.ignored
1 parent c651129 commit 08f6a1a

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var wp = new Watchpack({
4949
// ignored: "string" - a glob pattern for files or folders that should not be watched
5050
// ignored: ["string", "string"] - multiple glob patterns that should be ignored
5151
// ignored: /regexp/ - a regular expression for files or folders that should not be watched
52+
// ignored: (entry) => boolean - an arbirary function which must return truthy to ignore an entry
5253
// All subdirectories are ignored too
5354
});
5455

lib/watchpack.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ const stringToRegexp = ignored => {
3131
return matchingStart;
3232
};
3333

34-
const ignoredToRegexp = ignored => {
34+
const ignoredToRegexpLike = ignored => {
3535
if (Array.isArray(ignored)) {
3636
return new RegExp(ignored.map(i => stringToRegexp(i)).join("|"));
3737
} else if (typeof ignored === "string") {
3838
return new RegExp(stringToRegexp(ignored));
3939
} else if (ignored instanceof RegExp) {
4040
return ignored;
41+
} else if (ignored instanceof Function) {
42+
return { test: ignored };
4143
} else if (ignored) {
4244
throw new Error(`Invalid option for 'ignored': ${ignored}`);
4345
} else {
@@ -48,7 +50,7 @@ const ignoredToRegexp = ignored => {
4850
const normalizeOptions = options => {
4951
return {
5052
followSymlinks: !!options.followSymlinks,
51-
ignored: ignoredToRegexp(options.ignored),
53+
ignored: ignoredToRegexpLike(options.ignored),
5254
poll: options.poll
5355
};
5456
};

test/Watchpack.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,32 @@ describe("Watchpack", function() {
118118
});
119119
});
120120

121+
it("should not watch a single ignored file (function)", function(done) {
122+
var w = new Watchpack({
123+
aggregateTimeout: 300,
124+
ignored: (entry) => entry.includes("a")
125+
});
126+
var changeEvents = 0;
127+
var aggregatedEvents = 0;
128+
w.on("change", () => {
129+
changeEvents++;
130+
});
131+
w.on("aggregated", () => {
132+
aggregatedEvents++;
133+
});
134+
w.watch([path.join(fixtures, "a")], []);
135+
testHelper.tick(() => {
136+
testHelper.file("a");
137+
testHelper.tick(1000, () => {
138+
changeEvents.should.be.eql(0);
139+
aggregatedEvents.should.be.eql(0);
140+
testHelper.getNumberOfWatchers().should.be.eql(0);
141+
w.close();
142+
done();
143+
});
144+
});
145+
});
146+
121147
it("should watch multiple files", function(done) {
122148
var w = new Watchpack({
123149
aggregateTimeout: 1000

0 commit comments

Comments
 (0)