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 | * @param {string} stack stack trace
|
---|
14 | * @param {string} flag flag to cut off
|
---|
15 | * @returns {string} stack trace without the specified flag included
|
---|
16 | */
|
---|
17 | const cutOffByFlag = (stack, flag) => {
|
---|
18 | const errorStack = stack.split("\n");
|
---|
19 | for (let i = 0; i < errorStack.length; i++) {
|
---|
20 | if (errorStack[i].includes(flag)) {
|
---|
21 | errorStack.length = i;
|
---|
22 | }
|
---|
23 | }
|
---|
24 | return errorStack.join("\n");
|
---|
25 | };
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * @param {string} stack stack trace
|
---|
29 | * @returns {string} stack trace without the loader execution flag included
|
---|
30 | */
|
---|
31 | const cutOffLoaderExecution = stack => cutOffByFlag(stack, loaderFlag);
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @param {string} stack stack trace
|
---|
35 | * @returns {string} stack trace without the webpack options flag included
|
---|
36 | */
|
---|
37 | const cutOffWebpackOptions = stack => cutOffByFlag(stack, webpackOptionsFlag);
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * @param {string} stack stack trace
|
---|
41 | * @param {string} message error message
|
---|
42 | * @returns {string} stack trace without the message included
|
---|
43 | */
|
---|
44 | const cutOffMultilineMessage = (stack, message) => {
|
---|
45 | const stackSplitByLines = stack.split("\n");
|
---|
46 | const messageSplitByLines = message.split("\n");
|
---|
47 |
|
---|
48 | /** @type {string[]} */
|
---|
49 | const result = [];
|
---|
50 |
|
---|
51 | for (const [idx, line] of stackSplitByLines.entries()) {
|
---|
52 | if (!line.includes(messageSplitByLines[idx])) result.push(line);
|
---|
53 | }
|
---|
54 |
|
---|
55 | return result.join("\n");
|
---|
56 | };
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * @param {string} stack stack trace
|
---|
60 | * @param {string} message error message
|
---|
61 | * @returns {string} stack trace without the message included
|
---|
62 | */
|
---|
63 | const cutOffMessage = (stack, message) => {
|
---|
64 | const nextLine = stack.indexOf("\n");
|
---|
65 | if (nextLine === -1) {
|
---|
66 | return stack === message ? "" : stack;
|
---|
67 | }
|
---|
68 | const firstLine = stack.slice(0, nextLine);
|
---|
69 | return firstLine === message ? stack.slice(nextLine + 1) : stack;
|
---|
70 | };
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * @param {string} stack stack trace
|
---|
74 | * @param {string} message error message
|
---|
75 | * @returns {string} stack trace without the loader execution flag and message included
|
---|
76 | */
|
---|
77 | const cleanUp = (stack, message) => {
|
---|
78 | stack = cutOffLoaderExecution(stack);
|
---|
79 | stack = cutOffMessage(stack, message);
|
---|
80 | return stack;
|
---|
81 | };
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * @param {string} stack stack trace
|
---|
85 | * @param {string} message error message
|
---|
86 | * @returns {string} stack trace without the webpack options flag and message included
|
---|
87 | */
|
---|
88 | const cleanUpWebpackOptions = (stack, message) => {
|
---|
89 | stack = cutOffWebpackOptions(stack);
|
---|
90 | stack = cutOffMultilineMessage(stack, message);
|
---|
91 | return stack;
|
---|
92 | };
|
---|
93 |
|
---|
94 | module.exports.cutOffByFlag = cutOffByFlag;
|
---|
95 | module.exports.cutOffLoaderExecution = cutOffLoaderExecution;
|
---|
96 | module.exports.cutOffWebpackOptions = cutOffWebpackOptions;
|
---|
97 | module.exports.cutOffMultilineMessage = cutOffMultilineMessage;
|
---|
98 | module.exports.cutOffMessage = cutOffMessage;
|
---|
99 | module.exports.cleanUp = cleanUp;
|
---|
100 | module.exports.cleanUpWebpackOptions = cleanUpWebpackOptions;
|
---|