1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// @flow
import { EOL } from 'os';
import chalk from 'chalk';
import RequestShortener from 'webpack/lib/RequestShortener';
import { formatErrorMessage, stripLoaderFromPath } from './formatUtil';
import type { WebpackError, Stats, Compilation } from '../types';
const createGetFile = (requestShortener: RequestShortener) => (e: WebpackError): ?string => {
/* istanbul ignore if */
if (e.file) {
// webpack does this also, so there must be case when this happens
return e.file;
} else if (e.module && e.module.readableIdentifier && typeof e.module.readableIdentifier === 'function') {
// if we got a module, build a file path to the module without loader information
return stripLoaderFromPath(e.module.readableIdentifier(requestShortener));
}
/* istanbul ignore next */
return null;
};
// helper to transform strings in errors
const ensureWebpackErrors = (errors: Array<string | WebpackError>): Array<WebpackError> => errors
.map((e: string | WebpackError) => {
/* istanbul ignore if */
if (typeof e === 'string') {
// webpack does this also, so there must be case when this happens
return {
message: e,
};
}
return e;
});
const prependWarning = (message: string) => `${chalk.yellow('Warning')} ${message}`;
const prependError = (message: string) => `${chalk.red('Error')} ${message}`;
export default function createStatsFormatter(rootPath: string) {
const requestShortener = new RequestShortener(rootPath);
const getFile = createGetFile(requestShortener);
const formatError = (err: WebpackError) => {
const lines: Array<string> = [];
const file = getFile(err);
/* istanbul ignore else */
if (file != null) {
lines.push(`in ${chalk.underline(file)}`);
lines.push('');
} else {
// got no file, that happens only for more generic errors like the following from node-sass
// Missing binding /mocha-webpack-example/node_modules/node-sass/vendor/linux-x64-48/binding.node
// Node Sass could not find a binding for your current environment: Linux 64-bit with Node.js 6.x
// ...
// just print 2 lines like file
lines.push('');
lines.push('');
}
lines.push(formatErrorMessage(err.message));
return lines.join(EOL);
};
return function statsFormatter(stats: Stats) {
const compilation: Compilation = stats.compilation;
return {
errors: ensureWebpackErrors(compilation.errors).map(formatError).map(prependError),
warnings: ensureWebpackErrors(compilation.warnings).map(formatError).map(prependWarning),
};
};
}