Skip to content
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

feat: graceful exit terminal #3075

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
43 changes: 42 additions & 1 deletion packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2496,6 +2496,39 @@
return;
}

const isCacheEnabled = Boolean(
this.isMultipleCompiler(compiler)
? compiler.compilers.some((compiler) => compiler.options.cache)
: compiler.options.cache,
);

let needForceShutdown = false;

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

signals.forEach((signal) => {
const listener = () => {
// TODO" remove function check after webpack v4 support drop
if (needForceShutdown || typeof compiler.close !== "function") {
process.exit();

Check warning on line 2514 in packages/webpack-cli/src/webpack-cli.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L2514 was not covered by tests
}

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: WebpackCompiler): boolean =>
Boolean(
this.isMultipleCompiler(compiler)
Expand All @@ -2505,7 +2538,15 @@

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);

Check warning on line 2543 in packages/webpack-cli/src/webpack-cli.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L2543 was not covered by tests
}

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

module.exports = InfiniteWaitPlugin;