source: frontend/node_modules/webpack/lib/MultiStats.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 6.2 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const identifierUtils = require("./util/identifier");
9
10/** @typedef {import("../declarations/WebpackOptions").StatsOptions} StatsOptions */
11/** @typedef {import("../declarations/WebpackOptions").StatsValue} StatsValue */
12/** @typedef {import("./Compilation").CreateStatsOptionsContext} CreateStatsOptionsContext */
13/** @typedef {import("./Compilation").NormalizedStatsOptions} NormalizedStatsOptions */
14/** @typedef {import("./Stats")} Stats */
15/** @typedef {import("./stats/DefaultStatsFactoryPlugin").KnownStatsCompilation} KnownStatsCompilation */
16/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsCompilation} StatsCompilation */
17/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsError} StatsError */
18
19/**
20 * Returns indent.
21 * @param {string} str string
22 * @param {string} prefix pref
23 * @returns {string} indent
24 */
25const indent = (str, prefix) => {
26 const rem = str.replace(/\n([^\n])/g, `\n${prefix}$1`);
27 return prefix + rem;
28};
29
30/** @typedef {StatsOptions} MultiStatsOptions */
31/** @typedef {{ version: boolean, hash: boolean, errorsCount: boolean, warningsCount: boolean, errors: boolean, warnings: boolean, children: NormalizedStatsOptions[] }} ChildOptions */
32
33class MultiStats {
34 /**
35 * Creates an instance of MultiStats.
36 * @param {Stats[]} stats the child stats
37 */
38 constructor(stats) {
39 this.stats = stats;
40 }
41
42 get hash() {
43 return this.stats.map((stat) => stat.hash).join("");
44 }
45
46 /**
47 * Checks whether this multi stats has errors.
48 * @returns {boolean} true if a child compilation encountered an error
49 */
50 hasErrors() {
51 return this.stats.some((stat) => stat.hasErrors());
52 }
53
54 /**
55 * Checks whether this multi stats has warnings.
56 * @returns {boolean} true if a child compilation had a warning
57 */
58 hasWarnings() {
59 return this.stats.some((stat) => stat.hasWarnings());
60 }
61
62 /**
63 * Create child options.
64 * @param {undefined | StatsValue} options stats options
65 * @param {CreateStatsOptionsContext} context context
66 * @returns {ChildOptions} context context
67 */
68 _createChildOptions(options, context) {
69 const getCreateStatsOptions = () => {
70 if (!options) {
71 options = {};
72 }
73
74 const { children: childrenOptions = undefined, ...baseOptions } =
75 typeof options === "string"
76 ? { preset: options }
77 : /** @type {StatsOptions} */ (options);
78
79 return { childrenOptions, baseOptions };
80 };
81
82 const children = this.stats.map((stat, idx) => {
83 if (typeof options === "boolean") {
84 return stat.compilation.createStatsOptions(options, context);
85 }
86 const { childrenOptions, baseOptions } = getCreateStatsOptions();
87 const childOptions = Array.isArray(childrenOptions)
88 ? childrenOptions[idx]
89 : childrenOptions;
90 if (typeof childOptions === "boolean") {
91 return stat.compilation.createStatsOptions(childOptions, context);
92 }
93 return stat.compilation.createStatsOptions(
94 {
95 ...baseOptions,
96 ...(typeof childOptions === "string"
97 ? { preset: childOptions }
98 : childOptions && typeof childOptions === "object"
99 ? childOptions
100 : undefined)
101 },
102 context
103 );
104 });
105 return {
106 version: children.every((o) => o.version),
107 hash: children.every((o) => o.hash),
108 errorsCount: children.every((o) => o.errorsCount),
109 warningsCount: children.every((o) => o.warningsCount),
110 errors: children.every((o) => o.errors),
111 warnings: children.every((o) => o.warnings),
112 children
113 };
114 }
115
116 /**
117 * Returns json output.
118 * @param {StatsValue=} options stats options
119 * @returns {StatsCompilation} json output
120 */
121 toJson(options) {
122 const childOptions = this._createChildOptions(options, {
123 forToString: false
124 });
125 /** @type {KnownStatsCompilation} */
126 const obj = {};
127 obj.children = this.stats.map((stat, idx) => {
128 const obj = stat.toJson(childOptions.children[idx]);
129 const compilationName = stat.compilation.name;
130 const name =
131 compilationName &&
132 identifierUtils.makePathsRelative(
133 stat.compilation.compiler.context,
134 compilationName,
135 stat.compilation.compiler.root
136 );
137 obj.name = name;
138 return obj;
139 });
140 if (childOptions.version) {
141 obj.version = obj.children[0].version;
142 }
143 if (childOptions.hash) {
144 obj.hash = obj.children.map((j) => j.hash).join("");
145 }
146 /**
147 * Returns result.
148 * @param {StatsCompilation} j stats error
149 * @param {StatsError} obj Stats error
150 * @returns {StatsError} result
151 */
152 const mapError = (j, obj) => ({
153 ...obj,
154 compilerPath: obj.compilerPath ? `${j.name}.${obj.compilerPath}` : j.name
155 });
156 if (childOptions.errors) {
157 obj.errors = [];
158 for (const j of obj.children) {
159 const errors =
160 /** @type {NonNullable<KnownStatsCompilation["errors"]>} */
161 (j.errors);
162 for (const i of errors) {
163 obj.errors.push(mapError(j, i));
164 }
165 }
166 }
167 if (childOptions.warnings) {
168 obj.warnings = [];
169 for (const j of obj.children) {
170 const warnings =
171 /** @type {NonNullable<KnownStatsCompilation["warnings"]>} */
172 (j.warnings);
173 for (const i of warnings) {
174 obj.warnings.push(mapError(j, i));
175 }
176 }
177 }
178 if (childOptions.errorsCount) {
179 obj.errorsCount = 0;
180 for (const j of obj.children) {
181 obj.errorsCount += /** @type {number} */ (j.errorsCount);
182 }
183 }
184 if (childOptions.warningsCount) {
185 obj.warningsCount = 0;
186 for (const j of obj.children) {
187 obj.warningsCount += /** @type {number} */ (j.warningsCount);
188 }
189 }
190 return obj;
191 }
192
193 /**
194 * Returns a string representation.
195 * @param {StatsValue=} options stats options
196 * @returns {string} string output
197 */
198 toString(options) {
199 const childOptions = this._createChildOptions(options, {
200 forToString: true
201 });
202 const results = this.stats.map((stat, idx) => {
203 const str = stat.toString(childOptions.children[idx]);
204 const compilationName = stat.compilation.name;
205 const name =
206 compilationName &&
207 identifierUtils
208 .makePathsRelative(
209 stat.compilation.compiler.context,
210 compilationName,
211 stat.compilation.compiler.root
212 )
213 .replace(/\|/g, " ");
214 if (!str) return str;
215 return name ? `${name}:\n${indent(str, " ")}` : str;
216 });
217 return results.filter(Boolean).join("\n\n");
218 }
219}
220
221module.exports = MultiStats;
Note: See TracBrowser for help on using the repository browser.