Skip to content

Commit 58d5ed2

Browse files
committed
Hoist templateGlobalVars merge, update JSDoc to reflect new contract
Merges globalVars and globalDataVars once before the pMap loop instead of once per template render. Updates TemplateFunction and templateBuilder JSDoc to accurately describe that vars now includes global.data.js output.
1 parent 5ed14aa commit 58d5ed2

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

lib/build-pages/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ export async function buildPagesDirect (src, dest, siteData, _opts) {
254254
? siteData.templates.filter(t => templateFilterSet.has(t.templateFile.filepath))
255255
: siteData.templates
256256

257+
// Merge once — globalVars and globalDataVars are constant for this build.
258+
const templateGlobalVars = { ...globalVars, ...globalDataVars }
259+
257260
await Promise.all([
258261
pMap(pagesToRender, async (page) => {
259262
try {
@@ -276,7 +279,7 @@ export async function buildPagesDirect (src, dest, siteData, _opts) {
276279
const buildResult = await templateBuilder({
277280
src,
278281
dest,
279-
globalVars: { ...globalVars, ...globalDataVars },
282+
globalVars: templateGlobalVars,
280283
template,
281284
pages,
282285
})

lib/build-pages/page-builders/template-builder.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { writeFile, mkdir } from 'fs/promises'
1717
* @template {Record<string, any>} T - The type of variables for the template
1818
* @callback TemplateFunction
1919
* @param {object} params - The parameters for the template.
20-
* @param {T} params.vars - All of the site globalVars.
20+
* @param {T} params.vars - All of the site globalVars merged with global.data.js output.
2121
* @param {TemplateInfo} params.template - Info about the current template
2222
* @param {PageData<T, any, string>[]} params.pages - An array of info about every page
2323
* @returns {Promise<string | TemplateOutputOverride | TemplateOutputOverride[]>}
@@ -46,12 +46,13 @@ import { writeFile, mkdir } from 'fs/promises'
4646
*/
4747

4848
/**
49-
* The template builder renders templates agains the globalVars variables
49+
* The template builder renders templates against the globalVars variables.
50+
* globalVars passed here already includes global.data.js output merged in.
5051
* @template {Record<string, any>} T - The type of global variables for the template builder
5152
* @param {object} params
5253
* @param {string} params.src - The src path of the site build.
5354
* @param {string} params.dest - The dest path of the site build.
54-
* @param {T} params.globalVars - The resolvedGlobal vars object.
55+
* @param {T} params.globalVars - globalVars merged with global.data.js output.
5556
* @param {TemplateInfo} params.template - The TemplateInfo of the template.
5657
* @param {PageData<T, any, string>[]} params.pages - The array of PageData object.
5758
*/

0 commit comments

Comments
 (0)