UNPKG

1.75 kBJavaScriptView Raw
1const chalk = require('chalk')
2
3const friendlyModuleParseErrorLabel = 'Module parse failed:'
4const friendlyRedundantModuleWarningLabel = 'Module Warning'
5
6const filterWithLabel = (message, label) => message.indexOf(label) === -1
7
8const formatMessage = message => {
9 let lines = message.split('\n')
10
11 // Remove full path if relative path exists.
12 // This weird behavior happens when
13 // ts-loader and webpack tries to print both paths.
14 if (lines[0].includes(lines[1].slice(2))) {
15 lines.splice(0, 1)
16 }
17 if (lines[1].includes(lines[0])) {
18 lines.splice(0, 1)
19 }
20
21 // Remove excessive warning label
22 lines = lines.filter(message =>
23 filterWithLabel(message, friendlyRedundantModuleWarningLabel)
24 )
25
26 if (lines.length > 2 && lines[1] === '') {
27 // Remove extra newline.
28 lines.splice(1, 1)
29 }
30
31 // webpack adds a list of entry points to warning messages
32 // @ src/...
33 // we don't need this since we already have the path printed out
34 lines = lines.filter(line => line.indexOf(' @ ') !== 0)
35
36 // line #0 is filename
37 // line #1 is the main error message
38 if (!lines[0] || !lines[1]) {
39 return lines.join('\n')
40 }
41
42 lines[0] = chalk.underline.bold(lines[0])
43
44 // Reassemble the message.
45 message = lines.join('\n')
46
47 return message.trim()
48}
49
50const formatWebpackMessages = json => {
51 const errors = json.errors
52 .map(message => formatMessage(message))
53 .filter(message =>
54 filterWithLabel(message, friendlyModuleParseErrorLabel)
55 )
56 const warnings = json.warnings.map(message => formatMessage(message))
57
58 return {
59 errors,
60 warnings,
61 }
62}
63
64module.exports = formatWebpackMessages