Visitar URL original
feat(cli): support naming the CPU profile via --profile [name] (#23042) · vitejs/vite@a500dee · GitHub
Skip to content

Commit a500dee

Browse files
authored
feat(cli): support naming the CPU profile via --profile [name] (#23042)
1 parent 67a6807 commit a500dee

5 files changed

Lines changed: 35 additions & 11 deletions

File tree

‎docs/guide/cli.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ vite [root]
2828
| `-l, --logLevel <level>` | info \| warn \| error \| silent (`string`) |
2929
| `--clearScreen` | Allow/disable clear screen when logging (`boolean`) |
3030
| `--configLoader <loader>` | Use `bundle` to bundle the config with Rolldown, or `runner` (experimental) to process it on the fly, or `native` (experimental) to load using the native runtime (default: `bundle`) |
31-
| `--profile` | Start built-in Node.js inspector (check [Performance bottlenecks](/guide/troubleshooting#performance-bottlenecks)) |
31+
| `--profile [name]` | Start built-in Node.js inspector and write the profile to `<name>.cpuprofile` (check [Performance bottlenecks](/guide/troubleshooting#performance-bottlenecks)) (`boolean \| string`) |
3232
| `-d, --debug [feat]` | Show debug logs (`string \| boolean`) |
3333
| `-f, --filter <filter>` | Filter debug logs (`string`) |
3434
| `-m, --mode <mode>` | Set env mode (`string`) |
@@ -67,7 +67,7 @@ vite build [root]
6767
| `-l, --logLevel <level>` | info \| warn \| error \| silent (`string`) |
6868
| `--clearScreen` | Allow/disable clear screen when logging (`boolean`) |
6969
| `--configLoader <loader>` | Use `bundle` to bundle the config with Rolldown, or `runner` (experimental) to process it on the fly, or `native` (experimental) to load using the native runtime (default: `bundle`) |
70-
| `--profile` | Start built-in Node.js inspector (check [Performance bottlenecks](/guide/troubleshooting#performance-bottlenecks)) |
70+
| `--profile [name]` | Start built-in Node.js inspector and write the profile to `<name>.cpuprofile` (check [Performance bottlenecks](/guide/troubleshooting#performance-bottlenecks)) (`boolean \| string`) |
7171
| `-d, --debug [feat]` | Show debug logs (`string \| boolean`) |
7272
| `-f, --filter <filter>` | Filter debug logs (`string`) |
7373
| `-m, --mode <mode>` | Set env mode (`string`) |

‎docs/guide/troubleshooting.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ vite build --profile
240240
Once your application is opened in the browser, just await finish loading it and then go back to the terminal and press `p` key (will stop the Node.js inspector) then press `q` key to stop the dev server.
241241
:::
242242

243-
Node.js inspector will generate `vite-profile-0.cpuprofile` in the root folder, go to https://www.speedscope.app/, and upload the CPU profile using the `BROWSE` button to inspect the result.
243+
Node.js inspector will generate `vite-profile-0.cpuprofile` in the root folder. You can pass `--profile <name>` (or `--profile=<name>`) to write `<name>.cpuprofile` instead. Go to https://www.speedscope.app/, and upload the CPU profile using the `BROWSE` button to inspect the result.
244244

245245
You can install [vite-plugin-inspect](https://github.com/antfu/vite-plugin-inspect), which lets you inspect the intermediate state of Vite plugins and can also help you to identify which plugins or middlewares are the bottleneck in your applications. The plugin can be used in both dev and build modes. Check the readme file for more details.
246246

‎packages/vite/bin/vite.js‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ const debugIndex = process.argv.findIndex((arg) => /^(?:-d|--debug)$/.test(arg))
2020
const filterIndex = process.argv.findIndex((arg) =>
2121
/^(?:-f|--filter)$/.test(arg),
2222
)
23-
const profileIndex = process.argv.indexOf('--profile')
23+
const profileIndex = process.argv.findIndex(
24+
(arg) => arg === '--profile' || arg.startsWith('--profile='),
25+
)
2426

2527
if (debugIndex > 0) {
2628
let value = process.argv[debugIndex + 1]
@@ -63,10 +65,19 @@ function start() {
6365
}
6466

6567
if (profileIndex > 0) {
66-
process.argv.splice(profileIndex, 1)
67-
const next = process.argv[profileIndex]
68-
if (next && next[0] !== '-') {
69-
process.argv.splice(profileIndex, 1)
68+
const [profileArg] = process.argv.splice(profileIndex, 1)
69+
// `--profile [name]` writes the profile to `<name>.cpuprofile`. The value is
70+
// optional and consumed like cac does for other `[optional]` value flags.
71+
let profileName = profileArg.slice('--profile='.length)
72+
if (!profileName) {
73+
const next = process.argv[profileIndex]
74+
if (next && next[0] !== '-') {
75+
process.argv.splice(profileIndex, 1)
76+
profileName = next
77+
}
78+
}
79+
if (profileName) {
80+
global.__vite_profile_name = profileName
7081
}
7182
const inspector = await import('node:inspector').then((r) => r.default)
7283
const session = (global.__vite_profile_session = new inspector.Session())

‎packages/vite/src/node/cli.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ interface GlobalCLIOptions {
4545
logLevel?: LogLevel
4646
clearScreen?: boolean
4747
configLoader?: 'bundle' | 'runner' | 'native'
48+
profile?: boolean | string
4849
d?: boolean | string
4950
debug?: boolean | string
5051
f?: string
@@ -74,9 +75,14 @@ export const stopProfiler = (
7475
profileSession!.post('Profiler.stop', (err, { profile }) => {
7576
// Write profile to disk, upload, etc.
7677
if (!err) {
77-
const outPath = path.resolve(
78-
`./vite-profile-${profileCount++}.cpuprofile`,
79-
)
78+
const name = global.__vite_profile_name
79+
const count = profileCount++
80+
const fileName = name
81+
? count === 0
82+
? name
83+
: `${name}-${count}`
84+
: `vite-profile-${count}`
85+
const outPath = path.resolve(`./${fileName}.cpuprofile`)
8086
fs.writeFileSync(outPath, JSON.stringify(profile))
8187
log(
8288
colors.yellow(
@@ -114,6 +120,7 @@ function cleanGlobalCLIOptions<Options extends GlobalCLIOptions>(
114120
delete ret.logLevel
115121
delete ret.clearScreen
116122
delete ret.configLoader
123+
delete ret.profile
117124
delete ret.d
118125
delete ret.debug
119126
delete ret.f
@@ -183,6 +190,10 @@ cli
183190
'--configLoader <loader>',
184191
`[string] use 'bundle' to bundle the config with Rolldown, or 'runner' (experimental) to process it on the fly, or 'native' (experimental) to load using the native runtime (default: bundle)`,
185192
)
193+
.option(
194+
'--profile [name]',
195+
`[boolean | string] start built-in Node.js inspector`,
196+
)
186197
.option('-d, --debug [feat]', `[string | boolean] show debug logs`)
187198
.option('-f, --filter <filter>', `[string] filter debug logs`)
188199
.option('-m, --mode <mode>', `[string] set env mode`)

‎packages/vite/src/types/shims.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ declare module 'postcss-import' {
2626
// eslint-disable-next-line no-var
2727
declare var __vite_profile_session: import('node:inspector').Session | undefined
2828
// eslint-disable-next-line no-var
29+
declare var __vite_profile_name: string | undefined
30+
// eslint-disable-next-line no-var
2931
declare var __vite_start_time: number | undefined

0 commit comments

Comments
 (0)