@@ -17,11 +17,11 @@ import pretty from 'pretty'
1717 * Resolves a layout from an ESM module.
1818 *
1919 * @function
20- * @template T - The type of variables for the layout
20+ * @template {Record<string, any>} T - The type of variables for the layout
2121 * @template [U=any] U - The return type of the page function (defaults to any)
2222 * @template [V=string] V - The return type of the layout function (defaults to string)
2323 * @param {string } layoutPath - The string path to the layout ESM module.
24- * @returns {Promise<LayoutFunction <T, U, V>> } The resolved layout exported as default from the module.
24+ * @returns {Promise<InternalLayoutFunction <T, U, V>> } The resolved layout exported as default from the module.
2525 */
2626export async function resolveLayout ( layoutPath ) {
2727 const { default : layout } = await import ( layoutPath )
@@ -30,40 +30,98 @@ export async function resolveLayout (layoutPath) {
3030}
3131
3232/**
33- * Callback for rendering a layout.
33+ * Common parameters for layout functions .
3434 *
35- * @template T - The type of variables passed to the layout function
35+ * @template {Record<string, any>} T - The type of variables passed to the layout function
36+ * @template [U=any] U - The return type of the page function (defaults to any)
37+ * @template [V=string] V - The return type of the layout function (defaults to string)
38+ * @typedef {object } LayoutFunctionParams
39+ * @property {T } vars - All default, global, layout, page, and builder vars shallow merged.
40+ * @property {string[] } [scripts] - Array of script URLs to include.
41+ * @property {string[] } [styles] - Array of stylesheet URLs to include.
42+ * @property {U } children - The children content, either as a string or a render function.
43+ * @property {PageInfo } page - Info about the current page
44+ * @property {PageData<T, U, V>[] } pages - An array of info about every page
45+ * @property {Object<string, string> } [workers] - Map of worker names to their output paths
46+ */
47+
48+ /**
49+ * Synchronous callback for rendering a layout.
50+ *
51+ * @template {Record<string, any>} T - The type of variables passed to the layout function
3652 * @template [U=any] U - The return type of the page function (defaults to any)
3753 * @template [V=string] V - The return type of the layout function (defaults to string)
3854 * @callback LayoutFunction
39- * @param {object } params - The parameters for the layout.
40- * @param {T } params.vars - All default, global, layout, page, and builder vars shallow merged.
41- * @param {string[] } [params.scripts] - Array of script URLs to include.
42- * @param {string[] } [params.styles] - Array of stylesheet URLs to include.
43- * @param {U } params.children - The children content, either as a string or a render function.
44- * @param {PageInfo } params.page - Info about the current page
45- * @param {PageData<T, U, V>[] } params.pages - An array of info about every page
46- * @param {Object<string, string> } [params.workers] - Map of worker names to their output paths
47- * @returns {Promise<V> | V } The rendered content.
55+ * @param {LayoutFunctionParams<T, U, V> } params - The parameters for the layout.
56+ * @returns {V | Promise<V> } The rendered content.
57+ */
58+
59+ /**
60+ * Asynchronous callback for rendering a layout.
61+ *
62+ * @template {Record<string, any>} T - The type of variables passed to the layout function
63+ * @template [U=any] U - The return type of the page function (defaults to any)
64+ * @template [V=string] V - The return type of the layout function (defaults to string)
65+ * @callback AsyncLayoutFunction
66+ * @param {LayoutFunctionParams<T, U, V> } params - The parameters for the layout.
67+ * @returns {Promise<V> } The rendered content.
68+ */
69+
70+ /**
71+ * Callback for rendering a layout (can be sync or async).
72+ *
73+ * @template {Record<string, any>} T - The type of variables passed to the layout function
74+ * @template [U=any] U - The return type of the page function (defaults to any)
75+ * @template [V=string] V - The return type of the layout function (defaults to string)
76+ * @typedef {LayoutFunction<T, U, V> | AsyncLayoutFunction<T, U, V> } InternalLayoutFunction
4877 */
4978
5079/**
51- * postVars functions Can be used to generate page vars but access all page data
80+ * Common parameters for postVars functions.
81+ *
82+ * @template {Record<string, any>} T - The type of variables for the page
83+ * @template [U=any] U - The return type of the page function (defaults to any)
84+ * @template [V=string] V - The return type of the layout function (defaults to string)
85+ * @typedef {object } PostVarsFunctionParams
86+ * @property {T } vars - All default, global, layout, page, and builder vars shallow merged.
87+ * @property {string[] } [scripts] - Array of script URLs to include.
88+ * @property {string[] } [styles] - Array of stylesheet URLs to include.
89+ * @property {PageInfo } page - Info about the current page
90+ * @property {PageData<T, U, V>[] } pages - An array of info about every page
91+ * @property {Object<string, string> } [workers] - Map of worker names to their output paths
92+ */
93+
94+ /**
95+ * Synchronous postVars function to generate page vars with access to all page data.
5296 *
5397 * @template {Record<string, any>} T - The type of variables for the page
5498 * @template [U=any] U - The return type of the page function (defaults to any)
5599 * @template [V=string] V - The return type of the layout function (defaults to string)
56100 * @callback PostVarsFunction
57- * @param {object } params - The parameters for the pageLayout.
58- * @param {T } params.vars - All default, global, layout, page, and builder vars shallow merged.
59- * @param {string[] } [params.scripts] - Array of script URLs to include.
60- * @param {string[] } [params.styles] - Array of stylesheet URLs to include.
61- * @param {PageInfo } params.page - Info about the current page
62- * @param {PageData<T, U, V>[] } params.pages - An array of info about every page
63- * @param {Object<string, string> } [params.workers] - Map of worker names to their output paths
101+ * @param {PostVarsFunctionParams<T, U, V> } params - The parameters for the postVars function.
102+ * @returns {T | Promise<T> } The rendered postVars
103+ */
104+
105+ /**
106+ * Asynchronous postVars function to generate page vars with access to all page data.
107+ *
108+ * @template {Record<string, any>} T - The type of variables for the page
109+ * @template [U=any] U - The return type of the page function (defaults to any)
110+ * @template [V=string] V - The return type of the layout function (defaults to string)
111+ * @callback AsyncPostVarsFunction
112+ * @param {PostVarsFunctionParams<T, U, V> } params - The parameters for the postVars function.
64113 * @returns {Promise<T> } The rendered postVars
65114 */
66115
116+ /**
117+ * postVars functions can be used to generate page vars but access all page data (can be sync or async).
118+ *
119+ * @template {Record<string, any>} T - The type of variables for the page
120+ * @template [U=any] U - The return type of the page function (defaults to any)
121+ * @template [V=string] V - The return type of the layout function (defaults to string)
122+ * @typedef {PostVarsFunction<T, U, V> | AsyncPostVarsFunction<T, U, V> } InternalPostVarsFunction
123+ */
124+
67125/**
68126 * Represents the data for a page.
69127 * @template {Record<string, any>} T - The type of variables for the page data
@@ -79,7 +137,7 @@ export class PageData {
79137 /** @type {object? } */ builderVars = null
80138 /** @type {string[] } */ styles = [ ]
81139 /** @type {string[] } */ scripts = [ ]
82- /** @type {WorkerFiles|undefined } */ workerFiles = undefined
140+ /** @type {WorkerFiles } */ workerFiles = { }
83141 /** @type {boolean } */ #initialized = false
84142 /** @type {T? } */ #renderedPostVars = null
85143 /** @type {string? } */ #defaultStyle = null
@@ -138,14 +196,14 @@ export class PageData {
138196
139197 /**
140198 * Access web worker file paths associated with this page
141- * @return {WorkerFiles|undefined } Map of worker names to their output paths
199+ * @return {WorkerFiles } Map of worker names to their output paths
142200 */
143201 get workers ( ) {
144202 return this . workerFiles
145203 }
146204
147205 /**
148- * @type {PostVarsFunction <T, U, V> }
206+ * @type {AsyncPostVarsFunction <T, U, V> }
149207 */
150208 async #renderPostVars ( { vars, styles, scripts, pages, page, workers } ) {
151209 if ( ! this . #initialized) throw new Error ( 'Initialize PageData before accessing renderPostVars' )
@@ -218,7 +276,6 @@ export class PageData {
218276 // Initialize web workers if they exist
219277 if ( pageInfo . workers ) {
220278 /** @type {WorkerFiles } */
221- this . workerFiles = { }
222279 for ( const [ workerName , workerFile ] of Object . entries ( pageInfo . workers ) ) {
223280 if ( workerFile . outputRelname ) {
224281 this . workerFiles [ workerName ] = `./${ workerFile . outputName } `
0 commit comments