Skip to content

Commit

Permalink
Write templates synchronously
Browse files Browse the repository at this point in the history
Closes 11ty#3271
Closes 11ty#2627
  • Loading branch information
shivjm committed May 1, 2024
1 parent 9c3a761 commit 9c7e5e3
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions src/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import EleventyBaseError from "./Errors/EleventyBaseError.js";
import ReservedData from "./Util/ReservedData.js";

const { set: lodashSet, get: lodashGet } = lodash;
const writeFile = util.promisify(fs.writeFile);
const fsStat = util.promisify(fs.stat);

const debug = debugUtil("Eleventy:Template");
Expand Down Expand Up @@ -189,7 +188,7 @@ class Template extends TemplateContent {
keys.push(key);
if (key !== "build" && Array.isArray(permalink[key])) {
promises.push(
Promise.all([...permalink[key]].map((entry) => super.renderPermalink(entry, data))),
Promise.all([...permalink[key]].map((entry) => super.renderPermalink(entry, data)))
);
} else {
promises.push(super.renderPermalink(permalink[key], data));
Expand All @@ -207,7 +206,7 @@ class Template extends TemplateContent {
key,
this.inputPath,
permalink[key],
results[j],
results[j]
);
}
}
Expand Down Expand Up @@ -245,7 +244,7 @@ class Template extends TemplateContent {
this.baseFile,
this.extraOutputSubdirectory,
this.htmlIOException ? this.config.htmlOutputSuffix : "",
this.engine.defaultTemplateFileExtension,
this.engine.defaultTemplateFileExtension
);
p.setUrlTransforms(this.config.urlTransforms);
return p;
Expand Down Expand Up @@ -353,7 +352,7 @@ class Template extends TemplateContent {
globalData,
mergedLayoutData,
localData,
frontMatterData,
frontMatterData
);

if (this.config.freezeReservedData) {
Expand All @@ -375,8 +374,10 @@ class Template extends TemplateContent {
e.message.endsWith("not extensible"))
) {
throw new EleventyBaseError(
`You attempted to set one of Eleventy’s reserved data property names${e.reservedNames ? `: ${e.reservedNames.join(", ")}` : ""}. You can opt-out of this behavior with \`eleventyConfig.setFreezeReservedData(false)\` or rename/remove the property in your data cascade that conflicts with Eleventy’s reserved property names (e.g. \`eleventy\`, \`pkg\`, and others). Learn more: https://www.11ty.dev/docs/data-eleventy-supplied/`,
e,
`You attempted to set one of Eleventy’s reserved data property names${
e.reservedNames ? `: ${e.reservedNames.join(", ")}` : ""
}. You can opt-out of this behavior with \`eleventyConfig.setFreezeReservedData(false)\` or rename/remove the property in your data cascade that conflicts with Eleventy’s reserved property names (e.g. \`eleventy\`, \`pkg\`, and others). Learn more: https://www.11ty.dev/docs/data-eleventy-supplied/`,
e
);
}
throw e;
Expand All @@ -397,7 +398,7 @@ class Template extends TemplateContent {
data.page.filePathStem = this.filePathStem;
data.page.outputFileExtension = this.engine.defaultTemplateFileExtension;
data.page.templateSyntax = this.templateRender.getEnginesList(
data[this.config.keys.engineOverride],
data[this.config.keys.engineOverride]
);
// data.page.url
// data.page.outputPath
Expand Down Expand Up @@ -449,7 +450,7 @@ class Template extends TemplateContent {
},
str,
inputPath,
outputPath,
outputPath
);
}
}
Expand All @@ -476,17 +477,17 @@ class Template extends TemplateContent {
page: pageData,
},
str,
outputPath,
outputPath
);
if (hadStrBefore && !str) {
this.logger.warn(
`Warning: Transform \`${name}\` returned empty when writing ${outputPath} from ${inputPath}.`,
`Warning: Transform \`${name}\` returned empty when writing ${outputPath} from ${inputPath}.`
);
}
} catch (e) {
throw new EleventyTransformError(
`Transform \`${name}\` encountered an error when transforming ${inputPath}.`,
e,
e
);
}
}
Expand Down Expand Up @@ -515,7 +516,7 @@ class Template extends TemplateContent {
},
declaredDependencies,
this.getParseForSymbolsFunction(obj),
this,
this
);
} else {
// Numbers, booleans, etc
Expand All @@ -539,7 +540,7 @@ class Template extends TemplateContent {
},
data.permalink ? ["permalink"] : undefined,
false, // skip symbol resolution
this,
this
);

this.computedData.addTemplateString(
Expand All @@ -549,7 +550,7 @@ class Template extends TemplateContent {
},
data.permalink ? ["permalink"] : undefined,
false, // skip symbol resolution
this,
this
);

// Check for reserved properties in computed data
Expand Down Expand Up @@ -622,13 +623,13 @@ class Template extends TemplateContent {
throw new TemplateContentPrematureUseError(
`Tried to use templateContent too early on ${this.inputPath}${
this.pageNumber ? ` (page ${this.pageNumber})` : ""
}`,
}`
);
} else {
throw new TemplateContentUnrenderedTemplateError(
`Tried to use templateContent on unrendered template. You need a valid permalink (or permalink object) to use templateContent on ${
this.inputPath
}${this.pageNumber ? ` (page ${this.pageNumber})` : ""}`,
}${this.pageNumber ? ` (page ${this.pageNumber})` : ""}`
);
}
}
Expand Down Expand Up @@ -722,7 +723,7 @@ class Template extends TemplateContent {
if (!this.isDryRun) {
let engineList = this.templateRender.getReadableEnginesListDifferingFromFileExtension();
this.logger.log(
`${lang.start} ${outputPath} from ${this.inputPath}${engineList ? ` (${engineList})` : ""}`,
`${lang.start} ${outputPath} from ${this.inputPath}${engineList ? ` (${engineList})` : ""}`
);
} else if (this.isDryRun) {
return;
Expand All @@ -741,14 +742,16 @@ class Template extends TemplateContent {

if (!Buffer.isBuffer(finalContent) && typeof finalContent !== "string") {
throw new Error(
`The return value from the render function for the ${this.engine.name} template was not a String or Buffer. Received ${finalContent}`,
`The return value from the render function for the ${this.engine.name} template was not a String or Buffer. Received ${finalContent}`
);
}

let templateBenchmark = this.bench.get("Template Write");
templateBenchmark.before();

await writeFile(outputPath, finalContent);
// Note: This deliberately uses the synchronous version to avoid
// unbounded concurrency: https://github.com/11ty/eleventy/issues/3271
fs.writeFileSync(outputPath, finalContent);

templateBenchmark.after();
this.writeCount++;
Expand Down Expand Up @@ -845,7 +848,7 @@ class Template extends TemplateContent {
debug(
"Template not written %o from %o (via permalink: false, permalink.build: false, or a permalink object without a build property).",
page.outputPath,
page.template.inputPath,
page.template.inputPath
);
continue;
}
Expand All @@ -865,7 +868,7 @@ class Template extends TemplateContent {
this.inputPath,
this.templateData,
this.extensionMap,
this.eleventyConfig,
this.eleventyConfig
);

// We use this cheap property setter below instead
Expand Down Expand Up @@ -911,7 +914,7 @@ class Template extends TemplateContent {
key,
this.inputPath,
newDate,
stat.birthtimeMs,
stat.birthtimeMs
);

return newDate;
Expand Down Expand Up @@ -973,7 +976,7 @@ class Template extends TemplateContent {
"getMappedDate: using filename regex time for %o of %o: %o",
this.inputPath,
filepathRegex[1],
dateObj,
dateObj
);
return dateObj;
}
Expand Down

0 comments on commit 9c7e5e3

Please sign in to comment.