| 
									
										
										
										
											2017-03-22 20:00:57 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-22 20:00:57 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const loaderFlag = "LOADER_EXECUTION"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-27 15:43:27 +08:00
										 |  |  | const webpackOptionsFlag = "WEBPACK_OPTIONS"; | 
					
						
							| 
									
										
										
										
											2017-11-26 04:49:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-31 23:07:02 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {string} stack stack trace | 
					
						
							|  |  |  |  * @param {string} flag flag to cut off | 
					
						
							|  |  |  |  * @returns {string} stack trace without the specified flag included | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const cutOffByFlag = (stack, flag) => { | 
					
						
							|  |  |  | 	const errorStack = stack.split("\n"); | 
					
						
							|  |  |  | 	for (let i = 0; i < errorStack.length; i++) { | 
					
						
							|  |  |  | 		if (errorStack[i].includes(flag)) { | 
					
						
							|  |  |  | 			errorStack.length = i; | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-03-31 23:07:02 +08:00
										 |  |  | 	return errorStack.join("\n"); | 
					
						
							| 
									
										
										
										
											2017-03-22 20:00:57 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-31 23:07:02 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {string} stack stack trace | 
					
						
							|  |  |  |  * @returns {string} stack trace without the loader execution flag included | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const cutOffLoaderExecution = stack => cutOffByFlag(stack, loaderFlag); | 
					
						
							| 
									
										
										
										
											2017-11-27 15:43:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-31 23:07:02 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {string} stack stack trace | 
					
						
							|  |  |  |  * @returns {string} stack trace without the webpack options flag included | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const cutOffWebpackOptions = stack => cutOffByFlag(stack, webpackOptionsFlag); | 
					
						
							| 
									
										
										
										
											2017-11-27 15:43:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-31 23:07:02 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {string} stack stack trace | 
					
						
							|  |  |  |  * @param {string} message error message | 
					
						
							|  |  |  |  * @returns {string} stack trace without the message included | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const cutOffMultilineMessage = (stack, message) => { | 
					
						
							|  |  |  | 	const stackSplitByLines = stack.split("\n"); | 
					
						
							|  |  |  | 	const messageSplitByLines = message.split("\n"); | 
					
						
							| 
									
										
										
										
											2017-11-26 04:49:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 02:24:34 +08:00
										 |  |  | 	/** @type {string[]} */ | 
					
						
							| 
									
										
										
										
											2020-01-17 04:22:05 +08:00
										 |  |  | 	const result = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-31 23:07:02 +08:00
										 |  |  | 	stackSplitByLines.forEach((line, idx) => { | 
					
						
							|  |  |  | 		if (!line.includes(messageSplitByLines[idx])) result.push(line); | 
					
						
							| 
									
										
										
										
											2020-01-17 04:22:05 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return result.join("\n"); | 
					
						
							| 
									
										
										
										
											2017-11-26 04:49:59 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-31 23:07:02 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {string} stack stack trace | 
					
						
							|  |  |  |  * @param {string} message error message | 
					
						
							|  |  |  |  * @returns {string} stack trace without the message included | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const cutOffMessage = (stack, message) => { | 
					
						
							| 
									
										
										
										
											2017-03-22 20:00:57 +08:00
										 |  |  | 	const nextLine = stack.indexOf("\n"); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	if (nextLine === -1) { | 
					
						
							| 
									
										
										
										
											2017-03-22 20:00:57 +08:00
										 |  |  | 		return stack === message ? "" : stack; | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2022-03-14 05:54:18 +08:00
										 |  |  | 		const firstLine = stack.slice(0, nextLine); | 
					
						
							|  |  |  | 		return firstLine === message ? stack.slice(nextLine + 1) : stack; | 
					
						
							| 
									
										
										
										
											2017-03-22 20:00:57 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-31 23:07:02 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {string} stack stack trace | 
					
						
							|  |  |  |  * @param {string} message error message | 
					
						
							|  |  |  |  * @returns {string} stack trace without the loader execution flag and message included | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const cleanUp = (stack, message) => { | 
					
						
							|  |  |  | 	stack = cutOffLoaderExecution(stack); | 
					
						
							|  |  |  | 	stack = cutOffMessage(stack, message); | 
					
						
							| 
									
										
										
										
											2017-03-22 20:00:57 +08:00
										 |  |  | 	return stack; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-11-27 15:43:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-31 23:07:02 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {string} stack stack trace | 
					
						
							|  |  |  |  * @param {string} message error message | 
					
						
							|  |  |  |  * @returns {string} stack trace without the webpack options flag and message included | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const cleanUpWebpackOptions = (stack, message) => { | 
					
						
							|  |  |  | 	stack = cutOffWebpackOptions(stack); | 
					
						
							|  |  |  | 	stack = cutOffMultilineMessage(stack, message); | 
					
						
							| 
									
										
										
										
											2017-11-27 15:43:27 +08:00
										 |  |  | 	return stack; | 
					
						
							| 
									
										
										
										
											2017-11-27 16:38:03 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2023-03-31 23:07:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | exports.cutOffByFlag = cutOffByFlag; | 
					
						
							|  |  |  | exports.cutOffLoaderExecution = cutOffLoaderExecution; | 
					
						
							|  |  |  | exports.cutOffWebpackOptions = cutOffWebpackOptions; | 
					
						
							|  |  |  | exports.cutOffMultilineMessage = cutOffMultilineMessage; | 
					
						
							|  |  |  | exports.cutOffMessage = cutOffMessage; | 
					
						
							|  |  |  | exports.cleanUp = cleanUp; | 
					
						
							|  |  |  | exports.cleanUpWebpackOptions = cleanUpWebpackOptions; |