@@ -6,26 +6,56 @@ import { serveStatic } from '@hono/node-server/serve-static'
66import { Hono } from 'hono'
77import { cors } from 'hono/cors'
88import { marked } from 'marked'
9+ import type { ViteDevServer } from 'vite'
910
10- import * as accounts from '~/api/accounts'
11- import * as admin from '~/api/admin'
12- import * as ark from '~/api/ark'
11+ import * as _accounts from '~/api/accounts'
12+ import * as _admin from '~/api/admin'
13+ import * as _ark from '~/api/ark'
1314import { arkMiddleware } from '~/api/ark-middleware.server'
1415import type { AuthEnv } from '~/api/auth.server'
1516import { authMiddleware , requireAuth } from '~/api/auth.server'
16- import * as collections from '~/api/collections'
17- import * as files from '~/api/files'
18- import * as health from '~/api/health'
19- import * as kfAuth from '~/api/kf-auth'
20- import * as kfSummary from '~/api/kf-summary'
21- import * as query from '~/api/query'
22- import * as schemas from '~/api/schemas'
23- import * as uploads from '~/api/uploads'
24- import * as versions from '~/api/versions'
17+ import * as _collections from '~/api/collections'
18+ import * as _files from '~/api/files'
19+ import * as _health from '~/api/health'
20+ import * as _kfAuth from '~/api/kf-auth'
21+ import * as _kfSummary from '~/api/kf-summary'
22+ import * as _query from '~/api/query'
23+ import * as _schemas from '~/api/schemas'
24+ import * as _uploads from '~/api/uploads'
25+ import * as _versions from '~/api/versions'
2526import { getMirrorConfig } from '~/lib/mirror-config'
2627import { initOidc } from '~/lib/oidc.server'
2728
2829const isProd = process . env . NODE_ENV === 'production'
30+ let vite : ViteDevServer | undefined
31+
32+ // In dev, proxy API modules through Vite's SSR loader for hot reload
33+ function hot < T extends Record < string , any > > ( staticMod : T , modulePath : string ) : T {
34+ if ( isProd ) return staticMod
35+ return new Proxy ( staticMod as object , {
36+ get ( _ , prop ) {
37+ if ( typeof prop === 'symbol' ) return undefined
38+ return async ( ...args : unknown [ ] ) => {
39+ const mod = await vite ! . ssrLoadModule ( modulePath )
40+ return ( mod [ prop as string ] as Function ) ( ...args )
41+ }
42+ } ,
43+ } ) as T
44+ }
45+
46+ const accounts = hot ( _accounts , '/src/api/accounts.ts' )
47+ const admin = hot ( _admin , '/src/api/admin.ts' )
48+ const ark = hot ( _ark , '/src/api/ark.ts' )
49+ const collections = hot ( _collections , '/src/api/collections.ts' )
50+ const files = hot ( _files , '/src/api/files.ts' )
51+ const health = hot ( _health , '/src/api/health.ts' )
52+ const kfAuth = hot ( _kfAuth , '/src/api/kf-auth.ts' )
53+ const kfSummary = hot ( _kfSummary , '/src/api/kf-summary.ts' )
54+ const query = hot ( _query , '/src/api/query.ts' )
55+ const schemas = hot ( _schemas , '/src/api/schemas.ts' )
56+ const uploads = hot ( _uploads , '/src/api/uploads.ts' )
57+ const versions = hot ( _versions , '/src/api/versions.ts' )
58+
2959const app = new Hono < AuthEnv > ( )
3060
3161// --- CORS ---
@@ -256,7 +286,7 @@ if (isProd) {
256286 } )
257287} else {
258288 const { createServer : createViteServer } = await import ( 'vite' )
259- const vite = await createViteServer ( {
289+ vite = await createViteServer ( {
260290 server : { middlewareMode : true } ,
261291 appType : 'custom' ,
262292 } )
@@ -267,16 +297,16 @@ if (isProd) {
267297 const nodeRes = ( c . env as any ) . outgoing
268298 if ( ! nodeReq || ! nodeRes ) return next ( )
269299 return new Promise < Response | void > ( ( resolve ) => {
270- vite . middlewares ( nodeReq , nodeRes , ( ) => resolve ( next ( ) ) )
300+ vite ! . middlewares ( nodeReq , nodeRes , ( ) => resolve ( next ( ) ) )
271301 } )
272302 } )
273303
274304 app . get ( '*' , async ( c ) => {
275305 const url = c . req . url
276306 let template = readFileSync ( resolve ( 'index.html' ) , 'utf-8' )
277- template = await vite . transformIndexHtml ( url , template )
307+ template = await vite ! . transformIndexHtml ( url , template )
278308
279- const { render } = await vite . ssrLoadModule ( '/src/entry-server.tsx' )
309+ const { render } = await vite ! . ssrLoadModule ( '/src/entry-server.tsx' )
280310 const { html, ssrData, redirect, statusCode, title, description } = await render ( c . req . raw )
281311
282312 if ( redirect ) {
0 commit comments