Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Aug 29, 2021
1 parent 859a6c5 commit b3c5f1f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
55 changes: 34 additions & 21 deletions packages/serve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,42 @@ class ServeCommand {
return;
}

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", () => {
compiler.close();
stopAllServers();
});

const servers = [];

if (cli.needWatchStdin(compiler) || devServerCLIOptions.stdin) {
// TODO remove in the next major release
// Compatibility with old `stdin` option for `webpack-dev-server`
Expand All @@ -143,24 +173,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(() => {
compiler.close(() => {
process.exit(0);
});
});
stopAllServers();
});
process.stdin.resume();
}
Expand Down
18 changes: 13 additions & 5 deletions packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2125,9 +2125,13 @@ class WebpackCLI {
return;
}

process.on("SIGINT", () => {
compiler.close();
});
const isWebpack5 = this.webpack.version.startsWith("5");

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

const isWatch = (compiler) =>
compiler.compilers
Expand All @@ -2136,9 +2140,13 @@ class WebpackCLI {

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

0 comments on commit b3c5f1f

Please # to comment.