-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cli.ts
154 lines (132 loc) · 4.29 KB
/
cli.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import cac from 'cac'
import c from 'picocolors'
import { createServer } from 'vite'
import { version } from '../package.json'
import { ViteNodeServer } from './server'
import { ViteNodeRunner } from './client'
import type { ViteNodeServerOptions } from './types'
import { toArray } from './utils'
import { createHotContext, handleMessage, viteNodeHmrPlugin } from './hmr'
import { installSourcemapsSupport } from './source-map'
const cli = cac('vite-node')
cli
.version(version)
.option('-r, --root <path>', 'Use specified root directory')
.option('-c, --config <path>', 'Use specified config file')
.option('-m, --mode <mode>', 'Set env mode')
.option('-w, --watch', 'Restart on file changes, similar to "nodemon"')
.option('--script', 'Use vite-node as a script runner')
.option('--options <options>', 'Use specified Vite server options')
.help()
cli
.command('[...files]')
.allowUnknownOptions()
.action(run)
cli.parse()
export interface CliOptions {
root?: string
script?: boolean
config?: string
mode?: string
watch?: boolean
options?: ViteNodeServerOptionsCLI
'--'?: string[]
}
async function run(files: string[], options: CliOptions = {}) {
if (options.script) {
files = [files[0]]
options = {}
process.argv = [process.argv[0], files[0], ...process.argv.slice(2).filter(arg => arg !== '--script' && arg !== files[0])]
}
else {
process.argv = [...process.argv.slice(0, 2), ...(options['--'] || [])]
}
if (!files.length) {
console.error(c.red('No files specified.'))
cli.outputHelp()
process.exit(1)
}
const serverOptions = options.options
? parseServerOptions(options.options)
: {}
const server = await createServer({
logLevel: 'error',
configFile: options.config,
root: options.root,
mode: options.mode,
plugins: [
options.watch && viteNodeHmrPlugin(),
],
})
await server.pluginContainer.buildStart({})
const node = new ViteNodeServer(server, serverOptions)
installSourcemapsSupport({
getSourceMap: source => node.getSourceMap(source),
})
const runner = new ViteNodeRunner({
root: server.config.root,
base: server.config.base,
fetchModule(id) {
return node.fetchModule(id)
},
resolveId(id, importer) {
return node.resolveId(id, importer)
},
createHotContext(runner, url) {
return createHotContext(runner, server.emitter, files, url)
},
})
// provide the vite define variable in this context
await runner.executeId('/@vite/env')
for (const file of files)
await runner.executeFile(file)
if (!options.watch)
await server.close()
server.emitter?.on('message', (payload) => {
handleMessage(runner, server.emitter, files, payload)
})
if (options.watch) {
process.on('uncaughtException', (err) => {
console.error(c.red('[vite-node] Failed to execute file: \n'), err)
})
}
}
function parseServerOptions(serverOptions: ViteNodeServerOptionsCLI): ViteNodeServerOptions {
const inlineOptions = serverOptions.deps?.inline === true ? true : toArray(serverOptions.deps?.inline)
return {
...serverOptions,
deps: {
...serverOptions.deps,
inline: inlineOptions !== true
? inlineOptions.map((dep) => {
return (dep.startsWith('/') && dep.endsWith('/'))
? new RegExp(dep)
: dep
})
: true,
external: toArray(serverOptions.deps?.external).map((dep) => {
return (dep.startsWith('/') && dep.endsWith('/'))
? new RegExp(dep)
: dep
}),
},
transformMode: {
...serverOptions.transformMode,
ssr: toArray(serverOptions.transformMode?.ssr).map(dep => new RegExp(dep)),
web: toArray(serverOptions.transformMode?.web).map(dep => new RegExp(dep)),
},
}
}
type Optional<T> = T | undefined
type ComputeViteNodeServerOptionsCLI<T extends Record<string, any>> = {
[K in keyof T]: T[K] extends Optional<RegExp[]>
? string | string[]
: T[K] extends Optional<(string | RegExp)[]>
? string | string[]
: T[K] extends Optional<(string | RegExp)[] | true>
? string | string[] | true
: T[K] extends Optional<Record<string, any>>
? ComputeViteNodeServerOptionsCLI<T[K]>
: T[K]
}
export type ViteNodeServerOptionsCLI = ComputeViteNodeServerOptionsCLI<ViteNodeServerOptions>