Skip to content

Commit c0436ba

Browse files
fix: false positive warning when --watch used (#3783)
1 parent 8027f71 commit c0436ba

File tree

7 files changed

+46
-23
lines changed

7 files changed

+46
-23
lines changed

packages/serve/src/index.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ class ServeCommand {
4747
process.exit(2);
4848
}
4949

50-
const builtInOptions = cli.getBuiltInOptions().filter(
51-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
52-
(option: any) => option.name !== "watch",
53-
);
50+
const builtInOptions = cli.getBuiltInOptions();
5451

5552
return [...builtInOptions, ...devServerFlags];
5653
},
@@ -110,6 +107,8 @@ class ServeCommand {
110107
env: { WEBPACK_SERVE: true, ...options.env },
111108
};
112109

110+
webpackCLIOptions.isWatchingLikeCommand = true;
111+
113112
const compiler = await cli.createCompiler(webpackCLIOptions);
114113

115114
if (!compiler) {

packages/webpack-cli/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ interface WebpackRunOptions extends WebpackOptionsNormalized {
212212
argv?: Argv;
213213
env: Env;
214214
failOnWarnings?: boolean;
215+
isWatchingLikeCommand?: boolean;
215216
}
216217

217218
/**

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -2163,11 +2163,10 @@ class WebpackCLI implements IWebpackCLI {
21632163

21642164
// Output warnings
21652165
if (
2166-
(typeof originalWatchValue !== "undefined" ||
2167-
(options.argv && typeof options.argv.watch !== "undefined")) &&
2166+
options.isWatchingLikeCommand &&
21682167
options.argv &&
21692168
options.argv.env &&
2170-
(options.argv.env["WEBPACK_WATCH"] || options.argv.env["WEBPACK_SERVE"])
2169+
(typeof originalWatchValue !== "undefined" || typeof options.argv.watch !== "undefined")
21712170
) {
21722171
this.logger.warn(
21732172
`No need to use the '${
@@ -2418,6 +2417,7 @@ class WebpackCLI implements IWebpackCLI {
24182417

24192418
if (isWatchCommand) {
24202419
options.watch = true;
2420+
options.isWatchingLikeCommand = true;
24212421
}
24222422

24232423
compiler = await this.createCompiler(options as WebpackDevServerOptions, callback);

test/serve/basic/__snapshots__/serve-basic.test.js.snap.devServer4.webpack5

+15-11
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,14 @@ exports[`basic serve usage should log an error on unknown flag: stderr 1`] = `
88
exports[`basic serve usage should log an error on unknown flag: stdout 1`] = `""`;
99

1010
exports[`basic serve usage should log error on using '--watch' flag with serve: stderr 1`] = `
11-
"[webpack-cli] Error: Unknown option '--watch'
12-
[webpack-cli] Run 'webpack --help' to see available commands and options"
13-
`;
14-
15-
exports[`basic serve usage should log error on using '--watch' flag with serve: stdout 1`] = `""`;
16-
17-
exports[`basic serve usage should log error on using '-w' alias with serve: stderr 1`] = `
18-
"[webpack-cli] Error: Unknown option '-w'
19-
[webpack-cli] Run 'webpack --help' to see available commands and options"
11+
"[webpack-cli] No need to use the 'serve' command together with '{ watch: true | false }' or '--watch'/'--no-watch' configuration, it does not make sense.
12+
<i> [webpack-dev-server] Project is running at:
13+
<i> [webpack-dev-server] Loopback: http://localhost:<port>/
14+
<i> [webpack-dev-server] On Your Network (IPv4): http://<network-ip-v4>:<port>/
15+
<i> [webpack-dev-server] On Your Network (IPv6): http://[<network-ip-v6>]:<port>/
16+
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/test/serve/basic/public' directory"
2017
`;
2118

22-
exports[`basic serve usage should log error on using '-w' alias with serve: stdout 1`] = `""`;
23-
2419
exports[`basic serve usage should log used supplied config with serve: stderr 1`] = `
2520
" [webpack-cli] Compiler starting...
2621
[webpack-cli] Compiler is using config: '<cwd>/test/serve/basic/log.config.js'
@@ -34,6 +29,15 @@ exports[`basic serve usage should log used supplied config with serve: stderr 1`
3429
[webpack-cli] Compiler is watching files for updates..."
3530
`;
3631

32+
exports[`basic serve usage should log warning on using '-w' alias with serve: stderr 1`] = `
33+
"[webpack-cli] No need to use the 'serve' command together with '{ watch: true | false }' or '--watch'/'--no-watch' configuration, it does not make sense.
34+
<i> [webpack-dev-server] Project is running at:
35+
<i> [webpack-dev-server] Loopback: http://localhost:<port>/
36+
<i> [webpack-dev-server] On Your Network (IPv4): http://<network-ip-v4>:<port>/
37+
<i> [webpack-dev-server] On Your Network (IPv6): http://[<network-ip-v6>]:<port>/
38+
<i> [webpack-dev-server] Content not from webpack is served from '<cwd>/test/serve/basic/public' directory"
39+
`;
40+
3741
exports[`basic serve usage should respect the "publicPath" option from configuration (from the "devServer" options): stderr 1`] = `
3842
"<i> [webpack-dev-server] Project is running at:
3943
<i> [webpack-dev-server] Loopback: http://localhost:<port>/

test/serve/basic/serve-basic.test.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -373,17 +373,19 @@ describe("basic serve usage", () => {
373373
it("should log error on using '--watch' flag with serve", async () => {
374374
const { exitCode, stdout, stderr } = await runWatch(testPath, ["serve", "--watch"]);
375375

376-
expect(exitCode).toBe(2);
376+
expect(exitCode).toBe(0);
377377
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
378-
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
378+
expect(stdout).toContain("HotModuleReplacementPlugin");
379+
expect(stdout).toContain("main.js");
379380
});
380381

381-
it("should log error on using '-w' alias with serve", async () => {
382+
it("should log warning on using '-w' alias with serve", async () => {
382383
const { exitCode, stdout, stderr } = await runWatch(testPath, ["serve", "-w"]);
383384

384-
expect(exitCode).toBe(2);
385+
expect(exitCode).toBe(0);
385386
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
386-
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
387+
expect(stdout).toContain("HotModuleReplacementPlugin");
388+
expect(stdout).toContain("main.js");
387389
});
388390

389391
it("should log an error on unknown flag", async () => {

test/watch/basic/basic.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {};

test/watch/basic/basic.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ describe("basic", () => {
4747
}
4848
}
4949
});
50+
51+
proc.stderr.on("data", (chunk) => {
52+
const data = chunk.toString();
53+
54+
expect(data).not.toContain(
55+
" No need to use the 'watch' command together with '{ watch: true | false }' or '--watch'/'--no-watch' configuration, it does not make sense.",
56+
);
57+
});
5058
});
5159

5260
it("should recompile upon file change using the `watch` command", (done) => {
@@ -77,6 +85,14 @@ describe("basic", () => {
7785
}
7886
}
7987
});
88+
89+
proc.stderr.on("data", (chunk) => {
90+
const data = chunk.toString();
91+
92+
expect(data).not.toContain(
93+
" No need to use the 'watch' command together with '{ watch: true | false }' or '--watch'/'--no-watch' configuration, it does not make sense.",
94+
);
95+
});
8096
});
8197

8298
it("should recompile upon file change using the `watch` command and entries syntax", (done) => {

0 commit comments

Comments
 (0)