Vite Plugin Vue Tracer
vite-plugin-vue-tracer is a development-only Vite plugin designed to trace the source code location of elements and Virtual DOM nodes within Vue Single-File Components (SFCs). It serves as a modern replacement for `vite-plugin-vue-inspector`, leveraging Vite's internal source map capabilities to resolve source locations without injecting additional data attributes into the DOM. This approach aims for cleaner DOM and improved performance. The current stable version is `1.3.0`. The project maintains an active release cadence, frequently updating to support the latest versions of Vite and Vue DevTools. It differentiates itself by offering both a built-in overlay UI and headless APIs, allowing developers to build custom inspection tools or integrate tracing functionality more deeply into their development workflow.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'events') at main.ts
cause Attempting to access client-side `events` or `state` before the `vite-plugin-vue-tracer/client/overlay` module has been fully loaded, or when `import.meta.hot` is false in a non-development environment.fixEnsure the client-side import is properly asynchronous (`.then()`) and strictly guarded by `if (import.meta.hot)` to ensure the module is only loaded and executed when HMR is active in development. -
Error: vite-plugin-vue-tracer requires vite@^6.0.0 || ^7.0.0 but got vite@5.x.x
cause Your project's Vite version does not meet the peer dependency requirements of the plugin.fixUpgrade your project's `vite` dependency to a compatible version (6.x or 7.x) using `npm install vite@latest` or `yarn add vite@latest` and verify it matches the required range. -
Error: [vite] Internal server error: Failed to resolve import "vite-plugin-vue-tracer" from "vite.config.ts"
cause The `vite-plugin-vue-tracer` package is not correctly installed or resolvable in your project.fixRun `npm install -D vite-plugin-vue-tracer` or `yarn add -D vite-plugin-vue-tracer` to ensure the package is installed. Verify the import path in `vite.config.ts`. -
TypeError: VueTracer is not a function
cause Attempting to use `VueTracer` as a default export or an incorrectly destructured named export.fixEnsure `VueTracer` is imported as a named export: `import { VueTracer } from 'vite-plugin-vue-tracer'`. Also verify it's correctly called as a function: `VueTracer()`.
Warnings
- breaking The plugin's integration with Vite DevTools has undergone multiple iterations and API changes. Older configurations for DevTools integration might cease to function or behave unexpectedly with newer plugin versions.
- gotcha Client-side components like `vite-plugin-vue-tracer/client/overlay` and `/client/record` are intended for development environments only and rely on Vite's HMR (`import.meta.hot`). Including them in production builds without conditional checks can lead to errors or unnecessary bundle size.
- breaking As of v1.1.2, Vite DevTools integration was disabled by default. If you were relying on this integration, you might find it no longer active.
- breaking The plugin has peer dependencies on `vite` (`^6.0.0 || ^7.0.0`) and `vue` (`^3.5.0`). Mismatched versions can lead to build failures or runtime errors.
Install
-
npm install vite-plugin-vue-tracer -
yarn add vite-plugin-vue-tracer -
pnpm add vite-plugin-vue-tracer
Imports
- VueTracer
const VueTracer = require('vite-plugin-vue-tracer')import { VueTracer } from 'vite-plugin-vue-tracer' - { events, state }
import { events, state } from 'vite-plugin-vue-tracer/client/overlay' - { findTraceFromElement }
import { findTraceFromElement } from 'vite-plugin-vue-tracer/client/record'
Quickstart
import { defineConfig } from 'vite'
import { VueTracer } from 'vite-plugin-vue-tracer'
export default defineConfig({
plugins: [
VueTracer(),
],
})
// In your main client-side entry file (e.g., main.ts)
// Only apply in development and if HMR is active
if (import.meta.hot) {
import('vite-plugin-vue-tracer/client/overlay').then(({ events, state }) => {
// Enable the visual overlay
state.isEnabled = true
events.on('hover', (info) => {
console.log('Hovered:', info.filepath, info.line, info.column)
})
events.on('click', (info) => {
console.log('Clicked:', info.filepath, info.line, info.column)
// Example: Open in VS Code (requires custom setup for 'openInEditor')
// openInEditor(`${info.filepath}:${info.line}:${info.column}`)
state.isEnabled = false // Disable overlay after click for single use
})
})
}