Skip to content
Merged
11 changes: 11 additions & 0 deletions docs/config/shared-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,17 @@ Whether your application is a Single Page Application (SPA), a [Multi Page Appli

Learn more in Vite's [SSR guide](/guide/ssr#vite-cli). Related: [`server.middlewareMode`](./server-options#server-middlewaremode).

## devtools

- **Experimental:** [Give Feedback](https://github.com/vitejs/devtools/discussions)
- **Type:** `boolean` | `DevToolsConfig`
- **Default:** `false`

Enable devtools integration for visualizing the internal state and build analysis.
Ensure that `@vitejs/devtools` is installed as a dependency. This feature is currently supported only in build mode.

See [Vite DevTools](https://github.com/vitejs/devtools) for more details.

## future

- **Type:** `Record<string, 'warn' | undefined>`
Expand Down
1 change: 1 addition & 0 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"@rollup/pluginutils": "^5.3.0",
"@types/escape-html": "^1.0.4",
"@types/pnpapi": "^0.0.5",
"@vitejs/devtools": "^0.0.0-alpha.24",
Comment thread
webfansplz marked this conversation as resolved.
"artichokie": "^0.4.2",
"baseline-browser-mapping": "^2.9.14",
"cac": "^6.7.14",
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/rolldown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const nodeConfig = defineConfig({
'supports-color',
'utf-8-validate', // ws
'bufferutil', // ws
'@vitejs/devtools/cli-commands',
...Object.keys(pkg.dependencies),
...Object.keys(pkg.peerDependencies),
],
Expand Down Expand Up @@ -143,6 +144,7 @@ const moduleRunnerConfig = defineConfig({
'fsevents',
'lightningcss',
/^rolldown\//,
'@vitejs/devtools/cli-commands',
...Object.keys(pkg.dependencies),
],
plugins: [bundleSizeLimit(54), enableSourceMapsInWatchModePlugin()],
Expand Down
17 changes: 16 additions & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import type {
ResolvedConfig,
ResolvedEnvironmentOptions,
} from './config'
import { resolveConfig } from './config'
import { resolveConfig, resolveDevToolsConfig } from './config'
import type { PartialEnvironment } from './baseEnvironment'
import { buildReporterPlugin } from './plugins/reporter'
import { buildEsbuildPlugin } from './plugins/esbuild'
Expand Down Expand Up @@ -1679,6 +1679,7 @@ export interface ViteBuilder {
build(
environment: BuildEnvironment,
): Promise<RolldownOutput | RolldownOutput[] | RolldownWatcher>
runDevTools(): Promise<void>
}

export interface BuilderOptions {
Expand Down Expand Up @@ -1790,6 +1791,20 @@ export async function createBuilder(
environment.isBuilt = true
return output
},
async runDevTools() {
const devtoolsConfig = await resolveDevToolsConfig(config)
if (devtoolsConfig.enabled) {
try {
const { start } = await import(`@vitejs/devtools/cli-commands`)
await start(devtoolsConfig.config)
} catch (e) {
config.logger.error(
colors.red(`Failed to run Vite DevTools: ${e.message || e.stack}`),
Comment thread
webfansplz marked this conversation as resolved.
{ error: e },
)
}
}
},
}

async function setupEnvironment(name: string, config: ResolvedConfig) {
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ cli
}
const builder = await createBuilder(inlineConfig, null)
await builder.buildApp()
await builder.runDevTools()
} catch (e) {
createLogger(options.logLevel).error(
colors.red(`error during build:\n${e.stack}`),
Expand Down
42 changes: 42 additions & 0 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
type RolldownOptions,
rolldown,
} from 'rolldown'
import type { StartOptions } from '@vitejs/devtools/cli-commands'
import type { Alias, AliasOptions } from '#dep-types/alias'
import type { AnymatchFn } from '../types/anymatch'
import { withTrailingSlash } from '../shared/utils'
Expand Down Expand Up @@ -90,6 +91,7 @@ import {
nodeLikeBuiltins,
normalizeAlias,
normalizePath,
resolveHostname,
setupRollupOptionCompat,
} from './utils'
import {
Expand Down Expand Up @@ -507,6 +509,13 @@ export interface UserConfig extends DefaultEnvironmentOptions {
* @default 'spa'
*/
appType?: AppType
/**
* Enable devtools integration. Ensure that `@vitejs/devtools` is installed as a dependency.
* This feature is currently supported only in build mode.
* @experimental
* @default false
*/
devtools?: boolean | DevToolsConfig
}

export interface HTMLOptions {
Expand Down Expand Up @@ -612,6 +621,10 @@ export interface ResolvedWorkerOptions {
rolldownOptions: RolldownOptions
}

export interface DevToolsConfig extends Partial<StartOptions> {
enabled: boolean
}

export interface InlineConfig extends UserConfig {
configFile?: string | false
/** @experimental */
Expand Down Expand Up @@ -726,6 +739,28 @@ export interface ResolvedConfig extends Readonly<
} & PluginHookUtils
> {}

export async function resolveDevToolsConfig(
config: InlineConfig | ResolvedConfig,
): Promise<{
config: Omit<DevToolsConfig, 'enabled'> & { host: string }
enabled: boolean
}> {
const resolvedHostname = await resolveHostname(config.server?.host)
const fallbackHostname = resolvedHostname.host ?? 'localhost'

return {
enabled:
config.devtools === true ||
!!(config.devtools && config.devtools.enabled),
config: {
...(isObject(config.devtools) ? config.devtools : {}),
host: isObject(config.devtools)
? (config.devtools?.host ?? fallbackHostname)
: fallbackHostname,
Comment thread
webfansplz marked this conversation as resolved.
},
Comment thread
webfansplz marked this conversation as resolved.
}
}

// inferred ones are omitted
const configDefaults = Object.freeze({
define: {},
Expand Down Expand Up @@ -2005,6 +2040,13 @@ export async function resolveConfig(
resolved.build.ssrEmitAssets || resolved.build.emitAssets
}

// Enable `rolldownOptions.devtools` if devtools is enabled
if ((await resolveDevToolsConfig(config)).enabled) {
Comment thread
webfansplz marked this conversation as resolved.
Outdated
if (!resolved.build.rolldownOptions.devtools) {
resolved.build.rolldownOptions.devtools = {}
}
Comment thread
sapphi-red marked this conversation as resolved.
Outdated
}

applyDepOptimizationOptionCompat(resolved)
await setOptimizeDepsPluginNames(resolved)

Expand Down
12 changes: 12 additions & 0 deletions playground/devtools/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>DevTools</title>
</head>

<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
19 changes: 19 additions & 0 deletions playground/devtools/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@vitejs/test-devtools",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.5.25"
},
"devDependencies": {
"vite": "workspace:*",
"@vitejs/devtools": "^0.0.0-alpha.24"
}
}
19 changes: 19 additions & 0 deletions playground/devtools/src/counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ref } from 'vue'

export function useCounter() {
const count = ref(0)

function increment() {
count.value++
}

function decrement() {
count.value--
}

return {
count,
increment,
decrement,
}
}
17 changes: 17 additions & 0 deletions playground/devtools/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { watchEffect } from 'vue'
import { useCounter } from './counter'

const { count, increment, decrement } = useCounter()

watchEffect(() => {
document.querySelector('#app')!.textContent = `${count.value}`
})

const timer = setInterval(() => {
if (count.value < 6) {
increment()
} else {
decrement()
clearInterval(timer)
}
}, 1000)
9 changes: 9 additions & 0 deletions playground/devtools/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'vite'
Comment thread
webfansplz marked this conversation as resolved.

export default defineConfig({
base: '/',
devtools: {
enabled: false,
Comment thread
webfansplz marked this conversation as resolved.
port: 5173,
},
})
Loading