diff --git a/packages/serve/src/index.ts b/packages/serve/src/index.ts index 28374fa9194..3770144b57b 100644 --- a/packages/serve/src/index.ts +++ b/packages/serve/src/index.ts @@ -47,10 +47,7 @@ class ServeCommand { process.exit(2); } - const builtInOptions = cli.getBuiltInOptions().filter( - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (option: any) => option.name !== "watch", - ); + const builtInOptions = cli.getBuiltInOptions(); return [...builtInOptions, ...devServerFlags]; }, @@ -110,6 +107,8 @@ class ServeCommand { env: { WEBPACK_SERVE: true, ...options.env }, }; + webpackCLIOptions.isWatchingLikeCommand = true; + const compiler = await cli.createCompiler(webpackCLIOptions); if (!compiler) { diff --git a/packages/webpack-cli/src/types.ts b/packages/webpack-cli/src/types.ts index 67b65c32074..a2aebc5c872 100644 --- a/packages/webpack-cli/src/types.ts +++ b/packages/webpack-cli/src/types.ts @@ -212,6 +212,7 @@ interface WebpackRunOptions extends WebpackOptionsNormalized { argv?: Argv; env: Env; failOnWarnings?: boolean; + isWatchingLikeCommand?: boolean; } /** diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 7c57942e0e9..72f8a515b86 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -2163,11 +2163,10 @@ class WebpackCLI implements IWebpackCLI { // Output warnings if ( - (typeof originalWatchValue !== "undefined" || - (options.argv && typeof options.argv.watch !== "undefined")) && + options.isWatchingLikeCommand && options.argv && options.argv.env && - (options.argv.env["WEBPACK_WATCH"] || options.argv.env["WEBPACK_SERVE"]) + (typeof originalWatchValue !== "undefined" || typeof options.argv.watch !== "undefined") ) { this.logger.warn( `No need to use the '${ @@ -2418,6 +2417,7 @@ class WebpackCLI implements IWebpackCLI { if (isWatchCommand) { options.watch = true; + options.isWatchingLikeCommand = true; } compiler = await this.createCompiler(options as WebpackDevServerOptions, callback); diff --git a/test/serve/basic/__snapshots__/serve-basic.test.js.snap.devServer4.webpack5 b/test/serve/basic/__snapshots__/serve-basic.test.js.snap.devServer4.webpack5 index 4112d980d0b..fc9215cd6bb 100644 --- a/test/serve/basic/__snapshots__/serve-basic.test.js.snap.devServer4.webpack5 +++ b/test/serve/basic/__snapshots__/serve-basic.test.js.snap.devServer4.webpack5 @@ -8,19 +8,14 @@ exports[`basic serve usage should log an error on unknown flag: stderr 1`] = ` exports[`basic serve usage should log an error on unknown flag: stdout 1`] = `""`; exports[`basic serve usage should log error on using '--watch' flag with serve: stderr 1`] = ` -"[webpack-cli] Error: Unknown option '--watch' -[webpack-cli] Run 'webpack --help' to see available commands and options" -`; - -exports[`basic serve usage should log error on using '--watch' flag with serve: stdout 1`] = `""`; - -exports[`basic serve usage should log error on using '-w' alias with serve: stderr 1`] = ` -"[webpack-cli] Error: Unknown option '-w' -[webpack-cli] Run 'webpack --help' to see available commands and options" +"[webpack-cli] No need to use the 'serve' command together with '{ watch: true | false }' or '--watch'/'--no-watch' configuration, it does not make sense. + [webpack-dev-server] Project is running at: + [webpack-dev-server] Loopback: http://localhost:/ + [webpack-dev-server] On Your Network (IPv4): http://:/ + [webpack-dev-server] On Your Network (IPv6): http://[]:/ + [webpack-dev-server] Content not from webpack is served from '/test/serve/basic/public' directory" `; -exports[`basic serve usage should log error on using '-w' alias with serve: stdout 1`] = `""`; - exports[`basic serve usage should log used supplied config with serve: stderr 1`] = ` " [webpack-cli] Compiler starting... [webpack-cli] Compiler is using config: '/test/serve/basic/log.config.js' @@ -34,6 +29,15 @@ exports[`basic serve usage should log used supplied config with serve: stderr 1` [webpack-cli] Compiler is watching files for updates..." `; +exports[`basic serve usage should log warning on using '-w' alias with serve: stderr 1`] = ` +"[webpack-cli] No need to use the 'serve' command together with '{ watch: true | false }' or '--watch'/'--no-watch' configuration, it does not make sense. + [webpack-dev-server] Project is running at: + [webpack-dev-server] Loopback: http://localhost:/ + [webpack-dev-server] On Your Network (IPv4): http://:/ + [webpack-dev-server] On Your Network (IPv6): http://[]:/ + [webpack-dev-server] Content not from webpack is served from '/test/serve/basic/public' directory" +`; + exports[`basic serve usage should respect the "publicPath" option from configuration (from the "devServer" options): stderr 1`] = ` " [webpack-dev-server] Project is running at: [webpack-dev-server] Loopback: http://localhost:/ diff --git a/test/serve/basic/serve-basic.test.js b/test/serve/basic/serve-basic.test.js index 863bc8c6ee4..217bf0a9725 100644 --- a/test/serve/basic/serve-basic.test.js +++ b/test/serve/basic/serve-basic.test.js @@ -373,17 +373,19 @@ describe("basic serve usage", () => { it("should log error on using '--watch' flag with serve", async () => { const { exitCode, stdout, stderr } = await runWatch(testPath, ["serve", "--watch"]); - expect(exitCode).toBe(2); + expect(exitCode).toBe(0); expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); - expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); + expect(stdout).toContain("HotModuleReplacementPlugin"); + expect(stdout).toContain("main.js"); }); - it("should log error on using '-w' alias with serve", async () => { + it("should log warning on using '-w' alias with serve", async () => { const { exitCode, stdout, stderr } = await runWatch(testPath, ["serve", "-w"]); - expect(exitCode).toBe(2); + expect(exitCode).toBe(0); expect(normalizeStderr(stderr)).toMatchSnapshot("stderr"); - expect(normalizeStdout(stdout)).toMatchSnapshot("stdout"); + expect(stdout).toContain("HotModuleReplacementPlugin"); + expect(stdout).toContain("main.js"); }); it("should log an error on unknown flag", async () => { diff --git a/test/watch/basic/basic.config.js b/test/watch/basic/basic.config.js new file mode 100644 index 00000000000..f053ebf7976 --- /dev/null +++ b/test/watch/basic/basic.config.js @@ -0,0 +1 @@ +module.exports = {}; diff --git a/test/watch/basic/basic.test.js b/test/watch/basic/basic.test.js index 56752a2a608..852ed265a97 100644 --- a/test/watch/basic/basic.test.js +++ b/test/watch/basic/basic.test.js @@ -47,6 +47,14 @@ describe("basic", () => { } } }); + + proc.stderr.on("data", (chunk) => { + const data = chunk.toString(); + + expect(data).not.toContain( + " No need to use the 'watch' command together with '{ watch: true | false }' or '--watch'/'--no-watch' configuration, it does not make sense.", + ); + }); }); it("should recompile upon file change using the `watch` command", (done) => { @@ -77,6 +85,14 @@ describe("basic", () => { } } }); + + proc.stderr.on("data", (chunk) => { + const data = chunk.toString(); + + expect(data).not.toContain( + " No need to use the 'watch' command together with '{ watch: true | false }' or '--watch'/'--no-watch' configuration, it does not make sense.", + ); + }); }); it("should recompile upon file change using the `watch` command and entries syntax", (done) => {