| 66 | let profileCount = 0 |
| 67 | |
| 68 | export const stopProfiler = ( |
| 69 | log: (message: string) => void, |
| 70 | ): void | Promise<void> => { |
| 71 | if (!profileSession) return |
| 72 | return new Promise((res, rej) => { |
| 73 | profileSession!.post('Profiler.stop', (err, { profile }) => { |
| 74 | // Write profile to disk, upload, etc. |
| 75 | if (!err) { |
| 76 | const outPath = path.resolve( |
| 77 | `./vite-profile-${profileCount++}.cpuprofile`, |
| 78 | ) |
| 79 | fs.writeFileSync(outPath, JSON.stringify(profile)) |
| 80 | log( |
| 81 | colors.yellow( |
| 82 | `CPU profile written to ${colors.white(colors.dim(outPath))}`, |
| 83 | ), |
| 84 | ) |
| 85 | profileSession = undefined |
| 86 | res() |
| 87 | } else { |
| 88 | rej(err) |
| 89 | } |
| 90 | }) |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | const filterDuplicateOptions = <T extends object>(options: T) => { |
| 95 | for (const [key, value] of Object.entries(options)) { |