Skip to content

Commit

Permalink
fix: close compiler on SIGINT
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Aug 29, 2021
1 parent 06cd267 commit 52d9a0c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
51 changes: 35 additions & 16 deletions packages/serve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,40 @@ class ServeCommand {

const servers = [];

const stopAllServers = () => {
Promise.all(
servers.map((server) => {
if (typeof server.stop === "function") {
return server.stop();
}

// TODO remove in the next major release
return new Promise<void>((resolve) => {
server.close(() => {
resolve();
});
});
}),
).then(() => {
servers.map((server) => {
if (typeof server.stopCallback === "function") {
return server.stopCallback(() => {
process.exit(0);
});
}

// TODO remove in the next major release
return server.close(() => {
process.exit(0);
});
});
});
};

process.on("SIGINT", () => {
stopAllServers();
});

if (cli.needWatchStdin(compiler) || devServerCLIOptions.stdin) {
// TODO remove in the next major release
// Compatibility with old `stdin` option for `webpack-dev-server`
Expand All @@ -141,22 +175,7 @@ class ServeCommand {
}

process.stdin.on("end", () => {
Promise.all(
servers.map((server) => {
if (typeof server.stop === "function") {
return server.stop();
}

// TODO remove in the next major release
return new Promise<void>((resolve) => {
server.close(() => {
resolve();
});
});
}),
).then(() => {
process.exit(0);
});
stopAllServers();
});
process.stdin.resume();
}
Expand Down
18 changes: 17 additions & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2167,14 +2167,30 @@ class WebpackCLI {
return;
}

const isWebpack5 = this.webpack.version.startsWith("5");

if (isWebpack5) {
process.on("SIGINT", () => {
compiler.close(() => {
process.exit(0);
});
});
}

const isWatch = (compiler) =>
compiler.compilers
? compiler.compilers.some((compiler) => compiler.options.watch)
: compiler.options.watch;

if (isWatch(compiler) && this.needWatchStdin(compiler)) {
process.stdin.on("end", () => {
process.exit(0);
if (isWebpack5) {
compiler.close(() => {
process.exit(0);
});
} else {
process.exit(0);
}
});
process.stdin.resume();
}
Expand Down

0 comments on commit 52d9a0c

Please # to comment.