import chokidar , { FSWatcher } from 'chokidar' ; import { createServer as createViteServer , ViteDevServer } from 'vite' ; import { renderer } from '@viterjs/renderer' ; import { resolveConfig , convertConfig } from './config' ; import { InlineConfig , ViterDevServer } from './interface' ; export const createServer = async ( inlineConfig ) : Promise < ViterDevServer > => { const config = await resolveConfig ( inlineConfig , 'serve' , 'development' ) ; const viteConfig = convertConfig ( config ) ; const { configFile } = config ; renderer ( config ) ; const viteServer = await createViteServer ( viteConfig ) ; const watcher = chokidar . watch ( configFile || [ ] , { ignoreInitial : true , ignorePermissionErrors : true , disableGlobbing : true , } ) ; const server = { config , viteServer , listen ( port ? , isRestart ? ) { return viteServer . listen ( port , isRestart ) ; } , async close ( ) { await Promise . all ( [ watcher . close ( ) , viteServer . close ( ) ] ) ; } , } ; watcher . on ( 'change' , ( ) => { restartServer ( server ) ; } ) ; return server ; } ; export async function restartServer ( server ) { let newServer = null ; try { newServer = await createServer ( server . config . inlineConfig ) ; } catch ( err ) { return ; } await server . close ( ) ; ( Object . keys ( newServer ) as Array < keyof ViteDevServer > ) . forEach ( ( key ) => { if ( key !== 'app' ) { server [ key ] = newServer [ key ] ; } } ) ; if ( ! server . viteServer . config . server . middlewareMode ) { await server . listen ( undefined , true ) ; } else { server . viteServer . config . logger . info ( 'server restarted.' , { timestamp : true } ) ; } }