Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup for --formats, templateFormats config, and setTemplateFormats addTemplateFormats configuration API methods #3255

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ const debug = require("debug")("Eleventy:cmd");
// reuse ErrorHandler instance in Eleventy
errorHandler = elev.errorHandler;

// Before init
elev.setFormats(argv.formats);

// careful, we can’t use async/await here to error properly
// with old node versions in `please-upgrade-node` above.
elev
Expand All @@ -92,7 +95,6 @@ const debug = require("debug")("Eleventy:cmd");

elev.setIgnoreInitial(argv["ignore-initial"]);
elev.setIncrementalBuild(argv.incremental);
elev.setFormats(argv.formats);

try {
if (argv.serve) {
Expand Down
36 changes: 26 additions & 10 deletions src/Data/TemplateData.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from "node:path";
import semver from "semver";

import lodash from "@11ty/lodash-custom";
import { TemplatePath, isPlainObject } from "@11ty/eleventy-utils";
Expand All @@ -10,15 +11,12 @@ import TemplateGlob from "../TemplateGlob.js";
import EleventyExtensionMap from "../EleventyExtensionMap.js";
import EleventyBaseError from "../Errors/EleventyBaseError.js";
import TemplateDataInitialGlobalData from "./TemplateDataInitialGlobalData.js";
import { getEleventyPackageJson } from "../Util/ImportJsonSync.js";
import {
EleventyImport,
EleventyLoadContent,
normalizeFilePathInEleventyPackage,
} from "../Util/Require.js";
import { getEleventyPackageJson, getWorkingProjectPackageJson } from "../Util/ImportJsonSync.js";
import { EleventyImport, EleventyLoadContent } from "../Util/Require.js";
import { DeepFreeze } from "../Util/DeepFreeze.js";

const { set: lodashSet, get: lodashGet } = lodash;

const debugWarn = debugUtil("Eleventy:Warnings");
const debug = debugUtil("Eleventy:TemplateData");
const debugDev = debugUtil("Dev:Eleventy:TemplateData");
Expand Down Expand Up @@ -85,7 +83,8 @@ class TemplateData {

get extensionMap() {
if (!this._extensionMap) {
this._extensionMap = new EleventyExtensionMap([], this.eleventyConfig);
this._extensionMap = new EleventyExtensionMap(this.eleventyConfig);
this._extensionMap.setFormats([]);
}
return this._extensionMap;
}
Expand All @@ -108,16 +107,24 @@ class TemplateData {
}

getRawImports() {
if (!this.config.keys.package) {
debug(
"Opted-out of package.json assignment for global data with falsy value for `keys.package` configuration.",
);
return this.rawImports;
} else if (Object.keys(this.rawImports).length > 0) {
return this.rawImports;
}

try {
let pkgJson = getEleventyPackageJson();
let pkgJson = getWorkingProjectPackageJson();
this.rawImports[this.config.keys.package] = pkgJson;

if (this.config.freezeReservedData) {
DeepFreeze(this.rawImports);
}
} catch (e) {
let pkgPath = normalizeFilePathInEleventyPackage("package.json");
debug("Could not find and/or require package.json for data preprocessing at %o", pkgPath);
debug("Could not find or require package.json import for global data.");
}

return this.rawImports;
Expand Down Expand Up @@ -311,6 +318,15 @@ class TemplateData {
this.configApiGlobalData = new Promise(async (resolve) => {
let globalData = await this.initialGlobalData.getData();

if (!("eleventy" in globalData)) {
globalData.eleventy = {};
}

// #2293 for meta[name=generator]
const pkg = getEleventyPackageJson();
globalData.eleventy.version = semver.coerce(pkg.version).toString();
globalData.eleventy.generator = `Eleventy v${globalData.eleventy.version}`;

if (this.environmentVariables) {
if (!("env" in globalData.eleventy)) {
globalData.eleventy.env = {};
Expand Down
10 changes: 0 additions & 10 deletions src/Data/TemplateDataInitialGlobalData.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import semver from "semver";
import lodash from "@11ty/lodash-custom";

import EleventyBaseError from "../Errors/EleventyBaseError.js";
import { getEleventyPackageJson } from "../Util/ImportJsonSync.js";

const { set: lodashSet } = lodash;
const pkg = getEleventyPackageJson();

class TemplateDataConfigError extends EleventyBaseError {}

Expand Down Expand Up @@ -35,13 +32,6 @@ class TemplateDataInitialGlobalData {
}
}

if (!("eleventy" in globalData)) {
globalData.eleventy = {};
}
// #2293 for meta[name=generator]
globalData.eleventy.version = semver.coerce(pkg.version).toString();
globalData.eleventy.generator = `Eleventy v${globalData.eleventy.version}`;

return globalData;
}
}
Expand Down
30 changes: 18 additions & 12 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import RenderPlugin, * as RenderPluginExtras from "./Plugins/RenderPlugin.js";
import I18nPlugin, * as I18nPluginExtras from "./Plugins/I18nPlugin.js";
import HtmlBasePlugin, * as HtmlBasePluginExtras from "./Plugins/HtmlBasePlugin.js";
import { TransformPlugin as InputPathToUrlTransformPlugin } from "./Plugins/InputPathToUrl.js";
import ProjectTemplateFormats from "./Util/ProjectTemplateFormats.js";

const pkg = getEleventyPackageJson();
const debug = debugUtil("Eleventy");
Expand All @@ -42,8 +43,9 @@ const debug = debugUtil("Eleventy");
* @returns {module:11ty/eleventy/Eleventy~Eleventy}
*/
class Eleventy {
#directories; /* ProjectDirectories instance */
#projectPackageJson; /* userspace package.json file contents */
#directories; /* ProjectDirectories instance */
#templateFormats; /* ProjectTemplateFormats instance */

constructor(input, output, options = {}, eleventyConfig = null) {
/** @member {String} - Holds the path to the input (might be a file or folder) */
Expand Down Expand Up @@ -97,12 +99,6 @@ class Eleventy {
*/
this.needsInit = true;

/**
* @member {Array<String>} - Subset of template types.
* @default null
*/
this.formatsOverride = null;

/**
* @member {Boolean} - Is this an incremental build? (only operates on a subset of input files)
* @default false
Expand Down Expand Up @@ -139,6 +135,7 @@ class Eleventy {
this.eleventyConfig.setProjectUsingEsm(this.isEsm);
this.eleventyConfig.setLogger(this.logger);
this.eleventyConfig.setDirectories(this.directories);
this.eleventyConfig.setTemplateFormats(this.templateFormats);

if (this.pathPrefix || this.pathPrefix === "") {
this.eleventyConfig.setPathPrefix(this.pathPrefix);
Expand Down Expand Up @@ -423,8 +420,10 @@ class Eleventy {
await this.config.events.emit("eleventy.env", this.env);
}

let formats = this.formatsOverride || this.config.templateFormats;
this.extensionMap = new EleventyExtensionMap(formats, this.eleventyConfig);
let formats = this.templateFormats.getTemplateFormats();

this.extensionMap = new EleventyExtensionMap(this.eleventyConfig);
this.extensionMap.setFormats(formats);
await this.config.events.emit("eleventy.extensionmap", this.extensionMap);

// eleventyServe is always available, even when not in --serve mode
Expand Down Expand Up @@ -617,16 +616,23 @@ Verbose Output: ${this.verboseMode}`;
this.eleventyConfig.verbose = isVerbose;
}

get templateFormats() {
if (!this.#templateFormats) {
let tf = new ProjectTemplateFormats();
this.#templateFormats = tf;
}

return this.#templateFormats;
}

/**
* Updates the template formats of Eleventy.
*
* @method
* @param {String} formats - The new template formats.
*/
setFormats(formats) {
if (formats && formats !== "*") {
this.formatsOverride = formats.split(",");
}
this.templateFormats.setViaCommandLine(formats);
}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/EleventyExtensionMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import EleventyBaseError from "./Errors/EleventyBaseError.js";
class EleventyExtensionMapConfigError extends EleventyBaseError {}

class EleventyExtensionMap {
constructor(formatKeys, config) {
constructor(config) {
this.config = config;

this.formatKeys = formatKeys;

this.setFormats(formatKeys);

this._spiderJsDepsCache = {};
}

setFormats(formatKeys = []) {
// raw
this.formatKeys = formatKeys;

this.unfilteredFormatKeys = formatKeys.map(function (key) {
return key.trim().toLowerCase();
});
Expand Down Expand Up @@ -251,6 +250,10 @@ class EleventyExtensionMap {
return this._extensionToKeyMap;
}

getAllTemplateKeys() {
return Object.keys(this.extensionToKeyMap);
}

getReadableFileExtensions() {
return Object.keys(this.extensionToKeyMap).join(" ");
}
Expand Down
3 changes: 2 additions & 1 deletion src/EleventyFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ class EleventyFiles {
get extensionMap() {
// for tests
if (!this._extensionMap) {
this._extensionMap = new EleventyExtensionMap(this.formats, this.eleventyConfig);
this._extensionMap = new EleventyExtensionMap(this.eleventyConfig);
this._extensionMap.setFormats(this.formats);
this._extensionMap.config = this.eleventyConfig;
}
return this._extensionMap;
Expand Down
3 changes: 2 additions & 1 deletion src/Engines/TemplateEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class TemplateEngine {

get extensionMap() {
if (!this._extensionMap) {
this._extensionMap = new EleventyExtensionMap([], this.eleventyConfig);
this._extensionMap = new EleventyExtensionMap(this.eleventyConfig);
this._extensionMap.setFormats([]);
}
return this._extensionMap;
}
Expand Down
70 changes: 37 additions & 33 deletions src/TemplateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import UserConfig from "./UserConfig.js";
import GlobalDependencyMap from "./GlobalDependencyMap.js";
import ExistsCache from "./Util/ExistsCache.js";
import merge from "./Util/Merge.js";
import unique from "./Util/Unique.js";
import eventBus from "./EventBus.js";
import ProjectTemplateFormats from "./Util/ProjectTemplateFormats.js";

const debug = debugUtil("Eleventy:TemplateConfig");
const debugDev = debugUtil("Dev:Eleventy:TemplateConfig");
Expand Down Expand Up @@ -50,6 +50,8 @@ class EleventyPluginError extends EleventyBaseError {}
* @param {String} projectConfigPath - Path to local project config.
*/
class TemplateConfig {
#templateFormats;

constructor(customRootConfig, projectConfigPath) {
this.userConfig = new UserConfig();

Expand Down Expand Up @@ -101,6 +103,18 @@ class TemplateConfig {
this.userConfig.directories = directories.getUserspaceInstance();
}

/* Setter for TemplateFormats instance */
setTemplateFormats(templateFormats) {
this.#templateFormats = templateFormats;
}

get templateFormats() {
if (!this.#templateFormats) {
this.#templateFormats = new ProjectTemplateFormats();
}
return this.#templateFormats;
}

/* Backwards compat */
get inputDir() {
return this.directories.input;
Expand Down Expand Up @@ -335,8 +349,6 @@ class TemplateConfig {
this.directories.setViaConfigObject(exportedConfigObject.dir);
}

// TODO (config) sync `exportedConfigObject` to the rest of `userConfig` (e.g. templateFormats)

if (typeof configDefaultReturn === "function") {
localConfig = await configDefaultReturn(this.userConfig);
} else {
Expand Down Expand Up @@ -399,20 +411,28 @@ class TemplateConfig {
}
}

// Template Formats:
// 1. Root Config (usually defaultConfig.js)
// 2. Local Config return object (project .eleventy.js)
// 3.
let templateFormats = this.rootConfig.templateFormats || [];
if (localConfig && localConfig.templateFormats) {
templateFormats = localConfig.templateFormats;
delete localConfig.templateFormats;
// `templateFormats` is an override via `setTemplateFormats`
if (this.userConfig && this.userConfig.templateFormats) {
this.templateFormats.setViaConfig(this.userConfig.templateFormats);
} else if (localConfig?.templateFormats || this.rootConfig?.templateFormats) {
// Local project config or defaultConfig.js
this.templateFormats.setViaConfig(
localConfig.templateFormats || this.rootConfig?.templateFormats,
);
}

// `templateFormatsAdded` is additive via `addTemplateFormats`
if (this.userConfig && this.userConfig.templateFormatsAdded) {
this.templateFormats.addViaConfig(this.userConfig.templateFormatsAdded);
}

let mergedConfig = merge({}, this.rootConfig, localConfig);

// Setup a few properties for plugins:

// Set frozen templateFormats
mergedConfig.templateFormats = Object.freeze(this.templateFormats.getTemplateFormats());

// Setup pathPrefix set via command line for plugin consumption
if (this.overrides.pathPrefix) {
mergedConfig.pathPrefix = this.overrides.pathPrefix;
Expand All @@ -433,9 +453,6 @@ class TemplateConfig {
// But BEFORE the rest of the config options are merged
// this way we can pass directories and other template information to plugins

// Temporarily restore templateFormats
mergedConfig.templateFormats = templateFormats;

await this.userConfig.events.emit("eleventy.beforeConfig", this.userConfig);

let benchmarkManager = this.userConfig.benchmarkManager.get("Aggregate");
Expand All @@ -444,30 +461,17 @@ class TemplateConfig {
await this.processPlugins(mergedConfig);
pluginsBench.after();

delete mergedConfig.templateFormats;

let eleventyConfigApiMergingObject = this.userConfig.getMergingConfigObject();

// `templateFormats` is an override via `setTemplateFormats`
// `templateFormatsAdded` is additive via `addTemplateFormats`
if (eleventyConfigApiMergingObject && eleventyConfigApiMergingObject.templateFormats) {
templateFormats = eleventyConfigApiMergingObject.templateFormats;
delete eleventyConfigApiMergingObject.templateFormats;
if ("templateFormats" in eleventyConfigApiMergingObject) {
throw new Error(
"Internal error: templateFormats should not return from `getMergingConfigObject`",
);
}

let templateFormatsAdded = eleventyConfigApiMergingObject.templateFormatsAdded || [];
delete eleventyConfigApiMergingObject.templateFormatsAdded;

templateFormats = unique([...templateFormats, ...templateFormatsAdded]);

merge(mergedConfig, eleventyConfigApiMergingObject);

// Apply overrides, currently only pathPrefix uses this I think!
// Overrides are only used by pathPrefix
debug("Configuration overrides: %o", this.overrides);
merge(mergedConfig, this.overrides);

// Restore templateFormats
mergedConfig.templateFormats = templateFormats;
merge(mergedConfig, eleventyConfigApiMergingObject, this.overrides);

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

Expand Down
3 changes: 2 additions & 1 deletion src/TemplateContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class TemplateContent {
/* Used by tests */
get extensionMap() {
if (!this._extensionMap) {
this._extensionMap = new EleventyExtensionMap([], this.eleventyConfig);
this._extensionMap = new EleventyExtensionMap(this.eleventyConfig);
this._extensionMap.setFormats([]);
}
return this._extensionMap;
}
Expand Down
Loading