Skip to content
Open
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
13 changes: 8 additions & 5 deletions packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2484,18 +2484,22 @@ class WebpackCLI implements IWebpackCLI {

compiler = await this.createCompiler(options as WebpackDevServerOptions, callback);

if (!compiler) {
return;
}
if (!compiler) return;

const isWatch = (compiler: WebpackCompiler): boolean =>
Boolean(
this.isMultipleCompiler(compiler)
? compiler.compilers.some((compiler) => compiler.options.watch)
: compiler.options.watch,
);
const isFileSystemCacheEnabled = (compiler: WebpackCompiler): boolean =>
Boolean(
this.isMultipleCompiler(compiler)
? compiler.compilers.some((compiler) => compiler.options.cache)
: compiler.options.cache,
);

if (isWatch(compiler)) {
if (isWatch(compiler) || isFileSystemCacheEnabled(compiler)) {
let needForceShutdown = false;

for (const signal of EXIT_SIGNALS) {
Expand All @@ -2508,7 +2512,6 @@ class WebpackCLI implements IWebpackCLI {
this.logger.info(
"Gracefully shutting down. To force exit, press ^C again. Please wait...",
);

needForceShutdown = true;

compiler.close(() => {
Expand Down
28 changes: 27 additions & 1 deletion test/build/cache/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require("node:fs");
const path = require("node:path");
const { run } = require("../../utils/test-utils");
const { processKill, run, runAndGetProcess } = require("../../utils/test-utils");

describe("cache", () => {
it("should work", async () => {
Expand Down Expand Up @@ -218,4 +218,30 @@ describe("cache", () => {
expect(stderr).toBeTruthy();
expect(stdout).toBeTruthy();
});

it("should gracefully exit when cache is true", (done) => {
fs.rmSync(path.join(__dirname, "../../../node_modules/.cache/webpack/graceful-exit-test"), {
recursive: true,
force: true,
});

const proc = runAndGetProcess(__dirname, [
"--config",
"./graceful-exit.webpack.config.js",
"--name",
"graceful-exit-test",
]);
proc.stdout.on("data", (chunk) => {
const data = chunk.toString();
if (data.includes("app.bundle.js")) {
Copy link
Member Author

Choose a reason for hiding this comment

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

after compile is done and we got an async plugin the sigterm is invoked while we're in the child process without an infinite loop fyi @alexander-akait

processKill(proc);
return;
}
expect(data).toContain(
"Gracefully shutting down. To force exit, press ^C again. Please wait...",
);
});

proc.on("exit", () => done());
});
});
40 changes: 40 additions & 0 deletions test/build/cache/graceful-exit.webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const path = require("node:path");

class InfiniteWaitPlugin {
constructor(options = {}) {
this.wait = options.wait || 10e3;
}

apply(compiler) {
compiler.hooks.done.tapPromise("Graceful Exit Test", async () => {
// eslint-disable-next-line no-new
new Promise((res) => {
setTimeout(res, this.wait);
});
});
}
}

module.exports = {
mode: "development",
name: "graceful-exit-test",
cache: {
type: "filesystem",
Copy link
Member Author

Choose a reason for hiding this comment

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

Might need to specify cache.type in the check at isFileSystemCacheEnabled

buildDependencies: {
config: [__filename],
},
},
infrastructureLogging: {
debug: /cache/,
},
entry: {
app: "./src/main.js",
},
output: {
filename: "[name].bundle.js",
chunkFilename: "[name].bundle.js",
path: path.resolve(__dirname, "dist"),
publicPath: "/",
},
plugins: [new InfiniteWaitPlugin()],
};