From 5a1fa077db67a268edee4f1f789625c3a5d4aba4 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Thu, 25 Jan 2024 14:17:26 +0800 Subject: [PATCH 1/6] fix(build): do not output build time when build fails --- packages/vite/src/node/build.ts | 3 +++ packages/vite/src/node/logger.ts | 4 ++++ packages/vite/src/node/plugins/reporter.ts | 8 +++----- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 55efd6b5398229..157a8f3f43d098 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -37,6 +37,7 @@ import { withTrailingSlash, } from './utils' import { manifestPlugin } from './plugins/manifest' +import { buildFailInfo } from './logger' import type { Logger } from './logger' import { dataURIPlugin } from './plugins/dataUri' import { buildImportAnalysisPlugin } from './plugins/importAnalysisBuild' @@ -554,6 +555,7 @@ export async function build( if (e.frame) { msg += `\n` + colors.yellow(e.frame) } + buildFailInfo.failed = true return msg } @@ -659,6 +661,7 @@ export async function build( const outDirs = normalizedOutputs.map(({ dir }) => resolve(dir!)) + buildFailInfo.failed = false // watch file changes with rollup if (config.build.watch) { config.logger.info(colors.cyan(`\nwatching for file changes...`)) diff --git a/packages/vite/src/node/logger.ts b/packages/vite/src/node/logger.ts index 8600228e305de1..b8ef9b06c23c59 100644 --- a/packages/vite/src/node/logger.ts +++ b/packages/vite/src/node/logger.ts @@ -26,6 +26,10 @@ export interface LogErrorOptions extends LogOptions { error?: Error | RollupError | null } +export const buildFailInfo: Record = { + failed: false, +} + export const LogLevels: Record = { silent: 0, error: 1, diff --git a/packages/vite/src/node/plugins/reporter.ts b/packages/vite/src/node/plugins/reporter.ts index 1feae9cbfaa876..4a30c979d751be 100644 --- a/packages/vite/src/node/plugins/reporter.ts +++ b/packages/vite/src/node/plugins/reporter.ts @@ -10,7 +10,7 @@ import { normalizePath, withTrailingSlash, } from '../utils' -import { LogLevels } from '../logger' +import { LogLevels, buildFailInfo } from '../logger' const groups = [ { name: 'Assets', color: colors.green }, @@ -48,7 +48,6 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin { let chunkCount = 0 let compressedCount = 0 let startTime = Date.now() - let buildFailed = false async function getCompressedSize( code: string | Uint8Array, @@ -109,8 +108,7 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin { transformedCount = 0 }, - buildEnd(error?: Error) { - buildFailed = !!error + buildEnd() { if (shouldLogInfo) { if (tty) { clearLine() @@ -303,7 +301,7 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin { }, closeBundle() { - if (shouldLogInfo && !config.build.watch && !buildFailed) { + if (shouldLogInfo && !config.build.watch && !buildFailInfo.failed) { config.logger.info( `${colors.green( `✓ built in ${displayTime(Date.now() - startTime)}`, From 221c998f3a317d23ec18bcce31684b78a0be309d Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Mon, 29 Jan 2024 18:31:41 +0800 Subject: [PATCH 2/6] fix: update --- packages/vite/src/node/build.ts | 21 ++++++++++-- packages/vite/src/node/logger.ts | 4 --- packages/vite/src/node/plugins/reporter.ts | 37 +--------------------- packages/vite/src/node/utils.ts | 20 ++++++++++++ 4 files changed, 39 insertions(+), 43 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 157a8f3f43d098..9a0f2f42d1b30f 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, @@ -37,7 +38,7 @@ import { withTrailingSlash, } from './utils' import { manifestPlugin } from './plugins/manifest' -import { buildFailInfo } from './logger' +import { LogLevels } from './logger' import type { Logger } from './logger' import { dataURIPlugin } from './plugins/dataUri' import { buildImportAnalysisPlugin } from './plugins/importAnalysisBuild' @@ -555,7 +556,6 @@ export async function build( if (e.frame) { msg += `\n` + colors.yellow(e.frame) } - buildFailInfo.failed = true return msg } @@ -566,6 +566,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 @@ -661,7 +662,6 @@ export async function build( const outDirs = normalizedOutputs.map(({ dir }) => resolve(dir!)) - buildFailInfo.failed = false // watch file changes with rollup if (config.build.watch) { config.logger.info(colors.cyan(`\nwatching for file changes...`)) @@ -700,6 +700,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) { @@ -710,10 +711,24 @@ export async function build( for (const output of normalizedOutputs) { res.push(await bundle[options.write ? 'write' : 'generate'](output)) } + const shouldLogInfo = LogLevels[config.logLevel || 'info'] >= LogLevels.info + if (shouldLogInfo) { + 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 Builed failed in ${displayTime(Date.now() - startTime)}`, + )}`, + ) + startTime = undefined + } throw e } finally { if (bundle) await bundle.close() diff --git a/packages/vite/src/node/logger.ts b/packages/vite/src/node/logger.ts index b8ef9b06c23c59..8600228e305de1 100644 --- a/packages/vite/src/node/logger.ts +++ b/packages/vite/src/node/logger.ts @@ -26,10 +26,6 @@ export interface LogErrorOptions extends LogOptions { error?: Error | RollupError | null } -export const buildFailInfo: Record = { - failed: false, -} - export const LogLevels: Record = { silent: 0, error: 1, diff --git a/packages/vite/src/node/plugins/reporter.ts b/packages/vite/src/node/plugins/reporter.ts index 4a30c979d751be..d4f39aa5c4b8e4 100644 --- a/packages/vite/src/node/plugins/reporter.ts +++ b/packages/vite/src/node/plugins/reporter.ts @@ -10,7 +10,7 @@ import { normalizePath, withTrailingSlash, } from '../utils' -import { LogLevels, buildFailInfo } from '../logger' +import { LogLevels } from '../logger' const groups = [ { name: 'Assets', color: colors.green }, @@ -47,7 +47,6 @@ 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, @@ -100,10 +99,6 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin { return null }, - options() { - startTime = Date.now() - }, - buildStart() { transformedCount = 0 }, @@ -299,16 +294,6 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin { ) } }, - - closeBundle() { - if (shouldLogInfo && !config.build.watch && !buildFailInfo.failed) { - config.logger.info( - `${colors.green( - `✓ built in ${displayTime(Date.now() - startTime)}`, - )}`, - ) - } - }, } } @@ -336,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 a949fd60851313..f16b941c99a2ce 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1423,3 +1423,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`}` +} From 2243fdc92facc1b771a91eaeead39520cce72705 Mon Sep 17 00:00:00 2001 From: patak <583075+patak-dev@users.noreply.github.com> Date: Mon, 12 Feb 2024 09:39:29 +0100 Subject: [PATCH 3/6] chore: typo --- packages/vite/src/node/build.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 0d69a13b798c65..ae636668453497 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -719,7 +719,7 @@ export async function build( if (startTime) { config.logger.error( `${colors.red( - `x Builed failed in ${displayTime(Date.now() - startTime)}`, + `x Build failed in ${displayTime(Date.now() - startTime)}`, )}`, ) startTime = undefined From 07786357b09b9d23443d8a3fd99d8c0f13917c8e Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Mon, 12 Feb 2024 17:34:16 +0800 Subject: [PATCH 4/6] chore: update message --- packages/vite/src/node/build.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index ae636668453497..62a03f3497b95f 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -718,9 +718,7 @@ export async function build( clearLine() if (startTime) { config.logger.error( - `${colors.red( - `x Build failed in ${displayTime(Date.now() - startTime)}`, - )}`, + `${colors.red('x')} Build failed in ${displayTime(Date.now() - startTime)}`, ) startTime = undefined } From e82fc8323938d54cf661e8f05b168b0118854c12 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Mon, 12 Feb 2024 21:13:33 +0800 Subject: [PATCH 5/6] chore: remove redundant check --- packages/vite/src/node/build.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 62a03f3497b95f..f7e3ae94a34da6 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -38,7 +38,6 @@ import { withTrailingSlash, } from './utils' import { manifestPlugin } from './plugins/manifest' -import { LogLevels } from './logger' import type { Logger } from './logger' import { dataURIPlugin } from './plugins/dataUri' import { buildImportAnalysisPlugin } from './plugins/importAnalysisBuild' @@ -706,12 +705,9 @@ export async function build( for (const output of normalizedOutputs) { res.push(await bundle[options.write ? 'write' : 'generate'](output)) } - const shouldLogInfo = LogLevels[config.logLevel || 'info'] >= LogLevels.info - if (shouldLogInfo) { - config.logger.info( - `${colors.green(`✓ built in ${displayTime(Date.now() - startTime)}`)}`, - ) - } + config.logger.info( + `${colors.green(`✓ built in ${displayTime(Date.now() - startTime)}`)}`, + ) return Array.isArray(outputs) ? res : res[0] } catch (e) { e.message = mergeRollupError(e) From 2b9d58499598a42fbddd5e14e8745882749aa6ea Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 12 Feb 2024 19:09:10 +0100 Subject: [PATCH 6/6] chore: remove Vite Rollup plugins compat list --- docs/.vitepress/config.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 8db949d482cfaa..6222cdbd530af3 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -167,10 +167,6 @@ export default defineConfig({ text: 'DEV Community', link: 'https://dev.to/t/vite', }, - { - text: 'Rollup Plugins Compat', - link: 'https://vite-rollup-plugins.patak.dev/', - }, { text: 'Changelog', link: 'https://github.com/vitejs/vite/blob/main/packages/vite/CHANGELOG.md',