formatWebpackMessages.js 4.27 KB
Newer Older
1
2
3
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
Sophie Alpert's avatar
Sophie Alpert committed
4
5
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
6
7
 */

8
9
'use strict';

10
11
const chalk = require('chalk');
const friendlySyntaxErrorLabel = 'Syntax error:';
12

13
14
15
function isLikelyASyntaxError(message) {
  return message.indexOf(friendlySyntaxErrorLabel) !== -1;
}
16
17

// Cleans up webpack error messages.
Joe Haddad's avatar
Joe Haddad committed
18
// eslint-disable-next-line no-unused-vars
19
function formatMessage(message, isError) {
20
  let lines = message.split('\n');
21

22
23
24
  // Strip Webpack-added headers off errors/warnings
  // https://github.com/webpack/webpack/blob/master/lib/ModuleError.js
  lines = lines.filter(line => !/Module [A-z ]+\(from/.test(line));
25

26
27
28
29
30
31
32
33
34
35
36
37
  // Transform parsing error into syntax error
  // TODO: move this to our ESLint formatter?
  lines = lines.map(line => {
    const parsingError = /Line (\d+):(?:(\d+):)?\s*Parsing error: (.+)$/.exec(
      line
    );
    if (!parsingError) {
      return line;
    }
    const [, errorLine, errorColumn, errorMessage] = parsingError;
    return `${friendlySyntaxErrorLabel} ${errorMessage} (${errorLine}:${errorColumn})`;
  });
38

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  message = lines.join('\n');
  // Smoosh syntax errors (commonly found in CSS)
  message = message.replace(
    /SyntaxError\s+\((\d+):(\d+)\)\s*(.+?)\n/g,
    `${friendlySyntaxErrorLabel} $3 ($1:$2)\n`
  );
  // Remove columns from ESLint formatter output (we added these for more
  // accurate syntax errors)
  message = message.replace(/Line (\d+):\d+:/g, 'Line $1:');
  // Clean up export errors
  message = message.replace(
    /^.*export '(.+?)' was not found in '(.+?)'.*$/gm,
    `Attempted import error: '$1' is not exported from '$2'.`
  );
  message = message.replace(
    /^.*export 'default' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm,
    `Attempted import error: '$2' does not contain a default export (imported as '$1').`
  );
  message = message.replace(
    /^.*export '(.+?)' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm,
    `Attempted import error: '$1' is not exported from '$3' (imported as '$2').`
  );
  lines = message.split('\n');
62

63
64
  // Remove leading newline
  if (lines.length > 2 && lines[1].trim() === '') {
65
66
    lines.splice(1, 1);
  }
67
68
  // Clean up file name
  lines[0] = lines[0].replace(/^(.*) \d+:\d+-\d+$/, '$1');
69

70
  // Cleans up verbose "module not found" messages for files and packages.
71
  if (lines[1] && lines[1].indexOf('Module not found: ') === 0) {
72
73
    lines = [
      lines[0],
74
      lines[1]
75
        .replace('Error: ', '')
76
        .replace('Module not found: Cannot find file:', 'Cannot find file:'),
77
    ];
78
79
  }

80
81
82
  // Add helpful message for users trying to use Sass for the first time
  if (lines[1] && lines[1].match(/Cannot find module.+node-sass/)) {
    lines[1] = 'To import Sass files, you first need to install node-sass.\n';
83
    lines[1] +=
84
      'Run `npm install node-sass` or `yarn add node-sass` inside your workspace.';
85
  }
86

87
  lines[0] = chalk.inverse(lines[0]);
88

89
  message = lines.join('\n');
90
91
  // Internal stacks are generally useless so we strip them... with the
  // exception of stacks containing `webpack:` because they're normally
92
  // from user code generated by Webpack. For more information see
93
  // https://github.com/facebook/create-react-app/pull/1050
94
  message = message.replace(
Joe Haddad's avatar
Joe Haddad committed
95
    /^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm,
96
    ''
97
  ); // at ... ...:x:y
Joe Haddad's avatar
Joe Haddad committed
98
  message = message.replace(/^\s*at\s<anonymous>(\n|$)/gm, ''); // at <anonymous>
99
  lines = message.split('\n');
100

101
102
103
104
105
106
107
108
  // Remove duplicated newlines
  lines = lines.filter(
    (line, index, arr) =>
      index === 0 || line.trim() !== '' || line.trim() !== arr[index - 1].trim()
  );

  // Reassemble the message
  message = lines.join('\n');
109
  return message.trim();
110
111
}

112
function formatWebpackMessages(json) {
113
  const formattedErrors = json.errors.map(function(message) {
114
    return formatMessage(message, true);
115
  });
116
  const formattedWarnings = json.warnings.map(function(message) {
117
    return formatMessage(message, false);
118
  });
119
  const result = { errors: formattedErrors, warnings: formattedWarnings };
120
121
122
123
124
125
126
127
  if (result.errors.some(isLikelyASyntaxError)) {
    // If there are any syntax errors, show just them.
    result.errors = result.errors.filter(isLikelyASyntaxError);
  }
  return result;
}

module.exports = formatWebpackMessages;