You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.5 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _path = _interopRequireDefault(require("path"));
  7. var _os = _interopRequireDefault(require("os"));
  8. var _fs = _interopRequireDefault(require("fs"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. // A typical sass error looks like this
  11. // const SassError = {
  12. // message: "invalid property name",
  13. // column: 14,
  14. // line: 1,
  15. // file: "stdin",
  16. // status: 1
  17. // };
  18. /**
  19. * Enhances the sass error with additional information about what actually went wrong.
  20. *
  21. * @param {SassError} error
  22. * @param {string} resourcePath
  23. */
  24. function formatSassError(error, resourcePath) {
  25. // Instruct webpack to hide the JS stack from the console
  26. // Usually you're only interested in the SASS stack in this case.
  27. // eslint-disable-next-line no-param-reassign
  28. error.hideStack = true; // The file property is missing in rare cases.
  29. // No improvement in the error is possible.
  30. if (!error.file) {
  31. return;
  32. }
  33. let msg = error.message;
  34. if (error.file === 'stdin') {
  35. // eslint-disable-next-line no-param-reassign
  36. error.file = resourcePath;
  37. } // node-sass returns UNIX-style paths
  38. // eslint-disable-next-line no-param-reassign
  39. error.file = _path.default.normalize(error.file); // The 'Current dir' hint of node-sass does not help us, we're providing
  40. // additional information by reading the err.file property
  41. msg = msg.replace(/\s*Current dir:\s*/, ''); // msg = msg.replace(/(\s*)(stdin)(\s*)/, `$1${err.file}$3`);
  42. // eslint-disable-next-line no-param-reassign
  43. error.message = `${getFileExcerptIfPossible(error) + msg.charAt(0).toUpperCase() + msg.slice(1) + _os.default.EOL} in ${error.file} (line ${error.line}, column ${error.column})`;
  44. }
  45. /**
  46. * Tries to get an excerpt of the file where the error happened.
  47. * Uses err.line and err.column.
  48. *
  49. * Returns an empty string if the excerpt could not be retrieved.
  50. *
  51. * @param {SassError} error
  52. * @returns {string}
  53. */
  54. function getFileExcerptIfPossible(error) {
  55. try {
  56. const content = _fs.default.readFileSync(error.file, 'utf8');
  57. return `${_os.default.EOL + content.split(/\r?\n/)[error.line - 1] + _os.default.EOL + new Array(error.column - 1).join(' ')}^${_os.default.EOL} `;
  58. } catch (ignoreError) {
  59. // If anything goes wrong here, we don't want any errors to be reported to the user
  60. return '';
  61. }
  62. }
  63. var _default = formatSassError;
  64. exports.default = _default;