Skip to content

Commit bb2820a

Browse files
committed
Add some doc/comments
1 parent 62e6faa commit bb2820a

File tree

7 files changed

+53
-32
lines changed

7 files changed

+53
-32
lines changed

src/extractError.js renamed to src/core/extractWebpackError.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
const RequestShortener = require("webpack/lib/RequestShortener");
2+
3+
// TODO: allow the location to be customized in options
24
const requestShortener = new RequestShortener(process.cwd());
35

6+
/*
7+
This logic is mostly duplicated from webpack/lib/Stats.js#toJson()
8+
See: https://github.com/webpack/webpack/blob/2f618e733aab4755deb42e9d8e859609005607c0/lib/Stats.js#L89
9+
*/
10+
411
function extractError (e) {
512
return {
613
message: e.message,

src/formatErrors.js renamed to src/core/formatErrors.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Applies formatters to all AnnotatedErrors.
3+
*/
14
function formatErrors(errors, formatters, errorType) {
25
const format = (formatter) => formatter(errors, errorType) || [];
36
const flatten = (accum, curr) => accum.concat(curr);

src/core/transformErrors.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const extractError = require('./extractWebpackError');
2+
3+
/**
4+
* Applies all transformers to all errors and returns "annotated"
5+
* errors.
6+
*
7+
* Each transformer should have the following signature WebpackError => AnnotatedError
8+
*
9+
* A WebpackError has the following fields:
10+
* - message
11+
* - file
12+
* - origin
13+
* - name
14+
* - severity
15+
* - webpackError (original error)
16+
*
17+
* An AnnotatedError should be an extension (Object.assign) of the WebpackError
18+
* and add whatever information is convenient for formatter.
19+
* In particular, they should have a 'type' field and a 'priority' field.
20+
*
21+
* The plugin will only display errors having maximum priority at the same time.
22+
*/
23+
function processErrors (errors, transformers) {
24+
const transform = (error, transformer) => transformer(error);
25+
const applyTransformations = (error) => transformers.reduce(transform, error);
26+
27+
return errors.map(extractError).map(applyTransformations);
28+
}
29+
30+
module.exports = processErrors;

src/formatters/defaultError.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function displayError(index, severity, { file, message, origin }) {
99
message,
1010
(origin ? origin : undefined),
1111
''
12-
].filter((chunk) => chunk !== undefined);
12+
].filter(chunk => chunk !== undefined);
1313
}
1414

1515
function isDefaultError(error) {

src/friendly-errors-plugin.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const path = require('path');
22
const chalk = require('chalk');
33
const os = require('os');
4-
const processErrors = require('./processError');
5-
const formatErrors = require('./formatErrors');
4+
const transformErrors = require('./core/transformErrors');
5+
const formatErrors = require('./core/formatErrors');
66
const debug = require('./debug');
77

88
const transformers = [
@@ -51,25 +51,30 @@ class FriendlyErrorsWebpackPlugin {
5151
if (!hasErrors && !hasWarnings) {
5252
const time = stats.endTime - stats.startTime;
5353
debug.log(chalk.green('Compiled successfully in ' + time + 'ms'));
54+
5455
if (this.compilationSuccessMessage) {
5556
debug.log(this.compilationSuccessMessage);
5657
}
58+
5759
} else if (hasErrors) {
60+
5861
const { errors } = stats.compilation;
59-
const processedErrors = processErrors(errors, transformers);
62+
const processedErrors = transformErrors(errors, transformers);
6063
const nbErrors = processedErrors.length;
6164
displayCompilationMessage(`Failed to compile with ${nbErrors} errors`, 'red');
6265

6366
if (this.notifier) {
6467
this.notify('Error', processedErrors[0]);
6568
}
6669

67-
const topErrors = getMaxSeverityErrors(processedErrors, 'severity');
70+
const topErrors = getMaxSeverityErrors(processedErrors);
6871
formatErrors(topErrors, formatters, 'Error')
6972
.forEach((chunk) => debug.log(chunk));
73+
7074
} else if (hasWarnings) {
75+
7176
const { warnings } = stats.compilation;
72-
const processedWarns = processErrors(warnings, transformers);
77+
const processedWarns = transformErrors(warnings, transformers);
7378
const nbWarning = processedWarns.length;
7479
displayCompilationMessage(`Compiled with ${nbWarning} warnings`, 'yellow');
7580

@@ -98,20 +103,6 @@ function getMaxInt(collection, propertyName) {
98103

99104
module.exports = FriendlyErrorsWebpackPlugin;
100105

101-
function displayError (index, severity, error) {
102-
if (error.file) {
103-
debug.log(chalk.red((index + 1) + ') ' + severity) + ' in ' + error.file);
104-
} else {
105-
debug.log(chalk.red((index + 1) + ') ' + severity));
106-
}
107-
debug.log();
108-
debug.log(error.message);
109-
if (error.origin) {
110-
debug.log(error.origin);
111-
}
112-
debug.log();
113-
}
114-
115106
function displayCompilationMessage (message, color) {
116107
debug.log();
117108
debug.log(chalk[color](message));

src/processError.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/unit/formatErrors.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const formatErrors = require('../../src/formatErrors');
1+
const formatErrors = require('../../src/core/formatErrors');
22
const expect = require('expect');
33
const test = require('ava');
44

@@ -11,7 +11,7 @@ const allCaps = (errors) => errors
1111
const notFound = (errors) => errors
1212
.filter(({ type }) => type === 'not-found').map(() => 'Not found');
1313

14-
const formatters = [allCaps]
14+
const formatters = [allCaps];
1515

1616
test('formats the error based on the matching formatters', () => {
1717
const errors = [

0 commit comments

Comments
 (0)