Skip to content

Commit

Permalink
feat(reporter): report built time
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day committed Feb 18, 2023
1 parent 083632c commit c169544
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/vite/src/node/plugins/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
let transformedCount = 0
let chunkCount = 0
let compressedCount = 0
let startTime = +new Date()

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

options() {
startTime = +new Date()
},

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

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

Expand Down Expand Up @@ -276,3 +287,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}min {Y}s
return `${mins}min ${seconds < 1 ? '' : `${seconds.toFixed(0)}s`}`
}

0 comments on commit c169544

Please sign in to comment.