diff --git a/app/build.ts b/app/build.ts index 6511124..e982777 100644 --- a/app/build.ts +++ b/app/build.ts @@ -166,7 +166,7 @@ function baseConfig(adapter: string): tsup.Options { minify, sourcemap, watch, - entry: [`src/adapter/${adapter}`], + entry: [`src/adapter/${adapter}/index.ts`], format: ["esm"], platform: "neutral", outDir: `dist/adapter/${adapter}`, diff --git a/app/src/adapter/vite/vite.adapter.ts b/app/src/adapter/vite/vite.adapter.ts index e94ece6..2f6b3e9 100644 --- a/app/src/adapter/vite/vite.adapter.ts +++ b/app/src/adapter/vite/vite.adapter.ts @@ -1,10 +1,11 @@ import { serveStatic } from "@hono/node-server/serve-static"; +import { type DevServerOptions, default as honoViteDevServer } from "@hono/vite-dev-server"; import { type RuntimeBkndConfig, createRuntimeApp } from "adapter"; import type { App } from "bknd"; export type ViteBkndConfig = RuntimeBkndConfig & { setAdminHtml?: boolean; - forceDev?: boolean; + forceDev?: boolean | { mainPath: string }; html?: string; }; @@ -24,20 +25,26 @@ ${addBkndContext ? "" : ""} ); } -async function createApp(config: ViteBkndConfig, env?: any) { +async function createApp(config: ViteBkndConfig = {}, env?: any) { return await createRuntimeApp( { ...config, - adminOptions: config.setAdminHtml - ? { html: config.html, forceDev: config.forceDev } - : undefined, + adminOptions: + config.setAdminHtml === false + ? undefined + : { + html: config.html, + forceDev: config.forceDev ?? { + mainPath: "/src/main.tsx" + } + }, serveStatic: ["/assets/*", serveStatic({ root: config.distPath ?? "./" })] }, env ); } -export async function serveFresh(config: ViteBkndConfig) { +export function serveFresh(config: ViteBkndConfig = {}) { return { async fetch(request: Request, env: any, ctx: ExecutionContext) { const app = await createApp(config, env); @@ -47,7 +54,7 @@ export async function serveFresh(config: ViteBkndConfig) { } let app: App; -export async function serveCached(config: ViteBkndConfig) { +export function serveCached(config: ViteBkndConfig = {}) { return { async fetch(request: Request, env: any, ctx: ExecutionContext) { if (!app) { @@ -58,3 +65,21 @@ export async function serveCached(config: ViteBkndConfig) { } }; } + +export function devServer(options: DevServerOptions) { + return honoViteDevServer({ + entry: "./server.ts", + exclude: [ + /.*\.tsx?($|\?)/, + /^(?!.*\/__admin).*\.(s?css|less)($|\?)/, + // exclude except /api + /^(?!.*\/api).*\.(ico|mp4|jpg|jpeg|svg|png|vtt|mp3|js)($|\?)/, + /^\/@.+$/, + /\/components.*?\.json.*/, // @todo: improve + /^\/(public|assets|static)\/.+/, + /^\/node_modules\/.*/ + ], + injectClientScript: false, + ...options + }); +}