diff --git a/packages/core/src/node/__tests__/integration.test.ts b/packages/core/src/node/__tests__/integration.test.ts index ad6459fad..60aec230b 100644 --- a/packages/core/src/node/__tests__/integration.test.ts +++ b/packages/core/src/node/__tests__/integration.test.ts @@ -169,6 +169,16 @@ describe('devToolsIntegration', () => { ) }) + it('starts standalone DevTools once for multiple build environments', async () => { + const plugins = await DevToolsIntegration(createIntegrationOptions('build')) + const config = createResolvedConfig('build', { client: {} as never, ssr: {} as never }) + Object.assign(config, { plugins, devtools: normalizeDevToolsConfig({ apply: 'build' }, 'localhost') }) + + await runDevTools({ config }) + + expect(startDevTools).toHaveBeenCalledTimes(1) + }) + it('does not run standalone DevTools for a manual plugin config', async () => { const config = createResolvedConfig('build', { client: {} as never }) Object.assign(config, { diff --git a/packages/core/src/node/plugins/integration.ts b/packages/core/src/node/plugins/integration.ts index 8ed2c68e2..603222a82 100644 --- a/packages/core/src/node/plugins/integration.ts +++ b/packages/core/src/node/plugins/integration.ts @@ -54,20 +54,23 @@ export async function runDevTools( if (!isDevToolsEnabled(devtoolsConfig, config.command)) return - for (const _environment of getDevToolsEnvironments(config, devtoolsConfig)) { - try { - const { startDevTools } = await import('../start') - await startDevTools({ - ...devtoolsConfig.config, - root: devtoolsConfig.config.root ?? config.root, - }, devtoolsConfig) - } - catch (error: any) { - config.logger.error( - `Failed to run Vite DevTools: ${error?.message || error?.stack || error}`, - { error }, - ) - } + + // A single standalone server covers every environment, so start it once. + if (!getDevToolsEnvironments(config, devtoolsConfig).length) + return + + try { + const { startDevTools } = await import('../start') + await startDevTools({ + ...devtoolsConfig.config, + root: devtoolsConfig.config.root ?? config.root, + }, devtoolsConfig) + } + catch (error: any) { + config.logger.error( + `Failed to run Vite DevTools: ${error?.message || error?.stack || error}`, + { error }, + ) } }