You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.5 KiB

  1. #!/usr/bin/env node
  2. import { performance } from 'node:perf_hooks'
  3. import module from 'node:module'
  4. if (!import.meta.url.includes('node_modules')) {
  5. try {
  6. // only available as dev dependency
  7. await import('source-map-support').then((r) => r.default.install())
  8. } catch {}
  9. process.on('unhandledRejection', (err) => {
  10. throw new Error('UNHANDLED PROMISE REJECTION', { cause: err })
  11. })
  12. }
  13. global.__vite_start_time = performance.now()
  14. // check debug mode first before requiring the CLI.
  15. const debugIndex = process.argv.findIndex((arg) => /^(?:-d|--debug)$/.test(arg))
  16. const filterIndex = process.argv.findIndex((arg) =>
  17. /^(?:-f|--filter)$/.test(arg),
  18. )
  19. const profileIndex = process.argv.indexOf('--profile')
  20. if (debugIndex > 0) {
  21. let value = process.argv[debugIndex + 1]
  22. if (!value || value.startsWith('-')) {
  23. value = 'vite:*'
  24. } else {
  25. // support debugging multiple flags with comma-separated list
  26. value = value
  27. .split(',')
  28. .map((v) => `vite:${v}`)
  29. .join(',')
  30. }
  31. process.env.DEBUG = `${
  32. process.env.DEBUG ? process.env.DEBUG + ',' : ''
  33. }${value}`
  34. if (filterIndex > 0) {
  35. const filter = process.argv[filterIndex + 1]
  36. if (filter && !filter.startsWith('-')) {
  37. process.env.VITE_DEBUG_FILTER = filter
  38. }
  39. }
  40. }
  41. function start() {
  42. try {
  43. // eslint-disable-next-line n/no-unsupported-features/node-builtins -- it is supported in Node 22.8.0+ and only called if it exists
  44. module.enableCompileCache?.()
  45. // flush the cache after 10s because the cache is not flushed until process end
  46. // for dev server, the cache is never flushed unless manually flushed because the process.exit is called
  47. // also flushing the cache in SIGINT handler seems to cause the process to hang
  48. setTimeout(() => {
  49. try {
  50. // eslint-disable-next-line n/no-unsupported-features/node-builtins -- it is supported in Node 22.12.0+ and only called if it exists
  51. module.flushCompileCache?.()
  52. } catch {}
  53. }, 10 * 1000).unref()
  54. } catch {}
  55. return import('../dist/node/cli.js')
  56. }
  57. if (profileIndex > 0) {
  58. process.argv.splice(profileIndex, 1)
  59. const next = process.argv[profileIndex]
  60. if (next && !next.startsWith('-')) {
  61. process.argv.splice(profileIndex, 1)
  62. }
  63. const inspector = await import('node:inspector').then((r) => r.default)
  64. const session = (global.__vite_profile_session = new inspector.Session())
  65. session.connect()
  66. session.post('Profiler.enable', () => {
  67. session.post('Profiler.start', start)
  68. })
  69. } else {
  70. start()
  71. }