Skip to content
Open
35 changes: 34 additions & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2326,14 +2326,47 @@ class WebpackCLI {
return;
}

const isCacheEnabled = compiler.compilers
? compiler.compilers.some((compiler) => compiler.options.cache)
: compiler.options.cache;

let needForceShutdown = false;

if (isCacheEnabled) {
const signals = ["SIGINT", "SIGTERM"];

signals.forEach((signal) => {
const listener = () => {
if (needForceShutdown) {
process.exit();
}

this.logger.info(
"Gracefully shutting down. To force exit, press ^C again. Please wait...",
);

needForceShutdown = true;

compiler.close(() => {
process.exit();
});
};

process.on(signal, listener);
});
}

const isWatch = (compiler) =>
compiler.compilers
? compiler.compilers.some((compiler) => compiler.options.watch)
: compiler.options.watch;

if (isWatch(compiler) && this.needWatchStdin(compiler)) {
process.stdin.on("end", () => {
process.exit(0);
compiler.close(() => {
process.exit(0);
});
needForceShutdown = true;
});
process.stdin.resume();
}
Expand Down
26 changes: 26 additions & 0 deletions test/build/cache/wait.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const path = require("path");
const InfiniteWaitPlugin = require("../../utils/infinite-wait-plugin");

module.exports = {
mode: "development",
name: "cache-test-default",
cache: {
type: "filesystem",
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()],
};
16 changes: 16 additions & 0 deletions test/utils/infinite-wait-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class InfiniteWaitPlugin {
constructor(options) {
this.time = options.time || 10000;
}
apply(compiler) {
compiler.hooks.done.tapPromise("Infinite Wait Plugin", async () => {
await new Promise((resolve) => {
setTimeout(() => {
resolve();
}, this.time);
});
});
}
}

module.exports = InfiniteWaitPlugin;