diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.ts index c2f02bbba8..b559fb2c81 100644 --- a/packages/core/src/config/index.ts +++ b/packages/core/src/config/index.ts @@ -20,7 +20,6 @@ import { import { Logger, - clearScreen, colors, isArray, isEmptyObject, @@ -161,7 +160,12 @@ export async function resolveConfig( ...vitePluginAdapters ]); - const config = await resolveConfigHook(userConfig, sortFarmJsPlugins); + const config = await resolveConfigHook( + userConfig, + configEnv, + sortFarmJsPlugins + ); + // may be user push plugin when config hooks const allPlugins = await resolvePlugins(config, defaultMode); const farmJsPlugins = getSortedPlugins([ diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index cd9920d7c9..8efee20be3 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -97,7 +97,7 @@ export async function build( logger.info( `Build completed in ${bold( - green(`${logger.formatExecutionTime(elapsedTime)}`) + green(`${logger.formatTime(elapsedTime)}`) )} ${persistentCacheText} Resources emitted to ${bold( green(output.path) )}.` diff --git a/packages/core/src/plugin/index.ts b/packages/core/src/plugin/index.ts index 5f8f1927b3..5355b47c55 100644 --- a/packages/core/src/plugin/index.ts +++ b/packages/core/src/plugin/index.ts @@ -3,6 +3,7 @@ export * from './rust/index.js'; import { CompilationMode, + ConfigEnv, ResolvedUserConfig, type UserConfig } from '../config/index.js'; @@ -91,6 +92,7 @@ export async function resolveAsyncPlugins(arr: T[]): Promise { export async function resolveConfigHook( config: UserConfig, + configEnv: ConfigEnv, plugins: JsPlugin[] ): Promise { let conf = config; @@ -107,7 +109,7 @@ export async function resolveConfigHook( for (const p of uniqueVitePlugins.values()) { if (p.config) { - const res = await p.config(conf); + const res = await p.config(conf, configEnv); if (res) { conf = merge(conf, res); diff --git a/packages/core/src/plugin/type.ts b/packages/core/src/plugin/type.ts index f5cecbe4d8..2b948e5f61 100644 --- a/packages/core/src/plugin/type.ts +++ b/packages/core/src/plugin/type.ts @@ -1,5 +1,6 @@ import { Compiler, + ConfigEnv, ModuleContext, ResolvedUserConfig, Server, @@ -155,7 +156,10 @@ export interface JsPlugin { name: string; priority?: number; - config?: (config: UserConfig) => UserConfig | Promise; + config?: ( + config: UserConfig, + configEnv: ConfigEnv + ) => UserConfig | Promise; configResolved?: (config: ResolvedUserConfig) => void | Promise; diff --git a/packages/core/src/server/hmr-engine.ts b/packages/core/src/server/hmr-engine.ts index 6c62c3e046..fb813dae7e 100644 --- a/packages/core/src/server/hmr-engine.ts +++ b/packages/core/src/server/hmr-engine.ts @@ -73,7 +73,7 @@ export class HmrEngine { const result = await this.app.compiler.update(queue); logger.info( - `${bold(cyan(updatedFilesStr))} updated in ${bold(green(logger.formatExecutionTime(performance.now() - start)))}` + `${bold(cyan(updatedFilesStr))} updated in ${bold(green(logger.formatTime(performance.now() - start)))}` ); // clear update queue after update finished diff --git a/packages/core/src/server/middlewares/lazyCompilation.ts b/packages/core/src/server/middlewares/lazyCompilation.ts index bad1b9749c..e24b9cf410 100644 --- a/packages/core/src/server/middlewares/lazyCompilation.ts +++ b/packages/core/src/server/middlewares/lazyCompilation.ts @@ -70,7 +70,7 @@ export function lazyCompilationMiddleware( `${bold(green(`✓ Lazy compilation done`))} ${bold( cyan(pathsStr) )} in ${bold( - green(config.logger.formatExecutionTime(performance.now() - start)) + green(config.logger.formatTime(performance.now() - start)) )}.` ); diff --git a/packages/core/src/utils/logger.ts b/packages/core/src/utils/logger.ts index 08cfb965ea..48a4725faa 100644 --- a/packages/core/src/utils/logger.ts +++ b/packages/core/src/utils/logger.ts @@ -104,7 +104,7 @@ export class Logger implements ILogger { this.prefix = color ? color(formattedPrefix) : formattedPrefix; } - formatExecutionTime(duration: number): string { + formatTime(duration: number): string { if (this.timeUnit === 's') { return `${(duration / 1000).toFixed(3)}s`; } else { @@ -140,7 +140,7 @@ export class Logger implements ILogger { loggerMessage = message.replace(timeRegex, (_, durationStr) => { const duration = parseFloat(durationStr); - return this.formatExecutionTime(duration); + return this.formatTime(duration); }); } else { loggerMessage = message.message; @@ -260,7 +260,7 @@ export function bootstrapLogger(options?: LoggerOptions): Logger { } export function bootstrap( - times: number, + time: number, config: ResolvedUserConfig, hasCacheDir: boolean ): void { @@ -282,7 +282,7 @@ export function bootstrap( `${colors.bold(colors.green(` ✓`))} ${colors.bold( 'Compile in' )} ${colors.bold( - colors.green(config.logger.formatExecutionTime(times)) + colors.green(config.logger.formatTime(time)) )} ${persistentCacheFlag}`, '\n' ); diff --git a/packages/core/src/utils/share.ts b/packages/core/src/utils/share.ts index bdef17aabc..20eddd35d5 100644 --- a/packages/core/src/utils/share.ts +++ b/packages/core/src/utils/share.ts @@ -187,10 +187,7 @@ export function tryStatSync(file: string): fs.Stats | undefined { } catch {} } -export function formatExecutionTime( - time: number, - format: 'ms' | 's' = 'ms' -): string { +export function formatTime(time: number, format: 'ms' | 's' = 'ms'): string { switch (format) { case 's': return `${Math.floor(time) / 1000}s`; diff --git a/packages/core/src/watcher/index.ts b/packages/core/src/watcher/index.ts index 50ff628654..99168a42e0 100644 --- a/packages/core/src/watcher/index.ts +++ b/packages/core/src/watcher/index.ts @@ -229,7 +229,7 @@ export async function handlerWatcher( const elapsedTime = Math.floor(performance.now() - start); logger.info( `update completed in ${bold( - green(`${logger.formatExecutionTime(elapsedTime)}ms`) + green(`${logger.formatTime(elapsedTime)}`) )} Resources emitted to ${bold( green(resolvedUserConfig.compilation.output.path) )}.`