From 6d3ba0d5a7ead842a682a640c08d9541991bd4a4 Mon Sep 17 00:00:00 2001 From: Even Stensberg Date: Thu, 24 Jun 2021 21:17:51 +0200 Subject: [PATCH 1/5] feat: watch mode config change --- packages/webpack-cli/lib/webpack-cli.js | 25 ++++++++++++++----------- packages/webpack-cli/package.json | 2 ++ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 5ab16847c01..0f816649caa 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -24,13 +24,15 @@ class WebpackCLI { ), }); } - async tryRequireThenImport(module, handleError = true) { let result; try { + delete require.cache[module]; result = require(module); + console.log(result); } catch (error) { + console.log(error, "HEY"); let previousModuleCompile; // TODO Workaround https://github.com/zertosh/v8-compile-cache/issues/30 @@ -67,7 +69,6 @@ class WebpackCLI { throw error; } } - // For babel/typescript if (result.default) { result = result.default; @@ -1545,10 +1546,8 @@ class WebpackCLI { process.exit(2); } - return { options, path: configPath }; }; - const evaluateConfig = async (loadedConfig, argv) => { const isMultiCompiler = Array.isArray(loadedConfig.options); const config = isMultiCompiler ? loadedConfig.options : [loadedConfig.options]; @@ -1581,7 +1580,7 @@ class WebpackCLI { return loadedConfig; }; - const config = { options: {}, path: new WeakMap() }; + const config = { options: {}, path: new Map() }; if (options.config && options.config.length > 0) { const evaluatedConfigs = await Promise.all( @@ -1638,9 +1637,7 @@ class WebpackCLI { if (foundDefaultConfigFile) { const loadedConfig = await loadConfig(foundDefaultConfigFile.path); const evaluatedConfig = await evaluateConfig(loadedConfig, options.argv || {}); - config.options = evaluatedConfig.options; - if (Array.isArray(config.options)) { config.options.forEach((options) => { config.path.set(options, evaluatedConfig.path); @@ -2041,9 +2038,7 @@ class WebpackCLI { async createCompiler(options, callback) { this.applyNodeEnv(options); - let config = await this.resolveConfig(options); - config = await this.applyOptions(config, options); config = await this.applyCLIPlugin(config, options); @@ -2077,7 +2072,7 @@ class WebpackCLI { if (compiler && compiler.compiler) { compiler = compiler.compiler; } - + compiler.config = config; return compiler; } @@ -2171,7 +2166,15 @@ class WebpackCLI { } compiler = await this.createCompiler(options, callback); - + if (options.watch) { + // eslint-disable-next-line + for (const [_, filePath] of compiler.config.path.entries()) { + // eslint-disable-next-line + fs.watchFile(filePath, async () => { + compiler = await this.createCompiler(options, callback); + }); + } + } if (!compiler) { return; } diff --git a/packages/webpack-cli/package.json b/packages/webpack-cli/package.json index 10aca903e59..aea2a31f788 100644 --- a/packages/webpack-cli/package.json +++ b/packages/webpack-cli/package.json @@ -33,6 +33,7 @@ "@webpack-cli/configtest": "^1.0.4", "@webpack-cli/info": "^1.3.0", "@webpack-cli/serve": "^1.5.1", + "async-require": "^1.2.2", "colorette": "^1.2.1", "commander": "^7.0.0", "execa": "^5.0.0", @@ -40,6 +41,7 @@ "import-local": "^3.0.2", "interpret": "^2.2.0", "rechoir": "^0.7.0", + "require.async": "^0.1.1", "v8-compile-cache": "^2.2.0", "webpack-merge": "^5.7.3" }, From 05703b086705159fd934556b3a3e2a2d3e13fd35 Mon Sep 17 00:00:00 2001 From: Even Stensberg Date: Fri, 25 Jun 2021 17:51:54 +0200 Subject: [PATCH 2/5] chore: remove unneded deps --- packages/webpack-cli/package.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/webpack-cli/package.json b/packages/webpack-cli/package.json index aea2a31f788..10aca903e59 100644 --- a/packages/webpack-cli/package.json +++ b/packages/webpack-cli/package.json @@ -33,7 +33,6 @@ "@webpack-cli/configtest": "^1.0.4", "@webpack-cli/info": "^1.3.0", "@webpack-cli/serve": "^1.5.1", - "async-require": "^1.2.2", "colorette": "^1.2.1", "commander": "^7.0.0", "execa": "^5.0.0", @@ -41,7 +40,6 @@ "import-local": "^3.0.2", "interpret": "^2.2.0", "rechoir": "^0.7.0", - "require.async": "^0.1.1", "v8-compile-cache": "^2.2.0", "webpack-merge": "^5.7.3" }, From bc1f853f5fe8b72cf6b94b62024b32dfc3b17f45 Mon Sep 17 00:00:00 2001 From: Even Stensberg Date: Fri, 25 Jun 2021 17:52:19 +0200 Subject: [PATCH 3/5] chore: remove console.logs --- packages/webpack-cli/lib/webpack-cli.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 0f816649caa..61cbea21f91 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -30,9 +30,7 @@ class WebpackCLI { try { delete require.cache[module]; result = require(module); - console.log(result); } catch (error) { - console.log(error, "HEY"); let previousModuleCompile; // TODO Workaround https://github.com/zertosh/v8-compile-cache/issues/30 From ae86f4383cbd0cc31da569c26e39c682ab372138 Mon Sep 17 00:00:00 2001 From: Even Stensberg Date: Fri, 25 Jun 2021 21:05:48 +0200 Subject: [PATCH 4/5] chore: check for existence of cliplugin --- packages/webpack-cli/lib/webpack-cli.js | 10 +++++++--- ...om-config.test.js.snap.devServer3.webpack5 | 2 ++ test/watch/config-watch/config-watch.test.js | 19 +++++++++++++++++++ test/watch/config-watch/webpack.config.js | 7 +++++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 test/watch/config-watch/config-watch.test.js create mode 100644 test/watch/config-watch/webpack.config.js diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 61cbea21f91..8333433a575 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -1993,7 +1993,12 @@ class WebpackCLI { if (!configOptions.plugins) { configOptions.plugins = []; } - + const alreadyHasPLugin = configOptions.plugins.find( + (plugin) => plugin instanceof CLIPlugin, + ); + if (alreadyHasPLugin) { + return configOptions; + } configOptions.plugins.unshift( new CLIPlugin({ configPath: config.path.get(configOptions), @@ -2039,7 +2044,7 @@ class WebpackCLI { let config = await this.resolveConfig(options); config = await this.applyOptions(config, options); config = await this.applyCLIPlugin(config, options); - + console.log(config); let compiler; try { @@ -2075,7 +2080,6 @@ class WebpackCLI { } async buildCommand(options, isWatchCommand) { - // eslint-disable-next-line prefer-const let compiler; let createJsonStringifyStream; diff --git a/test/serve/with-custom-port/__snapshots__/serve-custom-config.test.js.snap.devServer3.webpack5 b/test/serve/with-custom-port/__snapshots__/serve-custom-config.test.js.snap.devServer3.webpack5 index bac74d1f3d6..2d275f5eaef 100644 --- a/test/serve/with-custom-port/__snapshots__/serve-custom-config.test.js.snap.devServer3.webpack5 +++ b/test/serve/with-custom-port/__snapshots__/serve-custom-config.test.js.snap.devServer3.webpack5 @@ -7,3 +7,5 @@ exports[`serve with devServer in config Port flag should override the config por exports[`serve with devServer in config Should pick up the host and port from config: stderr 1`] = `""`; exports[`serve with devServer in config works fine when no-hot flag is passed alongside other server config: stderr 1`] = `""`; + +exports[`serve with devServer in config works fine when no-hot flag is passed alongside other server config: stderr 2`] = `""`; diff --git a/test/watch/config-watch/config-watch.test.js b/test/watch/config-watch/config-watch.test.js new file mode 100644 index 00000000000..656f7a8d517 --- /dev/null +++ b/test/watch/config-watch/config-watch.test.js @@ -0,0 +1,19 @@ +"use strict"; + +const fs = require("fs"); +const { runWatch } = require("../../utils/test-utils"); + +describe("config-watch", () => { + it("should compile a different entry", async () => { + const { stderr, stdout } = await runWatch(__dirname, []); + expect(stdout).toContain("first.js"); + expect(stderr).toBeFalsy(); + fs.writeFile( + "./webpack.config.js", + "module.exports = { entry: './src/second.js' }", + "utf8", + ); + const { stdout_two } = await runWatch(__dirname, []); + expect(stdout_two).toContain("second.js"); + }); +}); diff --git a/test/watch/config-watch/webpack.config.js b/test/watch/config-watch/webpack.config.js new file mode 100644 index 00000000000..e29774ed94d --- /dev/null +++ b/test/watch/config-watch/webpack.config.js @@ -0,0 +1,7 @@ +module.exports = { + entry: "./src/first.js", + devtool: false, + mode: "development", + bail: true, + watch: true, +}; From bf73f5b8118c42da88d4201a0c4292e8e1b4c818 Mon Sep 17 00:00:00 2001 From: Even Stensberg Date: Sat, 10 Jul 2021 21:46:00 +0200 Subject: [PATCH 5/5] chore: use new version of watch traverse --- packages/webpack-cli/lib/webpack-cli.js | 4 ++-- packages/webpack-cli/package.json | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 8333433a575..5954c5a3fe5 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -2,6 +2,7 @@ const fs = require("fs"); const path = require("path"); const { pathToFileURL } = require("url"); const Module = require("module"); +const watchTraverse = require("watch-traverse"); const { program, Option } = require("commander"); const utils = require("./utils"); @@ -2044,7 +2045,6 @@ class WebpackCLI { let config = await this.resolveConfig(options); config = await this.applyOptions(config, options); config = await this.applyCLIPlugin(config, options); - console.log(config); let compiler; try { @@ -2172,7 +2172,7 @@ class WebpackCLI { // eslint-disable-next-line for (const [_, filePath] of compiler.config.path.entries()) { // eslint-disable-next-line - fs.watchFile(filePath, async () => { + watchTraverse(filePath, async () => { compiler = await this.createCompiler(options, callback); }); } diff --git a/packages/webpack-cli/package.json b/packages/webpack-cli/package.json index 10aca903e59..a92a7619c60 100644 --- a/packages/webpack-cli/package.json +++ b/packages/webpack-cli/package.json @@ -41,6 +41,7 @@ "interpret": "^2.2.0", "rechoir": "^0.7.0", "v8-compile-cache": "^2.2.0", + "watch-traverse": "^1.0.1", "webpack-merge": "^5.7.3" }, "peerDependencies": {