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. return stack
  25. .reduce(
  26. (acc, line, idx) =>
  27. line.includes(message[idx]) ? acc : acc.concat(line),
  28. []
  29. )
  30. .join("\n");
  31. };
  32. exports.cutOffMessage = (stack, message) => {
  33. const nextLine = stack.indexOf("\n");
  34. if (nextLine === -1) {
  35. return stack === message ? "" : stack;
  36. } else {
  37. const firstLine = stack.substr(0, nextLine);
  38. return firstLine === message ? stack.substr(nextLine + 1) : stack;
  39. }
  40. };
  41. exports.cleanUp = (stack, message) => {
  42. stack = exports.cutOffLoaderExecution(stack);
  43. stack = exports.cutOffMessage(stack, message);
  44. return stack;
  45. };
  46. exports.cleanUpWebpackOptions = (stack, message) => {
  47. stack = exports.cutOffWebpackOptions(stack);
  48. stack = exports.cutOffMultilineMessage(stack, message);
  49. return stack;
  50. };