Skip to content

Commit

Permalink
chore: make tracing available
Browse files Browse the repository at this point in the history
  • Loading branch information
IWANABETHATGUY committed Aug 24, 2024
1 parent fdd453c commit 285d215
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 36 deletions.
51 changes: 18 additions & 33 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import type {
ModuleFormat,
OutputOptions,
Plugin,
RollupBuild,
RollupError,
RollupLog,
RollupOptions,
RollupOutput,
// RollupWatcher,
// WatcherOptions,
} from 'rolldown'
Expand All @@ -29,13 +27,12 @@ import {
import type { InlineConfig, ResolvedConfig } from './config'
import { resolveConfig } from './config'
import { buildReporterPlugin } from './plugins/reporter'
import { buildEsbuildPlugin } from './plugins/esbuild'
// import { buildEsbuildPlugin } from './plugins/esbuild'
import { type TerserOptions, terserPlugin } from './plugins/terser'
import {
arraify,
asyncFlatten,
copyDir,
displayTime,
emptyDir,
joinUrlSegments,
normalizePath,
Expand Down Expand Up @@ -446,7 +443,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
],
post: [
buildImportAnalysisPlugin(config),
...(config.esbuild !== false ? [buildEsbuildPlugin(config)] : []),
// ...(config.esbuild !== false ? [buildEsbuildPlugin(config)] : []),
...(options.minify ? [terserPlugin(config)] : []),
...(!config.isWorker
? [
Expand All @@ -459,14 +456,21 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
],
}
}
interface CanonicalRolldownOptions {
rollupOptions: RollupOptions
resolvedOutDirs: Set<string>,
emptyOutDir: boolean,
config:ResolvedConfig
normalizedOutputs: OutputOptions[]
}

/**
* Bundles the app for production.
* Returns a Promise containing the build result.
*/
export async function build(
inlineConfig: InlineConfig = {},
): Promise<RollupOutput | RollupOutput[] /*| RollupWatcher */> {
): Promise<CanonicalRolldownOptions> {
const config = await resolveConfig(
inlineConfig,
'build',
Expand Down Expand Up @@ -607,8 +611,6 @@ export async function build(
// config.logger.error(e.message, { error: e })
// }

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
Expand Down Expand Up @@ -756,44 +758,27 @@ export async function build(

// return watcher
// }

// write or generate files with rollup
const { rolldown } = await import('rolldown')
startTime = Date.now()
bundle = await rolldown(rollupOptions)

if (options.write) {
prepareOutDir(resolvedOutDirs, emptyOutDir, config)
}

const res: RollupOutput[] = []
for (const output of normalizedOutputs) {
res.push(await bundle[options.write ? 'write' : 'generate'](output))
return {
rollupOptions,
resolvedOutDirs,
emptyOutDir,
config,
normalizedOutputs
}
logger.info(
`${colors.green(`✓ built in ${displayTime(Date.now() - startTime)}`)}`,
)
return Array.isArray(outputs) ? res : res[0]
} catch (e) {
enhanceRollupError(e)
clearLine()
if (startTime) {
logger.error(
`${colors.red('x')} Build failed in ${displayTime(Date.now() - startTime)}`,
)
startTime = undefined
}
throw e
} finally {
// if (bundle) await bundle.close()
}
}

function prepareOutDir(
export function prepareOutDir(
outDirs: Set<string>,
emptyOutDir: boolean | null,
config: ResolvedConfig,
) {
): void {
const outDirsArray = [...outDirs]
for (const outDir of outDirs) {
if (emptyOutDir !== false && fs.existsSync(outDir)) {
Expand Down
40 changes: 37 additions & 3 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { performance } from 'node:perf_hooks'
import { cac } from 'cac'
import colors from 'picocolors'
import { VERSION } from './constants'
import type { BuildOptions } from './build'
import { prepareOutDir, BuildOptions } from './build'
import type { ServerOptions } from './server'
import type { CLIShortcut } from './shortcuts'
import type { LogLevel } from './logger'
import { createLogger } from './logger'
import { resolveConfig } from './config'
import { RollupOutput } from 'rolldown'
import { displayTime } from './utils'

const cli = cac('vite')

Expand Down Expand Up @@ -268,8 +270,15 @@ cli
const { build } = await import('./build')
const buildOptions: BuildOptions = cleanOptions(options)

let startTime
try {
await build({
const {
rollupOptions,
resolvedOutDirs,
emptyOutDir,
config,
normalizedOutputs
} = await build({
root,
base: options.base,
mode: options.mode,
Expand All @@ -278,11 +287,36 @@ cli
clearScreen: options.clearScreen,
build: buildOptions,
})

// write or generate files with rollup
const { rolldown } = await import('rolldown')
startTime = Date.now()
let bundle = await rolldown(rollupOptions)

if (options.write) {
prepareOutDir(resolvedOutDirs, emptyOutDir, config)
}

const res: RollupOutput[] = []
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) {
createLogger(options.logLevel).error(
let logger = createLogger(options.logLevel);
logger.error(
colors.red(`error during build:\n${e.stack}`),
{ error: e },
)

if (startTime) {
logger.error(
`${colors.red(`✓ built in ${displayTime(Date.now() - startTime)}`)}`,
)
}
process.exit(1)
} finally {
stopProfiler((message) => createLogger(options.logLevel).info(message))
Expand Down

0 comments on commit 285d215

Please sign in to comment.