Skip to content

Commit f2ad222

Browse files
authoredFeb 22, 2023
feat(reporter): report built time (#12100)
1 parent 1a6a0c2 commit f2ad222

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
 

‎packages/vite/src/node/plugins/reporter.ts

+35
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
3232
let transformedCount = 0
3333
let chunkCount = 0
3434
let compressedCount = 0
35+
let startTime = Date.now()
3536

3637
async function getCompressedSize(
3738
code: string | Uint8Array,
@@ -84,6 +85,10 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
8485
return null
8586
},
8687

88+
options() {
89+
startTime = Date.now()
90+
},
91+
8792
buildEnd() {
8893
if (shouldLogInfo) {
8994
if (tty) {
@@ -242,6 +247,16 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
242247
)
243248
}
244249
},
250+
251+
closeBundle() {
252+
if (shouldLogInfo && !config.build.watch) {
253+
config.logger.info(
254+
`${colors.green(`✓`)} built in ${displayTime(
255+
Date.now() - startTime,
256+
)}`,
257+
)
258+
}
259+
},
245260
}
246261
}
247262

@@ -276,3 +291,23 @@ function displaySize(bytes: number) {
276291
minimumFractionDigits: 2,
277292
})} kB`
278293
}
294+
295+
function displayTime(time: number) {
296+
// display: {X}ms
297+
if (time < 1000) {
298+
return `${time}ms`
299+
}
300+
301+
time = time / 1000
302+
303+
// display: {X}s
304+
if (time < 60) {
305+
return `${time.toFixed(2)}s`
306+
}
307+
308+
const mins = parseInt((time / 60).toString())
309+
const seconds = time % 60
310+
311+
// display: {X}m {Y}s
312+
return `${mins}m${seconds < 1 ? '' : ` ${seconds.toFixed(0)}s`}`
313+
}

0 commit comments

Comments
 (0)
Please sign in to comment.