Skip to content

Commit

Permalink
11ty#1028 11ty#1029 Possible first implementation for additional defa…
Browse files Browse the repository at this point in the history
…ult eleventy config files

Currently only .eleventy.js is supported as the default config file.

This PR implements a possible solution for multiple config file. It intentionally just implements support for multiple default config files.

The implementation might be a little flaky around unittests, but it passes all of them consistently.

A future PR should probably look into allowing usage of multiple config files at once. This only uses the first existing one.

Signed-off-by: Raphael Höser <raphael@hoeser.info>
  • Loading branch information
Snapstromegon committed Jun 29, 2022
1 parent d082ead commit ecae808
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
Original file line number Diff line number Diff line change
Expand Up @@ -652,21 +652,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 @@ -796,7 +798,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 @@ -834,7 +836,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,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 @@ -107,6 +110,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 ecae808

Please sign in to comment.