| 
									
										
										
										
											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 */ | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | /** @typedef {import("./Compilation").CreateStatsOptionsContext} CreateStatsOptionsContext */ | 
					
						
							|  |  |  | /** @typedef {import("./Compilation").NormalizedStatsOptions} NormalizedStatsOptions */ | 
					
						
							| 
									
										
										
										
											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 */ | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsError} StatsError */ | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {string} str string | 
					
						
							|  |  |  |  * @param {string} prefix pref | 
					
						
							|  |  |  |  * @returns {string} indent | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | const indent = (str, prefix) => { | 
					
						
							| 
									
										
										
										
											2024-07-31 10:39:30 +08:00
										 |  |  | 	const rem = str.replace(/\n([^\n])/g, `\n${prefix}$1`); | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 	return prefix + rem; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-01-07 14:56:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | /** @typedef {{ version: boolean, hash: boolean, errorsCount: boolean, warningsCount: boolean, errors: boolean, warnings: boolean, children: NormalizedStatsOptions[] }} ChildOptions */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string | boolean | StatsOptions | undefined} options stats options | 
					
						
							|  |  |  | 	 * @param {CreateStatsOptionsContext} context context | 
					
						
							|  |  |  | 	 * @returns {ChildOptions} context context | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 	_createChildOptions(options, context) { | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		const getCreateStatsOptions = () => { | 
					
						
							|  |  |  | 			if (!options) { | 
					
						
							|  |  |  | 				options = {}; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const { children: childrenOptions = undefined, ...baseOptions } = | 
					
						
							|  |  |  | 				typeof options === "string" | 
					
						
							|  |  |  | 					? { preset: options } | 
					
						
							|  |  |  | 					: /** @type {StatsOptions} */ (options); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			return { childrenOptions, baseOptions }; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 		const children = this.stats.map((stat, idx) => { | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 			if (typeof options === "boolean") { | 
					
						
							|  |  |  | 				return stat.compilation.createStatsOptions(options, context); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			const { childrenOptions, baseOptions } = getCreateStatsOptions(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 	 * @param {(string | boolean | StatsOptions)=} 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) { | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		const childOptions = 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) => { | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 			const obj = stat.toJson(childOptions.children[idx]); | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 			const compilationName = stat.compilation.name; | 
					
						
							|  |  |  | 			const name = | 
					
						
							|  |  |  | 				compilationName && | 
					
						
							|  |  |  | 				identifierUtils.makePathsRelative( | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 					stat.compilation.compiler.context, | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 					compilationName, | 
					
						
							|  |  |  | 					stat.compilation.compiler.root | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			obj.name = name; | 
					
						
							|  |  |  | 			return obj; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		if (childOptions.version) { | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 			obj.version = obj.children[0].version; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		if (childOptions.hash) { | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 			obj.hash = obj.children.map(j => j.hash).join(""); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @param {StatsCompilation} j stats error | 
					
						
							|  |  |  | 		 * @param {StatsError} obj Stats error | 
					
						
							| 
									
										
										
										
											2025-04-04 21:38:51 +08:00
										 |  |  | 		 * @returns {StatsError} result | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2024-07-31 11:31:11 +08:00
										 |  |  | 		const mapError = (j, obj) => ({ | 
					
						
							|  |  |  | 			...obj, | 
					
						
							|  |  |  | 			compilerPath: obj.compilerPath ? `${j.name}.${obj.compilerPath}` : j.name | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		if (childOptions.errors) { | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 			obj.errors = []; | 
					
						
							|  |  |  | 			for (const j of obj.children) { | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 				const errors = | 
					
						
							|  |  |  | 					/** @type {NonNullable<KnownStatsCompilation["errors"]>} */ | 
					
						
							|  |  |  | 					(j.errors); | 
					
						
							|  |  |  | 				for (const i of errors) { | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 					obj.errors.push(mapError(j, i)); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		if (childOptions.warnings) { | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 			obj.warnings = []; | 
					
						
							|  |  |  | 			for (const j of obj.children) { | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 				const warnings = | 
					
						
							|  |  |  | 					/** @type {NonNullable<KnownStatsCompilation["warnings"]>} */ | 
					
						
							|  |  |  | 					(j.warnings); | 
					
						
							|  |  |  | 				for (const i of warnings) { | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 					obj.warnings.push(mapError(j, i)); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2020-01-18 04:19:37 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		if (childOptions.errorsCount) { | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 			obj.errorsCount = 0; | 
					
						
							|  |  |  | 			for (const j of obj.children) { | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 				obj.errorsCount += /** @type {number} */ (j.errorsCount); | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		if (childOptions.warningsCount) { | 
					
						
							| 
									
										
										
										
											2020-09-04 04:09:43 +08:00
										 |  |  | 			obj.warningsCount = 0; | 
					
						
							|  |  |  | 			for (const j of obj.children) { | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 				obj.warningsCount += /** @type {number} */ (j.warningsCount); | 
					
						
							| 
									
										
										
										
											2020-01-18 04:19:37 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 		return obj; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {(string | boolean | StatsOptions)=} options stats options | 
					
						
							|  |  |  | 	 * @returns {string} string output | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 	toString(options) { | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		const childOptions = this._createChildOptions(options, { | 
					
						
							|  |  |  | 			forToString: true | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 		const results = this.stats.map((stat, idx) => { | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 			const str = stat.toString(childOptions.children[idx]); | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 			const compilationName = stat.compilation.name; | 
					
						
							|  |  |  | 			const name = | 
					
						
							|  |  |  | 				compilationName && | 
					
						
							|  |  |  | 				identifierUtils | 
					
						
							|  |  |  | 					.makePathsRelative( | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 						stat.compilation.compiler.context, | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 						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; |