| 
									
										
										
										
											2017-01-07 14:56:47 +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-01-25 06:31:03 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2017-01-07 14:56:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | const identifierUtils = require("./util/identifier"); | 
					
						
							| 
									
										
										
										
											2017-01-07 14:56:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-10 07:46:29 +08:00
										 |  |  | /** @typedef {import("../declarations/WebpackOptions").StatsOptions} StatsOptions */ | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | /** @typedef {import("./Stats")} Stats */ | 
					
						
							| 
									
										
										
										
											2021-02-05 20:19:32 +08:00
										 |  |  | /** @typedef {import("./stats/DefaultStatsFactoryPlugin").KnownStatsCompilation} KnownStatsCompilation */ | 
					
						
							|  |  |  | /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsCompilation} StatsCompilation */ | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const indent = (str, prefix) => { | 
					
						
							|  |  |  | 	const rem = str.replace(/\n([^\n])/g, "\n" + prefix + "$1"); | 
					
						
							|  |  |  | 	return prefix + rem; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-01-07 14:56:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | class MultiStats { | 
					
						
							| 
									
										
										
										
											2018-12-10 18:34:59 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Stats[]} stats the child stats | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 	constructor(stats) { | 
					
						
							|  |  |  | 		this.stats = stats; | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get hash() { | 
					
						
							|  |  |  | 		return this.stats.map(stat => stat.hash).join(""); | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-07 14:56:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-10 18:34:59 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {boolean} true if a child compilation encountered an error | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 	hasErrors() { | 
					
						
							| 
									
										
										
										
											2018-12-10 18:34:59 +08:00
										 |  |  | 		return this.stats.some(stat => stat.hasErrors()); | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-10 18:34:59 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {boolean} true if a child compilation had a warning | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 	hasWarnings() { | 
					
						
							| 
									
										
										
										
											2018-12-10 18:34:59 +08:00
										 |  |  | 		return this.stats.some(stat => stat.hasWarnings()); | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 	_createChildOptions(options, context) { | 
					
						
							| 
									
										
										
										
											2018-12-27 18:21:26 +08:00
										 |  |  | 		if (!options) { | 
					
						
							|  |  |  | 			options = {}; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-02-14 02:52:37 +08:00
										 |  |  | 		const { children: childrenOptions = undefined, ...baseOptions } = | 
					
						
							|  |  |  | 			typeof options === "string" ? { preset: options } : options; | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 		const children = this.stats.map((stat, idx) => { | 
					
						
							| 
									
										
										
										
											2021-02-14 02:52:37 +08:00
										 |  |  | 			const childOptions = Array.isArray(childrenOptions) | 
					
						
							|  |  |  | 				? childrenOptions[idx] | 
					
						
							|  |  |  | 				: childrenOptions; | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 			return stat.compilation.createStatsOptions( | 
					
						
							| 
									
										
										
										
											2019-06-19 19:16:05 +08:00
										 |  |  | 				{ | 
					
						
							|  |  |  | 					...baseOptions, | 
					
						
							| 
									
										
										
										
											2021-02-14 02:52:37 +08:00
										 |  |  | 					...(typeof childOptions === "string" | 
					
						
							|  |  |  | 						? { preset: childOptions } | 
					
						
							|  |  |  | 						: childOptions && typeof childOptions === "object" | 
					
						
							| 
									
										
										
										
											2024-01-14 09:41:34 +08:00
										 |  |  | 							? childOptions | 
					
						
							|  |  |  | 							: undefined) | 
					
						
							| 
									
										
										
										
											2019-06-19 19:16:05 +08:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 				context | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 		return { | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 			version: children.every(o => o.version), | 
					
						
							|  |  |  | 			hash: children.every(o => o.hash), | 
					
						
							|  |  |  | 			errorsCount: children.every(o => o.errorsCount), | 
					
						
							|  |  |  | 			warningsCount: children.every(o => o.warningsCount), | 
					
						
							|  |  |  | 			errors: children.every(o => o.errors), | 
					
						
							|  |  |  | 			warnings: children.every(o => o.warnings), | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 			children | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-10 07:46:29 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {any} options stats options | 
					
						
							| 
									
										
										
										
											2021-02-05 20:19:32 +08:00
										 |  |  | 	 * @returns {StatsCompilation} json output | 
					
						
							| 
									
										
										
										
											2021-01-10 07:46:29 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 	toJson(options) { | 
					
						
							|  |  |  | 		options = this._createChildOptions(options, { forToString: false }); | 
					
						
							| 
									
										
										
										
											2021-02-05 20:19:32 +08:00
										 |  |  | 		/** @type {KnownStatsCompilation} */ | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 		const obj = {}; | 
					
						
							|  |  |  | 		obj.children = this.stats.map((stat, idx) => { | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 			const obj = stat.toJson(options.children[idx]); | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 			const compilationName = stat.compilation.name; | 
					
						
							|  |  |  | 			const name = | 
					
						
							|  |  |  | 				compilationName && | 
					
						
							|  |  |  | 				identifierUtils.makePathsRelative( | 
					
						
							|  |  |  | 					options.context, | 
					
						
							|  |  |  | 					compilationName, | 
					
						
							|  |  |  | 					stat.compilation.compiler.root | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			obj.name = name; | 
					
						
							|  |  |  | 			return obj; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 		if (options.version) { | 
					
						
							|  |  |  | 			obj.version = obj.children[0].version; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (options.hash) { | 
					
						
							|  |  |  | 			obj.hash = obj.children.map(j => j.hash).join(""); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		const mapError = (j, obj) => { | 
					
						
							|  |  |  | 			return { | 
					
						
							|  |  |  | 				...obj, | 
					
						
							|  |  |  | 				compilerPath: obj.compilerPath | 
					
						
							|  |  |  | 					? `${j.name}.${obj.compilerPath}` | 
					
						
							|  |  |  | 					: j.name | 
					
						
							| 
									
										
										
										
											2020-01-18 04:19:37 +08:00
										 |  |  | 			}; | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 		if (options.errors) { | 
					
						
							|  |  |  | 			obj.errors = []; | 
					
						
							|  |  |  | 			for (const j of obj.children) { | 
					
						
							|  |  |  | 				for (const i of j.errors) { | 
					
						
							|  |  |  | 					obj.errors.push(mapError(j, i)); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (options.warnings) { | 
					
						
							|  |  |  | 			obj.warnings = []; | 
					
						
							|  |  |  | 			for (const j of obj.children) { | 
					
						
							|  |  |  | 				for (const i of j.warnings) { | 
					
						
							|  |  |  | 					obj.warnings.push(mapError(j, i)); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2020-01-18 04:19:37 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (options.errorsCount) { | 
					
						
							|  |  |  | 			obj.errorsCount = 0; | 
					
						
							|  |  |  | 			for (const j of obj.children) { | 
					
						
							|  |  |  | 				obj.errorsCount += j.errorsCount; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (options.warningsCount) { | 
					
						
							|  |  |  | 			obj.warningsCount = 0; | 
					
						
							|  |  |  | 			for (const j of obj.children) { | 
					
						
							|  |  |  | 				obj.warningsCount += j.warningsCount; | 
					
						
							| 
									
										
										
										
											2020-01-18 04:19:37 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 		return obj; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	toString(options) { | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 		options = this._createChildOptions(options, { forToString: true }); | 
					
						
							|  |  |  | 		const results = this.stats.map((stat, idx) => { | 
					
						
							|  |  |  | 			const str = stat.toString(options.children[idx]); | 
					
						
							|  |  |  | 			const compilationName = stat.compilation.name; | 
					
						
							|  |  |  | 			const name = | 
					
						
							|  |  |  | 				compilationName && | 
					
						
							|  |  |  | 				identifierUtils | 
					
						
							|  |  |  | 					.makePathsRelative( | 
					
						
							|  |  |  | 						options.context, | 
					
						
							|  |  |  | 						compilationName, | 
					
						
							|  |  |  | 						stat.compilation.compiler.root | 
					
						
							|  |  |  | 					) | 
					
						
							|  |  |  | 					.replace(/\|/g, " "); | 
					
						
							|  |  |  | 			if (!str) return str; | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 			return name ? `${name}:\n${indent(str, "  ")}` : str; | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 		return results.filter(Boolean).join("\n\n"); | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-07 14:56:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = MultiStats; |