@@ -8,9 +8,11 @@ import { logger } from '@modern-js/utils';
88import { devPlugin , manager } from './dev' ;
99import { getDevAssetPrefix , getDevOptions } from './helpers' ;
1010import { ResourceType } from './helpers/utils' ;
11- import serverHmrPlugin from './plugins/serverHmr ' ;
11+ import serverHmrPlugin from './plugins/serverReload ' ;
1212import type { ApplyPlugins , ModernDevServerOptions } from './types' ;
1313
14+ export let serverReload : ( ( ) => Promise < void > ) | null = null ;
15+
1416async function createServerOptions (
1517 options : ModernDevServerOptions ,
1618 serverConfigPath : string ,
@@ -44,8 +46,8 @@ export async function createDevServer(
4446 distDir ,
4547 ) ;
4648
47- const server = createServerBase ( prodServerOptions ) ;
48- let currentServer = server ;
49+ let currentServer = createServerBase ( prodServerOptions ) ;
50+
4951 let isReloading = false ;
5052
5153 const devHttpsOption = typeof dev === 'object' && dev . https ;
@@ -75,8 +77,10 @@ export async function createDevServer(
7577 if ( isReloading ) {
7678 return ;
7779 }
80+ isReloading = true ;
81+
7882 try {
79- isReloading = true ;
83+ await currentServer . close ( ) ;
8084
8185 const updatedProdServerOptions = await createServerOptions (
8286 options ,
@@ -88,25 +92,30 @@ export async function createDevServer(
8892 await manager . close ( ResourceType . Watcher ) ;
8993
9094 newServer . addPlugins ( [
91- serverHmrPlugin ( reload ) ,
92- devPlugin ( {
93- ...options ,
94- } ) ,
95+ serverHmrPlugin ( ) ,
96+ devPlugin (
97+ {
98+ ...options ,
99+ builderDevServer : undefined ,
100+ } ,
101+ true ,
102+ ) ,
95103 ] ) ;
96- await applyPlugins ( newServer , updatedProdServerOptions , nodeServer ) ;
104+ await applyPlugins ( newServer , updatedProdServerOptions ) ;
97105 await newServer . init ( ) ;
98106
99107 currentServer = newServer ;
108+
100109 logger . info ( `Custom Web Server HMR succeeded` ) ;
101110 } catch ( e ) {
102111 logger . error ( '[Custom Web Server HMR failed]:' , e ) ;
103112 } finally {
104113 isReloading = false ;
105114 }
106115 } ;
107-
108- server . addPlugins ( [
109- serverHmrPlugin ( reload ) ,
116+ serverReload = reload ;
117+ currentServer . addPlugins ( [
118+ serverHmrPlugin ( ) ,
110119 devPlugin ( {
111120 ...options ,
112121 builderDevServer,
@@ -119,9 +128,9 @@ export async function createDevServer(
119128 prodServerOptions . config . output . assetPrefix = assetPrefix ;
120129 }
121130
122- await applyPlugins ( server , prodServerOptions , nodeServer ) ;
131+ await applyPlugins ( currentServer , prodServerOptions , nodeServer ) ;
123132
124- await server . init ( ) ;
133+ await currentServer . init ( ) ;
125134
126135 const afterListen = async ( ) => {
127136 await builderDevServer ?. afterListen ( ) ;
0 commit comments