import { type FastifyPluginCallback } from 'fastify' import _ from 'lodash' import fp from 'fastify-plugin' import { type Template } from '../lib/html.ts' export interface Entry { name: string path: string template: (...args: any[]) => Template routes?: ANY[] ssr?: boolean hostname?: string ctx?: Record setupRebuildCache?: (buildCache: () => any) => any preHandler?: ANY createPreHandler?: (route: ANY, entry: Entry) => any createErrorHandler?: (handler?: ANY) => ANY } export type EntryWithoutName = Omit export interface Config { mode: 'development' | 'production' createPreHandler?: (route: ANY, entry: Entry) => any createErrorHandler?: (handler?: ANY) => ANY entries: Record } export interface ParsedConfig { mode: 'development' | 'production' entries: Entry[] } const vitePlugin: FastifyPluginCallback = async function (fastify, config) { const parsedConfig = { mode: config.mode || 'production', entries: _.map(config.entries, (entry, name) => getEntryConfig(name, entry, config)) as Entry[], } const setup = (await import(`./vite/${config.mode}.ts`)).default await setup(fastify, parsedConfig) } function getEntryConfig(name: string, entry: EntryWithoutName, config: Config): Entry { return Object.assign(_.pick(config, ['createErrorHandler', 'createPreHandler', 'ssr', 'template']), entry, { name }) } export default fp(vitePlugin, { name: 'vite', fastify: '5.x', })