Trace your Svelte components in the DOM—effortlessly.
A Svelte 5 preprocessor that injects data-svelte-trace metadata into every DOM element for reliable tooling, debugging, and automation.
Visual editors, dev tools, and scripts need to map DOM elements back to source code. Svelte Trace solves this by embedding base64-encoded source metadata directly into your components—no config, no fuss.
data-svelte-trace attribute with line, column, and file path.npm install svelte-trace --save-dev
svelte.config.jsimport adapter from "@sveltejs/adapter-auto";
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";
import { svelteTrace } from "svelte-trace";
const config = {
preprocess: [vitePreprocess(), svelteTrace()],
kit: { adapter: adapter() },
};
export default config;
npm run dev
Svelte Trace adds a base64-encoded data-svelte-trace attribute to every element into the browser DOM so that you could build dev tooling around that:
Before:
<div class="container">
<h1>Hello World</h1>
</div>
After:
<div class="container" data-svelte-trace="...">
<h1 data-svelte-trace="...">Hello World</h1>
</div>
Decode the trace to get source info:
Browser:
const el = document.querySelector("[data-svelte-trace]");
const decoded = atob(el.getAttribute("data-svelte-trace"));
console.log(decoded);
// Example: tag[1:1]-class[5:52]-f[/path/to/component.svelte]
Node.js:
const encoded = "dGFnWzI6M10tY2xhc3NbLTE6LTFdLWZbL3BhdGgvc3ZlbHRlXQ==";
const decoded = Buffer.from(encoded, "base64").toString("utf8");
console.log(decoded);
Format:
tag[line:column] — element positionclass[line:column] — class attribute position (-1:-1 if missing)f[filepath] — source file pathEnable "Open In VSCode" with Ctrl+Click:
svelteTrace({
openInCode: true,
});
Issues and PRs welcome! GitHub
MIT — see LICENSE.
Built with ❤️ for the Svelte community