Skip to content

Commit d2633b8

Browse files
authored
Merge pull request #240 from bcomnes/fix/template-global-data-vars
Pass global.data.js output to template vars
2 parents 280cc3b + 07c29e6 commit d2633b8

7 files changed

Lines changed: 29 additions & 11 deletions

File tree

lib/build-pages/index.js

Lines changed: 4 additions & 2 deletions
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 {
@@ -274,9 +277,8 @@ export async function buildPagesDirect (src, dest, siteData, _opts) {
274277
pMap(templatesToRender, async (template) => {
275278
try {
276279
const buildResult = await templateBuilder({
277-
src,
278280
dest,
279-
globalVars,
281+
globalVars: templateGlobalVars,
280282
template,
281283
pages,
282284
})

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

Lines changed: 4 additions & 4 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,12 @@ 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
52-
* @param {string} params.src - The src path of the site build.
5353
* @param {string} params.dest - The dest path of the site build.
54-
* @param {T} params.globalVars - The resolvedGlobal vars object.
54+
* @param {T} params.globalVars - globalVars merged with global.data.js output.
5555
* @param {TemplateInfo} params.template - The TemplateInfo of the template.
5656
* @param {PageData<T, any, string>[]} params.pages - The array of PageData object.
5757
*/

lib/build-pages/page-data.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ export class PageData {
168168
const { pageVars, type } = pageInfo
169169
this.pageVars = await resolveVars({
170170
varsPath: pageVars?.filepath,
171-
resolveVars: globalVars,
172171
})
173172
await resolvePostVars({ varsPath: pageVars?.filepath }) // throws if postVars export is detected
174173

lib/build-pages/resolve-vars.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*
44
* @param {object} params
55
* @param {string} [params.varsPath] - Path to the file containing the variables.
6-
* @param {object} [params.resolveVars] - Any variables you want passed to the resolveFunction.
76
* @param {string} [params.key='default'] - The key to extract from the imported module. Default: 'default'
87
* @returns {Promise<object>} - Returns the resolved variables. If the imported variable is a function, it executes and returns its result. Otherwise, it returns the variable directly.
98
*/

test-cases/general-features/index.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,21 @@ test.describe('general-features', () => {
103103
const jsChunkFiles = files.filter(f => f.relname.match(/chunks\/js\/chunk-.+\.js$/))
104104
assert.ok(jsChunkFiles.length > 0, 'at least one shared JS chunk was produced with a hashed name')
105105

106+
// Verify that global.data.js output reaches template vars
107+
const feedJsonPath = path.join(dest, 'feeds/feed.json')
108+
try {
109+
const feedContent = await readFile(feedJsonPath, 'utf8')
110+
const feedData = JSON.parse(feedContent)
111+
assert.strictEqual(
112+
feedData._globalDataSentinel,
113+
'data-from-global-dot-data',
114+
'feeds template received globalDataSentinel from global.data.js via vars'
115+
)
116+
} catch (err) {
117+
const error = err instanceof Error ? err : new Error('Unknown error', { cause: err })
118+
assert.fail('Failed to verify global.data.js output in template vars: ' + error.message)
119+
}
120+
106121
// Special test for global.data.js blogPostsHtml
107122
const indexPath = path.join(dest, 'index.html')
108123
try {

test-cases/general-features/src/feeds.template.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import jsonfeedToAtom from 'jsonfeed-to-atom'
1212
* authorUrl: string,
1313
* authorImgUrl: string,
1414
* publishDate: string,
15-
* siteDescription: string
15+
* siteDescription: string,
16+
* globalDataSentinel: string,
1617
* }>}
1718
*/
1819
export default async function * feedsTemplate ({
@@ -23,6 +24,7 @@ export default async function * feedsTemplate ({
2324
authorUrl,
2425
authorImgUrl,
2526
siteDescription,
27+
globalDataSentinel,
2628
},
2729
pages,
2830
}) {
@@ -39,6 +41,7 @@ export default async function * feedsTemplate ({
3941
home_page_url: homePageUrl,
4042
feed_url: `${homePageUrl}/feed.json`,
4143
description: siteDescription,
44+
_globalDataSentinel: globalDataSentinel,
4245
author: {
4346
name: authorName,
4447
url: authorUrl,

test-cases/general-features/src/global.data.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import { html } from 'htm/preact'
66
import { render } from 'preact-render-to-string'
77

8-
/** @type {AsyncGlobalDataFunction<{ blogPostsHtml: string }>} */
8+
/** @type {AsyncGlobalDataFunction<{ blogPostsHtml: string, globalDataSentinel: string }>} */
99
export default async function ({ pages }) {
1010
const blogPosts = pages
1111
.filter(page => page.vars?.layout === 'blog' && page.vars?.publishDate)
@@ -36,5 +36,5 @@ export default async function ({ pages }) {
3636
</ul>
3737
`)
3838

39-
return { blogPostsHtml }
39+
return { blogPostsHtml, globalDataSentinel: 'data-from-global-dot-data' }
4040
}

0 commit comments

Comments
 (0)