Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore empty strings in an array #242

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions lib/watchpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,28 @@ function addWatchersToSet(watchers, set) {
}

const stringToRegexp = ignored => {
if (ignored.length === 0) {
return;
}
const source = globToRegExp(ignored, { globstar: true, extended: true })
.source;
const matchingStart = source.slice(0, source.length - 1) + "(?:$|\\/)";
return matchingStart;
return source.slice(0, source.length - 1) + "(?:$|\\/)";
};

const ignoredToFunction = ignored => {
if (Array.isArray(ignored)) {
if (ignored.length === 0) {
const stringRegexps = ignored.map(i => stringToRegexp(i)).filter(Boolean);
if (stringRegexps.length === 0) {
return () => false;
}
const regexp = new RegExp(ignored.map(i => stringToRegexp(i)).join("|"));
const regexp = new RegExp(stringRegexps.join("|"));
return x => regexp.test(x.replace(/\\/g, "/"));
} else if (typeof ignored === "string") {
if (ignored.length === 0) {
const stringRegexp = stringToRegexp(ignored);
if (!stringRegexp) {
return () => false;
}
const regexp = new RegExp(stringToRegexp(ignored));
const regexp = new RegExp(stringRegexp);
return x => regexp.test(x.replace(/\\/g, "/"));
} else if (ignored instanceof RegExp) {
return x => ignored.test(x.replace(/\\/g, "/"));
Expand Down
22 changes: 22 additions & 0 deletions test/Watchpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,28 @@ describe("Watchpack", function() {
});
});

it("should watch a file when ignore is an array with empty string", function(done) {
var w = new Watchpack({
aggregateTimeout: 1000,
ignored: ["", ""]
});
var changeEvents = 0;
w.on("change", function(file) {
file.should.be.eql(path.join(fixtures, "a"));
changeEvents++;
});
w.on("aggregated", function(changes) {
Array.from(changes).should.be.eql([path.join(fixtures, "a")]);
changeEvents.should.be.greaterThan(0);
w.close();
done();
});
w.watch([path.join(fixtures, "a")], []);
testHelper.tick(function() {
testHelper.file("a");
});
});

it("should watch a file when ignore is empty string", function(done) {
var w = new Watchpack({
aggregateTimeout: 1000,
Expand Down
Loading