Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): build --profile #10719

Merged
merged 2 commits into from Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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}`))
bluwy marked this conversation as resolved.
Show resolved Hide resolved
} 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