Skip to content

Commit c9ccc96

Browse files
authored
fix: respect infastructureLogging.level for client.logging (#3613)
1 parent f67179c commit c9ccc96

File tree

15 files changed

+913
-103
lines changed

15 files changed

+913
-103
lines changed

lib/Server.js

+74-55
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,47 @@ class Server {
126126
return path.resolve(dir, "node_modules/.cache/webpack-dev-server");
127127
}
128128

129-
getCompilerConfigArray() {
130-
const compilers = this.compiler.compilers
131-
? this.compiler.compilers
132-
: [this.compiler];
129+
getCompilerOptions() {
130+
if (typeof this.compiler.compilers !== "undefined") {
131+
if (this.compiler.compilers.length === 1) {
132+
return this.compiler.compilers[0].options;
133+
}
134+
135+
// Configuration with the `devServer` options
136+
const compilerWithDevServer = this.compiler.compilers.find(
137+
(config) => config.options.devServer
138+
);
139+
140+
if (compilerWithDevServer) {
141+
return compilerWithDevServer.options;
142+
}
143+
144+
// Configuration with `web` preset
145+
const compilerWithWebPreset = this.compiler.compilers.find(
146+
(config) =>
147+
(config.options.externalsPresets &&
148+
config.options.externalsPresets.web) ||
149+
[
150+
"web",
151+
"webworker",
152+
"electron-preload",
153+
"electron-renderer",
154+
"node-webkit",
155+
// eslint-disable-next-line no-undefined
156+
undefined,
157+
null,
158+
].includes(config.options.target)
159+
);
160+
161+
if (compilerWithWebPreset) {
162+
return compilerWithWebPreset.options;
163+
}
164+
165+
// Fallback
166+
return this.compiler.compilers[0].options;
167+
}
133168

134-
return compilers.map((compiler) => compiler.options);
169+
return this.compiler.options;
135170
}
136171

137172
// eslint-disable-next-line class-methods-use-this
@@ -142,15 +177,9 @@ class Server {
142177
this.logger = this.compiler.getInfrastructureLogger("webpack-dev-server");
143178
}
144179

145-
// TODO: improve this to not use .find for compiler watchOptions
146-
const configArray = this.getCompilerConfigArray();
147-
const watchOptionsConfig = configArray.find(
148-
(config) => config.watch !== false && config.watchOptions
149-
);
150-
const watchOptions = watchOptionsConfig
151-
? watchOptionsConfig.watchOptions
152-
: {};
153-
180+
const compilerOptions = this.getCompilerOptions();
181+
// TODO remove `{}` after drop webpack v4 support
182+
const watchOptions = compilerOptions.watchOptions || {};
154183
const defaultOptionsForStatic = {
155184
directory: path.join(process.cwd(), "public"),
156185
staticOptions: {},
@@ -218,6 +247,13 @@ class Server {
218247
...options.client.overlay,
219248
};
220249
}
250+
251+
// Respect infrastructureLogging.level
252+
if (typeof options.client.logging === "undefined") {
253+
options.client.logging = compilerOptions.infrastructureLogging
254+
? compilerOptions.infrastructureLogging.level
255+
: "info";
256+
}
221257
}
222258

223259
if (typeof options.compress === "undefined") {
@@ -493,12 +529,11 @@ class Server {
493529
return level;
494530
};
495531

496-
const configWithDevServer =
497-
configArray.find((config) => config.devServer) || configArray[0];
498-
499532
if (typeof proxyOptions.logLevel === "undefined") {
500533
proxyOptions.logLevel = getLogLevelForProxy(
501-
configWithDevServer.infrastructureLogging.level
534+
compilerOptions.infrastructureLogging
535+
? compilerOptions.infrastructureLogging.level
536+
: "info"
502537
);
503538
}
504539

@@ -707,6 +742,17 @@ class Server {
707742
this.app = new express();
708743
}
709744

745+
getStats(statsObj) {
746+
const stats = Server.DEFAULT_STATS;
747+
const compilerOptions = this.getCompilerOptions();
748+
749+
if (compilerOptions.stats && compilerOptions.stats.warningsFilter) {
750+
stats.warningsFilter = compilerOptions.stats.warningsFilter;
751+
}
752+
753+
return statsObj.toJson(stats);
754+
}
755+
710756
setupHooks() {
711757
const addHooks = (compiler) => {
712758
compiler.hooks.invalid.tap("webpack-dev-server", () => {
@@ -1260,13 +1306,16 @@ class Server {
12601306
logStatus() {
12611307
const colorette = require("colorette");
12621308

1263-
const getColorsOption = (configArray) => {
1264-
const statsOption = this.getStatsOption(configArray);
1309+
const getColorsOption = (compilerOptions) => {
1310+
let colorsEnabled;
12651311

1266-
let colorsEnabled = false;
1267-
1268-
if (typeof statsOption === "object" && statsOption.colors) {
1269-
colorsEnabled = statsOption.colors;
1312+
if (
1313+
compilerOptions.stats &&
1314+
typeof compilerOptions.stats.colors !== "undefined"
1315+
) {
1316+
colorsEnabled = compilerOptions.stats;
1317+
} else {
1318+
colorsEnabled = colorette.options.enabled;
12701319
}
12711320

12721321
return colorsEnabled;
@@ -1288,7 +1337,7 @@ class Server {
12881337
return msg;
12891338
},
12901339
};
1291-
const useColor = getColorsOption(this.getCompilerConfigArray());
1340+
const useColor = getColorsOption(this.getCompilerOptions());
12921341

12931342
if (this.options.ipc) {
12941343
this.logger.info(`Project is running at: "${this.server.address()}"`);
@@ -1420,36 +1469,6 @@ class Server {
14201469
}
14211470
}
14221471

1423-
// eslint-disable-next-line class-methods-use-this
1424-
getStatsOption(configArray) {
1425-
const isEmptyObject = (val) =>
1426-
typeof val === "object" && Object.keys(val).length === 0;
1427-
1428-
// in webpack@4 stats will not be defined if not provided,
1429-
// but in webpack@5 it will be an empty object
1430-
const statsConfig = configArray.find(
1431-
(configuration) =>
1432-
typeof configuration === "object" &&
1433-
configuration.stats &&
1434-
!isEmptyObject(configuration.stats)
1435-
);
1436-
1437-
return statsConfig ? statsConfig.stats : {};
1438-
}
1439-
1440-
getStats(statsObj) {
1441-
const stats = Server.DEFAULT_STATS;
1442-
1443-
const configArray = this.getCompilerConfigArray();
1444-
const statsOption = this.getStatsOption(configArray);
1445-
1446-
if (typeof statsOption === "object" && statsOption.warningsFilter) {
1447-
stats.warningsFilter = statsOption.warningsFilter;
1448-
}
1449-
1450-
return statsObj.toJson(stats);
1451-
}
1452-
14531472
setHeaders(req, res, next) {
14541473
let { headers } = this.options;
14551474

test/fixtures/client-config/webpack.config.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"use strict";
22

3+
const webpack = require("webpack");
4+
5+
const isWebpack5 = webpack.version.startsWith("5");
6+
37
const HTMLContent = `
48
<!doctype html>
59
<html>
@@ -20,9 +24,16 @@ module.exports = {
2024
output: {
2125
path: "/",
2226
},
23-
infrastructureLogging: {
24-
level: "warn",
25-
},
27+
infrastructureLogging: isWebpack5
28+
? {
29+
level: "info",
30+
stream: {
31+
write: () => {},
32+
},
33+
}
34+
: {
35+
level: "info",
36+
},
2637
plugins: [
2738
{
2839
apply(compiler) {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"use strict";
22

3+
const webpack = require("webpack");
4+
5+
const isWebpack5 = webpack.version.startsWith("5");
6+
37
module.exports = [
48
{
59
mode: "development",
@@ -10,8 +14,15 @@ module.exports = [
1014
path: "/",
1115
},
1216
node: false,
13-
infrastructureLogging: {
14-
level: "warn",
15-
},
17+
infrastructureLogging: isWebpack5
18+
? {
19+
level: "info",
20+
stream: {
21+
write: () => {},
22+
},
23+
}
24+
: {
25+
level: "info",
26+
},
1627
},
1728
];

test/fixtures/overlay-config/foo.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
"use strict";
2-
3-
console.log("Hey.");
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"use strict";
22

3+
const webpack = require("webpack");
4+
5+
const isWebpack5 = webpack.version.startsWith("5");
6+
37
module.exports = {
48
mode: "development",
59
context: __dirname,
@@ -8,7 +12,14 @@ module.exports = {
812
output: {
913
path: "/",
1014
},
11-
infrastructureLogging: {
12-
level: "warn",
13-
},
15+
infrastructureLogging: isWebpack5
16+
? {
17+
level: "info",
18+
stream: {
19+
write: () => {},
20+
},
21+
}
22+
: {
23+
level: "info",
24+
},
1425
};
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"use strict";
22

3+
const webpack = require("webpack");
4+
5+
const isWebpack5 = webpack.version.startsWith("5");
6+
37
module.exports = {
48
mode: "development",
59
context: __dirname,
@@ -9,7 +13,14 @@ module.exports = {
913
path: "/",
1014
},
1115
node: false,
12-
infrastructureLogging: {
13-
level: "warn",
14-
},
16+
infrastructureLogging: isWebpack5
17+
? {
18+
level: "info",
19+
stream: {
20+
write: () => {},
21+
},
22+
}
23+
: {
24+
level: "info",
25+
},
1526
};
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"use strict";
22

3+
const webpack = require("webpack");
4+
5+
const isWebpack5 = webpack.version.startsWith("5");
6+
37
module.exports = {
48
mode: "development",
59
context: __dirname,
@@ -9,7 +13,14 @@ module.exports = {
913
path: "/",
1014
},
1115
node: false,
12-
infrastructureLogging: {
13-
level: "warn",
14-
},
16+
infrastructureLogging: isWebpack5
17+
? {
18+
level: "info",
19+
stream: {
20+
write: () => {},
21+
},
22+
}
23+
: {
24+
level: "info",
25+
},
1526
};
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"use strict";
22

3+
const webpack = require("webpack");
4+
5+
const isWebpack5 = webpack.version.startsWith("5");
6+
37
module.exports = {
48
mode: "development",
59
context: __dirname,
@@ -9,7 +13,14 @@ module.exports = {
913
path: "/",
1014
},
1115
node: false,
12-
infrastructureLogging: {
13-
level: "warn",
14-
},
16+
infrastructureLogging: isWebpack5
17+
? {
18+
level: "info",
19+
stream: {
20+
write: () => {},
21+
},
22+
}
23+
: {
24+
level: "info",
25+
},
1526
};
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"use strict";
22

3+
const webpack = require("webpack");
4+
5+
const isWebpack5 = webpack.version.startsWith("5");
6+
37
module.exports = {
48
mode: "development",
59
context: __dirname,
@@ -9,7 +13,14 @@ module.exports = {
913
path: "/",
1014
},
1115
node: false,
12-
infrastructureLogging: {
13-
level: "warn",
14-
},
16+
infrastructureLogging: isWebpack5
17+
? {
18+
level: "info",
19+
stream: {
20+
write: () => {},
21+
},
22+
}
23+
: {
24+
level: "info",
25+
},
1526
};

0 commit comments

Comments
 (0)