Skip to content

Commit

Permalink
feat(cli): build --profile (#10719)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Nov 10, 2022
1 parent cb84f37 commit 9c808cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
37 changes: 33 additions & 4 deletions packages/vite/src/node/cli.ts
@@ -1,3 +1,5 @@
import path from 'node:path'
import fs from 'node:fs'
import { performance } from 'node:perf_hooks'
import { cac } from 'cac'
import colors from 'picocolors'
Expand Down Expand Up @@ -28,6 +30,27 @@ interface GlobalCLIOptions {
force?: boolean
}

export const stopProfiler = (log: (message: string) => void): void => {
// @ts-ignore
const profileSession = global.__vite_profile_session
if (profileSession) {
profileSession.post('Profiler.stop', (err: any, { profile }: any) => {
// Write profile to disk, upload, etc.
if (!err) {
const outPath = path.resolve('./vite-profile.cpuprofile')
fs.writeFileSync(outPath, JSON.stringify(profile))
log(
colors.yellow(
`CPU profile written to ${colors.white(colors.dim(outPath))}`
)
)
} else {
throw err
}
})
}
}

const filterDuplicateOptions = <T extends object>(options: T) => {
for (const [key, value] of Object.entries(options)) {
if (Array.isArray(value)) {
Expand Down Expand Up @@ -125,11 +148,13 @@ cli
)

server.printUrls()
stopProfiler((message) => server.config.logger.info(` ${message}`))
} catch (e) {
createLogger(options.logLevel).error(
colors.red(`error when starting dev server:\n${e.stack}`),
{ error: e }
)
const logger = createLogger(options.logLevel)
logger.error(colors.red(`error when starting dev server:\n${e.stack}`), {
error: e
})
stopProfiler(logger.info)
process.exit(1)
}
})
Expand Down Expand Up @@ -193,6 +218,8 @@ cli
{ error: e }
)
process.exit(1)
} finally {
stopProfiler((message) => createLogger(options.logLevel).info(message))
}
})

Expand Down Expand Up @@ -276,6 +303,8 @@ cli
{ error: e }
)
process.exit(1)
} finally {
stopProfiler((message) => createLogger(options.logLevel).info(message))
}
}
)
Expand Down
21 changes: 0 additions & 21 deletions packages/vite/src/node/server/index.ts
@@ -1,4 +1,3 @@
import fs from 'node:fs'
import path from 'node:path'
import type * as net from 'node:net'
import type * as http from 'node:http'
Expand Down Expand Up @@ -650,7 +649,6 @@ async function startServer(
const hostname = await resolveHostname(options.host)

const protocol = options.https ? 'https' : 'http'
const info = server.config.logger.info

const serverPort = await httpServerStart(httpServer, {
port,
Expand All @@ -659,25 +657,6 @@ async function startServer(
logger: server.config.logger
})

// @ts-ignore
const profileSession = global.__vite_profile_session
if (profileSession) {
profileSession.post('Profiler.stop', (err: any, { profile }: any) => {
// Write profile to disk, upload, etc.
if (!err) {
const outPath = path.resolve('./vite-profile.cpuprofile')
fs.writeFileSync(outPath, JSON.stringify(profile))
info(
colors.yellow(
` CPU profile written to ${colors.white(colors.dim(outPath))}\n`
)
)
} else {
throw err
}
})
}

if (options.open && !isRestart) {
const path =
typeof options.open === 'string' ? options.open : server.config.base
Expand Down

0 comments on commit 9c808cd

Please sign in to comment.