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