-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathcontext.ts
More file actions
145 lines (129 loc) · 4.68 KB
/
context.ts
File metadata and controls
145 lines (129 loc) · 4.68 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
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
import type { DevToolsNodeContext, JsonRenderer, JsonRenderSpec } from '@vitejs/devtools-kit'
import type { ResolvedConfig, ViteDevServer } from 'vite'
import { createDebug } from 'obug'
import { debounce } from 'perfect-debounce'
import { ContextUtils } from './context-utils'
import { DevToolsDockHost } from './host-docks'
import { RpcFunctionsHost } from './host-functions'
import { DevToolsLogsHost } from './host-logs'
import { DevToolsTerminalHost } from './host-terminals'
import { DevToolsViewHost } from './host-views'
import { builtinRpcDeclarations } from './rpc'
import { isObject } from './utils'
const debugSetup = createDebug('vite:devtools:context:setup')
function shouldSkipSetupByCapabilities(
plugin: ResolvedConfig['plugins'][number],
mode: 'dev' | 'build',
): boolean {
const modeCapabilities = plugin.devtools?.capabilities?.[mode]
if (modeCapabilities === false)
return true
if (!isObject(modeCapabilities))
return false
return Object.values(modeCapabilities).includes(false)
}
export async function createDevToolsContext(
viteConfig: ResolvedConfig,
viteServer?: ViteDevServer,
): Promise<DevToolsNodeContext> {
const cwd = viteConfig.root
const { searchForWorkspaceRoot } = await import('vite')
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!,
logs: undefined!,
createJsonRenderer: undefined!,
}
const rpcHost = new RpcFunctionsHost(context)
const docksHost = new DevToolsDockHost(context)
const viewsHost = new DevToolsViewHost(context)
const terminalsHost = new DevToolsTerminalHost(context)
const logsHost = new DevToolsLogsHost(context)
context.rpc = rpcHost
context.docks = docksHost
context.views = viewsHost
context.terminals = terminalsHost
context.logs = logsHost
// json-render factory
let jrCounter = 0
context.createJsonRenderer = (initialSpec: JsonRenderSpec): JsonRenderer => {
const stateKey = `devtoolskit:internal:json-render:${jrCounter++}`
const statePromise = rpcHost.sharedState.get(stateKey as any, {
initialValue: initialSpec as any,
})
return {
_stateKey: stateKey,
async updateSpec(spec) {
const state = await statePromise
state.mutate(() => spec as any)
},
async updateState(newState) {
const state = await statePromise
state.mutate((draft: any) => {
draft.state = { ...draft.state, ...newState }
})
},
}
}
// Build-in function to list all RPC functions
for (const fn of builtinRpcDeclarations) {
rpcHost.register(fn)
}
await docksHost.init()
const docksSharedState = await rpcHost.sharedState.get('devtoolskit:internal:docks', { initialValue: [] })
// Register hosts side effects
docksHost.events.on('dock:entry:updated', debounce(() => {
docksSharedState.mutate(() => context.docks.values())
}, context.mode === 'build' ? 0 : 10))
terminalsHost.events.on('terminal:session:updated', debounce(() => {
rpcHost.broadcast({
method: 'devtoolskit:internal:terminals:updated',
args: [],
})
docksSharedState.mutate(() => context.docks.values())
}, context.mode === 'build' ? 0 : 10))
terminalsHost.events.on('terminal:session:stream-chunk', (data) => {
rpcHost.broadcast({
method: 'devtoolskit:internal:terminals:stream-chunk',
args: [data],
})
})
const debouncedLogsUpdate = debounce(() => {
rpcHost.broadcast({
method: 'devtoolskit:internal:logs:updated',
args: [],
})
docksSharedState.mutate(() => context.docks.values())
}, context.mode === 'build' ? 0 : 10)
logsHost.events.on('log:added', () => debouncedLogsUpdate())
logsHost.events.on('log:updated', () => debouncedLogsUpdate())
logsHost.events.on('log:removed', () => debouncedLogsUpdate())
logsHost.events.on('log:cleared', () => debouncedLogsUpdate())
// Register plugins
const plugins = viteConfig.plugins.filter(plugin => 'devtools' in plugin)
for (const plugin of plugins) {
if (!plugin.devtools?.setup)
continue
if (shouldSkipSetupByCapabilities(plugin, context.mode)) {
debugSetup(`skipping plugin ${JSON.stringify(plugin.name)} due to disabled capabilities in ${context.mode} mode`)
continue
}
try {
debugSetup(`setting up plugin ${JSON.stringify(plugin.name)}`)
await plugin.devtools?.setup?.(context)
}
catch (error) {
console.error(`[Vite DevTools] Error setting up plugin ${plugin.name}:`, error)
throw error
}
}
return context
}