-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathcontext.ts
More file actions
89 lines (81 loc) · 2.65 KB
/
context.ts
File metadata and controls
89 lines (81 loc) · 2.65 KB
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
import type { DevToolsNodeContext } from '@vitejs/devtools-kit'
import type { ResolvedConfig, ViteDevServer } from 'vite'
import Debug from 'debug'
import { debounce } from 'perfect-debounce'
import { searchForWorkspaceRoot } from 'vite'
import { ContextUtils } from './context-utils'
import { DevToolsDockHost } from './host-docks'
import { RpcFunctionsHost } from './host-functions'
import { DevToolsTerminalHost } from './host-terminals'
import { DevToolsViewHost } from './host-views'
import { builtinRpcDecalrations } from './rpc'
const debug = Debug('vite:devtools:context')
export async function createDevToolsContext(
viteConfig: ResolvedConfig,
viteServer?: ViteDevServer,
): Promise<DevToolsNodeContext> {
const cwd = viteConfig.root
const context: DevToolsNodeContext = {
cwd,
workspaceRoot: searchForWorkspaceRoot(cwd) ?? cwd,
viteConfig,
viteServer,
mode: viteConfig.command === 'serve' ? 'dev' : 'build',
rpc: undefined!,
docks: undefined!,
views: undefined!,
utils: ContextUtils,
terminals: undefined!,
}
const rpcHost = new RpcFunctionsHost(context)
const docksHost = new DevToolsDockHost(context)
const viewsHost = new DevToolsViewHost(context)
const terminalsHost = new DevToolsTerminalHost(context)
context.rpc = rpcHost
context.docks = docksHost
context.views = viewsHost
context.terminals = terminalsHost
// Build-in function to list all RPC functions
for (const fn of builtinRpcDecalrations) {
rpcHost.register(fn)
}
// Register hosts side effects
docksHost.events.on('dock:entry:updated', debounce(() => {
rpcHost.broadcast({
method: 'vite:internal:docks:updated',
args: [],
})
}, 10))
terminalsHost.events.on('terminal:session:updated', debounce(() => {
rpcHost.broadcast({
method: 'vite:internal:terminals:updated',
args: [],
})
// New terminals might affect the visibility of the terminals dock entry, we trigger it here as well
rpcHost.broadcast({
method: 'vite:internal:docks:updated',
args: [],
})
}, 10))
terminalsHost.events.on('terminal:session:stream-chunk', (data) => {
rpcHost.broadcast({
method: 'vite:internal:terminals:stream-chunk',
args: [data],
})
})
// Register plugins
const plugins = viteConfig.plugins.filter(plugin => 'devtools' in plugin)
for (const plugin of plugins) {
if (!plugin.devtools?.setup)
continue
try {
debug(`Setting up plugin ${plugin.name}`)
await plugin.devtools?.setup?.(context)
}
catch (error) {
console.error(`[Vite DevTools] Error setting up plugin ${plugin.name}:`, error)
throw error
}
}
return context
}