Skip to content

Commit

Permalink
fix(build): do not output build time when build fails (#15711)
Browse files Browse the repository at this point in the history
Co-authored-by: patak <583075+patak-dev@users.noreply.github.com>
  • Loading branch information
btea and patak-dev authored Feb 12, 2024
1 parent 738ecae commit added3e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
12 changes: 12 additions & 0 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
arraify,
asyncFlatten,
copyDir,
displayTime,
emptyDir,
joinUrlSegments,
normalizePath,
Expand Down Expand Up @@ -559,6 +560,7 @@ export async function build(
}

let bundle: RollupBuild | undefined
let startTime: number | undefined
try {
const buildOutputOptions = (output: OutputOptions = {}): OutputOptions => {
// @ts-expect-error See https://github.com/vitejs/vite/issues/5812#issuecomment-984345618
Expand Down Expand Up @@ -692,6 +694,7 @@ export async function build(

// write or generate files with rollup
const { rollup } = await import('rollup')
startTime = Date.now()
bundle = await rollup(rollupOptions)

if (options.write) {
Expand All @@ -702,10 +705,19 @@ export async function build(
for (const output of normalizedOutputs) {
res.push(await bundle[options.write ? 'write' : 'generate'](output))
}
config.logger.info(
`${colors.green(`✓ built in ${displayTime(Date.now() - startTime)}`)}`,
)
return Array.isArray(outputs) ? res : res[0]
} catch (e) {
e.message = mergeRollupError(e)
clearLine()
if (startTime) {
config.logger.error(
`${colors.red('x')} Build failed in ${displayTime(Date.now() - startTime)}`,
)
startTime = undefined
}
throw e
} finally {
if (bundle) await bundle.close()
Expand Down
39 changes: 1 addition & 38 deletions packages/vite/src/node/plugins/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
let transformedCount = 0
let chunkCount = 0
let compressedCount = 0
let startTime = Date.now()
let buildFailed = false

async function getCompressedSize(
code: string | Uint8Array,
Expand Down Expand Up @@ -101,16 +99,11 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
return null
},

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

buildStart() {
transformedCount = 0
},

buildEnd(error?: Error) {
buildFailed = !!error
buildEnd() {
if (shouldLogInfo) {
if (tty) {
clearLine()
Expand Down Expand Up @@ -301,16 +294,6 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
)
}
},

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

Expand Down Expand Up @@ -338,23 +321,3 @@ function throttle(fn: Function) {
}, 100)
}
}

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`}`
}
20 changes: 20 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1426,3 +1426,23 @@ export function sortObjectKeys<T extends Record<string, any>>(obj: T): T {
}
return sorted as T
}

export function displayTime(time: number): string {
// 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 added3e

Please sign in to comment.