formatWebpackMessages.js 5.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

10
11
'use strict';

12
13
14
// WARNING: this code is untranspiled and is used in browser too.
// Please make sure any changes are in ES5 or contribute a Babel compile step.

15
// Some custom utilities to prettify Webpack output.
16
17
18
// This is quite hacky and hopefully won't be needed when Webpack fixes this.
// https://github.com/webpack/webpack/issues/2878

19
var friendlySyntaxErrorLabel = 'Syntax error:';
20

21
22
23
function isLikelyASyntaxError(message) {
  return message.indexOf(friendlySyntaxErrorLabel) !== -1;
}
24
25

// Cleans up webpack error messages.
26
function formatMessage(message, isError) {
27
28
  var lines = message.split('\n');

29
30
31
32
33
  if (lines.length > 2 && lines[1] === '') {
    // Remove extra newline.
    lines.splice(1, 1);
  }

34
35
36
37
38
39
40
41
42
  // Remove webpack-specific loader notation from filename.
  // Before:
  // ./~/css-loader!./~/postcss-loader!./src/App.css
  // After:
  // ./src/App.css
  if (lines[0].lastIndexOf('!') !== -1) {
    lines[0] = lines[0].substr(lines[0].lastIndexOf('!') + 1);
  }

43
44
45
46
47
48
49
50
51
  lines = lines.filter(function(line) {
    // Webpack adds a list of entry points to warning messages:
    //  @ ./src/index.js
    //  @ multi react-scripts/~/react-dev-utils/webpackHotDevClient.js ...
    // It is misleading (and unrelated to the warnings) so we clean it up.
    // It is only useful for syntax errors but we have beautiful frames for them.
    return line.indexOf(' @ ') !== 0;
  });

52
53
54
55
56
57
  // line #0 is filename
  // line #1 is the main error message
  if (!lines[0] || !lines[1]) {
    return lines.join('\n');
  }

58
59
60
61
62
  // Cleans up verbose "module not found" messages for files and packages.
  if (lines[1].indexOf('Module not found: ') === 0) {
    lines = [
      lines[0],
      // Clean up message because "Module not found: " is descriptive enough.
63
64
65
66
67
      lines[1]
        .replace("Cannot resolve 'file' or 'directory' ", '')
        .replace('Cannot resolve module ', '')
        .replace('Error: ', ''),
    ];
68
69
70
71
72
  }

  // Cleans up syntax error messages.
  if (lines[1].indexOf('Module build failed: ') === 0) {
    lines[1] = lines[1].replace(
73
74
      'Module build failed: SyntaxError:',
      friendlySyntaxErrorLabel
75
76
77
    );
  }

78
79
80
81
82
83
84
85
86
  // Clean up export errors.
  // TODO: we should really send a PR to Webpack for this.
  var exportError = /\s*(.+?)\s*(")?export '(.+?)' was not found in '(.+?)'/;
  if (lines[1].match(exportError)) {
    lines[1] = lines[1].replace(
      exportError,
      "$1 '$4' does not contain an export named '$3'."
    );
  }
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  // TODO: Ideally we should write a custom ESLint formatter instead.

  // If the second line already includes a filename, and it's a warning,
  // this is likely coming from ESLint. Skip it because Webpack also prints it.
  // Let's omit that in this case.
  var BEGIN_ESLINT_FILENAME = String.fromCharCode(27) + '[4m';
  // Also filter out ESLint summaries for each file
  var BEGIN_ESLINT_WARNING_SUMMARY = String.fromCharCode(27) +
    '[33m' +
    String.fromCharCode(27) +
    '[1m' +
    String.fromCharCode(10006);
  var BEGIN_ESLINT_ERROR_SUMMARY = String.fromCharCode(27) +
    '[31m' +
    String.fromCharCode(27) +
    '[1m' +
    String.fromCharCode(10006);
  // ESLint puts separators like this between groups. We don't need them:
  var ESLINT_EMPTY_SEPARATOR = String.fromCharCode(27) +
    '[22m' +
    String.fromCharCode(27) +
    '[39m';
  // Go!
  lines = lines.filter(function(line) {
    if (line === ESLINT_EMPTY_SEPARATOR) {
      return false;
    }
    if (
      line.indexOf(BEGIN_ESLINT_FILENAME) === 0 ||
      line.indexOf(BEGIN_ESLINT_WARNING_SUMMARY) === 0 ||
      line.indexOf(BEGIN_ESLINT_ERROR_SUMMARY) === 0
    ) {
      return false;
    }
    return true;
  });

125
126
127
128
129
130
131
132
133
134
135
136
  var ESLINT_WARNING_LABEL = String.fromCharCode(27) +
    '[33m' +
    'warning' +
    String.fromCharCode(27) +
    '[39m';
  // If there were errors, omit any warnings.
  if (isError) {
    lines = lines.filter(function(line) {
      return line.indexOf(ESLINT_WARNING_LABEL) === -1;
    });
  }

137
138
139
140
141
142
143
144
145
146
147
148
  // Prepend filename with an explanation.
  lines[0] =
    // Underline
    String.fromCharCode(27) +
    '[4m' +
    // Filename
    lines[0] +
    // End underline
    String.fromCharCode(27) +
    '[24m' +
    (isError ? ' contains errors.' : ' contains warnings.');

149
150
  // Reassemble the message.
  message = lines.join('\n');
151
152
153
154
  // Internal stacks are generally useless so we strip them... with the
  // exception of stacks containing `webpack:` because they're normally
  // from user code generated by WebPack. For more information see
  // https://github.com/facebookincubator/create-react-app/pull/1050
155
  message = message.replace(
156
157
    /^\s*at\s((?!webpack:).)*:\d+:\d+[\s\)]*(\n|$)/gm,
    ''
158
159
  ); // at ... ...:x:y

160
  return message.trim();
161
162
}

163
164
function formatWebpackMessages(json) {
  var formattedErrors = json.errors.map(function(message) {
165
    return formatMessage(message, true);
166
167
  });
  var formattedWarnings = json.warnings.map(function(message) {
168
    return formatMessage(message, false);
169
  });
170
171
  var result = {
    errors: formattedErrors,
172
    warnings: formattedWarnings,
173
174
175
176
177
178
179
180
181
182
183
  };
  if (result.errors.some(isLikelyASyntaxError)) {
    // If there are any syntax errors, show just them.
    // This prevents a confusing ESLint parsing error
    // preceding a much more useful Babel syntax error.
    result.errors = result.errors.filter(isLikelyASyntaxError);
  }
  return result;
}

module.exports = formatWebpackMessages;