Skip to content

Commit dee7809

Browse files
committed
fix: fix cases where error fails to display
1 parent b162cab commit dee7809

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed
+33-11
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,47 @@
11
const chalk = require('chalk')
2-
const TYPE = 'cant-resolve-loader'
3-
const errorRE = /Can't resolve '(.*loader)'/
2+
3+
const rules = [
4+
{
5+
type: 'cant-resolve-loader',
6+
re: /Can't resolve '(.*loader)'/,
7+
msg: (e, match) => (
8+
`Failed to resolve loader: ${chalk.yellow(match[1])}\n` +
9+
`You may need to install it.`
10+
)
11+
}
12+
]
413

514
exports.transformer = error => {
6-
if (error.webpackError && error.webpackError.message) {
7-
const match = error.webpackError.message.match(errorRE)
8-
if (match) {
15+
if (error.webpackError) {
16+
const message = typeof error.webpackError === 'string'
17+
? error.webpackError
18+
: error.webpackError.message || ''
19+
for (const { re, msg, type } of rules) {
20+
const match = message.match(re)
21+
if (match) {
22+
return Object.assign({}, error, {
23+
// type is necessary to avoid being printed as defualt error
24+
// by friendly-error-webpack-plugin
25+
type,
26+
shortMessage: msg(error, match)
27+
})
28+
}
29+
}
30+
// no match, unknown webpack error withotu a message.
31+
// friendly-error-webpack-plugin fails to handle this.
32+
if (!error.message) {
933
return Object.assign({}, error, {
10-
type: TYPE,
11-
loader: match[1]
34+
type: 'unknown-webpack-error',
35+
shortMessage: message
1236
})
1337
}
1438
}
1539
return error
1640
}
1741

1842
exports.formatter = errors => {
19-
errors = errors.filter(e => e.type === TYPE)
43+
errors = errors.filter(e => e.shortMessage)
2044
if (errors.length) {
21-
return errors.map(e => {
22-
return `Failed to resolve loader: ${chalk.yellow(e.loader)}`
23-
}).concat(`\nYou may need to install the missing loader.`)
45+
return errors.map(e => e.shortMessage)
2446
}
2547
}

0 commit comments

Comments
 (0)