source: frontend/node_modules/webpack/lib/ErrorHelpers.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const loaderFlag = "LOADER_EXECUTION";
9
10const 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 */
18const 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 */
33const 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 */
40const 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 */
48const 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 */
68const 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 */
83const 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 */
95const cleanUpWebpackOptions = (stack, message) => {
96 stack = cutOffWebpackOptions(stack);
97 stack = cutOffMultilineMessage(stack, message);
98 return stack;
99};
100
101module.exports.cleanUp = cleanUp;
102module.exports.cleanUpWebpackOptions = cleanUpWebpackOptions;
103module.exports.cutOffByFlag = cutOffByFlag;
104module.exports.cutOffLoaderExecution = cutOffLoaderExecution;
105module.exports.cutOffMessage = cutOffMessage;
106module.exports.cutOffMultilineMessage = cutOffMultilineMessage;
107module.exports.cutOffWebpackOptions = cutOffWebpackOptions;
Note: See TracBrowser for help on using the repository browser.