Skip to content

Commit

Permalink
fix: gracefully shutting down (#4145)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Apr 10, 2024
1 parent 3818d7c commit 90720e2
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions packages/webpack-cli/src/webpack-cli.ts
Expand Up @@ -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[];
Expand Down Expand Up @@ -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();
}
}
}
}
Expand Down

0 comments on commit 90720e2

Please sign in to comment.