From 3fbaf9c4d21e62062490c58c13cbf1b074417004 Mon Sep 17 00:00:00 2001 From: Miroma Date: Thu, 30 Jul 2026 12:31:58 +0200 Subject: [PATCH] refactor(logVersion)!: avoid onAfterConfigResolve previously, cli.ts was injecting onAfterConfigResolve solely to print the version banner. Now, build() itself will log the version after resolving the config, unless the caller has supplied a different onAfterConfigResolve. BREAKING CHANGE: callers of vitepress' build() will now notice the version banner gets printed. To disable that, pass a noop function: ```ts build(root, { onAfterConfigResolve() {}, }) ``` --- src/node/build/build.ts | 7 ++++++- src/node/cli.ts | 18 +++--------------- src/node/utils/logVersion.ts | 10 ++++++++++ 3 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 src/node/utils/logVersion.ts diff --git a/src/node/build/build.ts b/src/node/build/build.ts index 771d8bd4..592d680b 100644 --- a/src/node/build/build.ts +++ b/src/node/build/build.ts @@ -12,6 +12,7 @@ import { clearCache } from '../markdownToVue' import type { PageMeta } from '../plugin' import { slash, type Awaitable, type HeadConfig } from '../shared' import { deserializeFunctions, serializeFunctions } from '../utils/fnSerialize' +import { logVersion } from '../utils/logVersion' import { nativeImport } from '../utils/nativeImport' import { task } from '../utils/task' import { bundle } from './bundle' @@ -33,7 +34,11 @@ export async function build( process.env.NODE_ENV = 'production' const siteConfig = await resolveConfig(root, 'build', 'production') - await buildOptions.onAfterConfigResolve?.(siteConfig) + if (buildOptions.onAfterConfigResolve) { + await buildOptions.onAfterConfigResolve(siteConfig) + } else { + logVersion(siteConfig.logger) + } delete buildOptions.onAfterConfigResolve const unlinkVue = await linkVue() diff --git a/src/node/cli.ts b/src/node/cli.ts index 2e12748c..76da98c4 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -1,6 +1,6 @@ import minimist from 'minimist' import c from 'picocolors' -import { createLogger, version as viteVersion, type Logger } from 'vite' +import { createLogger } from 'vite' import { build, createServer, @@ -8,10 +8,10 @@ import { resolveConfig, serve } from '.' -import { version } from '../../package.json' import { init } from './init/init' import { clearCache } from './markdownToVue' import { bindShortcuts } from './shortcuts' +import { logVersion } from './utils/logVersion' const argv: any = minimist(process.argv.slice(2)) @@ -23,13 +23,6 @@ Object.keys(argv).forEach((key) => { } }) -const logVersion = (logger: Logger) => { - logger.info( - `\n ${c.green(`${c.bold('vitepress')} ${version}`)} ${c.gray(`(using vite ${viteVersion})`)}\n`, - { clear: !logger.hasWarned } - ) -} - const command = argv._[0] const root = argv._[command ? 1 : 0] if (root) { @@ -81,12 +74,7 @@ if (!command || command === 'dev') { init(argv.root) } else { if (command === 'build') { - build(root, { - ...argv, - onAfterConfigResolve(siteConfig) { - logVersion(siteConfig.logger) - } - }).catch(logErrorAndExit.bind(null, `build error:`)) + build(root, argv).catch(logErrorAndExit.bind(null, `build error:`)) } else if (command === 'serve' || command === 'preview') { serve(argv).catch( logErrorAndExit.bind(null, `failed to start server. error:`) diff --git a/src/node/utils/logVersion.ts b/src/node/utils/logVersion.ts new file mode 100644 index 00000000..4e551282 --- /dev/null +++ b/src/node/utils/logVersion.ts @@ -0,0 +1,10 @@ +import c from 'picocolors' +import { version as viteVersion, type Logger } from 'vite' +import { version } from '../../../package.json' + +export function logVersion(logger: Logger) { + logger.info( + `\n ${c.green(`${c.bold('vitepress')} ${version}`)} ${c.gray(`(using vite ${viteVersion})`)}\n`, + { clear: !logger.hasWarned } + ) +}