@@ -8,9 +8,11 @@ import { logger } from '@modern-js/utils';
8
8
import { devPlugin , manager } from './dev' ;
9
9
import { getDevAssetPrefix , getDevOptions } from './helpers' ;
10
10
import { ResourceType } from './helpers/utils' ;
11
- import serverHmrPlugin from './plugins/serverHmr ' ;
11
+ import serverHmrPlugin from './plugins/serverReload ' ;
12
12
import type { ApplyPlugins , ModernDevServerOptions } from './types' ;
13
13
14
+ export let serverReload : ( ( ) => Promise < void > ) | null = null ;
15
+
14
16
async function createServerOptions (
15
17
options : ModernDevServerOptions ,
16
18
serverConfigPath : string ,
@@ -44,8 +46,8 @@ export async function createDevServer(
44
46
distDir ,
45
47
) ;
46
48
47
- const server = createServerBase ( prodServerOptions ) ;
48
- let currentServer = server ;
49
+ let currentServer = createServerBase ( prodServerOptions ) ;
50
+
49
51
let isReloading = false ;
50
52
51
53
const devHttpsOption = typeof dev === 'object' && dev . https ;
@@ -75,8 +77,10 @@ export async function createDevServer(
75
77
if ( isReloading ) {
76
78
return ;
77
79
}
80
+ isReloading = true ;
81
+
78
82
try {
79
- isReloading = true ;
83
+ await currentServer . close ( ) ;
80
84
81
85
const updatedProdServerOptions = await createServerOptions (
82
86
options ,
@@ -88,25 +92,30 @@ export async function createDevServer(
88
92
await manager . close ( ResourceType . Watcher ) ;
89
93
90
94
newServer . addPlugins ( [
91
- serverHmrPlugin ( reload ) ,
92
- devPlugin ( {
93
- ...options ,
94
- } ) ,
95
+ serverHmrPlugin ( ) ,
96
+ devPlugin (
97
+ {
98
+ ...options ,
99
+ builderDevServer : undefined ,
100
+ } ,
101
+ true ,
102
+ ) ,
95
103
] ) ;
96
- await applyPlugins ( newServer , updatedProdServerOptions , nodeServer ) ;
104
+ await applyPlugins ( newServer , updatedProdServerOptions ) ;
97
105
await newServer . init ( ) ;
98
106
99
107
currentServer = newServer ;
108
+
100
109
logger . info ( `Custom Web Server HMR succeeded` ) ;
101
110
} catch ( e ) {
102
111
logger . error ( '[Custom Web Server HMR failed]:' , e ) ;
103
112
} finally {
104
113
isReloading = false ;
105
114
}
106
115
} ;
107
-
108
- server . addPlugins ( [
109
- serverHmrPlugin ( reload ) ,
116
+ serverReload = reload ;
117
+ currentServer . addPlugins ( [
118
+ serverHmrPlugin ( ) ,
110
119
devPlugin ( {
111
120
...options ,
112
121
builderDevServer,
@@ -119,9 +128,9 @@ export async function createDevServer(
119
128
prodServerOptions . config . output . assetPrefix = assetPrefix ;
120
129
}
121
130
122
- await applyPlugins ( server , prodServerOptions , nodeServer ) ;
131
+ await applyPlugins ( currentServer , prodServerOptions , nodeServer ) ;
123
132
124
- await server . init ( ) ;
133
+ await currentServer . init ( ) ;
125
134
126
135
const afterListen = async ( ) => {
127
136
await builderDevServer ?. afterListen ( ) ;
0 commit comments