| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685 | /*	MIT License http://www.opensource.org/licenses/mit-license.php	Author Tobias Koppers @sokra*/"use strict";const RequestShortener = require("./RequestShortener");const SizeFormatHelpers = require("./SizeFormatHelpers");const formatLocation = require("./formatLocation");const identifierUtils = require("./util/identifier");const compareLocations = require("./compareLocations");const { LogType } = require("./logging/Logger");const optionsOrFallback = (...args) => {	let optionValues = [];	optionValues.push(...args);	return optionValues.find(optionValue => optionValue !== undefined);};const compareId = (a, b) => {	if (typeof a !== typeof b) {		return typeof a < typeof b ? -1 : 1;	}	if (a < b) return -1;	if (a > b) return 1;	return 0;};class Stats {	constructor(compilation) {		this.compilation = compilation;		this.hash = compilation.hash;		this.startTime = undefined;		this.endTime = undefined;	}	static filterWarnings(warnings, warningsFilter) {		// we dont have anything to filter so all warnings can be shown		if (!warningsFilter) {			return warnings;		}		// create a chain of filters		// if they return "true" a warning should be suppressed		const normalizedWarningsFilters = [].concat(warningsFilter).map(filter => {			if (typeof filter === "string") {				return warning => warning.includes(filter);			}			if (filter instanceof RegExp) {				return warning => filter.test(warning);			}			if (typeof filter === "function") {				return filter;			}			throw new Error(				`Can only filter warnings with Strings or RegExps. (Given: ${filter})`			);		});		return warnings.filter(warning => {			return !normalizedWarningsFilters.some(check => check(warning));		});	}	formatFilePath(filePath) {		const OPTIONS_REGEXP = /^(\s|\S)*!/;		return filePath.includes("!")			? `${filePath.replace(OPTIONS_REGEXP, "")} (${filePath})`			: `${filePath}`;	}	hasWarnings() {		return (			this.compilation.warnings.length > 0 ||			this.compilation.children.some(child => child.getStats().hasWarnings())		);	}	hasErrors() {		return (			this.compilation.errors.length > 0 ||			this.compilation.children.some(child => child.getStats().hasErrors())		);	}	// remove a prefixed "!" that can be specified to reverse sort order	normalizeFieldKey(field) {		if (field[0] === "!") {			return field.substr(1);		}		return field;	}	// if a field is prefixed by a "!" reverse sort order	sortOrderRegular(field) {		if (field[0] === "!") {			return false;		}		return true;	}	toJson(options, forToString) {		if (typeof options === "boolean" || typeof options === "string") {			options = Stats.presetToOptions(options);		} else if (!options) {			options = {};		}		const optionOrLocalFallback = (v, def) =>			v !== undefined ? v : options.all !== undefined ? options.all : def;		const testAgainstGivenOption = item => {			if (typeof item === "string") {				const regExp = new RegExp(					`[\\\\/]${item.replace(						// eslint-disable-next-line no-useless-escape						/[-[\]{}()*+?.\\^$|]/g,						"\\$&"					)}([\\\\/]|$|!|\\?)`				);				return ident => regExp.test(ident);			}			if (item && typeof item === "object" && typeof item.test === "function") {				return ident => item.test(ident);			}			if (typeof item === "function") {				return item;			}			if (typeof item === "boolean") {				return () => item;			}		};		const compilation = this.compilation;		const context = optionsOrFallback(			options.context,			compilation.compiler.context		);		const requestShortener =			compilation.compiler.context === context				? compilation.requestShortener				: new RequestShortener(context);		const showPerformance = optionOrLocalFallback(options.performance, true);		const showHash = optionOrLocalFallback(options.hash, true);		const showEnv = optionOrLocalFallback(options.env, false);		const showVersion = optionOrLocalFallback(options.version, true);		const showTimings = optionOrLocalFallback(options.timings, true);		const showBuiltAt = optionOrLocalFallback(options.builtAt, true);		const showAssets = optionOrLocalFallback(options.assets, true);		const showEntrypoints = optionOrLocalFallback(options.entrypoints, true);		const showChunkGroups = optionOrLocalFallback(			options.chunkGroups,			!forToString		);		const showChunks = optionOrLocalFallback(options.chunks, !forToString);		const showChunkModules = optionOrLocalFallback(options.chunkModules, true);		const showChunkOrigins = optionOrLocalFallback(			options.chunkOrigins,			!forToString		);		const showModules = optionOrLocalFallback(options.modules, true);		const showNestedModules = optionOrLocalFallback(			options.nestedModules,			true		);		const showModuleAssets = optionOrLocalFallback(			options.moduleAssets,			!forToString		);		const showDepth = optionOrLocalFallback(options.depth, !forToString);		const showCachedModules = optionOrLocalFallback(options.cached, true);		const showCachedAssets = optionOrLocalFallback(options.cachedAssets, true);		const showReasons = optionOrLocalFallback(options.reasons, !forToString);		const showUsedExports = optionOrLocalFallback(			options.usedExports,			!forToString		);		const showProvidedExports = optionOrLocalFallback(			options.providedExports,			!forToString		);		const showOptimizationBailout = optionOrLocalFallback(			options.optimizationBailout,			!forToString		);		const showChildren = optionOrLocalFallback(options.children, true);		const showSource = optionOrLocalFallback(options.source, !forToString);		const showModuleTrace = optionOrLocalFallback(options.moduleTrace, true);		const showErrors = optionOrLocalFallback(options.errors, true);		const showErrorDetails = optionOrLocalFallback(			options.errorDetails,			!forToString		);		const showWarnings = optionOrLocalFallback(options.warnings, true);		const warningsFilter = optionsOrFallback(options.warningsFilter, null);		const showPublicPath = optionOrLocalFallback(			options.publicPath,			!forToString		);		const showLogging = optionOrLocalFallback(			options.logging,			forToString ? "info" : true		);		const showLoggingTrace = optionOrLocalFallback(			options.loggingTrace,			!forToString		);		const loggingDebug = []			.concat(optionsOrFallback(options.loggingDebug, []))			.map(testAgainstGivenOption);		const excludeModules = []			.concat(optionsOrFallback(options.excludeModules, options.exclude, []))			.map(testAgainstGivenOption);		const excludeAssets = []			.concat(optionsOrFallback(options.excludeAssets, []))			.map(testAgainstGivenOption);		const maxModules = optionsOrFallback(			options.maxModules,			forToString ? 15 : Infinity		);		const sortModules = optionsOrFallback(options.modulesSort, "id");		const sortChunks = optionsOrFallback(options.chunksSort, "id");		const sortAssets = optionsOrFallback(options.assetsSort, "");		const showOutputPath = optionOrLocalFallback(			options.outputPath,			!forToString		);		if (!showCachedModules) {			excludeModules.push((ident, module) => !module.built);		}		const createModuleFilter = () => {			let i = 0;			return module => {				if (excludeModules.length > 0) {					const ident = requestShortener.shorten(module.resource);					const excluded = excludeModules.some(fn => fn(ident, module));					if (excluded) return false;				}				const result = i < maxModules;				i++;				return result;			};		};		const createAssetFilter = () => {			return asset => {				if (excludeAssets.length > 0) {					const ident = asset.name;					const excluded = excludeAssets.some(fn => fn(ident, asset));					if (excluded) return false;				}				return showCachedAssets || asset.emitted;			};		};		const sortByFieldAndOrder = (fieldKey, a, b) => {			if (a[fieldKey] === null && b[fieldKey] === null) return 0;			if (a[fieldKey] === null) return 1;			if (b[fieldKey] === null) return -1;			if (a[fieldKey] === b[fieldKey]) return 0;			if (typeof a[fieldKey] !== typeof b[fieldKey])				return typeof a[fieldKey] < typeof b[fieldKey] ? -1 : 1;			return a[fieldKey] < b[fieldKey] ? -1 : 1;		};		const sortByField = (field, originalArray) => {			const originalMap = originalArray.reduce((map, v, i) => {				map.set(v, i);				return map;			}, new Map());			return (a, b) => {				if (field) {					const fieldKey = this.normalizeFieldKey(field);					// if a field is prefixed with a "!" the sort is reversed!					const sortIsRegular = this.sortOrderRegular(field);					const cmp = sortByFieldAndOrder(						fieldKey,						sortIsRegular ? a : b,						sortIsRegular ? b : a					);					if (cmp) return cmp;				}				return originalMap.get(a) - originalMap.get(b);			};		};		const formatError = e => {			let text = "";			if (typeof e === "string") {				e = { message: e };			}			if (e.chunk) {				text += `chunk ${e.chunk.name || e.chunk.id}${					e.chunk.hasRuntime()						? " [entry]"						: e.chunk.canBeInitial()						? " [initial]"						: ""				}\n`;			}			if (e.file) {				text += `${e.file}\n`;			}			if (				e.module &&				e.module.readableIdentifier &&				typeof e.module.readableIdentifier === "function"			) {				text += this.formatFilePath(					e.module.readableIdentifier(requestShortener)				);				if (typeof e.loc === "object") {					const locInfo = formatLocation(e.loc);					if (locInfo) text += ` ${locInfo}`;				}				text += "\n";			}			text += e.message;			if (showErrorDetails && e.details) {				text += `\n${e.details}`;			}			if (showErrorDetails && e.missing) {				text += e.missing.map(item => `\n[${item}]`).join("");			}			if (showModuleTrace && e.origin) {				text += `\n @ ${this.formatFilePath(					e.origin.readableIdentifier(requestShortener)				)}`;				if (typeof e.originLoc === "object") {					const locInfo = formatLocation(e.originLoc);					if (locInfo) text += ` ${locInfo}`;				}				if (e.dependencies) {					for (const dep of e.dependencies) {						if (!dep.loc) continue;						if (typeof dep.loc === "string") continue;						const locInfo = formatLocation(dep.loc);						if (!locInfo) continue;						text += ` ${locInfo}`;					}				}				let current = e.origin;				while (current.issuer) {					current = current.issuer;					text += `\n @ ${current.readableIdentifier(requestShortener)}`;				}			}			return text;		};		const obj = {			errors: compilation.errors.map(formatError),			warnings: Stats.filterWarnings(				compilation.warnings.map(formatError),				warningsFilter			)		};		//We just hint other renderers since actually omitting		//errors/warnings from the JSON would be kind of weird.		Object.defineProperty(obj, "_showWarnings", {			value: showWarnings,			enumerable: false		});		Object.defineProperty(obj, "_showErrors", {			value: showErrors,			enumerable: false		});		if (showVersion) {			obj.version = require("../package.json").version;		}		if (showHash) obj.hash = this.hash;		if (showTimings && this.startTime && this.endTime) {			obj.time = this.endTime - this.startTime;		}		if (showBuiltAt && this.endTime) {			obj.builtAt = this.endTime;		}		if (showEnv && options._env) {			obj.env = options._env;		}		if (compilation.needAdditionalPass) {			obj.needAdditionalPass = true;		}		if (showPublicPath) {			obj.publicPath = this.compilation.mainTemplate.getPublicPath({				hash: this.compilation.hash			});		}		if (showOutputPath) {			obj.outputPath = this.compilation.mainTemplate.outputOptions.path;		}		if (showAssets) {			const assetsByFile = {};			const compilationAssets = compilation				.getAssets()				.sort((a, b) => (a.name < b.name ? -1 : 1));			obj.assetsByChunkName = {};			obj.assets = compilationAssets				.map(({ name, source, info }) => {					const obj = {						name,						size: source.size(),						chunks: [],						chunkNames: [],						info,						// TODO webpack 5: remove .emitted						emitted: source.emitted || compilation.emittedAssets.has(name)					};					if (showPerformance) {						obj.isOverSizeLimit = source.isOverSizeLimit;					}					assetsByFile[name] = obj;					return obj;				})				.filter(createAssetFilter());			obj.filteredAssets = compilationAssets.length - obj.assets.length;			for (const chunk of compilation.chunks) {				for (const asset of chunk.files) {					if (assetsByFile[asset]) {						for (const id of chunk.ids) {							assetsByFile[asset].chunks.push(id);						}						if (chunk.name) {							assetsByFile[asset].chunkNames.push(chunk.name);							if (obj.assetsByChunkName[chunk.name]) {								obj.assetsByChunkName[chunk.name] = []									.concat(obj.assetsByChunkName[chunk.name])									.concat([asset]);							} else {								obj.assetsByChunkName[chunk.name] = asset;							}						}					}				}			}			obj.assets.sort(sortByField(sortAssets, obj.assets));		}		const fnChunkGroup = groupMap => {			const obj = {};			for (const keyValuePair of groupMap) {				const name = keyValuePair[0];				const cg = keyValuePair[1];				const children = cg.getChildrenByOrders();				obj[name] = {					chunks: cg.chunks.map(c => c.id),					assets: cg.chunks.reduce(						(array, c) => array.concat(c.files || []),						[]					),					children: Object.keys(children).reduce((obj, key) => {						const groups = children[key];						obj[key] = groups.map(group => ({							name: group.name,							chunks: group.chunks.map(c => c.id),							assets: group.chunks.reduce(								(array, c) => array.concat(c.files || []),								[]							)						}));						return obj;					}, Object.create(null)),					childAssets: Object.keys(children).reduce((obj, key) => {						const groups = children[key];						obj[key] = Array.from(							groups.reduce((set, group) => {								for (const chunk of group.chunks) {									for (const asset of chunk.files) {										set.add(asset);									}								}								return set;							}, new Set())						);						return obj;					}, Object.create(null))				};				if (showPerformance) {					obj[name].isOverSizeLimit = cg.isOverSizeLimit;				}			}			return obj;		};		if (showEntrypoints) {			obj.entrypoints = fnChunkGroup(compilation.entrypoints);		}		if (showChunkGroups) {			obj.namedChunkGroups = fnChunkGroup(compilation.namedChunkGroups);		}		const fnModule = module => {			const path = [];			let current = module;			while (current.issuer) {				path.push((current = current.issuer));			}			path.reverse();			const obj = {				id: module.id,				identifier: module.identifier(),				name: module.readableIdentifier(requestShortener),				index: module.index,				index2: module.index2,				size: module.size(),				cacheable: module.buildInfo.cacheable,				built: !!module.built,				optional: module.optional,				prefetched: module.prefetched,				chunks: Array.from(module.chunksIterable, chunk => chunk.id),				issuer: module.issuer && module.issuer.identifier(),				issuerId: module.issuer && module.issuer.id,				issuerName:					module.issuer && module.issuer.readableIdentifier(requestShortener),				issuerPath:					module.issuer &&					path.map(module => ({						id: module.id,						identifier: module.identifier(),						name: module.readableIdentifier(requestShortener),						profile: module.profile					})),				profile: module.profile,				failed: !!module.error,				errors: module.errors ? module.errors.length : 0,				warnings: module.warnings ? module.warnings.length : 0			};			if (showModuleAssets) {				obj.assets = Object.keys(module.buildInfo.assets || {});			}			if (showReasons) {				obj.reasons = module.reasons					.sort((a, b) => {						if (a.module && !b.module) return -1;						if (!a.module && b.module) return 1;						if (a.module && b.module) {							const cmp = compareId(a.module.id, b.module.id);							if (cmp) return cmp;						}						if (a.dependency && !b.dependency) return -1;						if (!a.dependency && b.dependency) return 1;						if (a.dependency && b.dependency) {							const cmp = compareLocations(a.dependency.loc, b.dependency.loc);							if (cmp) return cmp;							if (a.dependency.type < b.dependency.type) return -1;							if (a.dependency.type > b.dependency.type) return 1;						}						return 0;					})					.map(reason => {						const obj = {							moduleId: reason.module ? reason.module.id : null,							moduleIdentifier: reason.module								? reason.module.identifier()								: null,							module: reason.module								? reason.module.readableIdentifier(requestShortener)								: null,							moduleName: reason.module								? reason.module.readableIdentifier(requestShortener)								: null,							type: reason.dependency ? reason.dependency.type : null,							explanation: reason.explanation,							userRequest: reason.dependency								? reason.dependency.userRequest								: null						};						if (reason.dependency) {							const locInfo = formatLocation(reason.dependency.loc);							if (locInfo) {								obj.loc = locInfo;							}						}						return obj;					});			}			if (showUsedExports) {				if (module.used === true) {					obj.usedExports = module.usedExports;				} else if (module.used === false) {					obj.usedExports = false;				}			}			if (showProvidedExports) {				obj.providedExports = Array.isArray(module.buildMeta.providedExports)					? module.buildMeta.providedExports					: null;			}			if (showOptimizationBailout) {				obj.optimizationBailout = module.optimizationBailout.map(item => {					if (typeof item === "function") return item(requestShortener);					return item;				});			}			if (showDepth) {				obj.depth = module.depth;			}			if (showNestedModules) {				if (module.modules) {					const modules = module.modules;					obj.modules = modules						.sort(sortByField("depth", modules))						.filter(createModuleFilter())						.map(fnModule);					obj.filteredModules = modules.length - obj.modules.length;					obj.modules.sort(sortByField(sortModules, obj.modules));				}			}			if (showSource && module._source) {				obj.source = module._source.source();			}			return obj;		};		if (showChunks) {			obj.chunks = compilation.chunks.map(chunk => {				const parents = new Set();				const children = new Set();				const siblings = new Set();				const childIdByOrder = chunk.getChildIdsByOrders();				for (const chunkGroup of chunk.groupsIterable) {					for (const parentGroup of chunkGroup.parentsIterable) {						for (const chunk of parentGroup.chunks) {							parents.add(chunk.id);						}					}					for (const childGroup of chunkGroup.childrenIterable) {						for (const chunk of childGroup.chunks) {							children.add(chunk.id);						}					}					for (const sibling of chunkGroup.chunks) {						if (sibling !== chunk) siblings.add(sibling.id);					}				}				const obj = {					id: chunk.id,					rendered: chunk.rendered,					initial: chunk.canBeInitial(),					entry: chunk.hasRuntime(),					recorded: chunk.recorded,					reason: chunk.chunkReason,					size: chunk.modulesSize(),					names: chunk.name ? [chunk.name] : [],					files: chunk.files.slice(),					hash: chunk.renderedHash,					siblings: Array.from(siblings).sort(compareId),					parents: Array.from(parents).sort(compareId),					children: Array.from(children).sort(compareId),					childrenByOrder: childIdByOrder				};				if (showChunkModules) {					const modules = chunk.getModules();					obj.modules = modules						.slice()						.sort(sortByField("depth", modules))						.filter(createModuleFilter())						.map(fnModule);					obj.filteredModules = chunk.getNumberOfModules() - obj.modules.length;					obj.modules.sort(sortByField(sortModules, obj.modules));				}				if (showChunkOrigins) {					obj.origins = Array.from(chunk.groupsIterable, g => g.origins)						.reduce((a, b) => a.concat(b), [])						.map(origin => ({							moduleId: origin.module ? origin.module.id : undefined,							module: origin.module ? origin.module.identifier() : "",							moduleIdentifier: origin.module ? origin.module.identifier() : "",							moduleName: origin.module								? origin.module.readableIdentifier(requestShortener)								: "",							loc: formatLocation(origin.loc),							request: origin.request,							reasons: origin.reasons || []						}))						.sort((a, b) => {							const cmp1 = compareId(a.moduleId, b.moduleId);							if (cmp1) return cmp1;							const cmp2 = compareId(a.loc, b.loc);							if (cmp2) return cmp2;							const cmp3 = compareId(a.request, b.request);							if (cmp3) return cmp3;							return 0;						});				}				return obj;			});			obj.chunks.sort(sortByField(sortChunks, obj.chunks));		}		if (showModules) {			obj.modules = compilation.modules				.slice()				.sort(sortByField("depth", compilation.modules))				.filter(createModuleFilter())				.map(fnModule);			obj.filteredModules = compilation.modules.length - obj.modules.length;			obj.modules.sort(sortByField(sortModules, obj.modules));		}		if (showLogging) {			const util = require("util");			obj.logging = {};			let acceptedTypes;			let collapsedGroups = false;			switch (showLogging) {				case "none":					acceptedTypes = new Set([]);					break;				case "error":					acceptedTypes = new Set([LogType.error]);					break;				case "warn":					acceptedTypes = new Set([LogType.error, LogType.warn]);					break;				case "info":					acceptedTypes = new Set([LogType.error, LogType.warn, LogType.info]);					break;				case true:				case "log":					acceptedTypes = new Set([						LogType.error,						LogType.warn,						LogType.info,						LogType.log,						LogType.group,						LogType.groupEnd,						LogType.groupCollapsed,						LogType.clear					]);					break;				case "verbose":					acceptedTypes = new Set([						LogType.error,						LogType.warn,						LogType.info,						LogType.log,						LogType.group,						LogType.groupEnd,						LogType.groupCollapsed,						LogType.profile,						LogType.profileEnd,						LogType.time,						LogType.status,						LogType.clear					]);					collapsedGroups = true;					break;			}			for (const [origin, logEntries] of compilation.logging) {				const debugMode = loggingDebug.some(fn => fn(origin));				let collapseCounter = 0;				let processedLogEntries = logEntries;				if (!debugMode) {					processedLogEntries = processedLogEntries.filter(entry => {						if (!acceptedTypes.has(entry.type)) return false;						if (!collapsedGroups) {							switch (entry.type) {								case LogType.groupCollapsed:									collapseCounter++;									return collapseCounter === 1;								case LogType.group:									if (collapseCounter > 0) collapseCounter++;									return collapseCounter === 0;								case LogType.groupEnd:									if (collapseCounter > 0) {										collapseCounter--;										return false;									}									return true;								default:									return collapseCounter === 0;							}						}						return true;					});				}				processedLogEntries = processedLogEntries.map(entry => {					let message = undefined;					if (entry.type === LogType.time) {						message = `${entry.args[0]}: ${entry.args[1] * 1000 +							entry.args[2] / 1000000}ms`;					} else if (entry.args && entry.args.length > 0) {						message = util.format(entry.args[0], ...entry.args.slice(1));					}					return {						type:							(debugMode || collapsedGroups) &&							entry.type === LogType.groupCollapsed								? LogType.group								: entry.type,						message,						trace: showLoggingTrace && entry.trace ? entry.trace : undefined					};				});				let name = identifierUtils					.makePathsRelative(context, origin, compilation.cache)					.replace(/\|/g, " ");				if (name in obj.logging) {					let i = 1;					while (`${name}#${i}` in obj.logging) {						i++;					}					name = `${name}#${i}`;				}				obj.logging[name] = {					entries: processedLogEntries,					filteredEntries: logEntries.length - processedLogEntries.length,					debug: debugMode				};			}		}		if (showChildren) {			obj.children = compilation.children.map((child, idx) => {				const childOptions = Stats.getChildOptions(options, idx);				const obj = new Stats(child).toJson(childOptions, forToString);				delete obj.hash;				delete obj.version;				if (child.name) {					obj.name = identifierUtils.makePathsRelative(						context,						child.name,						compilation.cache					);				}				return obj;			});		}		return obj;	}	toString(options) {		if (typeof options === "boolean" || typeof options === "string") {			options = Stats.presetToOptions(options);		} else if (!options) {			options = {};		}		const useColors = optionsOrFallback(options.colors, false);		const obj = this.toJson(options, true);		return Stats.jsonToString(obj, useColors);	}	static jsonToString(obj, useColors) {		const buf = [];		const defaultColors = {			bold: "\u001b[1m",			yellow: "\u001b[1m\u001b[33m",			red: "\u001b[1m\u001b[31m",			green: "\u001b[1m\u001b[32m",			cyan: "\u001b[1m\u001b[36m",			magenta: "\u001b[1m\u001b[35m"		};		const colors = Object.keys(defaultColors).reduce(			(obj, color) => {				obj[color] = str => {					if (useColors) {						buf.push(							useColors === true || useColors[color] === undefined								? defaultColors[color]								: useColors[color]						);					}					buf.push(str);					if (useColors) {						buf.push("\u001b[39m\u001b[22m");					}				};				return obj;			},			{				normal: str => buf.push(str)			}		);		const coloredTime = time => {			let times = [800, 400, 200, 100];			if (obj.time) {				times = [obj.time / 2, obj.time / 4, obj.time / 8, obj.time / 16];			}			if (time < times[3]) colors.normal(`${time}ms`);			else if (time < times[2]) colors.bold(`${time}ms`);			else if (time < times[1]) colors.green(`${time}ms`);			else if (time < times[0]) colors.yellow(`${time}ms`);			else colors.red(`${time}ms`);		};		const newline = () => buf.push("\n");		const getText = (arr, row, col) => {			return arr[row][col].value;		};		const table = (array, align, splitter) => {			const rows = array.length;			const cols = array[0].length;			const colSizes = new Array(cols);			for (let col = 0; col < cols; col++) {				colSizes[col] = 0;			}			for (let row = 0; row < rows; row++) {				for (let col = 0; col < cols; col++) {					const value = `${getText(array, row, col)}`;					if (value.length > colSizes[col]) {						colSizes[col] = value.length;					}				}			}			for (let row = 0; row < rows; row++) {				for (let col = 0; col < cols; col++) {					const format = array[row][col].color;					const value = `${getText(array, row, col)}`;					let l = value.length;					if (align[col] === "l") {						format(value);					}					for (; l < colSizes[col] && col !== cols - 1; l++) {						colors.normal(" ");					}					if (align[col] === "r") {						format(value);					}					if (col + 1 < cols && colSizes[col] !== 0) {						colors.normal(splitter || "  ");					}				}				newline();			}		};		const getAssetColor = (asset, defaultColor) => {			if (asset.isOverSizeLimit) {				return colors.yellow;			}			return defaultColor;		};		if (obj.hash) {			colors.normal("Hash: ");			colors.bold(obj.hash);			newline();		}		if (obj.version) {			colors.normal("Version: webpack ");			colors.bold(obj.version);			newline();		}		if (typeof obj.time === "number") {			colors.normal("Time: ");			colors.bold(obj.time);			colors.normal("ms");			newline();		}		if (typeof obj.builtAt === "number") {			const builtAtDate = new Date(obj.builtAt);			let timeZone = undefined;			try {				builtAtDate.toLocaleTimeString();			} catch (err) {				// Force UTC if runtime timezone is unsupported				timeZone = "UTC";			}			colors.normal("Built at: ");			colors.normal(				builtAtDate.toLocaleDateString(undefined, {					day: "2-digit",					month: "2-digit",					year: "numeric",					timeZone				})			);			colors.normal(" ");			colors.bold(builtAtDate.toLocaleTimeString(undefined, { timeZone }));			newline();		}		if (obj.env) {			colors.normal("Environment (--env): ");			colors.bold(JSON.stringify(obj.env, null, 2));			newline();		}		if (obj.publicPath) {			colors.normal("PublicPath: ");			colors.bold(obj.publicPath);			newline();		}		if (obj.assets && obj.assets.length > 0) {			const t = [				[					{						value: "Asset",						color: colors.bold					},					{						value: "Size",						color: colors.bold					},					{						value: "Chunks",						color: colors.bold					},					{						value: "",						color: colors.bold					},					{						value: "",						color: colors.bold					},					{						value: "Chunk Names",						color: colors.bold					}				]			];			for (const asset of obj.assets) {				t.push([					{						value: asset.name,						color: getAssetColor(asset, colors.green)					},					{						value: SizeFormatHelpers.formatSize(asset.size),						color: getAssetColor(asset, colors.normal)					},					{						value: asset.chunks.join(", "),						color: colors.bold					},					{						value: [							asset.emitted && "[emitted]",							asset.info.immutable && "[immutable]",							asset.info.development && "[dev]",							asset.info.hotModuleReplacement && "[hmr]"						]							.filter(Boolean)							.join(" "),						color: colors.green					},					{						value: asset.isOverSizeLimit ? "[big]" : "",						color: getAssetColor(asset, colors.normal)					},					{						value: asset.chunkNames.join(", "),						color: colors.normal					}				]);			}			table(t, "rrrlll");		}		if (obj.filteredAssets > 0) {			colors.normal(" ");			if (obj.assets.length > 0) colors.normal("+ ");			colors.normal(obj.filteredAssets);			if (obj.assets.length > 0) colors.normal(" hidden");			colors.normal(obj.filteredAssets !== 1 ? " assets" : " asset");			newline();		}		const processChunkGroups = (namedGroups, prefix) => {			for (const name of Object.keys(namedGroups)) {				const cg = namedGroups[name];				colors.normal(`${prefix} `);				colors.bold(name);				if (cg.isOverSizeLimit) {					colors.normal(" ");					colors.yellow("[big]");				}				colors.normal(" =");				for (const asset of cg.assets) {					colors.normal(" ");					colors.green(asset);				}				for (const name of Object.keys(cg.childAssets)) {					const assets = cg.childAssets[name];					if (assets && assets.length > 0) {						colors.normal(" ");						colors.magenta(`(${name}:`);						for (const asset of assets) {							colors.normal(" ");							colors.green(asset);						}						colors.magenta(")");					}				}				newline();			}		};		if (obj.entrypoints) {			processChunkGroups(obj.entrypoints, "Entrypoint");		}		if (obj.namedChunkGroups) {			let outputChunkGroups = obj.namedChunkGroups;			if (obj.entrypoints) {				outputChunkGroups = Object.keys(outputChunkGroups)					.filter(name => !obj.entrypoints[name])					.reduce((result, name) => {						result[name] = obj.namedChunkGroups[name];						return result;					}, {});			}			processChunkGroups(outputChunkGroups, "Chunk Group");		}		const modulesByIdentifier = {};		if (obj.modules) {			for (const module of obj.modules) {				modulesByIdentifier[`$${module.identifier}`] = module;			}		} else if (obj.chunks) {			for (const chunk of obj.chunks) {				if (chunk.modules) {					for (const module of chunk.modules) {						modulesByIdentifier[`$${module.identifier}`] = module;					}				}			}		}		const processModuleAttributes = module => {			colors.normal(" ");			colors.normal(SizeFormatHelpers.formatSize(module.size));			if (module.chunks) {				for (const chunk of module.chunks) {					colors.normal(" {");					colors.yellow(chunk);					colors.normal("}");				}			}			if (typeof module.depth === "number") {				colors.normal(` [depth ${module.depth}]`);			}			if (module.cacheable === false) {				colors.red(" [not cacheable]");			}			if (module.optional) {				colors.yellow(" [optional]");			}			if (module.built) {				colors.green(" [built]");			}			if (module.assets && module.assets.length) {				colors.magenta(					` [${module.assets.length} asset${						module.assets.length === 1 ? "" : "s"					}]`				);			}			if (module.prefetched) {				colors.magenta(" [prefetched]");			}			if (module.failed) colors.red(" [failed]");			if (module.warnings) {				colors.yellow(					` [${module.warnings} warning${module.warnings === 1 ? "" : "s"}]`				);			}			if (module.errors) {				colors.red(					` [${module.errors} error${module.errors === 1 ? "" : "s"}]`				);			}		};		const processModuleContent = (module, prefix) => {			if (Array.isArray(module.providedExports)) {				colors.normal(prefix);				if (module.providedExports.length === 0) {					colors.cyan("[no exports]");				} else {					colors.cyan(`[exports: ${module.providedExports.join(", ")}]`);				}				newline();			}			if (module.usedExports !== undefined) {				if (module.usedExports !== true) {					colors.normal(prefix);					if (module.usedExports === null) {						colors.cyan("[used exports unknown]");					} else if (module.usedExports === false) {						colors.cyan("[no exports used]");					} else if (						Array.isArray(module.usedExports) &&						module.usedExports.length === 0					) {						colors.cyan("[no exports used]");					} else if (Array.isArray(module.usedExports)) {						const providedExportsCount = Array.isArray(module.providedExports)							? module.providedExports.length							: null;						if (							providedExportsCount !== null &&							providedExportsCount === module.usedExports.length						) {							colors.cyan("[all exports used]");						} else {							colors.cyan(								`[only some exports used: ${module.usedExports.join(", ")}]`							);						}					}					newline();				}			}			if (Array.isArray(module.optimizationBailout)) {				for (const item of module.optimizationBailout) {					colors.normal(prefix);					colors.yellow(item);					newline();				}			}			if (module.reasons) {				for (const reason of module.reasons) {					colors.normal(prefix);					if (reason.type) {						colors.normal(reason.type);						colors.normal(" ");					}					if (reason.userRequest) {						colors.cyan(reason.userRequest);						colors.normal(" ");					}					if (reason.moduleId !== null) {						colors.normal("[");						colors.normal(reason.moduleId);						colors.normal("]");					}					if (reason.module && reason.module !== reason.moduleId) {						colors.normal(" ");						colors.magenta(reason.module);					}					if (reason.loc) {						colors.normal(" ");						colors.normal(reason.loc);					}					if (reason.explanation) {						colors.normal(" ");						colors.cyan(reason.explanation);					}					newline();				}			}			if (module.profile) {				colors.normal(prefix);				let sum = 0;				if (module.issuerPath) {					for (const m of module.issuerPath) {						colors.normal("[");						colors.normal(m.id);						colors.normal("] ");						if (m.profile) {							const time = (m.profile.factory || 0) + (m.profile.building || 0);							coloredTime(time);							sum += time;							colors.normal(" ");						}						colors.normal("-> ");					}				}				for (const key of Object.keys(module.profile)) {					colors.normal(`${key}:`);					const time = module.profile[key];					coloredTime(time);					colors.normal(" ");					sum += time;				}				colors.normal("= ");				coloredTime(sum);				newline();			}			if (module.modules) {				processModulesList(module, prefix + "| ");			}		};		const processModulesList = (obj, prefix) => {			if (obj.modules) {				let maxModuleId = 0;				for (const module of obj.modules) {					if (typeof module.id === "number") {						if (maxModuleId < module.id) maxModuleId = module.id;					}				}				let contentPrefix = prefix + "    ";				if (maxModuleId >= 10) contentPrefix += " ";				if (maxModuleId >= 100) contentPrefix += " ";				if (maxModuleId >= 1000) contentPrefix += " ";				for (const module of obj.modules) {					colors.normal(prefix);					const name = module.name || module.identifier;					if (typeof module.id === "string" || typeof module.id === "number") {						if (typeof module.id === "number") {							if (module.id < 1000 && maxModuleId >= 1000) colors.normal(" ");							if (module.id < 100 && maxModuleId >= 100) colors.normal(" ");							if (module.id < 10 && maxModuleId >= 10) colors.normal(" ");						} else {							if (maxModuleId >= 1000) colors.normal(" ");							if (maxModuleId >= 100) colors.normal(" ");							if (maxModuleId >= 10) colors.normal(" ");						}						if (name !== module.id) {							colors.normal("[");							colors.normal(module.id);							colors.normal("]");							colors.normal(" ");						} else {							colors.normal("[");							colors.bold(module.id);							colors.normal("]");						}					}					if (name !== module.id) {						colors.bold(name);					}					processModuleAttributes(module);					newline();					processModuleContent(module, contentPrefix);				}				if (obj.filteredModules > 0) {					colors.normal(prefix);					colors.normal("   ");					if (obj.modules.length > 0) colors.normal(" + ");					colors.normal(obj.filteredModules);					if (obj.modules.length > 0) colors.normal(" hidden");					colors.normal(obj.filteredModules !== 1 ? " modules" : " module");					newline();				}			}		};		if (obj.chunks) {			for (const chunk of obj.chunks) {				colors.normal("chunk ");				if (chunk.id < 1000) colors.normal(" ");				if (chunk.id < 100) colors.normal(" ");				if (chunk.id < 10) colors.normal(" ");				colors.normal("{");				colors.yellow(chunk.id);				colors.normal("} ");				colors.green(chunk.files.join(", "));				if (chunk.names && chunk.names.length > 0) {					colors.normal(" (");					colors.normal(chunk.names.join(", "));					colors.normal(")");				}				colors.normal(" ");				colors.normal(SizeFormatHelpers.formatSize(chunk.size));				for (const id of chunk.parents) {					colors.normal(" <{");					colors.yellow(id);					colors.normal("}>");				}				for (const id of chunk.siblings) {					colors.normal(" ={");					colors.yellow(id);					colors.normal("}=");				}				for (const id of chunk.children) {					colors.normal(" >{");					colors.yellow(id);					colors.normal("}<");				}				if (chunk.childrenByOrder) {					for (const name of Object.keys(chunk.childrenByOrder)) {						const children = chunk.childrenByOrder[name];						colors.normal(" ");						colors.magenta(`(${name}:`);						for (const id of children) {							colors.normal(" {");							colors.yellow(id);							colors.normal("}");						}						colors.magenta(")");					}				}				if (chunk.entry) {					colors.yellow(" [entry]");				} else if (chunk.initial) {					colors.yellow(" [initial]");				}				if (chunk.rendered) {					colors.green(" [rendered]");				}				if (chunk.recorded) {					colors.green(" [recorded]");				}				if (chunk.reason) {					colors.yellow(` ${chunk.reason}`);				}				newline();				if (chunk.origins) {					for (const origin of chunk.origins) {						colors.normal("    > ");						if (origin.reasons && origin.reasons.length) {							colors.yellow(origin.reasons.join(" "));							colors.normal(" ");						}						if (origin.request) {							colors.normal(origin.request);							colors.normal(" ");						}						if (origin.module) {							colors.normal("[");							colors.normal(origin.moduleId);							colors.normal("] ");							const module = modulesByIdentifier[`$${origin.module}`];							if (module) {								colors.bold(module.name);								colors.normal(" ");							}						}						if (origin.loc) {							colors.normal(origin.loc);						}						newline();					}				}				processModulesList(chunk, " ");			}		}		processModulesList(obj, "");		if (obj.logging) {			for (const origin of Object.keys(obj.logging)) {				const logData = obj.logging[origin];				if (logData.entries.length > 0) {					newline();					if (logData.debug) {						colors.red("DEBUG ");					}					colors.bold("LOG from " + origin);					newline();					let indent = "";					for (const entry of logData.entries) {						let color = colors.normal;						let prefix = "    ";						switch (entry.type) {							case LogType.clear:								colors.normal(`${indent}-------`);								newline();								continue;							case LogType.error:								color = colors.red;								prefix = "<e> ";								break;							case LogType.warn:								color = colors.yellow;								prefix = "<w> ";								break;							case LogType.info:								color = colors.green;								prefix = "<i> ";								break;							case LogType.log:								color = colors.bold;								break;							case LogType.trace:							case LogType.debug:								color = colors.normal;								break;							case LogType.status:								color = colors.magenta;								prefix = "<s> ";								break;							case LogType.profile:								color = colors.magenta;								prefix = "<p> ";								break;							case LogType.profileEnd:								color = colors.magenta;								prefix = "</p> ";								break;							case LogType.time:								color = colors.magenta;								prefix = "<t> ";								break;							case LogType.group:								color = colors.cyan;								prefix = "<-> ";								break;							case LogType.groupCollapsed:								color = colors.cyan;								prefix = "<+> ";								break;							case LogType.groupEnd:								if (indent.length >= 2)									indent = indent.slice(0, indent.length - 2);								continue;						}						if (entry.message) {							for (const line of entry.message.split("\n")) {								colors.normal(`${indent}${prefix}`);								color(line);								newline();							}						}						if (entry.trace) {							for (const line of entry.trace) {								colors.normal(`${indent}| ${line}`);								newline();							}						}						switch (entry.type) {							case LogType.group:								indent += "  ";								break;						}					}					if (logData.filteredEntries) {						colors.normal(`+ ${logData.filteredEntries} hidden lines`);						newline();					}				}			}		}		if (obj._showWarnings && obj.warnings) {			for (const warning of obj.warnings) {				newline();				colors.yellow(`WARNING in ${warning}`);				newline();			}		}		if (obj._showErrors && obj.errors) {			for (const error of obj.errors) {				newline();				colors.red(`ERROR in ${error}`);				newline();			}		}		if (obj.children) {			for (const child of obj.children) {				const childString = Stats.jsonToString(child, useColors);				if (childString) {					if (child.name) {						colors.normal("Child ");						colors.bold(child.name);						colors.normal(":");					} else {						colors.normal("Child");					}					newline();					buf.push("    ");					buf.push(childString.replace(/\n/g, "\n    "));					newline();				}			}		}		if (obj.needAdditionalPass) {			colors.yellow(				"Compilation needs an additional pass and will compile again."			);		}		while (buf[buf.length - 1] === "\n") {			buf.pop();		}		return buf.join("");	}	static presetToOptions(name) {		// Accepted values: none, errors-only, minimal, normal, detailed, verbose		// Any other falsy value will behave as 'none', truthy values as 'normal'		const pn =			(typeof name === "string" && name.toLowerCase()) || name || "none";		switch (pn) {			case "none":				return {					all: false				};			case "verbose":				return {					entrypoints: true,					chunkGroups: true,					modules: false,					chunks: true,					chunkModules: true,					chunkOrigins: true,					depth: true,					env: true,					reasons: true,					usedExports: true,					providedExports: true,					optimizationBailout: true,					errorDetails: true,					publicPath: true,					logging: "verbose",					exclude: false,					maxModules: Infinity				};			case "detailed":				return {					entrypoints: true,					chunkGroups: true,					chunks: true,					chunkModules: false,					chunkOrigins: true,					depth: true,					usedExports: true,					providedExports: true,					optimizationBailout: true,					errorDetails: true,					publicPath: true,					logging: true,					exclude: false,					maxModules: Infinity				};			case "minimal":				return {					all: false,					modules: true,					maxModules: 0,					errors: true,					warnings: true,					logging: "warn"				};			case "errors-only":				return {					all: false,					errors: true,					moduleTrace: true,					logging: "error"				};			case "errors-warnings":				return {					all: false,					errors: true,					warnings: true,					logging: "warn"				};			default:				return {};		}	}	static getChildOptions(options, idx) {		let innerOptions;		if (Array.isArray(options.children)) {			if (idx < options.children.length) {				innerOptions = options.children[idx];			}		} else if (typeof options.children === "object" && options.children) {			innerOptions = options.children;		}		if (typeof innerOptions === "boolean" || typeof innerOptions === "string") {			innerOptions = Stats.presetToOptions(innerOptions);		}		if (!innerOptions) {			return options;		}		const childOptions = Object.assign({}, options);		delete childOptions.children; // do not inherit children		return Object.assign(childOptions, innerOptions);	}}module.exports = Stats;
 |