Skip to content

Commit

Permalink
Merge pull request #2464 from Snapstromegon/#1028-#1029-add-more-poss…
Browse files Browse the repository at this point in the history
…ible-default-config-files

#1028 #1029 Possible first implementation for additional default eleventy config files
  • Loading branch information
zachleat committed Aug 16, 2022
2 parents ced063f + ecae808 commit 56742fc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 22 deletions.
28 changes: 15 additions & 13 deletions src/Eleventy.js
Expand Up @@ -662,21 +662,23 @@ Arguments:
}

_shouldResetConfig() {
let configFilePath = this.eleventyConfig.getLocalProjectConfigFile();
let configFileChanged = this.watchManager.hasQueuedFile(configFilePath);
if (configFileChanged) {
let configFilePaths = this.eleventyConfig.getLocalProjectConfigFiles();
let configFilesChanged = this.watchManager.hasQueuedFiles(configFilePaths);
if (configFilesChanged) {
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));
for (const configFilePath of configFilePaths) {
// 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 true;
}
}
}

Expand Down Expand Up @@ -806,7 +808,7 @@ Arguments:
this.watchTargets.add(this.eleventyFiles.getGlobWatcherFiles());

// Watch the local project config file
this.watchTargets.add(this.eleventyConfig.getLocalProjectConfigFile());
this.watchTargets.add(this.eleventyConfig.getLocalProjectConfigFiles());

// Template and Directory Data Files
this.watchTargets.add(
Expand Down Expand Up @@ -844,7 +846,7 @@ Arguments:

// Config file dependencies
this.watchTargets.addDependencies(
this.eleventyConfig.getLocalProjectConfigFile(),
this.eleventyConfig.getLocalProjectConfigFiles(),
filterOutGlobalDataFiles
);

Expand Down
9 changes: 9 additions & 0 deletions src/EleventyWatch.js
Expand Up @@ -74,6 +74,15 @@ class EleventyWatch {
return false;
}

hasQueuedFiles(files) {
for (const file of files) {
if (this.hasQueuedFile(file)) {
return true;
}
}
return false;
}

get pendingQueue() {
if (!this._queue) {
this._queue = [];
Expand Down
41 changes: 32 additions & 9 deletions src/TemplateConfig.js
Expand Up @@ -53,9 +53,14 @@ class TemplateConfig {
* @member {String} - Path to local project config.
* @default .eleventy.js
*/
this.projectConfigPath = ".eleventy.js";
this.projectConfigPaths = [
".eleventy.js",
".eleventy.cjs",
"eleventy.config.js",
"eleventy.config.cjs",
];
if (projectConfigPath !== undefined) {
this.projectConfigPath = projectConfigPath;
this.projectConfigPaths = [projectConfigPath];
}

if (customRootConfig) {
Expand Down Expand Up @@ -84,11 +89,24 @@ class TemplateConfig {
* @returns {String} - The normalised local project config file path.
*/
getLocalProjectConfigFile() {
if (this.projectConfigPath) {
return TemplatePath.addLeadingDotSlash(this.projectConfigPath);
let configFiles = this.getLocalProjectConfigFiles();
// Add the configFiles[0] in case of a test, where no file exists on the file system
let configFile =
configFiles.find((path) => path && fs.existsSync(path)) || configFiles[0];
if (configFile) {
return configFile;
}
}

getLocalProjectConfigFiles() {
if (this.projectConfigPaths && this.projectConfigPaths.length > 0) {
return TemplatePath.addLeadingDotSlashArray(
this.projectConfigPaths.filter((path) => path)
);
}
return [];
}

get inputDir() {
return this._inputDir;
}
Expand Down Expand Up @@ -147,7 +165,11 @@ class TemplateConfig {
* @param {String} path - The new config path.
*/
setProjectConfigPath(path) {
this.projectConfigPath = path;
if (path !== undefined) {
this.projectConfigPaths = [path];
} else {
this.projectConfigPaths = [];
}

if (this.hasConfigMerged) {
// merge it again
Expand Down Expand Up @@ -249,13 +271,14 @@ class TemplateConfig {
*/
mergeConfig() {
let localConfig = {};
let path = this.projectConfigPath
? TemplatePath.absolutePath(this.projectConfigPath)
: false;
let path = this.projectConfigPaths
.filter((path) => path)
.map((path) => TemplatePath.absolutePath(path))
.find((path) => fs.existsSync(path));

debug(`Merging config with ${path}`);

if (path && fs.existsSync(path)) {
if (path) {
try {
// remove from require cache so it will grab a fresh copy
deleteRequireCache(path);
Expand Down
6 changes: 6 additions & 0 deletions test/EleventyTest.js
Expand Up @@ -72,6 +72,9 @@ test("Eleventy file watching", async (t) => {
"./test/stubs/_includes/**",
"./test/stubs/_data/**",
"./.eleventy.js",
"./.eleventy.cjs",
"./eleventy.config.js",
"./eleventy.config.cjs",
"./test/stubs/**/*.json",
"./test/stubs/**/*.11tydata.cjs",
"./test/stubs/**/*.11tydata.js",
Expand Down Expand Up @@ -108,6 +111,9 @@ test("Eleventy file watching (no JS dependencies)", async (t) => {
"./test/stubs/_includes/**",
"./test/stubs/_data/**",
"./.eleventy.js",
"./.eleventy.cjs",
"./eleventy.config.js",
"./eleventy.config.cjs",
"./test/stubs/**/*.json",
"./test/stubs/**/*.11tydata.cjs",
"./test/stubs/**/*.11tydata.js",
Expand Down

0 comments on commit 56742fc

Please sign in to comment.