diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index ea8ff1f8826af8..f7e3ae94a34da6 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -30,6 +30,7 @@ import { arraify, asyncFlatten, copyDir, + displayTime, emptyDir, joinUrlSegments, normalizePath, @@ -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 @@ -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) { @@ -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() diff --git a/packages/vite/src/node/plugins/reporter.ts b/packages/vite/src/node/plugins/reporter.ts index 1feae9cbfaa876..d4f39aa5c4b8e4 100644 --- a/packages/vite/src/node/plugins/reporter.ts +++ b/packages/vite/src/node/plugins/reporter.ts @@ -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, @@ -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() @@ -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)}`, - )}`, - ) - } - }, } } @@ -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`}` -} diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 67c827e7000421..6f8004566cb6f9 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1426,3 +1426,23 @@ export function sortObjectKeys>(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`}` +}