Skip to content

Commit

Permalink
Fixes #1052. Use a dependency graph to manage config dependencies so …
Browse files Browse the repository at this point in the history
…we know when to reload the config.
  • Loading branch information
zachleat committed May 12, 2022
1 parent b3962fb commit fbf2037
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,28 @@ Arguments:
this.watchManager.addToPendingQueue(changedFilePath);
}

_shouldResetConfig() {
let configFilePath = this.eleventyConfig.getLocalProjectConfigFile();
let configFileChanged = this.watchManager.hasQueuedFile(configFilePath);
if (configFileChanged) {
return true;
}

// Any dependencies of the config file changed
let configFileDependencies =
this.watchTargets.getDependenciesOf(configFilePath);
for (let dep of configFileDependencies) {
if (this.watchManager.hasQueuedFile(dep)) {
// Delete from require cache so that updates to the module are re-required
deleteRequireCache(TemplatePath.absolutePath(dep));

return true;
}
}

return false;
}

/**
* tbd.
*
Expand All @@ -667,11 +689,7 @@ Arguments:
await this.config.events.emit("eleventy.beforeWatch", queue);

// reset and reload global configuration :O
if (
this.watchManager.hasQueuedFile(
this.eleventyConfig.getLocalProjectConfigFile()
)
) {
if (this._shouldResetConfig()) {
this.resetConfig();
}

Expand Down
29 changes: 29 additions & 0 deletions src/EleventyWatchTargets.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { TemplatePath } = require("@11ty/eleventy-utils");
const { DepGraph } = require("dependency-graph");

const deleteRequireCache = require("./Util/DeleteRequireCache");
const JavaScriptDependencies = require("./Util/JavaScriptDependencies");
Expand All @@ -9,6 +10,8 @@ class EleventyWatchTargets {
this.dependencies = new Set();
this.newTargets = new Set();
this._watchJavaScriptDependencies = true;

this.graph = new DepGraph();
}

set watchJavaScriptDependencies(watch) {
Expand Down Expand Up @@ -41,6 +44,29 @@ class EleventyWatchTargets {
return this.targets.has(target);
}

addToDependencyGraph(parent, deps) {
if (!this.graph.hasNode(parent)) {
this.graph.addNode(parent);
}
for (let dep of deps) {
if (!this.graph.hasNode(dep)) {
this.graph.addNode(dep);
}
this.graph.addDependency(parent, dep);
}
}

uses(parent, dep) {
return this.getDependenciesOf(parent).includes(dep);
}

getDependenciesOf(parent) {
if (!this.graph.hasNode(parent)) {
return [];
}
return this.graph.dependenciesOf(parent);
}

addRaw(targets, isDependency) {
for (let target of targets) {
let path = TemplatePath.addLeadingDotSlash(target);
Expand Down Expand Up @@ -81,6 +107,9 @@ class EleventyWatchTargets {
deps = deps.filter(filterCallback);
}

for (let target of targets) {
this.addToDependencyGraph(target, deps);
}
this.addRaw(deps, true);
}

Expand Down
54 changes: 54 additions & 0 deletions test/EleventyWatchTargetsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ test("JavaScript addDependencies", (t) => {
let targets = new EleventyWatchTargets();
targets.addDependencies("./test/stubs/config-deps.js");
t.deepEqual(targets.getTargets(), ["./test/stubs/config-deps-upstream.js"]);

t.true(
targets.uses(
"./test/stubs/config-deps.js",
"./test/stubs/config-deps-upstream.js"
)
);
t.false(
targets.uses("./test/stubs/config-deps.js", "./test/stubs/config-deps.js")
);
});

test("JavaScript addDependencies (one file has two dependencies)", (t) => {
Expand All @@ -54,13 +64,51 @@ test("JavaScript addDependencies (one file has two dependencies)", (t) => {
"./test/stubs/dependencies/dep1.js",
"./test/stubs/dependencies/dep2.js",
]);

t.true(
targets.uses(
"./test/stubs/dependencies/two-deps.11ty.js",
"./test/stubs/dependencies/dep1.js"
)
);
t.true(
targets.uses(
"./test/stubs/dependencies/two-deps.11ty.js",
"./test/stubs/dependencies/dep2.js"
)
);
t.false(
targets.uses(
"./test/stubs/dependencies/two-deps.11ty.js",
"./test/stubs/dependencies/dep3.js"
)
);
});

test("JavaScript addDependencies (skip JS deps)", (t) => {
let targets = new EleventyWatchTargets();
targets.watchJavaScriptDependencies = false;
targets.addDependencies("./test/stubs/dependencies/two-deps.11ty.js");
t.deepEqual(targets.getTargets(), []);

t.false(
targets.uses(
"./test/stubs/dependencies/two-deps.11ty.js",
"./test/stubs/dependencies/dep1.js"
)
);
t.false(
targets.uses(
"./test/stubs/dependencies/two-deps.11ty.js",
"./test/stubs/dependencies/dep2.js"
)
);
t.false(
targets.uses(
"./test/stubs/dependencies/two-deps.11ty.js",
"./test/stubs/dependencies/dep3.js"
)
);
});

test("JavaScript addDependencies with a filter", (t) => {
Expand All @@ -69,6 +117,12 @@ test("JavaScript addDependencies with a filter", (t) => {
return path.indexOf("./test/stubs/") === -1;
});
t.deepEqual(targets.getTargets(), []);
t.false(
targets.uses(
"./test/stubs/dependencies/config-deps.js",
"./test/stubs/dependencies/config-deps-upstream.js"
)
);
});

test("add, addDependencies falsy values are filtered", (t) => {
Expand Down

0 comments on commit fbf2037

Please sign in to comment.