Skip to content

Commit

Permalink
wip: profile
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 14, 2020
1 parent 9bf5959 commit e312645
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ explorations
.idea
*.local
/packages/vite/LICENSE
*.cpuprofile
3 changes: 2 additions & 1 deletion packages/plugin-react-refresh/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-check
const fs = require('fs')
const { transformSync } = require('@babel/core')

const runtimePublicPath = '/@react-refresh'
const runtimeFilePath = require.resolve(
Expand Down Expand Up @@ -77,7 +78,7 @@ const transform = {
}

const isReasonReact = id.endsWith('.bs.js')
const result = require('@babel/core').transformSync(code, {
const result = transformSync(code, {
plugins: [
require('@babel/plugin-syntax-import-meta'),
require('react-refresh/babel')
Expand Down
21 changes: 20 additions & 1 deletion packages/vite/bin/vite.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ global.__vite_start_time = Date.now()
// check debug mode first before requiring the CLI.
const debugIndex = process.argv.indexOf('--debug')
const filterIndex = process.argv.indexOf('--filter')
const profileIndex = process.argv.indexOf('--profile')

if (debugIndex > 0) {
let value = process.argv[debugIndex + 1]
Expand Down Expand Up @@ -31,4 +32,22 @@ if (debugIndex > 0) {
} catch (e) {}
}

require('../dist/node/cli')
function start() {
require('../dist/node/cli')
}

if (profileIndex > 0) {
process.argv.splice(profileIndex, 1)
const next = process.argv[profileIndex]
if (next && !next.startsWith('-')) {
process.argv.splice(profileIndex, 1)
}
const inspector = require('inspector')
const session = (global.__vite_profile_session = new inspector.Session())
session.connect()
session.post('Profiler.enable', () => {
session.post('Profiler.start', start)
})
} else {
start()
}
3 changes: 2 additions & 1 deletion packages/vite/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const nodeConfig = {
input: {
index: path.resolve(__dirname, 'src/node/index.ts'),
cli: path.resolve(__dirname, 'src/node/cli.ts'),
server: path.resolve(__dirname, 'src/node/server/index.ts')
server: path.resolve(__dirname, 'src/node/server/index.ts'),
build: path.resolve(__dirname, 'src/node/build/index.ts')
},
external: [
'fsevents',
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ function cleanOptions(options: GlobalCLIOptions) {

cli
.option('-c, --config <file>', `[string] use specified config file`)
.option('-d, --debug [feat]', `[string | boolean] show debug logs`)
.option('--filter [filter]', `[string] filter debug logs`)
.option('--root <path>', `[string] use specified config file`)
.option('--debug [feat]', `[string | boolean] show debug logs`)
.option('--filter [filter]', `[string] filter debug logs`)

// dev
cli
Expand Down
20 changes: 20 additions & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os from 'os'
import fs from 'fs'
import path from 'path'
import * as http from 'http'
import * as https from 'https'
Expand Down Expand Up @@ -343,12 +344,31 @@ export async function startServer(
console.log(` > ${type} ${chalk.cyan(url)}`)
})
)

console.log()
console.log(
// @ts-ignore
chalk.cyan(` ready in ${Date.now() - global.__vite_start_time}ms.`)
)
console.log()

// @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))
console.log(
chalk.yellow(` CPU profile written to ${chalk.white.dim(outPath)}`)
)
console.log()
} else {
throw err
}
})
}
})

if (options.open) {
Expand Down

0 comments on commit e312645

Please sign in to comment.