diff --git a/index.js b/index.js index 3a062db..aa9d6f4 100644 --- a/index.js +++ b/index.js @@ -2,12 +2,13 @@ * @import { DomStackOpts as DomStackOpts, Results, SiteData } from './lib/builder.js' * @import { Stats } from 'node:fs' * @import { FSWatcher } from 'chokidar' - * @import { AsyncLayoutFunction, LayoutFunction } from './lib/build-pages/page-data.js' - * @import { PageFunction, AsyncPageFunction } from './lib/build-pages/page-builders/page-writer.js' + * @import { AsyncLayoutFunction, LayoutFunction, LayoutFunctionParams } from './lib/build-pages/page-data.js' + * @import { PageFunction, AsyncPageFunction, PageFunctionParams } from './lib/build-pages/page-builders/page-writer.js' * @import { TemplateFunction } from './lib/build-pages/page-builders/template-builder.js' * @import { TemplateAsyncIterator } from './lib/build-pages/page-builders/template-builder.js' * @import { TemplateOutputOverride } from './lib/build-pages/page-builders/template-builder.js' - * @import { GlobalDataFunction, AsyncGlobalDataFunction, WorkerBuildStepResult } from './lib/build-pages/index.js' + * @import { TemplateFunctionParams } from './lib/build-pages/page-builders/template-builder.js' + * @import { GlobalDataFunction, AsyncGlobalDataFunction, WorkerBuildStepResult, GlobalDataFunctionParams } from './lib/build-pages/index.js' * @import { BuildOptions, BuildContext } from 'esbuild' * @import { PageInfo, TemplateInfo } from './lib/identify-pages.js' */ @@ -49,6 +50,8 @@ import { resolveVars } from './lib/build-pages/resolve-vars.js' import { ensureDest } from './lib/helpers/ensure-dest.js' import { DomStackAggregateError } from './lib/helpers/domstack-aggregate-error.js' +export { PageData } from './lib/build-pages/page-data.js' + /** * @typedef {BuildOptions} BuildOptions */ @@ -103,6 +106,36 @@ import { DomStackAggregateError } from './lib/helpers/domstack-aggregate-error.j * @typedef {TemplateOutputOverride} TemplateOutputOverride */ +/** + * @typedef {PageInfo} PageInfo + */ + +/** + * @typedef {TemplateInfo} TemplateInfo + */ + +/** + * @template {Record} T - The type of variables passed to the layout function + * @template [U=any] U - The return type of the page function + * @template [V=string] V - The return type of the layout function + * @typedef {LayoutFunctionParams} LayoutFunctionParams + */ + +/** + * @typedef {GlobalDataFunctionParams} GlobalDataFunctionParams + */ + +/** + * @template {Record} T - The type of variables passed to the page function + * @template [U=any] U - The return type of the page function + * @typedef {PageFunctionParams} PageFunctionParams + */ + +/** + * @template {Record} [T=Record] - The type of variables for the template function + * @typedef {TemplateFunctionParams} TemplateFunctionParams + */ + const DEFAULT_IGNORES = /** @type {const} */ ([ '.*', 'coverage', diff --git a/lib/build-pages/page-builders/template-builder.js b/lib/build-pages/page-builders/template-builder.js index cdd4bf4..d055aa8 100644 --- a/lib/build-pages/page-builders/template-builder.js +++ b/lib/build-pages/page-builders/template-builder.js @@ -11,6 +11,16 @@ import { writeFile, mkdir } from 'fs/promises' * content: string * }} TemplateOutputOverride */ +/** + * The parameters object passed to a {@link TemplateFunction} or {@link TemplateAsyncIterator}. + * + * @template {Record} [T=Record] - The type of variables for the template + * @typedef {object} TemplateFunctionParams + * @property {T} vars - All of the site globalVars. + * @property {TemplateInfo} template - Info about the current template. + * @property {PageData[]} pages - An array of info about every page. + */ + /** * Callback for rendering a template. * @@ -24,16 +34,11 @@ import { writeFile, mkdir } from 'fs/promises' * } - The results of a template build */ -/** - * @template {Record} T - The type of variables for the template function parameters - * @typedef {Parameters>} TemplateFunctionParams -*/ - /** * Callback for rendering a template with an async iterator. * @template T - The type of variables for the template async iterator * @callback TemplateAsyncIterator - * @param {TemplateFunctionParams[0]} params - Parameters of the template function. + * @param {TemplateFunctionParams} params - Parameters of the template function. * @returns {AsyncIterable} */ diff --git a/test-cases/type-exports/index.test.js b/test-cases/type-exports/index.test.js new file mode 100644 index 0000000..ac0ab5d --- /dev/null +++ b/test-cases/type-exports/index.test.js @@ -0,0 +1,20 @@ +// @ts-check +import { test } from 'node:test' +import assert from 'node:assert' +import { PageData } from '../../index.js' + +/** + * Smoke test that all public types are importable from the package entry point. + * The type imports below are verified by TypeScript at compile time via `npm run test:tsc`. + * + * @typedef {import('../../index.js').PageInfo} PageInfo + * @typedef {import('../../index.js').TemplateInfo} TemplateInfo + * @typedef {import('../../index.js').LayoutFunctionParams} LayoutFunctionParams + * @typedef {import('../../index.js').GlobalDataFunctionParams} GlobalDataFunctionParams + * @typedef {import('../../index.js').PageFunctionParams} PageFunctionParams + * @typedef {import('../../index.js').TemplateFunctionParams} TemplateFunctionParams + */ + +test('PageData is importable from the package entry point', () => { + assert.strictEqual(typeof PageData, 'function', 'PageData is a class') +})