ErrorHelpers.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const loaderFlag = "LOADER_EXECUTION";
  7. const webpackOptionsFlag = "WEBPACK_OPTIONS";
  8. exports.cutOffByFlag = (stack, flag) => {
  9. stack = stack.split("\n");
  10. for (let i = 0; i < stack.length; i++) {
  11. if (stack[i].includes(flag)) {
  12. stack.length = i;
  13. }
  14. }
  15. return stack.join("\n");
  16. };
  17. exports.cutOffLoaderExecution = stack =>
  18. exports.cutOffByFlag(stack, loaderFlag);
  19. exports.cutOffWebpackOptions = stack =>
  20. exports.cutOffByFlag(stack, webpackOptionsFlag);
  21. exports.cutOffMultilineMessage = (stack, message) => {
  22. stack = stack.split("\n");
  23. message = message.split("\n");
  24. const result = [];
  25. stack.forEach((line, idx) => {
  26. if (!line.includes(message[idx])) result.push(line);
  27. });
  28. return result.join("\n");
  29. };
  30. exports.cutOffMessage = (stack, message) => {
  31. const nextLine = stack.indexOf("\n");
  32. if (nextLine === -1) {
  33. return stack === message ? "" : stack;
  34. } else {
  35. const firstLine = stack.slice(0, nextLine);
  36. return firstLine === message ? stack.slice(nextLine + 1) : stack;
  37. }
  38. };
  39. exports.cleanUp = (stack, message) => {
  40. stack = exports.cutOffLoaderExecution(stack);
  41. stack = exports.cutOffMessage(stack, message);
  42. return stack;
  43. };
  44. exports.cleanUpWebpackOptions = (stack, message) => {
  45. stack = exports.cutOffWebpackOptions(stack);
  46. stack = exports.cutOffMultilineMessage(stack, message);
  47. return stack;
  48. };