Skip to content

Commit 94193b2

Browse files
fix: use graceful shutdown when file system cache is enabled (#4625)
1 parent 63a41c3 commit 94193b2

3 files changed

Lines changed: 88 additions & 8 deletions

File tree

packages/webpack-cli/src/webpack-cli.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,14 +2490,19 @@ class WebpackCLI implements IWebpackCLI {
24902490
return;
24912491
}
24922492

2493-
const isWatch = (compiler: WebpackCompiler): boolean =>
2493+
const needGracefulShutdown = (compiler: WebpackCompiler): boolean =>
24942494
Boolean(
24952495
this.isMultipleCompiler(compiler)
2496-
? compiler.compilers.some((compiler) => compiler.options.watch)
2497-
: compiler.options.watch,
2496+
? compiler.compilers.some(
2497+
(compiler) =>
2498+
compiler.options.watch ||
2499+
(compiler.options.cache && compiler.options.cache.type === "filesystem"),
2500+
)
2501+
: compiler.options.watch ||
2502+
(compiler.options.cache && compiler.options.cache.type === "filesystem"),
24982503
);
24992504

2500-
if (isWatch(compiler)) {
2505+
if (needGracefulShutdown(compiler)) {
25012506
let needForceShutdown = false;
25022507

25032508
for (const signal of EXIT_SIGNALS) {
@@ -2507,13 +2512,17 @@ class WebpackCLI implements IWebpackCLI {
25072512
process.exit(0);
25082513
}
25092514

2510-
this.logger.info(
2511-
"Gracefully shutting down. To force exit, press ^C again. Please wait...",
2512-
);
2515+
// Output message after delay to avoid extra logging
2516+
const timeout = setTimeout(() => {
2517+
this.logger.info(
2518+
"Gracefully shutting down. To force exit, press ^C again. Please wait...",
2519+
);
2520+
}, 2000);
25132521

25142522
needForceShutdown = true;
25152523

25162524
compiler.close(() => {
2525+
clearTimeout(timeout);
25172526
process.exit(0);
25182527
});
25192528
};

test/build/cache/cache.test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const fs = require("node:fs");
44
const path = require("node:path");
5-
const { run } = require("../../utils/test-utils");
5+
const { isWindows, processKill, run, runWatch } = require("../../utils/test-utils");
66

77
describe("cache", () => {
88
it("should work", async () => {
@@ -218,4 +218,39 @@ describe("cache", () => {
218218
expect(stderr).toBeTruthy();
219219
expect(stdout).toBeTruthy();
220220
});
221+
222+
if (!isWindows) {
223+
it("should graceful shutdown", async () => {
224+
fs.rmSync(
225+
path.join(
226+
__dirname,
227+
"../../../node_modules/.cache/webpack/cache-graceful-shutdown-development",
228+
),
229+
{
230+
recursive: true,
231+
force: true,
232+
},
233+
);
234+
235+
let stdout = "";
236+
237+
await runWatch(__dirname, ["--config", "./graceful-exit.webpack.config.js", "--watch"], {
238+
handler: (proc) => {
239+
proc.stdout.on("data", (chunk) => {
240+
const data = chunk.toString();
241+
242+
stdout += data;
243+
244+
if (data.includes("app.bundle.js")) {
245+
processKill(proc);
246+
}
247+
});
248+
},
249+
});
250+
251+
expect(stdout).toContain(
252+
"Gracefully shutting down. To force exit, press ^C again. Please wait...",
253+
);
254+
});
255+
}
221256
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const path = require("node:path");
2+
3+
class InfiniteWaitPlugin {
4+
apply(compiler) {
5+
compiler.hooks.shutdown.tapPromise("Graceful Exit Test", async () => {
6+
await new Promise((resolve) => {
7+
setTimeout(() => {
8+
resolve();
9+
}, 3000);
10+
});
11+
});
12+
}
13+
}
14+
15+
module.exports = {
16+
mode: "development",
17+
name: "cache-graceful-shutdown",
18+
cache: {
19+
type: "filesystem",
20+
buildDependencies: {
21+
config: [__filename],
22+
},
23+
},
24+
infrastructureLogging: {
25+
debug: /cache/,
26+
},
27+
entry: {
28+
app: "./src/main.js",
29+
},
30+
output: {
31+
filename: "[name].bundle.js",
32+
chunkFilename: "[name].bundle.js",
33+
path: path.resolve(__dirname, "dist"),
34+
},
35+
plugins: [new InfiniteWaitPlugin()],
36+
};

0 commit comments

Comments
 (0)