vite.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env node
  2. import module from 'node:module'
  3. import { performance } from 'node:perf_hooks'
  4. if (!import.meta.url.includes('node_modules')) {
  5. if (!process.env.DEBUG_DISABLE_SOURCE_MAP) {
  6. // eslint-disable-next-line n/no-unsupported-features/node-builtins -- only used in dev
  7. process.setSourceMapsEnabled(true)
  8. }
  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.findIndex(
  20. (arg) => arg === '--profile' || arg.startsWith('--profile='),
  21. )
  22. if (debugIndex > 0) {
  23. let value = process.argv[debugIndex + 1]
  24. if (!value || value[0] === '-') {
  25. value = 'vite:*'
  26. } else {
  27. // support debugging multiple flags with comma-separated list
  28. value = value
  29. .split(',')
  30. .map((v) => `vite:${v}`)
  31. .join(',')
  32. }
  33. process.env.DEBUG = `${
  34. process.env.DEBUG ? process.env.DEBUG + ',' : ''
  35. }${value}`
  36. if (filterIndex > 0) {
  37. const filter = process.argv[filterIndex + 1]
  38. if (filter && filter[0] !== '-') {
  39. process.env.VITE_DEBUG_FILTER = filter
  40. }
  41. }
  42. }
  43. function start() {
  44. try {
  45. // eslint-disable-next-line n/no-unsupported-features/node-builtins -- it is supported in Node 22.8.0+ and only called if it exists
  46. module.enableCompileCache?.()
  47. // flush the cache after 10s because the cache is not flushed until process end
  48. // for dev server, the cache is never flushed unless manually flushed because the process.exit is called
  49. // also flushing the cache in SIGINT handler seems to cause the process to hang
  50. setTimeout(() => {
  51. try {
  52. // eslint-disable-next-line n/no-unsupported-features/node-builtins -- it is supported in Node 22.12.0+ and only called if it exists
  53. module.flushCompileCache?.()
  54. } catch {}
  55. }, 10 * 1000).unref()
  56. } catch {}
  57. return import('../dist/node/cli.js')
  58. }
  59. if (profileIndex > 0) {
  60. const [profileArg] = process.argv.splice(profileIndex, 1)
  61. // `--profile [name]` writes the profile to `<name>.cpuprofile`. The value is
  62. // optional and consumed like cac does for other `[optional]` value flags.
  63. let profileName = profileArg.slice('--profile='.length)
  64. if (!profileName) {
  65. const next = process.argv[profileIndex]
  66. if (next && next[0] !== '-') {
  67. process.argv.splice(profileIndex, 1)
  68. profileName = next
  69. }
  70. }
  71. if (profileName) {
  72. global.__vite_profile_name = profileName
  73. }
  74. const inspector = await import('node:inspector').then((r) => r.default)
  75. const session = (global.__vite_profile_session = new inspector.Session())
  76. session.connect()
  77. session.post('Profiler.enable', () => {
  78. session.post('Profiler.start', start)
  79. })
  80. } else {
  81. start()
  82. }