Skip to content

Commit 0684e63

Browse files
committed
chore: wip
1 parent 9c1e017 commit 0684e63

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"test": "jest --reporters=default",
4242
"test:smoketests": "nyc node smoketests",
4343
"test:coverage": "nyc --no-clean jest",
44-
"test:cli": "jest test --reporters=default",
44+
"test:cli": "jest test/build/cache --reporters=default",
4545
"test:packages": "jest packages/ --reporters=default",
4646
"test:ci": "yarn test:cli && yarn test:packages",
4747
"test:watch": "jest test/ packages/ --watch",

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,14 +2492,14 @@ class WebpackCLI implements IWebpackCLI {
24922492
? compiler.compilers.some((compiler) => compiler.options.watch)
24932493
: compiler.options.watch,
24942494
);
2495-
const isCacheEnabled = (compiler: WebpackCompiler): boolean =>
2495+
const isFileSystemCacheEnabled = (compiler: WebpackCompiler): boolean =>
24962496
Boolean(
24972497
this.isMultipleCompiler(compiler)
24982498
? compiler.compilers.some((compiler) => compiler.options.cache)
24992499
: compiler.options.cache,
25002500
);
25012501

2502-
if (isWatch(compiler) || isCacheEnabled(compiler)) {
2502+
if (isWatch(compiler) || isFileSystemCacheEnabled(compiler)) {
25032503
let needForceShutdown = false;
25042504

25052505
for (const signal of EXIT_SIGNALS) {
@@ -2512,7 +2512,6 @@ class WebpackCLI implements IWebpackCLI {
25122512
this.logger.info(
25132513
"Gracefully shutting down. To force exit, press ^C again. Please wait...",
25142514
);
2515-
25162515
needForceShutdown = true;
25172516

25182517
compiler.close(() => {

test/build/cache/cache.test.js

Lines changed: 26 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 { processKill, run, runAndGetProcess } = require("../../utils/test-utils");
66

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

0 commit comments

Comments
 (0)