Skip to content

Commit 9c808cd

Browse files
authored
feat(cli): build --profile (#10719)
1 parent cb84f37 commit 9c808cd

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

packages/vite/src/node/cli.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'node:path'
2+
import fs from 'node:fs'
13
import { performance } from 'node:perf_hooks'
24
import { cac } from 'cac'
35
import colors from 'picocolors'
@@ -28,6 +30,27 @@ interface GlobalCLIOptions {
2830
force?: boolean
2931
}
3032

33+
export const stopProfiler = (log: (message: string) => void): void => {
34+
// @ts-ignore
35+
const profileSession = global.__vite_profile_session
36+
if (profileSession) {
37+
profileSession.post('Profiler.stop', (err: any, { profile }: any) => {
38+
// Write profile to disk, upload, etc.
39+
if (!err) {
40+
const outPath = path.resolve('./vite-profile.cpuprofile')
41+
fs.writeFileSync(outPath, JSON.stringify(profile))
42+
log(
43+
colors.yellow(
44+
`CPU profile written to ${colors.white(colors.dim(outPath))}`
45+
)
46+
)
47+
} else {
48+
throw err
49+
}
50+
})
51+
}
52+
}
53+
3154
const filterDuplicateOptions = <T extends object>(options: T) => {
3255
for (const [key, value] of Object.entries(options)) {
3356
if (Array.isArray(value)) {
@@ -125,11 +148,13 @@ cli
125148
)
126149

127150
server.printUrls()
151+
stopProfiler((message) => server.config.logger.info(` ${message}`))
128152
} catch (e) {
129-
createLogger(options.logLevel).error(
130-
colors.red(`error when starting dev server:\n${e.stack}`),
131-
{ error: e }
132-
)
153+
const logger = createLogger(options.logLevel)
154+
logger.error(colors.red(`error when starting dev server:\n${e.stack}`), {
155+
error: e
156+
})
157+
stopProfiler(logger.info)
133158
process.exit(1)
134159
}
135160
})
@@ -193,6 +218,8 @@ cli
193218
{ error: e }
194219
)
195220
process.exit(1)
221+
} finally {
222+
stopProfiler((message) => createLogger(options.logLevel).info(message))
196223
}
197224
})
198225

@@ -276,6 +303,8 @@ cli
276303
{ error: e }
277304
)
278305
process.exit(1)
306+
} finally {
307+
stopProfiler((message) => createLogger(options.logLevel).info(message))
279308
}
280309
}
281310
)

packages/vite/src/node/server/index.ts

-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fs from 'node:fs'
21
import path from 'node:path'
32
import type * as net from 'node:net'
43
import type * as http from 'node:http'
@@ -650,7 +649,6 @@ async function startServer(
650649
const hostname = await resolveHostname(options.host)
651650

652651
const protocol = options.https ? 'https' : 'http'
653-
const info = server.config.logger.info
654652

655653
const serverPort = await httpServerStart(httpServer, {
656654
port,
@@ -659,25 +657,6 @@ async function startServer(
659657
logger: server.config.logger
660658
})
661659

662-
// @ts-ignore
663-
const profileSession = global.__vite_profile_session
664-
if (profileSession) {
665-
profileSession.post('Profiler.stop', (err: any, { profile }: any) => {
666-
// Write profile to disk, upload, etc.
667-
if (!err) {
668-
const outPath = path.resolve('./vite-profile.cpuprofile')
669-
fs.writeFileSync(outPath, JSON.stringify(profile))
670-
info(
671-
colors.yellow(
672-
` CPU profile written to ${colors.white(colors.dim(outPath))}\n`
673-
)
674-
)
675-
} else {
676-
throw err
677-
}
678-
})
679-
}
680-
681660
if (options.open && !isRestart) {
682661
const path =
683662
typeof options.open === 'string' ? options.open : server.config.base

0 commit comments

Comments
 (0)