Skip to content

feat: graceful exit terminal #3075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
40 changes: 39 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,52 @@ 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 || typeof compiler.close !== "function") {
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);
// TODO: remove on dropping webpack 4 support
if (typeof compiler.close !== "function") {
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;