From 90720e26ba3b0d115ed066fb8ec3db074751163e Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Wed, 10 Apr 2024 18:11:37 +0300 Subject: [PATCH] fix: gracefully shutting down (#4145) --- packages/webpack-cli/src/webpack-cli.ts | 34 ++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 73b194338b8..7106410c6a7 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -72,6 +72,8 @@ const WEBPACK_DEV_SERVER_PACKAGE = WEBPACK_DEV_SERVER_PACKAGE_IS_CUSTOM ? (process.env.WEBPACK_DEV_SERVER_PACKAGE as string) : "webpack-dev-server"; +const EXIT_SIGNALS = ["SIGINT", "SIGTERM"]; + interface Information { Binaries?: string[]; Browsers?: string[]; @@ -2532,11 +2534,35 @@ class WebpackCLI implements IWebpackCLI { : compiler.options.watch, ); - if (isWatch(compiler) && this.needWatchStdin(compiler)) { - process.stdin.on("end", () => { - process.exit(0); + if (isWatch(compiler)) { + let needForceShutdown = false; + + EXIT_SIGNALS.forEach((signal) => { + const listener = () => { + if (needForceShutdown) { + process.exit(0); + } + + this.logger.info( + "Gracefully shutting down. To force exit, press ^C again. Please wait...", + ); + + needForceShutdown = true; + + compiler.close(() => { + process.exit(0); + }); + }; + + process.on(signal, listener); }); - process.stdin.resume(); + + if (this.needWatchStdin(compiler)) { + process.stdin.on("end", () => { + process.exit(0); + }); + process.stdin.resume(); + } } } }