Skip to content

Commit

Permalink
feat(reporter): report built time (#12100)
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day committed Feb 22, 2023
1 parent 1a6a0c2 commit f2ad222
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packages/vite/src/node/plugins/reporter.ts
Expand Up @@ -32,6 +32,7 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
let transformedCount = 0
let chunkCount = 0
let compressedCount = 0
let startTime = Date.now()

async function getCompressedSize(
code: string | Uint8Array,
Expand Down Expand Up @@ -84,6 +85,10 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
return null
},

options() {
startTime = Date.now()
},

buildEnd() {
if (shouldLogInfo) {
if (tty) {
Expand Down Expand Up @@ -242,6 +247,16 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
)
}
},

closeBundle() {
if (shouldLogInfo && !config.build.watch) {
config.logger.info(
`${colors.green(`✓`)} built in ${displayTime(
Date.now() - startTime,
)}`,
)
}
},
}
}

Expand Down Expand Up @@ -276,3 +291,23 @@ function displaySize(bytes: number) {
minimumFractionDigits: 2,
})} kB`
}

function displayTime(time: number) {
// display: {X}ms
if (time < 1000) {
return `${time}ms`
}

time = time / 1000

// display: {X}s
if (time < 60) {
return `${time.toFixed(2)}s`
}

const mins = parseInt((time / 60).toString())
const seconds = time % 60

// display: {X}m {Y}s
return `${mins}m${seconds < 1 ? '' : ` ${seconds.toFixed(0)}s`}`
}

0 comments on commit f2ad222

Please sign in to comment.