Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Aug 15, 2021
1 parent f5c319c commit d3c2cf5
Showing 1 changed file with 59 additions and 56 deletions.
115 changes: 59 additions & 56 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ if (!process.env.WEBPACK_SERVE) {
class Server {
constructor(options = {}, compiler) {
// TODO: remove this after plugin support is published

if (options.hooks) {
const showDeprecationWarning = util.deprecate(
() => {},
Expand Down Expand Up @@ -128,25 +127,54 @@ class Server {
return path.resolve(dir, "node_modules/.cache/webpack-dev-server");
}

getCompilerConfigArray() {
const compilers = this.compiler.compilers
? this.compiler.compilers
: [this.compiler];
getCompilerOptions() {
if (typeof this.compiler.compilers !== "undefined") {
if (this.compiler.compilers.length === 1) {
return this.compiler.compilers[0].options;
}

// Configuration with the `devServer` options
const compilerWithDevServer = this.compiler.compilers.find(
(config) => config.options.devServer
);

if (compilerWithDevServer) {
return compilerWithDevServer.options;
}

// Configuration with `web` preset
const compilerWithWebPreset = this.compiler.compilers.find(
(config) =>
(config.options.externalsPresets &&
config.options.externalsPresets.web) ||
[
"web",
"webworker",
"electron-preload",
"electron-renderer",
"node-webkit",
// eslint-disable-next-line no-undefined
undefined,
null,
].includes(config.options.target)
);

if (compilerWithWebPreset) {
return compilerWithWebPreset.options;
}

// Fallback
return this.compiler.compilers[0].options;
}

return compilers.map((compiler) => compiler.options);
return this.compiler.options;
}

// eslint-disable-next-line class-methods-use-this
normalizeOptions(options) {
// TODO: improve this to not use .find for compiler watchOptions
const configArray = this.getCompilerConfigArray(this.compiler);
const watchOptionsConfig = configArray.find(
(config) => config.watch !== false && config.watchOptions
);
const watchOptions = watchOptionsConfig
? watchOptionsConfig.watchOptions
: {};

const compilerOptions = this.getCompilerOptions();
// TODO remove `{}` after drop webpack v4 support
const watchOptions = compilerOptions.watchOptions || {};
const defaultOptionsForStatic = {
directory: path.join(process.cwd(), "public"),
staticOptions: {},
Expand Down Expand Up @@ -216,14 +244,9 @@ class Server {

// Respect infrastructureLogging.level
if (typeof options.client.logging === "undefined") {
const configWithInfrastructureLogging =
configArray.find(
(config) =>
config.infrastructureLogging && config.infrastructureLogging.level
) || configArray[0];

options.client.logging =
configWithInfrastructureLogging.infrastructureLogging.level;
options.client.logging = compilerOptions.infrastructureLogging
? compilerOptions.infrastructureLogging.level
: "info";
}
}

Expand Down Expand Up @@ -500,12 +523,11 @@ class Server {
return level;
};

const configWithDevServer =
configArray.find((config) => config.devServer) || configArray[0];

if (typeof proxyOptions.logLevel === "undefined") {
proxyOptions.logLevel = getLogLevelForProxy(
configWithDevServer.infrastructureLogging.level
compilerOptions.infrastructureLogging
? compilerOptions.infrastructureLogging.level
: "info"
);
}

Expand Down Expand Up @@ -1270,13 +1292,13 @@ class Server {
logStatus() {
const colorette = require("colorette");

const getColorsOption = (configArray) => {
const statsOption = this.getStatsOption(configArray);
const getColorsOption = (compilerOptions) => {
let colorsEnabled;

let colorsEnabled = false;

if (typeof statsOption === "object" && statsOption.colors) {
colorsEnabled = statsOption.colors;
if (typeof compilerOptions.stats.colors !== "undefined") {
colorsEnabled = compilerOptions.stats;
} else {
colorsEnabled = colorette.options.enabled;
}

return colorsEnabled;
Expand All @@ -1298,7 +1320,7 @@ class Server {
return msg;
},
};
const useColor = getColorsOption(this.getCompilerConfigArray());
const useColor = getColorsOption(this.getCompilerOptions());

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

// eslint-disable-next-line class-methods-use-this
getStatsOption(configArray) {
const isEmptyObject = (val) =>
typeof val === "object" && Object.keys(val).length === 0;

// in webpack@4 stats will not be defined if not provided,
// but in webpack@5 it will be an empty object
const statsConfig = configArray.find(
(configuration) =>
typeof configuration === "object" &&
configuration.stats &&
!isEmptyObject(configuration.stats)
);

return statsConfig ? statsConfig.stats : {};
}

getStats(statsObj) {
const stats = Server.DEFAULT_STATS;
const compilerOptions = this.getCompilerOptions();

const configArray = this.getCompilerConfigArray(this.compiler);
const statsOption = this.getStatsOption(configArray);

if (typeof statsOption === "object" && statsOption.warningsFilter) {
stats.warningsFilter = statsOption.warningsFilter;
if (compilerOptions.stats && compilerOptions.stats.warningsFilter) {
stats.warningsFilter = compilerOptions.stats.warningsFilter;
}

return statsObj.toJson(stats);
Expand Down

0 comments on commit d3c2cf5

Please sign in to comment.