1- import type { Compiler , DevServer } from "@rspack/core" ;
1+ import type { Compiler } from "@rspack/core" ;
22import type { RspackDevServer as RspackDevServerType } from "@rspack/dev-server" ;
33import type { RspackCLI } from "../cli" ;
4+ import { DEFAULT_SERVER_HOT } from "../constants" ;
45import type { RspackCommand } from "../types" ;
56import {
67 type CommonOptionsForBuildAndServe ,
@@ -17,14 +18,16 @@ type ServerOptions = CommonOptionsForBuildAndServe & {
1718 host ?: string ;
1819} ;
1920
20- function normalizeHotOption ( value : unknown ) : ServerOptions [ "hot" ] {
21- if ( typeof value === "boolean" || value === "only" ) {
22- return value ;
23- }
21+ function normalizeHotOption (
22+ value : boolean | "true" | "false" | "only" | undefined
23+ ) : ServerOptions [ "hot" ] {
2424 if ( value === "false" ) {
2525 return false ;
2626 }
27- return true ;
27+ if ( value === "true" ) {
28+ return true ;
29+ }
30+ return value ;
2831}
2932
3033export class ServeCommand implements RspackCommand {
@@ -40,15 +43,15 @@ export class ServeCommand implements RspackCommand {
4043 . option ( "--port <port>" , "allows to specify a port to use" )
4144 . option ( "--host <host>" , "allows to specify a hostname to use" ) ;
4245
43- command . action ( async ( options : ServerOptions ) => {
44- setDefaultNodeEnv ( options , "development" ) ;
45- normalizeCommonOptions ( options , "serve" ) ;
46- options . hot = normalizeHotOption ( options . hot ) ;
46+ command . action ( async ( cliOptions : ServerOptions ) => {
47+ setDefaultNodeEnv ( cliOptions , "development" ) ;
48+ normalizeCommonOptions ( cliOptions , "serve" ) ;
49+ cliOptions . hot = normalizeHotOption ( cliOptions . hot ) ;
4750
4851 // Lazy import @rspack /dev-server to avoid loading it on build mode
4952 const { RspackDevServer } = await import ( "@rspack/dev-server" ) ;
5053
51- const compiler = await cli . createCompiler ( options , "serve" ) ;
54+ const compiler = await cli . createCompiler ( cliOptions , "serve" ) ;
5255 if ( ! compiler ) {
5356 return ;
5457 }
@@ -65,7 +68,7 @@ export class ServeCommand implements RspackCommand {
6568 const servers : RspackDevServerType [ ] = [ ] ;
6669
6770 /**
68- * Webpack uses an Array of compilerForDevServer,
71+ * webpack uses an Array of compilerForDevServer,
6972 * however according to it's doc https://webpack.js.org/configuration/dev-server/#devserverhot
7073 * It should use only the first one
7174 *
@@ -79,7 +82,9 @@ export class ServeCommand implements RspackCommand {
7982 */
8083 for ( const compiler of compilers ) {
8184 const devServer = ( compiler . options . devServer ??= { } ) ;
82- devServer . hot = options . hot ?? devServer . hot ?? true ;
85+
86+ devServer . hot = cliOptions . hot ?? devServer . hot ?? DEFAULT_SERVER_HOT ;
87+
8388 if ( devServer . client !== false ) {
8489 if ( devServer . client === true || devServer . client == null ) {
8590 devServer . client = { } ;
@@ -94,12 +99,13 @@ export class ServeCommand implements RspackCommand {
9499 }
95100 }
96101
97- const result = ( compilerForDevServer . options . devServer ??= { } ) ;
98- const { setupMiddlewares } = result ;
102+ const devServerOptions = ( compilerForDevServer . options . devServer ??= { } ) ;
103+ const { setupMiddlewares } = devServerOptions ;
99104
100105 const lazyCompileMiddleware =
101106 rspack . experiments . lazyCompilationMiddleware ( compiler ) ;
102- result . setupMiddlewares = ( middlewares , server ) => {
107+
108+ devServerOptions . setupMiddlewares = ( middlewares , server ) => {
103109 let finalMiddlewares = middlewares ;
104110 if ( setupMiddlewares ) {
105111 finalMiddlewares = setupMiddlewares ( finalMiddlewares , server ) ;
@@ -110,23 +116,26 @@ export class ServeCommand implements RspackCommand {
110116 /**
111117 * Enable this to tell Rspack that we need to enable React Refresh by default
112118 */
113- result . hot = options . hot ?? result . hot ?? true ;
114- result . host = options . host || result . host ;
115- result . port = options . port ?? result . port ;
116- if ( result . client !== false ) {
117- if ( result . client === true || result . client == null ) {
118- result . client = { } ;
119+ devServerOptions . hot =
120+ cliOptions . hot ?? devServerOptions . hot ?? DEFAULT_SERVER_HOT ;
121+ devServerOptions . host = cliOptions . host || devServerOptions . host ;
122+ devServerOptions . port = cliOptions . port ?? devServerOptions . port ;
123+ if ( devServerOptions . client !== false ) {
124+ if (
125+ devServerOptions . client === true ||
126+ devServerOptions . client == null
127+ ) {
128+ devServerOptions . client = { } ;
119129 }
120- result . client = {
130+ devServerOptions . client = {
121131 overlay : {
122132 errors : true ,
123133 warnings : false
124134 } ,
125- ...result . client
135+ ...devServerOptions . client
126136 } ;
127137 }
128138
129- const devServerOptions = result as DevServer ;
130139 if ( devServerOptions . port ) {
131140 const portNumber = Number ( devServerOptions . port ) ;
132141
@@ -143,9 +152,7 @@ export class ServeCommand implements RspackCommand {
143152
144153 try {
145154 const server = new RspackDevServer ( devServerOptions , compiler ) ;
146-
147155 await server . start ( ) ;
148-
149156 servers . push ( server ) ;
150157 } catch ( error ) {
151158 const logger = cli . getLogger ( ) ;
0 commit comments