Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

formatWebpackMessages.js 4.3 KiB

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. 'use strict';
  8. const chalk = require('chalk');
  9. const friendlySyntaxErrorLabel = 'Syntax error:';
  10. function isLikelyASyntaxError(message) {
  11. return message.indexOf(friendlySyntaxErrorLabel) !== -1;
  12. }
  13. // Cleans up webpack error messages.
  14. // eslint-disable-next-line no-unused-vars
  15. function formatMessage(message, isError) {
  16. let lines = message.split('\n');
  17. // Strip Webpack-added headers off errors/warnings
  18. // https://github.com/webpack/webpack/blob/master/lib/ModuleError.js
  19. lines = lines.filter(line => !/Module [A-z ]+\(from/.test(line));
  20. // Transform parsing error into syntax error
  21. // TODO: move this to our ESLint formatter?
  22. lines = lines.map(line => {
  23. const parsingError = /Line (\d+):(?:(\d+):)?\s*Parsing error: (.+)$/.exec(
  24. line
  25. );
  26. if (!parsingError) {
  27. return line;
  28. }
  29. const [, errorLine, errorColumn, errorMessage] = parsingError;
  30. return `${friendlySyntaxErrorLabel} ${errorMessage} (${errorLine}:${errorColumn})`;
  31. });
  32. message = lines.join('\n');
  33. // Smoosh syntax errors (commonly found in CSS)
  34. message = message.replace(
  35. /SyntaxError\s+\((\d+):(\d+)\)\s*(.+?)\n/g,
  36. `${friendlySyntaxErrorLabel} $3 ($1:$2)\n`
  37. );
  38. // Remove columns from ESLint formatter output (we added these for more
  39. // accurate syntax errors)
  40. message = message.replace(/Line (\d+):\d+:/g, 'Line $1:');
  41. // Clean up export errors
  42. message = message.replace(
  43. /^.*export '(.+?)' was not found in '(.+?)'.*$/gm,
  44. `Attempted import error: '$1' is not exported from '$2'.`
  45. );
  46. message = message.replace(
  47. /^.*export 'default' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm,
  48. `Attempted import error: '$2' does not contain a default export (imported as '$1').`
  49. );
  50. message = message.replace(
  51. /^.*export '(.+?)' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm,
  52. `Attempted import error: '$1' is not exported from '$3' (imported as '$2').`
  53. );
  54. lines = message.split('\n');
  55. // Remove leading newline
  56. if (lines.length > 2 && lines[1].trim() === '') {
  57. lines.splice(1, 1);
  58. }
  59. // Clean up file name
  60. lines[0] = lines[0].replace(/^(.*) \d+:\d+-\d+$/, '$1');
  61. // Cleans up verbose "module not found" messages for files and packages.
  62. if (lines[1] && lines[1].indexOf('Module not found: ') === 0) {
  63. lines = [
  64. lines[0],
  65. lines[1]
  66. .replace('Error: ', '')
  67. .replace('Module not found: Cannot find file:', 'Cannot find file:'),
  68. ];
  69. }
  70. // Add helpful message for users trying to use Sass for the first time
  71. if (lines[1] && lines[1].match(/Cannot find module.+node-sass/)) {
  72. lines[1] = 'To import Sass files, you first need to install node-sass.\n';
  73. lines[1] +=
  74. 'Run `npm install node-sass` or `yarn add node-sass` inside your workspace.';
  75. }
  76. lines[0] = chalk.inverse(lines[0]);
  77. message = lines.join('\n');
  78. // Internal stacks are generally useless so we strip them... with the
  79. // exception of stacks containing `webpack:` because they're normally
  80. // from user code generated by Webpack. For more information see
  81. // https://github.com/facebook/create-react-app/pull/1050
  82. message = message.replace(
  83. /^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm,
  84. ''
  85. ); // at ... ...:x:y
  86. message = message.replace(/^\s*at\s<anonymous>(\n|$)/gm, ''); // at <anonymous>
  87. lines = message.split('\n');
  88. // Remove duplicated newlines
  89. lines = lines.filter(
  90. (line, index, arr) =>
  91. index === 0 || line.trim() !== '' || line.trim() !== arr[index - 1].trim()
  92. );
  93. // Reassemble the message
  94. message = lines.join('\n');
  95. return message.trim();
  96. }
  97. function formatWebpackMessages(json) {
  98. const formattedErrors = json.errors.map(function(message) {
  99. return formatMessage(message, true);
  100. });
  101. const formattedWarnings = json.warnings.map(function(message) {
  102. return formatMessage(message, false);
  103. });
  104. const result = { errors: formattedErrors, warnings: formattedWarnings };
  105. if (result.errors.some(isLikelyASyntaxError)) {
  106. // If there are any syntax errors, show just them.
  107. result.errors = result.errors.filter(isLikelyASyntaxError);
  108. }
  109. return result;
  110. }
  111. module.exports = formatWebpackMessages;