vite.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env node
  2. import { performance } from 'node:perf_hooks'
  3. if (!import.meta.url.includes('node_modules')) {
  4. try {
  5. // only available as dev dependency
  6. await import('source-map-support').then((r) => r.default.install())
  7. } catch (e) {}
  8. }
  9. global.__vite_start_time = performance.now()
  10. // check debug mode first before requiring the CLI.
  11. const debugIndex = process.argv.findIndex((arg) => /^(?:-d|--debug)$/.test(arg))
  12. const filterIndex = process.argv.findIndex((arg) =>
  13. /^(?:-f|--filter)$/.test(arg),
  14. )
  15. const profileIndex = process.argv.indexOf('--profile')
  16. if (debugIndex > 0) {
  17. let value = process.argv[debugIndex + 1]
  18. if (!value || value.startsWith('-')) {
  19. value = 'vite:*'
  20. } else {
  21. // support debugging multiple flags with comma-separated list
  22. value = value
  23. .split(',')
  24. .map((v) => `vite:${v}`)
  25. .join(',')
  26. }
  27. process.env.DEBUG = `${
  28. process.env.DEBUG ? process.env.DEBUG + ',' : ''
  29. }${value}`
  30. if (filterIndex > 0) {
  31. const filter = process.argv[filterIndex + 1]
  32. if (filter && !filter.startsWith('-')) {
  33. process.env.VITE_DEBUG_FILTER = filter
  34. }
  35. }
  36. }
  37. function start() {
  38. return import('../dist/node/cli.js')
  39. }
  40. if (profileIndex > 0) {
  41. process.argv.splice(profileIndex, 1)
  42. const next = process.argv[profileIndex]
  43. if (next && !next.startsWith('-')) {
  44. process.argv.splice(profileIndex, 1)
  45. }
  46. const inspector = await import('node:inspector').then((r) => r.default)
  47. const session = (global.__vite_profile_session = new inspector.Session())
  48. session.connect()
  49. session.post('Profiler.enable', () => {
  50. session.post('Profiler.start', start)
  51. })
  52. } else {
  53. start()
  54. }