@@ -2221,10 +2221,12 @@ class WebpackCLI {
22212221 return ;
22222222 }
22232223
2224- const DevServer : DevServerConstructor = cmd . context . devServer ;
2224+ const DevServer : DevServerConstructor = devServer ;
2225+ const isDevServerPlugin =
2226+ typeof ( DevServer . prototype as { apply ?: unknown } ) . apply === "function" ;
22252227 const servers : InstanceType < DevServerConstructor > [ ] = [ ] ;
22262228
2227- if ( this . #needWatchStdin( compiler ) ) {
2229+ if ( ! isDevServerPlugin && this . #needWatchStdin( compiler ) ) {
22282230 process . stdin . on ( "end" , ( ) => {
22292231 Promise . all ( servers . map ( ( server ) => server . stop ( ) ) ) . then ( ( ) => {
22302232 process . exit ( 0 ) ;
@@ -2240,6 +2242,7 @@ class WebpackCLI {
22402242 const usedPorts : number [ ] = [ ] ;
22412243 // @ts -expect-error different versions of the `Schema` type
22422244 const devServerArgs = this . #getArguments( webpack , devServer . schema ) ;
2245+ let appliedDevServers = 0 ;
22432246
22442247 for ( const compilerForDevServer of compilersForDevServer ) {
22452248 if ( compilerForDevServer . options . devServer === false ) {
@@ -2283,11 +2286,23 @@ class WebpackCLI {
22832286 }
22842287
22852288 try {
2286- const server = new DevServer ( devServerConfiguration , compiler ) ;
2289+ if ( isDevServerPlugin ) {
2290+ // v5 typings lack the plugin constructor.
2291+ const DevServerPlugin = DevServer as unknown as new (
2292+ options : DevServerConfiguration ,
2293+ ) => { apply ( compiler : Compiler | MultiCompiler ) : void } ;
2294+
2295+ // Serve all child compilers, regardless of which defines devServer.
2296+ new DevServerPlugin ( devServerConfiguration ) . apply ( compiler ) ;
2297+ } else {
2298+ const server = new DevServer ( devServerConfiguration , compiler ) ;
22872299
2288- await server . start ( ) ;
2300+ await server . start ( ) ;
2301+
2302+ servers . push ( server as unknown as InstanceType < DevServerConstructor > ) ;
2303+ }
22892304
2290- servers . push ( server as unknown as InstanceType < DevServerConstructor > ) ;
2305+ appliedDevServers += 1 ;
22912306 } catch ( error ) {
22922307 if ( this . isValidationError ( error as Error ) ) {
22932308 this . logger . error ( ( error as Error ) . message ) ;
@@ -2299,10 +2314,69 @@ class WebpackCLI {
22992314 }
23002315 }
23012316
2302- if ( servers . length === 0 ) {
2317+ if ( appliedDevServers === 0 ) {
23032318 this . logger . error ( "No dev server configurations to run" ) ;
23042319 process . exit ( 2 ) ;
23052320 }
2321+
2322+ // Older servers manage compilation and signals themselves.
2323+ if ( ! isDevServerPlugin ) {
2324+ return ;
2325+ }
2326+
2327+ // Closing the compiler stops the server through its shutdown hook.
2328+ this . #setupGracefulShutdown( compiler ) ;
2329+
2330+ if ( this . #needWatchStdin( compiler ) ) {
2331+ process . stdin . on ( "end" , ( ) => {
2332+ compiler . close ( ( ) => {
2333+ process . exit ( 0 ) ;
2334+ } ) ;
2335+ } ) ;
2336+ process . stdin . resume ( ) ;
2337+ }
2338+
2339+ const watchCallback = ( error : Error | null , stats ?: Stats | MultiStats ) : void => {
2340+ if ( error ) {
2341+ if ( this . isValidationError ( error ) ) {
2342+ this . logger . error ( error . message ) ;
2343+ } else {
2344+ this . logger . error ( error ) ;
2345+ }
2346+
2347+ process . exit ( 2 ) ;
2348+ }
2349+
2350+ if ( ! stats ) {
2351+ return ;
2352+ }
2353+
2354+ if ( stats . hasErrors ( ) || ( options . failOnWarnings && stats . hasWarnings ( ) ) ) {
2355+ process . exitCode = 1 ;
2356+ }
2357+
2358+ // Plugin mode leaves stats output to the CLI.
2359+ const statsOptions = this . isMultipleCompiler ( compiler )
2360+ ? {
2361+ children : compiler . compilers . map ( ( compiler ) => compiler . options . stats ) ,
2362+ }
2363+ : compiler . options . stats ;
2364+
2365+ const printedStats = stats . toString ( statsOptions as StatsOptions ) ;
2366+
2367+ if ( printedStats ) {
2368+ this . logger . raw ( printedStats ) ;
2369+ }
2370+ } ;
2371+
2372+ if ( this . isMultipleCompiler ( compiler ) ) {
2373+ compiler . watch (
2374+ compiler . compilers . map ( ( compiler ) => compiler . options . watchOptions || { } ) ,
2375+ watchCallback ,
2376+ ) ;
2377+ } else {
2378+ compiler . watch ( compiler . options . watchOptions || { } , watchCallback ) ;
2379+ }
23062380 } ,
23072381 } ,
23082382 help : {
@@ -3697,6 +3771,35 @@ class WebpackCLI {
36973771 return Boolean ( compiler . options . watchOptions ?. stdin ) ;
36983772 }
36993773
3774+ #setupGracefulShutdown( compiler : Compiler | MultiCompiler ) : void {
3775+ let needForceShutdown = false ;
3776+
3777+ for ( const signal of EXIT_SIGNALS ) {
3778+ // eslint-disable-next-line @typescript-eslint/no-loop-func
3779+ const listener = ( ) => {
3780+ if ( needForceShutdown ) {
3781+ process . exit ( 0 ) ;
3782+ }
3783+
3784+ // Keep fast shutdowns silent.
3785+ const timeout = setTimeout ( ( ) => {
3786+ this . logger . info (
3787+ "Gracefully shutting down. To force exit, press ^C again. Please wait..." ,
3788+ ) ;
3789+ } , 2000 ) ;
3790+
3791+ needForceShutdown = true ;
3792+
3793+ compiler . close ( ( ) => {
3794+ clearTimeout ( timeout ) ;
3795+ process . exit ( 0 ) ;
3796+ } ) ;
3797+ } ;
3798+
3799+ process . on ( signal , listener ) ;
3800+ }
3801+ }
3802+
37003803 async runWebpack ( options : Options , isWatchCommand : boolean ) : Promise < void > {
37013804 let compiler : Compiler | MultiCompiler ;
37023805 let stringifyChunked : typeof stringifyChunkedType ;
@@ -3794,32 +3897,7 @@ class WebpackCLI {
37943897 ) ;
37953898
37963899 if ( needGracefulShutdown ( compiler ) ) {
3797- let needForceShutdown = false ;
3798-
3799- for ( const signal of EXIT_SIGNALS ) {
3800- // eslint-disable-next-line @typescript-eslint/no-loop-func
3801- const listener = ( ) => {
3802- if ( needForceShutdown ) {
3803- process . exit ( 0 ) ;
3804- }
3805-
3806- // Output message after delay to avoid extra logging
3807- const timeout = setTimeout ( ( ) => {
3808- this . logger . info (
3809- "Gracefully shutting down. To force exit, press ^C again. Please wait..." ,
3810- ) ;
3811- } , 2000 ) ;
3812-
3813- needForceShutdown = true ;
3814-
3815- compiler . close ( ( ) => {
3816- clearTimeout ( timeout ) ;
3817- process . exit ( 0 ) ;
3818- } ) ;
3819- } ;
3820-
3821- process . on ( signal , listener ) ;
3822- }
3900+ this . #setupGracefulShutdown( compiler ) ;
38233901
38243902 if ( this . #needWatchStdin( compiler ) ) {
38253903 process . stdin . on ( "end" , ( ) => {
0 commit comments