Skip to content

Commit 2f8e1fb

Browse files
committed
avoid RegexpLike in favor of a function
move path normalization to option normalization
1 parent 08f6a1a commit 2f8e1fb

3 files changed

Lines changed: 15 additions & 20 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var wp = new Watchpack({
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
5252
// ignored: (entry) => boolean - an arbirary function which must return truthy to ignore an entry
53+
// For all cases expect the arbirary function the path will have path separator normalized to '/'.
5354
// All subdirectories are ignored too
5455
});
5556

lib/DirectoryWatcher.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class DirectoryWatcher extends EventEmitter {
7171
this.directories = new Map();
7272
this.lastWatchEvent = 0;
7373
this.initialScan = true;
74-
this.ignored = options.ignored;
74+
this.ignored = options.ignored || (() => false);
7575
this.nestedWatching = false;
7676
this.polledWatching =
7777
typeof options.poll === "number"
@@ -96,12 +96,6 @@ class DirectoryWatcher extends EventEmitter {
9696
this.doScan(true);
9797
}
9898

99-
checkIgnore(path) {
100-
if (!this.ignored) return false;
101-
path = path.replace(/\\/g, "/");
102-
return this.ignored.test(path);
103-
}
104-
10599
createWatcher() {
106100
try {
107101
if (this.polledWatching) {
@@ -178,7 +172,7 @@ class DirectoryWatcher extends EventEmitter {
178172
setFileTime(filePath, mtime, initial, ignoreWhenEqual, type) {
179173
const now = Date.now();
180174

181-
if (this.checkIgnore(filePath)) return;
175+
if (this.ignored(filePath)) return;
182176

183177
const old = this.files.get(filePath);
184178

@@ -237,7 +231,7 @@ class DirectoryWatcher extends EventEmitter {
237231
}
238232

239233
setDirectory(directoryPath, birthtime, initial, type) {
240-
if (this.checkIgnore(directoryPath)) return;
234+
if (this.ignored(directoryPath)) return;
241235
if (directoryPath === this.path) {
242236
if (!initial) {
243237
this.forEachWatcher(this.path, w =>
@@ -398,7 +392,7 @@ class DirectoryWatcher extends EventEmitter {
398392
}
399393

400394
const filePath = path.join(this.path, filename);
401-
if (this.checkIgnore(filePath)) return;
395+
if (this.ignored(filePath)) return;
402396

403397
if (this._activeEvents.get(filename) === undefined) {
404398
this._activeEvents.set(filename, false);

lib/watchpack.js

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

34-
const ignoredToRegexpLike = ignored => {
34+
const ignoredToFunction = ignored => {
3535
if (Array.isArray(ignored)) {
36-
return new RegExp(ignored.map(i => stringToRegexp(i)).join("|"));
36+
const regexp = new RegExp(ignored.map(i => stringToRegexp(i)).join("|"));
37+
return x => regexp.test(x.replace(/\\/g, "/"));
3738
} else if (typeof ignored === "string") {
38-
return new RegExp(stringToRegexp(ignored));
39+
const regexp = new RegExp(stringToRegexp(ignored));
40+
return x => regexp.test(x.replace(/\\/g, "/"));
3941
} else if (ignored instanceof RegExp) {
40-
return ignored;
42+
return x => ignored.test(x.replace(/\\/g, "/"));
4143
} else if (ignored instanceof Function) {
42-
return { test: ignored };
44+
return ignored;
4345
} else if (ignored) {
4446
throw new Error(`Invalid option for 'ignored': ${ignored}`);
4547
} else {
46-
return undefined;
48+
return () => false;
4749
}
4850
};
4951

5052
const normalizeOptions = options => {
5153
return {
5254
followSymlinks: !!options.followSymlinks,
53-
ignored: ignoredToRegexpLike(options.ignored),
55+
ignored: ignoredToFunction(options.ignored),
5456
poll: options.poll
5557
};
5658
};
@@ -104,9 +106,7 @@ class Watchpack extends EventEmitter {
104106
const oldFileWatchers = this.fileWatchers;
105107
const oldDirectoryWatchers = this.directoryWatchers;
106108
const ignored = this.watcherOptions.ignored;
107-
const filter = ignored
108-
? path => !ignored.test(path.replace(/\\/g, "/"))
109-
: () => true;
109+
const filter = path => !ignored(path);
110110
const addToMap = (map, key, item) => {
111111
const list = map.get(key);
112112
if (list === undefined) {

0 commit comments

Comments
 (0)