Skip to content

Commit

Permalink
Decouple processing ignores from watchIgnores. Fixes #893
Browse files Browse the repository at this point in the history
Whether we want to ignore a file during watch or ignore a file for processing are two separate concerns.
  • Loading branch information
zachleat committed Nov 18, 2022
1 parent e883f8f commit 0c27a80
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 42 deletions.
26 changes: 19 additions & 7 deletions src/EleventyFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fastglob = require("fast-glob");
const { TemplatePath } = require("@11ty/eleventy-utils");

const EleventyExtensionMap = require("./EleventyExtensionMap");
const EleventyWatchTargets = require("./EleventyWatchTargets");
const TemplateData = require("./TemplateData");
const TemplateGlob = require("./TemplateGlob");
const TemplatePassthroughManager = require("./TemplatePassthroughManager");
Expand Down Expand Up @@ -216,6 +217,12 @@ class EleventyFiles {
for (let ignore of this.extraIgnores) {
uniqueIgnores.add(ignore);
}
// Placing the config ignores last here is import to the tests
for (let ignore of this.config.ignores) {
uniqueIgnores.add(
TemplateGlob.normalizePath(this.localPathRoot || ".", ignore)
);
}
return Array.from(uniqueIgnores);
}

Expand Down Expand Up @@ -299,10 +306,6 @@ class EleventyFiles {
let rootDirectory = this.localPathRoot || ".";
let files = new Set();

for (let ignore of this.config.ignores) {
files.add(TemplateGlob.normalizePath(rootDirectory, ignore));
}

if (this.config.useGitIgnore) {
for (let ignore of EleventyFiles.getFileIgnores([
TemplatePath.join(rootDirectory, ".gitignore"),
Expand Down Expand Up @@ -474,11 +477,20 @@ class EleventyFiles {
/* Ignored by `eleventy --watch` */
getGlobWatcherIgnores() {
// convert to format without ! since they are passed in as a separate argument to glob watcher
let entries = this.fileIgnores.map((ignore) =>
TemplatePath.stripLeadingDotSlash(ignore)
let entries = new Set(
this.fileIgnores.map((ignore) =>
TemplatePath.stripLeadingDotSlash(ignore)
)
);

for (let ignore of this.config.watchIgnores) {
entries.add(
TemplateGlob.normalizePath(this.localPathRoot || ".", ignore)
);
}

// de-duplicated
return Array.from(new Set(entries));
return Array.from(entries);
}

_getIncludesAndDataDirs() {
Expand Down
34 changes: 18 additions & 16 deletions src/EleventyWatchTargets.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ class EleventyWatchTargets {
return this.dependencies.has(path);
}

_normalizeTargets(targets) {
if (!targets) {
return [];
} else if (Array.isArray(targets)) {
return targets;
}

return [targets];
}

reset() {
this.newTargets = new Set();
}
Expand Down Expand Up @@ -82,17 +72,29 @@ class EleventyWatchTargets {
}
}

static normalize(targets) {
if (!targets) {
return [];
} else if (Array.isArray(targets)) {
return targets;
}

return [targets];
}

// add only a target
add(targets) {
targets = this._normalizeTargets(targets);
this.addRaw(targets);
this.addRaw(EleventyWatchTargets.normalize(targets));
}

addAndMakeGlob(targets) {
targets = this._normalizeTargets(targets).map((entry) =>
static normalizeToGlobs(targets) {
return EleventyWatchTargets.normalize(targets).map((entry) =>
TemplatePath.convertToRecursiveGlobSync(entry)
);
this.addRaw(targets);
}

addAndMakeGlob(targets) {
this.addRaw(EleventyWatchTargets.normalizeToGlobs(targets));
}

// add only a target’s dependencies
Expand All @@ -101,7 +103,7 @@ class EleventyWatchTargets {
return;
}

targets = this._normalizeTargets(targets);
targets = EleventyWatchTargets.normalize(targets);
let deps = JavaScriptDependencies.getDependencies(targets);
if (filterCallback) {
deps = deps.filter(filterCallback);
Expand Down
10 changes: 7 additions & 3 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ class UserConfig {
this.dynamicPermalinks = true;

this.useGitIgnore = true;
this.ignores = new Set();
this.ignores.add("**/node_modules/**");
this.ignores.add(".git/**");

let defaultIgnores = new Set();
defaultIgnores.add("**/node_modules/**");
defaultIgnores.add(".git/**");
this.ignores = new Set(defaultIgnores);
this.watchIgnores = new Set(defaultIgnores);

this.dataDeepMerge = true;
this.extensionMap = new Set();
Expand Down Expand Up @@ -897,6 +900,7 @@ class UserConfig {
dynamicPermalinks: this.dynamicPermalinks,
useGitIgnore: this.useGitIgnore,
ignores: this.ignores,
watchIgnores: this.watchIgnores,
dataDeepMerge: this.dataDeepMerge,
watchJavaScriptDependencies: this.watchJavaScriptDependencies,
additionalWatchTargets: this.additionalWatchTargets,
Expand Down
59 changes: 43 additions & 16 deletions test/EleventyFilesGitIgnoreEleventyIgnoreTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ test("Get ignores (no .eleventyignore no .gitignore)", (t) => {
evf._setLocalPathRoot("./test/stubs/ignorelocalroot");

t.deepEqual(evf.getIgnores(), [
"./test/stubs/ignorelocalroot/**/node_modules/**",
"./test/stubs/ignorelocalroot/.git/**",
"./test/stubs/ignorelocalroot/test.md",
"./test/stubs/ignore1/_site/**",
]);

t.deepEqual(evf.getIgnoreGlobs().slice(-2), [
"./test/stubs/ignorelocalroot/**/node_modules/**",
"./test/stubs/ignorelocalroot/.git/**",
]);
});

test("Get ignores (no .eleventyignore)", (t) => {
Expand All @@ -38,12 +41,15 @@ test("Get ignores (no .eleventyignore)", (t) => {
evf._setLocalPathRoot("./test/stubs/ignorelocalrootgitignore");

t.deepEqual(evf.getIgnores(), [
"./test/stubs/ignorelocalrootgitignore/**/node_modules/**",
"./test/stubs/ignorelocalrootgitignore/.git/**",
"./test/stubs/ignorelocalrootgitignore/thisshouldnotexist12345",
"./test/stubs/ignorelocalrootgitignore/test.md",
"./test/stubs/ignore2/_site/**",
]);

t.deepEqual(evf.getIgnoreGlobs().slice(-2), [
"./test/stubs/ignorelocalrootgitignore/**/node_modules/**",
"./test/stubs/ignorelocalrootgitignore/.git/**",
]);
});

test("Get ignores (no .eleventyignore, using setUseGitIgnore(false))", (t) => {
Expand All @@ -65,10 +71,13 @@ test("Get ignores (no .eleventyignore, using setUseGitIgnore(false))", (t) => {
evf._setLocalPathRoot("./test/stubs/ignorelocalroot");

t.deepEqual(evf.getIgnores(), [
"./test/stubs/ignorelocalroot/**/node_modules/**",
"./test/stubs/ignorelocalroot/test.md",
"./test/stubs/ignore2/_site/**",
]);

t.deepEqual(evf.getIgnoreGlobs().slice(-1), [
"./test/stubs/ignorelocalroot/**/node_modules/**",
]);
});

test("Get ignores (no .gitignore)", (t) => {
Expand All @@ -83,13 +92,16 @@ test("Get ignores (no .gitignore)", (t) => {
evf._setLocalPathRoot("./test/stubs/ignorelocalroot");

t.deepEqual(evf.getIgnores(), [
"./test/stubs/ignorelocalroot/**/node_modules/**",
"./test/stubs/ignorelocalroot/.git/**",
"./test/stubs/ignorelocalroot/test.md",
"./test/stubs/ignore3/ignoredFolder/**",
"./test/stubs/ignore3/ignoredFolder/ignored.md",
"./test/stubs/ignore3/_site/**",
]);

t.deepEqual(evf.getIgnoreGlobs().slice(-2), [
"./test/stubs/ignorelocalroot/**/node_modules/**",
"./test/stubs/ignorelocalroot/.git/**",
]);
});

test("Get ignores (project .eleventyignore and root .gitignore)", (t) => {
Expand All @@ -104,14 +116,17 @@ test("Get ignores (project .eleventyignore and root .gitignore)", (t) => {
evf._setLocalPathRoot("./test/stubs/ignorelocalrootgitignore");

t.deepEqual(evf.getIgnores(), [
"./test/stubs/ignorelocalrootgitignore/**/node_modules/**",
"./test/stubs/ignorelocalrootgitignore/.git/**",
"./test/stubs/ignorelocalrootgitignore/thisshouldnotexist12345",
"./test/stubs/ignorelocalrootgitignore/test.md",
"./test/stubs/ignore4/ignoredFolder/**",
"./test/stubs/ignore4/ignoredFolder/ignored.md",
"./test/stubs/ignore4/_site/**",
]);

t.deepEqual(evf.getIgnoreGlobs().slice(-2), [
"./test/stubs/ignorelocalrootgitignore/**/node_modules/**",
"./test/stubs/ignorelocalrootgitignore/.git/**",
]);
});

test("Get ignores (project .eleventyignore and root .gitignore, using setUseGitIgnore(false))", (t) => {
Expand All @@ -133,12 +148,15 @@ test("Get ignores (project .eleventyignore and root .gitignore, using setUseGitI
evf._setLocalPathRoot("./test/stubs/ignorelocalrootgitignore");

t.deepEqual(evf.getIgnores(), [
"./test/stubs/ignorelocalrootgitignore/**/node_modules/**",
"./test/stubs/ignorelocalrootgitignore/test.md",
"./test/stubs/ignore4/ignoredFolder/**",
"./test/stubs/ignore4/ignoredFolder/ignored.md",
"./test/stubs/ignore4/_site/**",
]);

t.deepEqual(evf.getIgnoreGlobs().slice(-1), [
"./test/stubs/ignorelocalrootgitignore/**/node_modules/**",
]);
});

test("Get ignores (no .eleventyignore .gitignore exists but empty)", (t) => {
Expand All @@ -154,11 +172,14 @@ test("Get ignores (no .eleventyignore .gitignore exists but empty)", (t) => {
evf._setLocalPathRoot("./test/stubs/ignorelocalroot");

t.deepEqual(evf.getIgnores(), [
"./test/stubs/ignorelocalroot/**/node_modules/**",
"./test/stubs/ignorelocalroot/.git/**",
"./test/stubs/ignorelocalroot/test.md",
"./test/stubs/ignore5/_site/**",
]);

t.deepEqual(evf.getIgnoreGlobs().slice(-2), [
"./test/stubs/ignorelocalroot/**/node_modules/**",
"./test/stubs/ignorelocalroot/.git/**",
]);
});

test("Get ignores (both .eleventyignore and .gitignore exists, but .gitignore is empty)", (t) => {
Expand All @@ -173,13 +194,16 @@ test("Get ignores (both .eleventyignore and .gitignore exists, but .gitignore is
evf._setLocalPathRoot("./test/stubs/ignorelocalroot");

t.deepEqual(evf.getIgnores(), [
"./test/stubs/ignorelocalroot/**/node_modules/**",
"./test/stubs/ignorelocalroot/.git/**",
"./test/stubs/ignorelocalroot/test.md",
"./test/stubs/ignore6/ignoredFolder/**",
"./test/stubs/ignore6/ignoredFolder/ignored.md",
"./test/stubs/ignore6/_site/**",
]);

t.deepEqual(evf.getIgnoreGlobs().slice(-2), [
"./test/stubs/ignorelocalroot/**/node_modules/**",
"./test/stubs/ignorelocalroot/.git/**",
]);
});

test("Bad expected output, this indicates a bug upstream in a dependency. Input to 'src' and empty includes dir (issue #403, full paths in eleventyignore)", async (t) => {
Expand Down Expand Up @@ -324,9 +348,12 @@ test("De-duplicated ignores", (t) => {
]);

t.deepEqual(evf.getIgnores(), [
"./test/stubs/ignore-dedupe/**/node_modules/**",
"./test/stubs/ignore-dedupe/.git/**",
"./test/stubs/ignore-dedupe/ignoredFolder",
"./test/stubs/ignore-dedupe/_site/**",
]);

t.deepEqual(evf.getIgnoreGlobs().slice(-2), [
"./test/stubs/ignore-dedupe/**/node_modules/**",
"./test/stubs/ignore-dedupe/.git/**",
]);
});

0 comments on commit 0c27a80

Please sign in to comment.