Skip to content

Commit 52d9a0c

Browse files
committed
fix: close compiler on SIGINT
1 parent 06cd267 commit 52d9a0c

2 files changed

Lines changed: 52 additions & 17 deletions

File tree

packages/serve/src/index.ts

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,40 @@ class ServeCommand {
132132

133133
const servers = [];
134134

135+
const stopAllServers = () => {
136+
Promise.all(
137+
servers.map((server) => {
138+
if (typeof server.stop === "function") {
139+
return server.stop();
140+
}
141+
142+
// TODO remove in the next major release
143+
return new Promise<void>((resolve) => {
144+
server.close(() => {
145+
resolve();
146+
});
147+
});
148+
}),
149+
).then(() => {
150+
servers.map((server) => {
151+
if (typeof server.stopCallback === "function") {
152+
return server.stopCallback(() => {
153+
process.exit(0);
154+
});
155+
}
156+
157+
// TODO remove in the next major release
158+
return server.close(() => {
159+
process.exit(0);
160+
});
161+
});
162+
});
163+
};
164+
165+
process.on("SIGINT", () => {
166+
stopAllServers();
167+
});
168+
135169
if (cli.needWatchStdin(compiler) || devServerCLIOptions.stdin) {
136170
// TODO remove in the next major release
137171
// Compatibility with old `stdin` option for `webpack-dev-server`
@@ -141,22 +175,7 @@ class ServeCommand {
141175
}
142176

143177
process.stdin.on("end", () => {
144-
Promise.all(
145-
servers.map((server) => {
146-
if (typeof server.stop === "function") {
147-
return server.stop();
148-
}
149-
150-
// TODO remove in the next major release
151-
return new Promise<void>((resolve) => {
152-
server.close(() => {
153-
resolve();
154-
});
155-
});
156-
}),
157-
).then(() => {
158-
process.exit(0);
159-
});
178+
stopAllServers();
160179
});
161180
process.stdin.resume();
162181
}

packages/webpack-cli/lib/webpack-cli.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2167,14 +2167,30 @@ class WebpackCLI {
21672167
return;
21682168
}
21692169

2170+
const isWebpack5 = this.webpack.version.startsWith("5");
2171+
2172+
if (isWebpack5) {
2173+
process.on("SIGINT", () => {
2174+
compiler.close(() => {
2175+
process.exit(0);
2176+
});
2177+
});
2178+
}
2179+
21702180
const isWatch = (compiler) =>
21712181
compiler.compilers
21722182
? compiler.compilers.some((compiler) => compiler.options.watch)
21732183
: compiler.options.watch;
21742184

21752185
if (isWatch(compiler) && this.needWatchStdin(compiler)) {
21762186
process.stdin.on("end", () => {
2177-
process.exit(0);
2187+
if (isWebpack5) {
2188+
compiler.close(() => {
2189+
process.exit(0);
2190+
});
2191+
} else {
2192+
process.exit(0);
2193+
}
21782194
});
21792195
process.stdin.resume();
21802196
}

0 commit comments

Comments
 (0)