Skip to content

Commit 839f280

Browse files
committed
Refactor: extract functions for main plugin logic
1 parent 333bad8 commit 839f280

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

src/friendly-errors-plugin.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,37 +49,17 @@ class FriendlyErrorsWebpackPlugin {
4949
const hasWarnings = stats.hasWarnings();
5050

5151
if (!hasErrors && !hasWarnings) {
52-
const time = stats.endTime - stats.startTime;
53-
debug.log(chalk.green('Compiled successfully in ' + time + 'ms'));
54-
55-
if (this.compilationSuccessMessage) {
56-
debug.log(this.compilationSuccessMessage);
57-
}
58-
59-
} else if (hasErrors) {
60-
61-
const { errors } = stats.compilation;
62-
const processedErrors = transformErrors(errors, transformers);
63-
const nbErrors = processedErrors.length;
64-
displayCompilationMessage(`Failed to compile with ${nbErrors} errors`, 'red');
65-
66-
if (this.notifier) {
67-
this.notify('Error', processedErrors[0]);
68-
}
69-
70-
const topErrors = getMaxSeverityErrors(processedErrors);
71-
formatErrors(topErrors, formatters, 'Error')
72-
.forEach((chunk) => debug.log(chunk));
73-
74-
} else if (hasWarnings) {
52+
this.displaySuccess(stats);
53+
return;
54+
}
7555

76-
const { warnings } = stats.compilation;
77-
const processedWarns = transformErrors(warnings, transformers);
78-
const nbWarning = processedWarns.length;
79-
displayCompilationMessage(`Compiled with ${nbWarning} warnings`, 'yellow');
56+
if (hasErrors) {
57+
this.displayErrors(stats.compilation.errors, 'red', this.notifier);
58+
return;
59+
}
8060

81-
formatErrors(processedWarns, formatters, 'Warning')
82-
.forEach((chunk) => debug.log(chunk));
61+
if (hasWarnings) {
62+
this.displayErrors(stats.compilation.warnings, 'yellow');
8363
}
8464
});
8565

@@ -88,6 +68,30 @@ class FriendlyErrorsWebpackPlugin {
8868
debug.log(chalk.cyan('Compiling...'));
8969
});
9070
}
71+
72+
displaySuccess(stats) {
73+
const time = stats.endTime - stats.startTime;
74+
debug.log(chalk.green('Compiled successfully in ' + time + 'ms'));
75+
76+
if (this.compilationSuccessMessage) {
77+
debug.log(this.compilationSuccessMessage);
78+
}
79+
}
80+
81+
displayErrors(errors, color, notifier) {
82+
83+
const processedErrors = transformErrors(errors, transformers);
84+
const nbErrors = processedErrors.length;
85+
displayCompilationMessage(`Failed to compile with ${nbErrors} errors`, color);
86+
87+
if (notifier) {
88+
this.notify('Error', processedErrors[0]);
89+
}
90+
91+
const topErrors = getMaxSeverityErrors(processedErrors);
92+
formatErrors(topErrors, formatters, 'Error')
93+
.forEach((chunk) => debug.log(chunk));
94+
}
9195
}
9296

9397
function getMaxSeverityErrors (errors) {

0 commit comments

Comments
 (0)