From 1df8379dae62be93bde9b20d47b42cb406091b36 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Fri, 7 Jan 2022 01:15:50 +0530 Subject: [PATCH 01/11] feat: graceful exit --- packages/webpack-cli/lib/webpack-cli.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 42c25156f94..d28b37ff492 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -2324,6 +2324,27 @@ class WebpackCLI { return; } + const signals = ["SIGINT", "SIGTERM"]; + let needForceShutdown = false; + + 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) From a87c764329549f33c6494b1f80135bc4f49accd7 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Sun, 9 Jan 2022 11:55:45 +0530 Subject: [PATCH 02/11] chore: register if cache is available --- packages/webpack-cli/lib/webpack-cli.js | 36 ++++++++++++++----------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index d28b37ff492..b1eba25946d 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -2324,26 +2324,30 @@ class WebpackCLI { return; } - const signals = ["SIGINT", "SIGTERM"]; - let needForceShutdown = false; - - signals.forEach((signal) => { - const listener = () => { - if (needForceShutdown) { - process.exit(); - } + if (compiler.cache) { + const signals = ["SIGINT", "SIGTERM"]; + let needForceShutdown = false; + + signals.forEach((signal) => { + const listener = () => { + if (needForceShutdown) { + process.exit(); + } - this.logger.info("Gracefully shutting down. To force exit, press ^C again. Please wait..."); + this.logger.info( + "Gracefully shutting down. To force exit, press ^C again. Please wait...", + ); - needForceShutdown = true; + needForceShutdown = true; - compiler.close(() => { - process.exit(); - }); - }; + compiler.close(() => { + process.exit(); + }); + }; - process.on(signal, listener); - }); + process.on(signal, listener); + }); + } const isWatch = (compiler) => compiler.compilers From 6c8e65ab97a0ee89df602f287d1bc2647392cde8 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Sun, 9 Jan 2022 12:16:51 +0530 Subject: [PATCH 03/11] chore: check cache from options --- packages/webpack-cli/lib/webpack-cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index b1eba25946d..de74e1643cd 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -2324,7 +2324,7 @@ class WebpackCLI { return; } - if (compiler.cache) { + if (compiler.options.cache) { const signals = ["SIGINT", "SIGTERM"]; let needForceShutdown = false; From d5a1a0d629e4703fb99f256a01c1a7a62c980ad0 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Sun, 9 Jan 2022 13:27:15 +0530 Subject: [PATCH 04/11] chore: watch --- packages/webpack-cli/lib/webpack-cli.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index de74e1643cd..3aa5345d880 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -2324,9 +2324,9 @@ class WebpackCLI { return; } + let needForceShutdown = false; if (compiler.options.cache) { const signals = ["SIGINT", "SIGTERM"]; - let needForceShutdown = false; signals.forEach((signal) => { const listener = () => { @@ -2356,7 +2356,10 @@ class WebpackCLI { if (isWatch(compiler) && this.needWatchStdin(compiler)) { process.stdin.on("end", () => { - process.exit(0); + compiler.close(() => { + process.exit(0); + }); + needForceShutdown = true; }); process.stdin.resume(); } From d810c7c011a6ac1340fb4858a8c47fd32ecbc917 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Tue, 11 Jan 2022 18:12:35 +0530 Subject: [PATCH 05/11] chore: update support for multi compiler --- packages/webpack-cli/lib/webpack-cli.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 3aa5345d880..8e399d182de 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -2324,8 +2324,13 @@ class WebpackCLI { return; } + const isCacheEnabled = compiler.compilers + ? compiler.compilers.some((compiler) => compiler.options.cache) + : compiler.options.cache; + let needForceShutdown = false; - if (compiler.options.cache) { + + if (isCacheEnabled) { const signals = ["SIGINT", "SIGTERM"]; signals.forEach((signal) => { From 4e256a4b050be10861afaffa7c17d3be0b09450c Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Wed, 12 Jan 2022 10:42:03 +0530 Subject: [PATCH 06/11] chore: create plugin to wait --- test/utils/infinite-wait-plugin.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 test/utils/infinite-wait-plugin.js diff --git a/test/utils/infinite-wait-plugin.js b/test/utils/infinite-wait-plugin.js new file mode 100644 index 00000000000..1403b093836 --- /dev/null +++ b/test/utils/infinite-wait-plugin.js @@ -0,0 +1,16 @@ +class InfiniteWaitPlugin { + constructor(options) { + this.time = options.time || 1000; + } + apply(compiler) { + compiler.hooks.done.tapPromise("Infinite Wait Plugin", async () => { + await new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, this.time); + }); + }); + } +} + +module.exports = InfiniteWaitPlugin; From 5dc1863a1bce383a0d2c6ce562d62a67e940a985 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Fri, 28 Jan 2022 12:15:19 +0530 Subject: [PATCH 07/11] chore: create config for testing --- test/build/cache/wait.config.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 test/build/cache/wait.config.js diff --git a/test/build/cache/wait.config.js b/test/build/cache/wait.config.js new file mode 100644 index 00000000000..7e4053b60c0 --- /dev/null +++ b/test/build/cache/wait.config.js @@ -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()], +}; From 429a46d4436513571a134664d39b631bb0b6fe97 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Fri, 28 Jan 2022 12:35:32 +0530 Subject: [PATCH 08/11] chore: update timeout --- test/utils/infinite-wait-plugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/utils/infinite-wait-plugin.js b/test/utils/infinite-wait-plugin.js index 1403b093836..3d1e70d53f0 100644 --- a/test/utils/infinite-wait-plugin.js +++ b/test/utils/infinite-wait-plugin.js @@ -1,6 +1,6 @@ class InfiniteWaitPlugin { constructor(options) { - this.time = options.time || 1000; + this.time = options.time || 10000; } apply(compiler) { compiler.hooks.done.tapPromise("Infinite Wait Plugin", async () => { From d75d28722db376a73d3222349db23ecf919d474a Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Sat, 29 Jan 2022 10:04:49 +0530 Subject: [PATCH 09/11] chore: update webpack 4 support --- packages/webpack-cli/lib/webpack-cli.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index b28e05e651c..9d86fe1b37e 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -2337,7 +2337,7 @@ class WebpackCLI { signals.forEach((signal) => { const listener = () => { - if (needForceShutdown) { + if (needForceShutdown || typeof compiler.close !== "function") { process.exit(); } @@ -2363,6 +2363,11 @@ class WebpackCLI { if (isWatch(compiler) && this.needWatchStdin(compiler)) { process.stdin.on("end", () => { + // TODO: remove on dropping webpack 4 support + if (typeof compiler.close !== "function") { + process.exit(0); + } + compiler.close(() => { process.exit(0); }); From 0991d9743115cb2abc50283c48632b7f0a2a9824 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Wed, 2 Feb 2022 10:24:37 +0530 Subject: [PATCH 10/11] chore: update plugin --- test/utils/infinite-wait-plugin.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/utils/infinite-wait-plugin.js b/test/utils/infinite-wait-plugin.js index 3d1e70d53f0..8c430cf080a 100644 --- a/test/utils/infinite-wait-plugin.js +++ b/test/utils/infinite-wait-plugin.js @@ -1,9 +1,10 @@ class InfiniteWaitPlugin { - constructor(options) { + 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(); From 24a3106c8e6afa13c32e00173af66584e8750c16 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Tue, 22 Feb 2022 11:23:00 +0530 Subject: [PATCH 11/11] chore: add comment --- packages/webpack-cli/src/webpack-cli.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 0821ad1b6c1..2bb7d10974b 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -2509,6 +2509,7 @@ class WebpackCLI implements IWebpackCLI { signals.forEach((signal) => { const listener = () => { + // TODO" remove function check after webpack v4 support drop if (needForceShutdown || typeof compiler.close !== "function") { process.exit(); }