| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const loaderFlag = "LOADER_EXECUTION";
|
|---|
| 9 |
|
|---|
| 10 | const webpackOptionsFlag = "WEBPACK_OPTIONS";
|
|---|
| 11 |
|
|---|
| 12 | /**
|
|---|
| 13 | * Returns stack trace without the specified flag included.
|
|---|
| 14 | * @param {string} stack stack trace
|
|---|
| 15 | * @param {string} flag flag to cut off
|
|---|
| 16 | * @returns {string} stack trace without the specified flag included
|
|---|
| 17 | */
|
|---|
| 18 | const cutOffByFlag = (stack, flag) => {
|
|---|
| 19 | const errorStack = stack.split("\n");
|
|---|
| 20 | for (let i = 0; i < errorStack.length; i++) {
|
|---|
| 21 | if (errorStack[i].includes(flag)) {
|
|---|
| 22 | errorStack.length = i;
|
|---|
| 23 | }
|
|---|
| 24 | }
|
|---|
| 25 | return errorStack.join("\n");
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Cut off loader execution.
|
|---|
| 30 | * @param {string} stack stack trace
|
|---|
| 31 | * @returns {string} stack trace without the loader execution flag included
|
|---|
| 32 | */
|
|---|
| 33 | const cutOffLoaderExecution = (stack) => cutOffByFlag(stack, loaderFlag);
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Cut off webpack options.
|
|---|
| 37 | * @param {string} stack stack trace
|
|---|
| 38 | * @returns {string} stack trace without the webpack options flag included
|
|---|
| 39 | */
|
|---|
| 40 | const cutOffWebpackOptions = (stack) => cutOffByFlag(stack, webpackOptionsFlag);
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Cut off multiline message.
|
|---|
| 44 | * @param {string} stack stack trace
|
|---|
| 45 | * @param {string} message error message
|
|---|
| 46 | * @returns {string} stack trace without the message included
|
|---|
| 47 | */
|
|---|
| 48 | const cutOffMultilineMessage = (stack, message) => {
|
|---|
| 49 | const stackSplitByLines = stack.split("\n");
|
|---|
| 50 | const messageSplitByLines = message.split("\n");
|
|---|
| 51 |
|
|---|
| 52 | /** @type {string[]} */
|
|---|
| 53 | const result = [];
|
|---|
| 54 |
|
|---|
| 55 | for (const [idx, line] of stackSplitByLines.entries()) {
|
|---|
| 56 | if (!line.includes(messageSplitByLines[idx])) result.push(line);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | return result.join("\n");
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Returns stack trace without the message included.
|
|---|
| 64 | * @param {string} stack stack trace
|
|---|
| 65 | * @param {string} message error message
|
|---|
| 66 | * @returns {string} stack trace without the message included
|
|---|
| 67 | */
|
|---|
| 68 | const cutOffMessage = (stack, message) => {
|
|---|
| 69 | const nextLine = stack.indexOf("\n");
|
|---|
| 70 | if (nextLine === -1) {
|
|---|
| 71 | return stack === message ? "" : stack;
|
|---|
| 72 | }
|
|---|
| 73 | const firstLine = stack.slice(0, nextLine);
|
|---|
| 74 | return firstLine === message ? stack.slice(nextLine + 1) : stack;
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * Returns stack trace without the loader execution flag and message included.
|
|---|
| 79 | * @param {string} stack stack trace
|
|---|
| 80 | * @param {string} message error message
|
|---|
| 81 | * @returns {string} stack trace without the loader execution flag and message included
|
|---|
| 82 | */
|
|---|
| 83 | const cleanUp = (stack, message) => {
|
|---|
| 84 | stack = cutOffLoaderExecution(stack);
|
|---|
| 85 | stack = cutOffMessage(stack, message);
|
|---|
| 86 | return stack;
|
|---|
| 87 | };
|
|---|
| 88 |
|
|---|
| 89 | /**
|
|---|
| 90 | * Clean up webpack options.
|
|---|
| 91 | * @param {string} stack stack trace
|
|---|
| 92 | * @param {string} message error message
|
|---|
| 93 | * @returns {string} stack trace without the webpack options flag and message included
|
|---|
| 94 | */
|
|---|
| 95 | const cleanUpWebpackOptions = (stack, message) => {
|
|---|
| 96 | stack = cutOffWebpackOptions(stack);
|
|---|
| 97 | stack = cutOffMultilineMessage(stack, message);
|
|---|
| 98 | return stack;
|
|---|
| 99 | };
|
|---|
| 100 |
|
|---|
| 101 | module.exports.cleanUp = cleanUp;
|
|---|
| 102 | module.exports.cleanUpWebpackOptions = cleanUpWebpackOptions;
|
|---|
| 103 | module.exports.cutOffByFlag = cutOffByFlag;
|
|---|
| 104 | module.exports.cutOffLoaderExecution = cutOffLoaderExecution;
|
|---|
| 105 | module.exports.cutOffMessage = cutOffMessage;
|
|---|
| 106 | module.exports.cutOffMultilineMessage = cutOffMultilineMessage;
|
|---|
| 107 | module.exports.cutOffWebpackOptions = cutOffWebpackOptions;
|
|---|