Skip to content

Commit

Permalink
Better fix for #3246
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Apr 10, 2024
1 parent a569a09 commit a81cb75
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 41 deletions.
70 changes: 42 additions & 28 deletions src/TemplateConfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "node:fs";
import chalk from "kleur";
import { TemplatePath } from "@11ty/eleventy-utils";
import { TemplatePath, isPlainObject } from "@11ty/eleventy-utils";
import debugUtil from "debug";

import { EleventyImportRaw, EleventyImportRawFromEleventy } from "./Util/Require.js";
Expand Down Expand Up @@ -164,6 +164,7 @@ class TemplateConfig {
*/
async init(overrides) {
await this.initializeRootConfig();

if (overrides) {
this.appendToRootConfig(overrides);
}
Expand Down Expand Up @@ -251,11 +252,11 @@ class TemplateConfig {
}

if (typeof this.rootConfig === "function") {
// Not yet using async in defaultConfig.js
this.rootConfig = this.rootConfig.call(this, this.userConfig);
// debug( "rootConfig is a function, after calling, this.userConfig is %o", this.userConfig );
}

debug("rootConfig %o", this.rootConfig);
debug("Default Eleventy config %o", this.rootConfig);
}

/*
Expand Down Expand Up @@ -314,27 +315,32 @@ class TemplateConfig {
*/
async requireLocalConfigFile() {
let localConfig = {};
// TODO option to set config root dir
let exportedConfig = {};

let path = this.projectConfigPaths.filter((path) => path).find((path) => fs.existsSync(path));

debug(`Merging default config with ${path}`);
if (path) {
try {
let { default: cfg, directories } = await EleventyImportRaw(
path,
this.isEsm ? "esm" : "cjs",
);
localConfig = cfg;
let { default: configDefaultReturn, config: exportedConfigObject } =
await EleventyImportRaw(path, this.isEsm ? "esm" : "cjs");

exportedConfig = exportedConfigObject || {};

if (this.directories && Object.keys(directories || {}).length > 0) {
debug("Setting directories via `directories` export from config file: %o", directories);
this.directories.setViaConfigObject(directories);
if (this.directories && Object.keys(exportedConfigObject?.dir || {}).length > 0) {
debug(
"Setting directories via `config.dir` export from config file: %o",
exportedConfigObject.dir,
);
this.directories.setViaConfigObject(exportedConfigObject.dir);
}

// debug( "localConfig require return value: %o", localConfig );
if (typeof localConfig === "function") {
localConfig = await localConfig(this.userConfig);
// debug( "localConfig is a function, after calling, this.userConfig is %o", this.userConfig );
// TODO (config) sync `exportedConfigObject` to the rest of `userConfig` (e.g. templateFormats)

if (typeof configDefaultReturn === "function") {
localConfig = await configDefaultReturn(this.userConfig);
} else {
localConfig = configDefaultReturn;
}

// Removed a check for `filters` in 3.0.0-alpha.6 (now using addTransform instead) https://www.11ty.dev/docs/config/#transforms
Expand All @@ -349,10 +355,16 @@ class TemplateConfig {
);
}
} else {
debug("Eleventy local project config file not found, skipping.");
debug(
"Project config file not found (not an error—skipping). Looked in: %o",
this.projectConfigPaths,
);
}

return localConfig;
return {
localConfig,
exportedConfig,
};
}

/**
Expand All @@ -362,7 +374,12 @@ class TemplateConfig {
* @returns {{}} merged - The merged config file.
*/
async mergeConfig() {
let localConfig = await this.requireLocalConfigFile();
let { localConfig, exportedConfig } = await this.requireLocalConfigFile();

// Merge `export const config = {}` with `return {}` in config callback
if (isPlainObject(exportedConfig)) {
localConfig = merge(localConfig || {}, exportedConfig);
}

if (this.directories) {
if (Object.keys(this.userConfig.directoryAssignments || {}).length > 0) {
Expand All @@ -372,6 +389,7 @@ class TemplateConfig {
);
this.directories.setViaConfigObject(this.userConfig.directoryAssignments);
}

if (localConfig && Object.keys(localConfig?.dir || {}).length > 0) {
debug(
"Setting directories via `dir` object return from configuration file: %o",
Expand Down Expand Up @@ -453,7 +471,11 @@ class TemplateConfig {

debug("Current configuration: %o", mergedConfig);

this.afterConfigMergeActions(mergedConfig);
// Add to the merged config too
mergedConfig.uses = this.usesGraph;

// this is used for the layouts event
this.usesGraph.setConfig(mergedConfig);

return mergedConfig;
}
Expand All @@ -465,14 +487,6 @@ class TemplateConfig {
return this._usesGraph;
}

afterConfigMergeActions(eleventyConfig) {
// Add to the merged config too
eleventyConfig.uses = this.usesGraph;

// this is used for the layouts event
this.usesGraph.setConfig(eleventyConfig);
}

get uses() {
if (!this.usesGraph) {
throw new Error("The Eleventy Global Dependency Graph has not yet been initialized.");
Expand Down
2 changes: 2 additions & 0 deletions src/defaultConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default function (config) {
engineOverride: "templateEngineOverride",
computed: "eleventyComputed",
},

// Deprecated, define using `export const directories = {}` instead.
// Reference values using `eleventyConfig.directories` instead.
dir: {
Expand All @@ -135,6 +136,7 @@ export default function (config) {
data: "_data",
output: "_site",
},

// deprecated, use config.addNunjucksFilter
nunjucksFilters: {},
};
Expand Down
4 changes: 2 additions & 2 deletions test/EleventyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ test("setInputDirectory config method #1503 in a plugin throws error", async (t)
});
});

test("Eleventy directories export (ESM)", async (t) => {
test("Eleventy config export (ESM)", async (t) => {
t.plan(5);
let elev = new Eleventy("test/stubs/cfg-directories-export", null, {
configPath: "./test/stubs/cfg-directories-export/eleventy.config.js",
Expand All @@ -931,7 +931,7 @@ test("Eleventy directories export (ESM)", async (t) => {
let result = await elev.toJSON();
});

test("Eleventy directories export (CommonJS)", async (t) => {
test("Eleventy config export (CommonJS)", async (t) => {
t.plan(5);
let elev = new Eleventy("test/stubs/cfg-directories-export-cjs", null, {
configPath: "./test/stubs/cfg-directories-export-cjs/eleventy.config.cjs",
Expand Down
12 changes: 7 additions & 5 deletions test/stubs/cfg-directories-export-cjs/eleventy.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ module.exports = function(eleventyConfig) {

};

module.exports.directories = {
input: "src",
includes: "myincludes2",
data: "mydata2",
output: "dist2"
module.exports.config = {
dir: {
input: "src",
includes: "myincludes2",
data: "mydata2",
output: "dist2"
}
}
14 changes: 8 additions & 6 deletions test/stubs/cfg-directories-export/eleventy.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ export default function(eleventyConfig) {

};

export const directories = {
input: "src",
includes: "myincludes",
data: "mydata",
output: "dist"
}
export const config = {
dir: {
input: "src",
includes: "myincludes",
data: "mydata",
output: "dist"
}
};

0 comments on commit a81cb75

Please sign in to comment.