Skip to content

Commit

Permalink
Fixes #3468 by adding resetConfig option to addWatchTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Oct 1, 2024
1 parent dd28e89 commit 1b9b80e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import TemplateConfig from "./TemplateConfig.js";
import FileSystemSearch from "./FileSystemSearch.js";
import ProjectDirectories from "./Util/ProjectDirectories.js";
import PathNormalizer from "./Util/PathNormalizer.js";
import { isGlobMatch } from "./Util/GlobMatcher.js";

import simplePlural from "./Util/Pluralize.js";
import checkPassthroughCopyBehavior from "./Util/PassthroughCopyBehaviorCheck.js";
Expand Down Expand Up @@ -858,7 +859,6 @@ Arguments:
}

let relevantLayouts = this.eleventyConfig.usesGraph.getLayoutsUsedBy(changedFilePath);

// Note: these are sync events!
// `templateModified` is an alias for resourceModified but all listeners for this are cleared out when the config is reset.
eventBus.emit("eleventy.templateModified", changedFilePath, {
Expand All @@ -875,10 +875,16 @@ Arguments:

shouldTriggerConfigReset(changedFiles) {
let configFilePaths = new Set(this.eleventyConfig.getLocalProjectConfigFiles());
let resetConfigGlobs = EleventyWatchTargets.normalizeToGlobs(
Array.from(this.eleventyConfig.userConfig.watchTargetsConfigReset),
);
for (let filePath of changedFiles) {
if (configFilePaths.has(filePath)) {
return true;
}
if (isGlobMatch(filePath, resetConfigGlobs)) {
return true;
}
}

for (const configFilePath of configFilePaths) {
Expand Down
3 changes: 3 additions & 0 deletions src/TemplateWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ class TemplateWriter {
let wasCached = false;
if (tmpl) {
wasCached = true;
// Update config for https://github.com/11ty/eleventy/issues/3468
tmpl.eleventyConfig = this.eleventyConfig;

// TODO reset other constructor things here like inputDir/outputDir/extensionMap/
tmpl.setTemplateData(this.templateData);
} else {
Expand Down
10 changes: 9 additions & 1 deletion src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class UserConfig {
this.plugins = [];
this.templateFormatsAdded = [];
this.additionalWatchTargets = [];
this.watchTargetsConfigReset = new Set();
this.extensionMap = new Set();
this.dataExtensions = new Map();
this.urlTransforms = [];
Expand Down Expand Up @@ -172,6 +173,7 @@ class UserConfig {
this.extensionConflictMap = {};
this.watchJavaScriptDependencies = true;
this.additionalWatchTargets = [];
this.watchTargetsConfigReset = new Set();
/** @type {object} */
this.serverOptions = {};
/** @type {object} */
Expand Down Expand Up @@ -878,7 +880,12 @@ class UserConfig {
return this.#dataDeepMergeModified;
}

addWatchTarget(additionalWatchTargets) {
addWatchTarget(additionalWatchTargets, options = {}) {
// Reset the config when the target path changes
if (options.resetConfig) {
this.watchTargetsConfigReset.add(additionalWatchTargets);
}

this.additionalWatchTargets.push(additionalWatchTargets);
}

Expand Down Expand Up @@ -1238,6 +1245,7 @@ class UserConfig {
dataDeepMerge: this.dataDeepMerge,
watchJavaScriptDependencies: this.watchJavaScriptDependencies,
additionalWatchTargets: this.additionalWatchTargets,
watchTargetsConfigReset: this.watchTargetsConfigReset,
serverOptions: this.serverOptions,
chokidarConfig: this.chokidarConfig,
watchThrottleWaitTime: this.watchThrottleWaitTime,
Expand Down

0 comments on commit 1b9b80e

Please sign in to comment.