-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
100 lines (91 loc) · 2.64 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const util = require("util");
const toPrettySize = (value) => {
if (value < 1000) {
return `${value} bytes`.padStart(10, " ");
} else if (value < 1000000) {
return `${(value / 1024).toFixed(2)} KiB`.padStart(10, " ");
} else if (value < 1000000000) {
return `${(value / 1024 / 1024).toFixed(2)} MiB`.padStart(10, " ");
}
};
const colorized = (value, color) => {
return `\x1b[${color}m${value}\x1b[0m`;
};
const padded = (value, column, align = "left") => {
if (align === "right") {
return value.padStart(column.width, " ");
} else {
return value.padEnd(column.width, " ");
}
};
const createColumns = (assets) => {
return [
{
width: Math.max(...assets.map(({ name }) => name.length)),
},
{
width: 10,
},
{
width: 31,
},
];
};
const logAssetStats = (asset, columns) => {
const { name, chunks, chunkNames, info } = asset;
const color = asset.isOverSizeLimit ? "33" : "32";
const chunkTags = [
asset.emmited ? colorized("[emitted]", 32) : " ",
info.development ? colorized("[dev]", 32) : " ",
asset.isOverSizeLimit ? colorized(" [big] ", 33) : " ",
];
const chunkIndices = (chunks.length > 0 ? [chunks, 1].join(", ") : "") + " ";
const sizeColor = asset.isOverSizeLimit ? "33" : "0";
const chunksColumnItems = [
padded(chunkIndices, { width: 7 }, "right"),
...chunkTags,
];
const line = util.format(
"%s %s %s %s",
colorized(padded(name, columns[0], "right"), color),
colorized(padded(toPrettySize(asset.size), columns[1], "right"), sizeColor),
chunksColumnItems.join(" "),
chunkNames.join(" "),
);
console.log(line);
};
const logHeader = (stats, columns) => {
const builtAt = new Date(stats.builtAt);
const options = {
month: "2-digit",
day: "2-digit",
year: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
};
console.log(`Hash: ${stats.hash}`);
console.log(`Version: webpack ${stats.version}`);
console.log(`Time: ${stats.time}ms`);
console.log(`Built at: ${builtAt.toLocaleString(undefined, options)}`);
const header = util.format(
"%s %s %s %s",
padded("Asset", columns[0], "right"),
padded("Size", columns[1], "right"),
padded("Chunks", columns[2]),
"Chunk Names",
);
console.log(header);
};
const logStats = (stats) => {
const { assets } = stats;
// Make columns first (computes 1st column width basically)
const columns = createColumns(assets);
// Show a header (like normal webpack does)
logHeader(stats, columns);
// Show each asset
for (const asset of assets) {
logAssetStats(asset, columns);
}
};
module.exports = logStats;