-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathcontext.ts
More file actions
86 lines (77 loc) · 2.85 KB
/
Copy pathcontext.ts
File metadata and controls
86 lines (77 loc) · 2.85 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
import type { ViteDevToolsNodeContext } from '@vitejs/devtools-kit'
import type { RpcFunctionsHost } from 'devframe/node'
import type { ResolvedConfig, ViteDevServer } from 'vite'
import { createKitContext, createViteDevToolsHost } from '@vitejs/devtools-kit/node'
import { isObject } from 'devframe/node'
import { createDebug } from 'obug'
import { diagnostics } from './diagnostics'
import { builtinRpcDeclarations } from './rpc'
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<ViteDevToolsNodeContext> {
const cwd = viteConfig.root
const { searchForWorkspaceRoot } = await import('vite')
const mode = viteConfig.command === 'serve' ? 'dev' : 'build'
const workspaceRoot = searchForWorkspaceRoot(cwd) ?? cwd
const context = (await createKitContext({
cwd,
workspaceRoot,
mode,
host: createViteDevToolsHost({ viteConfig, viteServer, workspaceRoot }),
builtinRpcDeclarations,
viteConfig,
viteServer,
})) as ViteDevToolsNodeContext
// Fold the core (Vite) diagnostics into the shared host logger so plugin
// setup() hooks can reference DTK codes via `ctx.diagnostics.logger`.
context.diagnostics.register(diagnostics)
// Vite-specific built-in server commands.
const rpcHost = context.rpc as RpcFunctionsHost
context.commands.register({
id: 'vite:open-in-editor',
title: 'Open in Editor',
icon: 'ph:pencil-duotone',
category: 'editor',
showInPalette: false,
handler: (path: string) => rpcHost.invokeLocal('vite:core:open-in-editor', path),
})
context.commands.register({
id: 'vite:open-in-finder',
title: 'Open in Finder',
icon: 'ph:folder-open-duotone',
category: 'editor',
showInPalette: false,
handler: (path: string) => rpcHost.invokeLocal('vite:core:open-in-finder', path),
})
// Scan Vite plugins for `devtools` setup hooks.
const plugins = viteConfig.plugins.filter(plugin => 'devtools' in plugin)
for (const plugin of plugins) {
if (!plugin.devtools?.setup)
continue
if (shouldSkipSetupByCapabilities(plugin, mode)) {
debugSetup(`skipping plugin ${JSON.stringify(plugin.name)} due to disabled capabilities in ${mode} mode`)
continue
}
try {
debugSetup(`setting up plugin ${JSON.stringify(plugin.name)}`)
await plugin.devtools?.setup?.(context)
}
catch (error) {
throw diagnostics.DTK0014({ name: plugin.name, cause: error })
}
}
return context
}