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