|
| 1 | +import path from 'node:path' |
| 2 | +import fs from 'node:fs' |
1 | 3 | import { performance } from 'node:perf_hooks'
|
2 | 4 | import { cac } from 'cac'
|
3 | 5 | import colors from 'picocolors'
|
@@ -28,6 +30,27 @@ interface GlobalCLIOptions {
|
28 | 30 | force?: boolean
|
29 | 31 | }
|
30 | 32 |
|
| 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 | + |
31 | 54 | const filterDuplicateOptions = <T extends object>(options: T) => {
|
32 | 55 | for (const [key, value] of Object.entries(options)) {
|
33 | 56 | if (Array.isArray(value)) {
|
@@ -125,11 +148,13 @@ cli
|
125 | 148 | )
|
126 | 149 |
|
127 | 150 | server.printUrls()
|
| 151 | + stopProfiler((message) => server.config.logger.info(` ${message}`)) |
128 | 152 | } 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) |
133 | 158 | process.exit(1)
|
134 | 159 | }
|
135 | 160 | })
|
|
193 | 218 | { error: e }
|
194 | 219 | )
|
195 | 220 | process.exit(1)
|
| 221 | + } finally { |
| 222 | + stopProfiler((message) => createLogger(options.logLevel).info(message)) |
196 | 223 | }
|
197 | 224 | })
|
198 | 225 |
|
|
276 | 303 | { error: e }
|
277 | 304 | )
|
278 | 305 | process.exit(1)
|
| 306 | + } finally { |
| 307 | + stopProfiler((message) => createLogger(options.logLevel).info(message)) |
279 | 308 | }
|
280 | 309 | }
|
281 | 310 | )
|
|
0 commit comments