|
1 | 1 | 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 | +] |
4 | 13 |
|
5 | 14 | 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) { |
9 | 33 | return Object.assign({}, error, { |
10 | | - type: TYPE, |
11 | | - loader: match[1] |
| 34 | + type: 'unknown-webpack-error', |
| 35 | + shortMessage: message |
12 | 36 | }) |
13 | 37 | } |
14 | 38 | } |
15 | 39 | return error |
16 | 40 | } |
17 | 41 |
|
18 | 42 | exports.formatter = errors => { |
19 | | - errors = errors.filter(e => e.type === TYPE) |
| 43 | + errors = errors.filter(e => e.shortMessage) |
20 | 44 | 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) |
24 | 46 | } |
25 | 47 | } |
0 commit comments