Skip to content

Commit

Permalink
Adds eleventyConfig.setServerPassthroughCopyBehavior("copy"); configu…
Browse files Browse the repository at this point in the history
…ration API method to revert to previous passthrough copy file copy behavior for #2456
  • Loading branch information
zachleat committed Jun 23, 2022
1 parent 93ff7cc commit 2dc2770
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 26 deletions.
34 changes: 20 additions & 14 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -977,20 +977,22 @@ Arguments:
await watchRun(path);
});

// Separate watcher for passthrough copy, we only want to trigger a server reload for changes to these files
this.passthroughWatcher = chokidar.watch(
this.eleventyFiles.getGlobWatcherFilesForPassthroughCopy(),
this.getChokidarConfig()
);
this.passthroughWatcher.on("change", async (path) => {
this.logger.forceLog(`Passthrough copy file changed: ${path}`);
this.triggerServerReload([path]);
});
if (this.config.serverPassthroughCopyBehavior === "passthrough") {
// Separate watcher for passthrough copy, we only want to trigger a server reload for changes to these files
this.passthroughWatcher = chokidar.watch(
this.eleventyFiles.getGlobWatcherFilesForPassthroughCopy(),
this.getChokidarConfig()
);
this.passthroughWatcher.on("change", async (path) => {
this.logger.forceLog(`Passthrough copy file changed: ${path}`);
this.triggerServerReload([path]);
});

this.passthroughWatcher.on("add", async (path) => {
this.logger.forceLog(`Passthrough copy file added: ${path}`);
this.triggerServerReload([path]);
});
this.passthroughWatcher.on("add", async (path) => {
this.logger.forceLog(`Passthrough copy file added: ${path}`);
this.triggerServerReload([path]);
});
}

process.on("SIGINT", () => this.stopWatch());
}
Expand All @@ -999,7 +1001,11 @@ Arguments:
debug("Cleaning up chokidar and server instances, if they exist.");
this.eleventyServe.close();
this.watcher.close();
this.passthroughWatcher.close();

if (this.passthroughWatcher) {
this.passthroughWatcher.close();
}

process.exit();
}

Expand Down
10 changes: 7 additions & 3 deletions src/EleventyFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,14 @@ class EleventyFiles {
// TODO improvement: tie the includes and data to specific file extensions (currently using `**`)
let directoryGlobs = this._getIncludesAndDataDirs();

// TODO config API method to revert to previous passthrough copy behavior
// return this.validTemplateGlobs.concat(this.passthroughGlobs).concat(directoryGlobs);
if (this.config.serverPassthroughCopyBehavior === "passthrough") {
return this.validTemplateGlobs.concat(directoryGlobs);
}

return this.validTemplateGlobs.concat(directoryGlobs);
// Revert to old passthroughcopy copy files behavior
return this.validTemplateGlobs
.concat(this.passthroughGlobs)
.concat(directoryGlobs);
}

/* For `eleventy --watch` */
Expand Down
21 changes: 13 additions & 8 deletions src/EleventyServe.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,20 @@ class EleventyServe {

set eleventyConfig(config) {
this._eleventyConfig = config;
this._eleventyConfig.userConfig.events.on(
"eleventy.passthrough",
({ map }) => {
// for-free passthrough copy
if ("setAliases" in this.server) {
this.server.setAliases(map);
if (
this._eleventyConfig.userConfig.serverPassthroughCopyBehavior ===
"passthrough"
) {
this._eleventyConfig.userConfig.events.on(
"eleventy.passthrough",
({ map }) => {
// for-free passthrough copy
if ("setAliases" in this.server) {
this.server.setAliases(map);
}
}
}
);
);
}
}

// TODO this doesn’t seem to be used internally
Expand Down
5 changes: 4 additions & 1 deletion src/TemplatePassthrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ class TemplatePassthrough {

let promises = fileMap.map((entry) => {
// For-free passthrough copy
if (this.runMode === "serve" || this.runMode === "watch") {
if (
this.config.serverPassthroughCopyBehavior === "passthrough" &&
(this.runMode === "serve" || this.runMode === "watch")
) {
let aliasMap = {};
aliasMap[entry.inputPath] = entry.outputPath;

Expand Down
9 changes: 9 additions & 0 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class UserConfig {
this.dataFilterSelectors = new Set();

this.libraryAmendments = {};

this.serverPassthroughCopyBehavior = "passthrough";
}

versionCheck(expected) {
Expand Down Expand Up @@ -798,6 +800,12 @@ class UserConfig {
this.precompiledCollections = collections;
}

// "passthrough" is the default, no other value is explicitly required in code
// but opt-out via "copy" is suggested
setServerPassthroughCopyBehavior(behavior) {
this.serverPassthroughCopyBehavior = behavior;
}

getMergingConfigObject() {
return {
templateFormats: this.templateFormats,
Expand Down Expand Up @@ -851,6 +859,7 @@ class UserConfig {
precompiledCollections: this.precompiledCollections,
dataFilterSelectors: this.dataFilterSelectors,
libraryAmendments: this.libraryAmendments,
serverPassthroughCopyBehavior: this.serverPassthroughCopyBehavior,
};
}
}
Expand Down

0 comments on commit 2dc2770

Please sign in to comment.