import { serveStatic } from "@hono/node-server/serve-static"; import { type DevServerOptions, default as honoViteDevServer } from "@hono/vite-dev-server"; import type { App } from "bknd"; import { type RuntimeBkndConfig, createRuntimeApp, type FrameworkOptions } from "bknd/adapter"; import { registerLocalMediaAdapter } from "bknd/adapter/node"; import { devServerConfig } from "./dev-server-config"; import type { MiddlewareHandler } from "hono"; export type ViteEnv = NodeJS.ProcessEnv; export type ViteBkndConfig = RuntimeBkndConfig & { serveStatic?: false | MiddlewareHandler; }; export function addViteScript(html: string, addBkndContext: boolean = true) { return html.replace( "", ` ${addBkndContext ? "" : ""} `, ); } async function createApp( config: ViteBkndConfig = {}, env: ViteEnv = {} as ViteEnv, opts: FrameworkOptions = {}, ): Promise { registerLocalMediaAdapter(); return await createRuntimeApp( { ...config, adminOptions: config.adminOptions ?? { forceDev: { mainPath: "/src/main.tsx", }, }, serveStatic: config.serveStatic || [ "/assets/*", serveStatic({ root: config.distPath ?? "./" }), ], }, env, opts, ); } export function serve( config: ViteBkndConfig = {}, args?: ViteEnv, opts?: FrameworkOptions, ) { return { async fetch(request: Request, env: any, ctx: ExecutionContext) { const app = await createApp(config, env, opts); return app.fetch(request, env, ctx); }, }; } export function devServer(options: DevServerOptions) { return honoViteDevServer({ ...devServerConfig, ...options, }); }