Skip to content

Commit cffec4b

Browse files
committed
feat: auto inject vite plugins
1 parent e58849d commit cffec4b

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

packages/devtools/src/node/standalone.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { DevToolsNodeContext } from '@vitejs/devtools-kit'
2-
import type { ResolvedConfig } from 'vite'
2+
import type { Plugin, ResolvedConfig } from 'vite'
33
import process from 'node:process'
44
import { loadConfigFromFile, resolveConfig } from 'vite'
55
import { createDevToolsContext } from './context'
6+
import { DevTools } from './plugins'
67

78
export interface StandaloneDevToolsOptions {
89
cwd?: string
@@ -31,8 +32,18 @@ export async function startStandaloneDevTools(options: StandaloneDevToolsOptions
3132
cwd,
3233
)
3334

35+
// Inject devtools plugin
36+
const config = loaded?.config || {}
37+
config.plugins ||= []
38+
config.plugins.push(DevTools())
39+
40+
dedupeVitePlugins(
41+
config.plugins as Plugin[],
42+
plugin => plugin.name?.startsWith('vite:devtools'),
43+
)
44+
3445
const resolved = await resolveConfig(
35-
loaded?.config || {},
46+
config,
3647
command,
3748
mode,
3849
)
@@ -44,3 +55,28 @@ export async function startStandaloneDevTools(options: StandaloneDevToolsOptions
4455
context,
4556
}
4657
}
58+
59+
export function dedupeVitePlugins(plugins: Plugin[], include: (plugin: Plugin) => boolean) {
60+
const toDelete: number[] = []
61+
const map = new Map<string, Plugin>()
62+
63+
for (let i = 0; i < plugins.length; i++) {
64+
const plugin = plugins[i]
65+
if (!plugin || !include(plugin)) {
66+
continue
67+
}
68+
if (map.has(plugin.name)) {
69+
toDelete.push(i)
70+
}
71+
else {
72+
map.set(plugin.name, plugin)
73+
}
74+
}
75+
76+
toDelete.sort((a, b) => b - a)
77+
for (const i of toDelete) {
78+
plugins.splice(i, 1)
79+
}
80+
81+
return plugins
82+
}

0 commit comments

Comments
 (0)