Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to make this a variable and passing the variable to the promise expression

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