UNPKG

3.67 kBJavaScriptView Raw
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("./Stats")} Stats */
11
12const indent = (str, prefix) => {
13 const rem = str.replace(/\n([^\n])/g, "\n" + prefix + "$1");
14 return prefix + rem;
15};
16
17class MultiStats {
18 /**
19 * @param {Stats[]} stats the child stats
20 */
21 constructor(stats) {
22 this.stats = stats;
23 }
24
25 get hash() {
26 return this.stats.map(stat => stat.hash).join("");
27 }
28
29 /**
30 * @returns {boolean} true if a child compilation encountered an error
31 */
32 hasErrors() {
33 return this.stats.some(stat => stat.hasErrors());
34 }
35
36 /**
37 * @returns {boolean} true if a child compilation had a warning
38 */
39 hasWarnings() {
40 return this.stats.some(stat => stat.hasWarnings());
41 }
42
43 _createChildOptions(options, context) {
44 if (!options) {
45 options = {};
46 }
47 const { children: _, ...baseOptions } = options;
48 const children = this.stats.map((stat, idx) => {
49 const childOptions = Array.isArray(options.children)
50 ? options.children[idx]
51 : options.children;
52 return stat.compilation.createStatsOptions(
53 {
54 ...baseOptions,
55 ...(childOptions && typeof childOptions === "object"
56 ? childOptions
57 : { preset: childOptions })
58 },
59 context
60 );
61 });
62 return {
63 version: children.every(o => o.version),
64 hash: children.every(o => o.hash),
65 errorsCount: children.every(o => o.errorsCount),
66 warningsCount: children.every(o => o.warningsCount),
67 errors: children.every(o => o.errors),
68 warnings: children.every(o => o.warnings),
69 children
70 };
71 }
72
73 toJson(options) {
74 options = this._createChildOptions(options, { forToString: false });
75 const obj = {};
76 obj.children = this.stats.map((stat, idx) => {
77 const obj = stat.toJson(options.children[idx]);
78 const compilationName = stat.compilation.name;
79 const name =
80 compilationName &&
81 identifierUtils.makePathsRelative(
82 options.context,
83 compilationName,
84 stat.compilation.compiler.root
85 );
86 obj.name = name;
87 return obj;
88 });
89 if (options.version) {
90 obj.version = obj.children[0].version;
91 }
92 if (options.hash) {
93 obj.hash = obj.children.map(j => j.hash).join("");
94 }
95 const mapError = (j, obj) => {
96 return {
97 ...obj,
98 compilerPath: obj.compilerPath
99 ? `${j.name}.${obj.compilerPath}`
100 : j.name
101 };
102 };
103 if (options.errors) {
104 obj.errors = [];
105 for (const j of obj.children) {
106 for (const i of j.errors) {
107 obj.errors.push(mapError(j, i));
108 }
109 }
110 }
111 if (options.warnings) {
112 obj.warnings = [];
113 for (const j of obj.children) {
114 for (const i of j.warnings) {
115 obj.warnings.push(mapError(j, i));
116 }
117 }
118 }
119 if (options.errorsCount) {
120 obj.errorsCount = 0;
121 for (const j of obj.children) {
122 obj.errorsCount += j.errorsCount;
123 }
124 }
125 if (options.warningsCount) {
126 obj.warningsCount = 0;
127 for (const j of obj.children) {
128 obj.warningsCount += j.warningsCount;
129 }
130 }
131 return obj;
132 }
133
134 toString(options) {
135 options = this._createChildOptions(options, { forToString: true });
136 const results = this.stats.map((stat, idx) => {
137 const str = stat.toString(options.children[idx]);
138 const compilationName = stat.compilation.name;
139 const name =
140 compilationName &&
141 identifierUtils
142 .makePathsRelative(
143 options.context,
144 compilationName,
145 stat.compilation.compiler.root
146 )
147 .replace(/\|/g, " ");
148 if (!str) return str;
149 return name ? `${name}:\n${indent(str, " ")}` : str;
150 });
151 return results.filter(Boolean).join("\n\n");
152 }
153}
154
155module.exports = MultiStats;