Skip to content

Commit 3c8bf06

Browse files
authored
refactor: devtools integration (#22312)
1 parent 7c2aa3b commit 3c8bf06

6 files changed

Lines changed: 330 additions & 281 deletions

File tree

packages/vite/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
"@types/escape-html": "^1.0.4",
9595
"@types/pnpapi": "^0.0.5",
9696
"@vitest/utils": "4.1.5",
97-
"@vitejs/devtools": "^0.1.15",
97+
"@vitejs/devtools": "^0.1.18",
9898
"artichokie": "^0.4.3",
9999
"baseline-browser-mapping": "^2.10.23",
100100
"cac": "^7.0.0",
@@ -140,7 +140,7 @@
140140
},
141141
"peerDependencies": {
142142
"@types/node": "^20.19.0 || >=22.12.0",
143-
"@vitejs/devtools": "^0.1.0",
143+
"@vitejs/devtools": "^0.1.18",
144144
"esbuild": "^0.27.0 || ^0.28.0",
145145
"jiti": ">=1.21.0",
146146
"less": "^4.0.0",

packages/vite/src/node/build.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,14 +1847,15 @@ export async function createBuilder(
18471847
return output
18481848
},
18491849
async runDevTools() {
1850-
const devtoolsConfig = config.devtools
1851-
if (devtoolsConfig.enabled) {
1850+
if (config.devtools.enabled) {
18521851
try {
1853-
const { start } = await import(`@vitejs/devtools/cli-commands`)
1854-
await start(devtoolsConfig.config)
1852+
const { runDevTools } = await import('@vitejs/devtools/integration')
1853+
await runDevTools(builder)
18551854
} catch (e) {
18561855
config.logger.error(
1857-
colors.red(`Failed to run Vite DevTools: ${e.message || e.stack}`),
1856+
colors.red(
1857+
`Failed to run Vite DevTools: ${e?.message || e?.stack}`,
1858+
),
18581859
{ error: e },
18591860
)
18601861
}

packages/vite/src/node/config.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,11 +2075,6 @@ export async function resolveConfig(
20752075
resolved.build.ssrEmitAssets || resolved.build.emitAssets
20762076
}
20772077

2078-
// Enable `rolldownOptions.devtools` if devtools is enabled
2079-
if (resolved.devtools.enabled) {
2080-
resolved.build.rolldownOptions.devtools ??= {}
2081-
}
2082-
20832078
applyDepOptimizationOptionCompat(resolved)
20842079
await setOptimizeDepsPluginNames(resolved)
20852080

packages/vite/src/node/plugins/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import aliasPlugin, { type ResolverFunction } from '@rollup/plugin-alias'
2+
import colors from 'picocolors'
23
import type { ObjectHook } from 'rolldown'
34
import {
45
viteAliasPlugin as nativeAliasPlugin,
@@ -51,6 +52,10 @@ export async function resolvePlugins(
5152
const buildPlugins = isBundled
5253
? await (await import('../build')).resolveBuildPlugins(config)
5354
: { pre: [], post: [] }
55+
const devtoolsIntegrationPlugin =
56+
config.devtools.enabled && !isWorker
57+
? await loadDevToolsIntegrationPlugin(config)
58+
: null
5459
const { modulePreload } = config.build
5560

5661
return [
@@ -121,6 +126,7 @@ export async function resolvePlugins(
121126
...postPlugins,
122127

123128
...buildPlugins.post,
129+
devtoolsIntegrationPlugin,
124130

125131
// internal server-only plugins are always applied after everything else
126132
...(isBundled
@@ -133,6 +139,23 @@ export async function resolvePlugins(
133139
].filter(Boolean) as Plugin[]
134140
}
135141

142+
async function loadDevToolsIntegrationPlugin(
143+
config: ResolvedConfig,
144+
): Promise<Plugin | null> {
145+
try {
146+
const { DevToolsIntegration } = await import('@vitejs/devtools/integration')
147+
return DevToolsIntegration({ config })
148+
} catch (error: any) {
149+
config.logger.error(
150+
colors.red(
151+
`Failed to load Vite DevTools integration: ${error?.message || error?.stack}`,
152+
),
153+
{ error },
154+
)
155+
return null
156+
}
157+
}
158+
136159
export function createPluginHookUtils(
137160
plugins: readonly Plugin[],
138161
): PluginHookUtils {

playground/devtools/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
},
1515
"devDependencies": {
1616
"vite": "workspace:*",
17-
"@vitejs/devtools": "^0.1.15"
17+
"@vitejs/devtools": "^0.1.18"
1818
}
1919
}

0 commit comments

Comments
 (0)