Skip to content

Commit

Permalink
chore: minor config.logger refactor (#17770)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Jul 27, 2024
1 parent 4407839 commit b947fdc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
23 changes: 12 additions & 11 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,11 @@ export async function build(
'production',
)
const options = config.build
const { logger } = config
const ssr = !!options.ssr
const libOptions = options.lib

config.logger.info(
logger.info(
colors.cyan(
`vite v${VERSION} ${colors.green(
`building ${ssr ? `SSR bundle ` : ``}for ${config.mode}...`,
Expand Down Expand Up @@ -602,7 +603,7 @@ export async function build(
const outputBuildError = (e: RollupError) => {
enhanceRollupError(e)
clearLine()
config.logger.error(e.message, { error: e })
logger.error(e.message, { error: e })
}

let bundle: RollupBuild | undefined
Expand All @@ -611,7 +612,7 @@ export async function build(
const buildOutputOptions = (output: OutputOptions = {}): OutputOptions => {
// @ts-expect-error See https://github.com/vitejs/vite/issues/5812#issuecomment-984345618
if (output.output) {
config.logger.warn(
logger.warn(
`You've set "rollupOptions.output.output" in your config. ` +
`This is deprecated and will override all Vite.js default output options. ` +
`Please use "rollupOptions.output" instead.`,
Expand All @@ -624,7 +625,7 @@ export async function build(
)
}
if (output.sourcemap) {
config.logger.warnOnce(
logger.warnOnce(
colors.yellow(
`Vite does not support "rollupOptions.output.sourcemap". ` +
`Please use "build.sourcemap" instead.`,
Expand Down Expand Up @@ -688,7 +689,7 @@ export async function build(
const outputs = resolveBuildOutputs(
options.rollupOptions?.output,
libOptions,
config.logger,
logger,
)
const normalizedOutputs: OutputOptions[] = []

Expand All @@ -709,12 +710,12 @@ export async function build(
options.emptyOutDir,
config.root,
resolvedOutDirs,
config.logger,
logger,
)

// watch file changes with rollup
if (config.build.watch) {
config.logger.info(colors.cyan(`\nwatching for file changes...`))
logger.info(colors.cyan(`\nwatching for file changes...`))

const resolvedChokidarOptions = resolveChokidarOptions(
config,
Expand All @@ -735,13 +736,13 @@ export async function build(

watcher.on('event', (event) => {
if (event.code === 'BUNDLE_START') {
config.logger.info(colors.cyan(`\nbuild started...`))
logger.info(colors.cyan(`\nbuild started...`))
if (options.write) {
prepareOutDir(resolvedOutDirs, emptyOutDir, config)
}
} else if (event.code === 'BUNDLE_END') {
event.result.close()
config.logger.info(colors.cyan(`built in ${event.duration}ms.`))
logger.info(colors.cyan(`built in ${event.duration}ms.`))
} else if (event.code === 'ERROR') {
outputBuildError(event.error)
}
Expand All @@ -763,15 +764,15 @@ export async function build(
for (const output of normalizedOutputs) {
res.push(await bundle[options.write ? 'write' : 'generate'](output))
}
config.logger.info(
logger.info(
`${colors.green(`✓ built in ${displayTime(Date.now() - startTime)}`)}`,
)
return Array.isArray(outputs) ? res : res[0]
} catch (e) {
enhanceRollupError(e)
clearLine()
if (startTime) {
config.logger.error(
logger.error(
`${colors.red('x')} Build failed in ${displayTime(Date.now() - startTime)}`,
)
startTime = undefined
Expand Down
10 changes: 5 additions & 5 deletions packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async function createDepsOptimizer(
let newDepsToLogHandle: NodeJS.Timeout | undefined
const logNewlyDiscoveredDeps = () => {
if (newDepsToLog.length) {
config.logger.info(
logger.info(
colors.green(
`✨ new dependencies optimized: ${depsLogString(newDepsToLog)}`,
),
Expand All @@ -132,7 +132,7 @@ async function createDepsOptimizer(
let discoveredDepsWhileScanning: string[] = []
const logDiscoveredDepsWhileScanning = () => {
if (discoveredDepsWhileScanning.length) {
config.logger.info(
logger.info(
colors.green(
`✨ discovered while scanning: ${depsLogString(
discoveredDepsWhileScanning,
Expand Down Expand Up @@ -443,7 +443,7 @@ async function createDepsOptimizer(
logNewlyDiscoveredDeps()
if (warnAboutMissedDependencies) {
logDiscoveredDepsWhileScanning()
config.logger.info(
logger.info(
colors.magenta(
`❗ add these dependencies to optimizeDeps.include to speed up cold start`,
),
Expand Down Expand Up @@ -485,7 +485,7 @@ async function createDepsOptimizer(
logNewlyDiscoveredDeps()
if (warnAboutMissedDependencies) {
logDiscoveredDepsWhileScanning()
config.logger.info(
logger.info(
colors.magenta(
`❗ add these dependencies to optimizeDeps.include to avoid a full page reload during cold start`,
),
Expand All @@ -502,7 +502,7 @@ async function createDepsOptimizer(
},
)
if (needsInteropMismatch.length > 0) {
config.logger.warn(
logger.warn(
`Mixed ESM and CJS detected in ${colors.yellow(
needsInteropMismatch.join(', '),
)}, add ${
Expand Down

0 comments on commit b947fdc

Please sign in to comment.