| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | /** @typedef {import("./Stats")} Stats */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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; | 
					
						
							|  |  |  | 		this.hash = stats.map(stat => stat.hash).join(""); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											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 = {}; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 		const { children: _, ...baseOptions } = options; | 
					
						
							|  |  |  | 		const children = this.stats.map((stat, idx) => { | 
					
						
							|  |  |  | 			const childOptions = Array.isArray(options.children) | 
					
						
							|  |  |  | 				? options.children[idx] | 
					
						
							|  |  |  | 				: options.children; | 
					
						
							|  |  |  | 			return stat.compilation.createStatsOptions( | 
					
						
							|  |  |  | 				Object.assign( | 
					
						
							|  |  |  | 					{}, | 
					
						
							|  |  |  | 					baseOptions, | 
					
						
							|  |  |  | 					childOptions && typeof childOptions === "object" | 
					
						
							|  |  |  | 						? childOptions | 
					
						
							|  |  |  | 						: { preset: childOptions } | 
					
						
							|  |  |  | 				), | 
					
						
							|  |  |  | 				context | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 		const version = children.every(o => o.version); | 
					
						
							|  |  |  | 		const hash = children.every(o => o.hash); | 
					
						
							|  |  |  | 		if (version) { | 
					
						
							|  |  |  | 			for (const o of children) { | 
					
						
							|  |  |  | 				o.version = false; | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 		return { | 
					
						
							|  |  |  | 			version, | 
					
						
							|  |  |  | 			hash, | 
					
						
							|  |  |  | 			children | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	toJson(options) { | 
					
						
							|  |  |  | 		options = this._createChildOptions(options, { forToString: false }); | 
					
						
							|  |  |  | 		const obj = {}; | 
					
						
							|  |  |  | 		obj.children = this.stats.map((stat, idx) => { | 
					
						
							|  |  |  | 			return stat.toJson(options.children[idx]); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		if (options.version) { | 
					
						
							|  |  |  | 			obj.version = require("../package.json").version; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (options.hash) { | 
					
						
							|  |  |  | 			obj.hash = this.hash; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		const jsons = this.stats.map((stat, idx) => { | 
					
						
							|  |  |  | 			const childOptions = Array.isArray(options) ? options[idx] : options; | 
					
						
							|  |  |  | 			const obj = stat.toJson(childOptions); | 
					
						
							|  |  |  | 			const compilationName = stat.compilation.name; | 
					
						
							|  |  |  | 			const name = | 
					
						
							|  |  |  | 				compilationName && | 
					
						
							|  |  |  | 				identifierUtils.makePathsRelative( | 
					
						
							|  |  |  | 					options.context, | 
					
						
							|  |  |  | 					compilationName, | 
					
						
							|  |  |  | 					stat.compilation.compiler.root | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			obj.name = name; | 
					
						
							|  |  |  | 			return obj; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		obj.errors = jsons.reduce((arr, j) => { | 
					
						
							|  |  |  | 			if (!j.errors) return arr; | 
					
						
							|  |  |  | 			return arr.concat( | 
					
						
							|  |  |  | 				j.errors.map(obj => { | 
					
						
							|  |  |  | 					return Object.assign({}, obj, { | 
					
						
							|  |  |  | 						compilerPath: obj.compilerPath | 
					
						
							|  |  |  | 							? `${j.name}.${obj.compilerPath}` | 
					
						
							|  |  |  | 							: j.name | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		}, []); | 
					
						
							|  |  |  | 		obj.warnings = jsons.reduce((arr, j) => { | 
					
						
							|  |  |  | 			if (!j.warnings) return arr; | 
					
						
							|  |  |  | 			return arr.concat( | 
					
						
							|  |  |  | 				j.warnings.map(obj => { | 
					
						
							|  |  |  | 					return Object.assign({}, obj, { | 
					
						
							|  |  |  | 						compilerPath: obj.compilerPath | 
					
						
							|  |  |  | 							? `${j.name}.${obj.compilerPath}` | 
					
						
							|  |  |  | 							: j.name | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		}, []); | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 			const content = indent(str, "    "); | 
					
						
							|  |  |  | 			return name ? `Child ${name}:\n${content}` : `Child\n${content}`; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		if (options.version) { | 
					
						
							|  |  |  | 			results.unshift(`Version: webpack ${require("../package.json").version}`); | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-12-19 01:29:12 +08:00
										 |  |  | 		if (options.hash) { | 
					
						
							|  |  |  | 			results.unshift(`Hash: ${this.hash}`); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return results.filter(Boolean).join("\n"); | 
					
						
							| 
									
										
										
										
											2017-01-25 06:31:03 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-07 14:56:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = MultiStats; |