-
Notifications
You must be signed in to change notification settings - Fork 32
/
cli.js
executable file
·57 lines (50 loc) · 1.63 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env node
import path from 'path'
import { promises as fs } from 'fs'
import { createRequire } from 'module'
import cp from 'child_process'
const [, , command, ...args] = process.argv
const patchVite = async () => {
const require = createRequire(import.meta.url)
const vitePath = path.dirname(require.resolve('vite/package.json'))
const viteConfigLoader = path.resolve(vitePath, 'dist/node/config.js')
try {
await fs.writeFile(
viteConfigLoader,
(await fs.readFile(viteConfigLoader, 'utf8'))
// Use native ESM to import config file instead of Rollup
.replace(
/\srequire\(resolvedPath\)/,
' await import(resolvedPath); config = config.default || config'
)
// Ignore TS check to keep using native ESM with ts-node instead of Rollup
.replace(
/if\s+\(!isTS\)/i,
'if (!isTS || process[Symbol.for("ts-node.register.instance")])'
)
)
} catch (error) {
console.warn('Vitedge could not patch Vite:', error.message)
}
}
;(async () => {
if (command === 'build') {
const { default: build } = await import('vitedge/build/index.js')
await build()
process.exit()
} else if (command === 'dev') {
args.unshift('node_modules/.bin/vite')
const { default: config } = await import('vitedge/config.cjs')
if (config.isTS) {
args.unshift('--loader', 'ts-node/esm')
}
cp.spawn('node', args, {
stdio: [process.stdin, process.stdout, process.stderr],
})
} else if (command === 'patch') {
await patchVite()
process.exit()
} else {
console.log(`Command "${command}" not supported`)
}
})()