Skip to content

Commit d3c2cf5

Browse files
refactor: code
1 parent f5c319c commit d3c2cf5

File tree

1 file changed

+59
-56
lines changed

1 file changed

+59
-56
lines changed

lib/Server.js

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ if (!process.env.WEBPACK_SERVE) {
1919
class Server {
2020
constructor(options = {}, compiler) {
2121
// TODO: remove this after plugin support is published
22-
2322
if (options.hooks) {
2423
const showDeprecationWarning = util.deprecate(
2524
() => {},
@@ -128,25 +127,54 @@ class Server {
128127
return path.resolve(dir, "node_modules/.cache/webpack-dev-server");
129128
}
130129

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

136-
return compilers.map((compiler) => compiler.options);
170+
return this.compiler.options;
137171
}
138172

139173
// eslint-disable-next-line class-methods-use-this
140174
normalizeOptions(options) {
141-
// TODO: improve this to not use .find for compiler watchOptions
142-
const configArray = this.getCompilerConfigArray(this.compiler);
143-
const watchOptionsConfig = configArray.find(
144-
(config) => config.watch !== false && config.watchOptions
145-
);
146-
const watchOptions = watchOptionsConfig
147-
? watchOptionsConfig.watchOptions
148-
: {};
149-
175+
const compilerOptions = this.getCompilerOptions();
176+
// TODO remove `{}` after drop webpack v4 support
177+
const watchOptions = compilerOptions.watchOptions || {};
150178
const defaultOptionsForStatic = {
151179
directory: path.join(process.cwd(), "public"),
152180
staticOptions: {},
@@ -216,14 +244,9 @@ class Server {
216244

217245
// Respect infrastructureLogging.level
218246
if (typeof options.client.logging === "undefined") {
219-
const configWithInfrastructureLogging =
220-
configArray.find(
221-
(config) =>
222-
config.infrastructureLogging && config.infrastructureLogging.level
223-
) || configArray[0];
224-
225-
options.client.logging =
226-
configWithInfrastructureLogging.infrastructureLogging.level;
247+
options.client.logging = compilerOptions.infrastructureLogging
248+
? compilerOptions.infrastructureLogging.level
249+
: "info";
227250
}
228251
}
229252

@@ -500,12 +523,11 @@ class Server {
500523
return level;
501524
};
502525

503-
const configWithDevServer =
504-
configArray.find((config) => config.devServer) || configArray[0];
505-
506526
if (typeof proxyOptions.logLevel === "undefined") {
507527
proxyOptions.logLevel = getLogLevelForProxy(
508-
configWithDevServer.infrastructureLogging.level
528+
compilerOptions.infrastructureLogging
529+
? compilerOptions.infrastructureLogging.level
530+
: "info"
509531
);
510532
}
511533

@@ -1270,13 +1292,13 @@ class Server {
12701292
logStatus() {
12711293
const colorette = require("colorette");
12721294

1273-
const getColorsOption = (configArray) => {
1274-
const statsOption = this.getStatsOption(configArray);
1295+
const getColorsOption = (compilerOptions) => {
1296+
let colorsEnabled;
12751297

1276-
let colorsEnabled = false;
1277-
1278-
if (typeof statsOption === "object" && statsOption.colors) {
1279-
colorsEnabled = statsOption.colors;
1298+
if (typeof compilerOptions.stats.colors !== "undefined") {
1299+
colorsEnabled = compilerOptions.stats;
1300+
} else {
1301+
colorsEnabled = colorette.options.enabled;
12801302
}
12811303

12821304
return colorsEnabled;
@@ -1298,7 +1320,7 @@ class Server {
12981320
return msg;
12991321
},
13001322
};
1301-
const useColor = getColorsOption(this.getCompilerConfigArray());
1323+
const useColor = getColorsOption(this.getCompilerOptions());
13021324

13031325
if (this.options.ipc) {
13041326
this.logger.info(`Project is running at: "${this.server.address()}"`);
@@ -1578,31 +1600,12 @@ class Server {
15781600
}
15791601
}
15801602

1581-
// eslint-disable-next-line class-methods-use-this
1582-
getStatsOption(configArray) {
1583-
const isEmptyObject = (val) =>
1584-
typeof val === "object" && Object.keys(val).length === 0;
1585-
1586-
// in webpack@4 stats will not be defined if not provided,
1587-
// but in webpack@5 it will be an empty object
1588-
const statsConfig = configArray.find(
1589-
(configuration) =>
1590-
typeof configuration === "object" &&
1591-
configuration.stats &&
1592-
!isEmptyObject(configuration.stats)
1593-
);
1594-
1595-
return statsConfig ? statsConfig.stats : {};
1596-
}
1597-
15981603
getStats(statsObj) {
15991604
const stats = Server.DEFAULT_STATS;
1605+
const compilerOptions = this.getCompilerOptions();
16001606

1601-
const configArray = this.getCompilerConfigArray(this.compiler);
1602-
const statsOption = this.getStatsOption(configArray);
1603-
1604-
if (typeof statsOption === "object" && statsOption.warningsFilter) {
1605-
stats.warningsFilter = statsOption.warningsFilter;
1607+
if (compilerOptions.stats && compilerOptions.stats.warningsFilter) {
1608+
stats.warningsFilter = compilerOptions.stats.warningsFilter;
16061609
}
16071610

16081611
return statsObj.toJson(stats);

0 commit comments

Comments
 (0)