-
Notifications
You must be signed in to change notification settings - Fork 29
/
reporter.js
176 lines (154 loc) Β· 4.61 KB
/
reporter.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const path = require('path')
const chalk = require('picocolors')
module.exports = function reporter (compiler, log, options = {}) {
let builds = 0
function onError (errors) {
const sfx = errors.length > 1 ? 's' : ''
log.error(`Failed to compile! Found ${chalk.bold(chalk.red(errors.length))} error${sfx}:`)
errors.forEach(x => console.log('\n ' + x.replace(/(\r?\n)/g, '$1 ') + '\n'))
}
function onWarning (warnings) {
const sfx = warnings.length > 1 ? 's' : ''
log.warn(`Compiled with ${chalk.bold(chalk.yellow(warnings.length))} warning${sfx}:`)
warnings.forEach(x => console.log('\n ' + x.replace(/(\r?\n)/g, '$1 ') + '\n'))
}
compiler.hooks.invalid.tap('jetpack', file => {
file = path.relative(options.dir, file)
log.info(`File changed: ${chalk.bold(chalk.white(file))}`)
})
compiler.hooks.failed.tap('jetpack', (error) => {
log.error('Failed to compile!', error)
})
compiler.hooks.done.tap('jetpack', stats => {
builds++
const format = require('webpack-format-messages')
const { errors, warnings } = format(stats)
if (errors.length > 0) {
return onError(errors)
}
if (warnings.length > 0) {
onWarning(warnings)
}
if (options.printAssets) {
printAssets(stats.toJson(), log)
}
log.info(`${builds > 1 ? 'Rebuilt' : 'Built'} in ${formatTime(stats.endTime - stats.startTime)}`)
})
}
const getText = (arr, row, col) => {
return arr[row][col].value
}
const table = (array, align, log) => {
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++) {
let buf = ''
for (let col = 0; col < cols; col++) {
const format = (...args) => { buf += array[row][col].color(...args) }
const value = `${getText(array, row, col)}`
let l = value.length
if (align[col] === 'l') format(value)
if (col !== cols - 1) {
for (; l < colSizes[col]; l++) buf += chalk.white(' ')
}
if (align[col] === 'r') format(value)
if (col + 1 < cols && colSizes[col] !== 0) {
buf += chalk.white(' ')
}
}
console.log(' ' + buf)
}
}
const getAssetColor = (asset, defaultColor) => {
if (asset.name.endsWith('.js.map') ||
asset.name.endsWith('.hot-update.js') ||
asset.name.endsWith('.hot-update.json')) {
return chalk.gray
}
if (asset.isOverSizeLimit) {
return chalk.yellow
}
return defaultColor
}
function printAssets (obj, log) {
const modules = {}
obj.modules.forEach(module => {
module.chunks.forEach(chunk => {
modules[chunk] = modules[chunk] || 0
modules[chunk] += 1
})
})
const assets = obj.assets
.sort((a, b) => {
return a.name > b.name ? 1 : -1
})
.sort((a, b) => {
const aExt = a.name.split('.')[a.name.split('.').length - 1]
const bExt = b.name.split('.')[b.name.split('.').length - 1]
return aExt > bExt ? 1 : -1
})
if (assets && obj.assets.length > 0) {
const t = [
[
{
value: 'Asset',
color: chalk.bold
},
{
value: 'Modules',
color: chalk.bold
},
{
value: 'Size',
color: chalk.bold
}
]
]
for (const asset of assets) {
t.push([
{
value: asset.name,
color: getAssetColor(asset, chalk.white)
},
{
value: asset.name.endsWith('.js') && !asset.name.endsWith('.hot-update.js') && !asset.name.startsWith('runtime~')
? asset.chunks.reduce((acc, chunk) => acc + modules[chunk], 0)
: '-',
color: asset.name.endsWith('.js') && !asset.name.endsWith('.hot-update.js') && !asset.name.startsWith('runtime~')
? getAssetColor(asset, chalk.white)
: chalk.gray
},
{
value: formatSize(asset.size),
color: getAssetColor(asset, chalk.white)
}
])
}
console.log('')
table(t, 'lll', log)
console.log('')
}
}
function formatSize (size) {
if (size <= 0) {
return '0 bytes'
}
const abbreviations = ['bytes', 'KiB', 'MiB', 'GiB']
const index = Math.floor(Math.log(size) / Math.log(1024))
return `${+(size / Math.pow(1024, index)).toPrecision(3)} ${
abbreviations[index]
}`
}
function formatTime (ms = 0) {
return (ms / 1000).toFixed(2) + 's'
}