|
7 | 7 | <h1 align="center">Svelte Trace</h1> |
8 | 8 |
|
9 | 9 | <p align="center"> |
10 | | - <strong> |
11 | | - Instantly jump from your browser to your Svelte code in VS Code. |
12 | | - </strong> |
| 10 | + <strong>Inject traceable metadata into your Svelte components for tooling and debugging.</strong> |
13 | 11 | <br /> |
14 | | - Supercharge your development workflow by <code>Ctrl + Clicking</code> any element to open its source. |
| 12 | + A Svelte 5 preprocessor that adds a <code>data-svelte-trace</code> attribute to every DOM element, enabling tools to reliably identify elements and their source locations. |
15 | 13 | </p> |
16 | 14 |
|
17 | 15 | <p align="center"> |
18 | 16 | <a href="https://www.npmjs.com/package/svelte-trace"><img src="https://img.shields.io/npm/v/svelte-trace.svg" alt="NPM Version"></a> |
19 | 17 | <a href="https://github.com/Git002/svelte-trace/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/svelte-trace.svg" alt="License"></a> |
20 | 18 | </p> |
21 | 19 |
|
22 | | -> ⚠️ BETA Stage: This package is currently in BETA stage, and will evolve. Things might break, and your feedback is most welcomed. Happy Coding! |
23 | | -
|
24 | 20 | --- |
25 | 21 |
|
26 | | -`svelte-trace` is a Svelte 5 preprocessor that closes the gap between your rendered application and your source code. Stop hunting for components in your file tree—just `Ctrl + Click` in your browser, and **instantly land in the right file and line in VS Code!** |
27 | | - |
28 | | -Here's how the end result is gonna look like: |
29 | | - |
30 | | -<br/> |
31 | | -<p align="center"> |
32 | | - <img src="https://beeimg.com/images/v89261247551.gif" alt="Svelte Trace Demo" width="800" /> |
33 | | - </a> |
34 | | -</p> |
35 | | - |
36 | | -## 🚀 Key Features |
| 22 | +## 🎯 Why Svelte Trace? |
37 | 23 |
|
38 | | -- **🖱️ Click to Open in VS Code:** `Ctrl + Click` (or `Cmd + Click`) any element during development to open its source file directly in your editor, pinpointing the exact line and column. |
| 24 | +Building visual editors, dev tools, or automated scripts for Svelte? You need a reliable way to map DOM elements back to source code. `svelte-trace` solves this by injecting base64 metadata directly into your components at build time. |
39 | 25 |
|
40 | | -- **✨ Zero Configuration:** The client-side script that enables the click-to-open functionality is injected automatically. Just add the preprocessor to your config, and you're done. |
| 26 | +## ✨ Core Features |
41 | 27 |
|
42 | | -- **🛠️ Extensible for Tooling:** Under the hood, it works by adding source code metadata to your HTML elements. This powerful foundation can be used to build advanced tools like visual editors like webflow/figma, but for svelte and edit code in realtime visually. |
| 28 | +- **Automatic Metadata Injection:** Every HTML element gets a `data-svelte-trace` attribute with source location (line, column, file path). |
| 29 | +- **Zero Configuration:** Just add it into your preprocessors list and you're good to go. |
| 30 | +- **Optional VS Code Integration:** Enable `Ctrl + Click` in the browser to jump to source (development only, disabled by default). |
| 31 | +- **Extensible Foundation:** Power visual editors, custom dev tools, or automated transformations. |
43 | 32 |
|
44 | 33 | ## 📦 Installation |
45 | 34 |
|
46 | 35 | ```bash |
47 | 36 | npm install svelte-trace --save-dev |
48 | 37 | ``` |
49 | 38 |
|
50 | | -## 🔧 Getting Started in 3 Steps |
51 | | - |
52 | | -### Step 1: Update your svelte.config.js |
| 39 | +## 🚀 Quick Start |
53 | 40 |
|
54 | | -Add svelteTrace to your preprocessor array. It's that simple. |
| 41 | +### 1. Update `svelte.config.js` |
55 | 42 |
|
56 | 43 | ```js |
57 | | -// Basic svelte.config.js |
58 | 44 | import adapter from "@sveltejs/adapter-auto"; |
59 | 45 | import { vitePreprocess } from "@sveltejs/vite-plugin-svelte"; |
| 46 | +// Import svelteTrace |
60 | 47 | import { svelteTrace } from "svelte-trace"; |
61 | 48 |
|
62 | | -/** @type {import('@sveltejs/kit').Config} */ |
| 49 | +// Then add it to your preprocess list |
63 | 50 | const config = { |
64 | | - // Add svelteTrace() to your preprocessors |
65 | 51 | preprocess: [vitePreprocess(), svelteTrace()], |
66 | | - kit: { |
67 | | - adapter: adapter(), |
68 | | - }, |
| 52 | + kit: { adapter: adapter() }, |
69 | 53 | }; |
70 | 54 |
|
71 | 55 | export default config; |
72 | 56 | ``` |
73 | 57 |
|
74 | | -### Step 2: Run your dev server |
| 58 | +### 2. Run your dev server |
75 | 59 |
|
76 | 60 | ```bash |
77 | 61 | npm run dev |
78 | 62 | ``` |
79 | 63 |
|
80 | | -### Step 3: Ctrl + Click Anything! |
81 | | - |
82 | | -Open your application in the browser, hold down the `Ctrl` (or `Cmd` on Mac) key, and `click` on any element. It will instantly open in your VS Code editor. |
| 64 | +## 🔍 How It Works |
83 | 65 |
|
84 | | -## ⚙️ Configuration |
| 66 | +Every element gets a base64-encoded `data-svelte-trace` attribute: |
85 | 67 |
|
86 | | -The preprocessor is designed to work **out-of-the-box**. However, you can customize its behavior. |
87 | | - |
88 | | -```js |
89 | | -// svelte.config.js |
90 | | -import { svelteTrace } from "svelte-trace"; |
91 | | - |
92 | | -const config = { |
93 | | - // The client-side "Open in VS Code" script is injected by default. |
94 | | - // Set to false if you only want the metadata for building custom tools. |
95 | | - preprocess: [svelteTrace({ openInCode: false })], |
96 | | - // ... |
97 | | -}; |
98 | | -``` |
99 | | - |
100 | | -## 🤔 How It Works |
101 | | - |
102 | | -`svelte-trace` parses your Svelte components during the build process and injects a `data-svelte-trace` attribute into every HTML element. This attribute contains the element's exact location in your source code. |
103 | | - |
104 | | -Input Svelte Code: |
| 68 | +**Input:** |
105 | 69 |
|
106 | 70 | ```html |
107 | | -<div> |
| 71 | +<div class="container"> |
108 | 72 | <h1>Hello World</h1> |
109 | 73 | </div> |
110 | 74 | ``` |
111 | 75 |
|
112 | | -Output HTML: |
| 76 | +**Output:** |
113 | 77 |
|
114 | 78 | ```html |
115 | | -<div data-svelte-trace="dGFnWzQ6Ml0tbG9jWy0xOi0xXS1mW3NyYy9yb3V0ZXMvK3BhZ2Uuc3ZlbHRlXQ=="> |
116 | | - <h1 data-svelte-trace="dGFnWzU6NF0tbG9jWy0xOi0xXS1mW3NyYy9yb3V0ZXMvK3BhZ2Uuc3ZlbHRlXQ=="> |
| 79 | +<div |
| 80 | + class="container" |
| 81 | + data-svelte-trace="dGFnWzE6MV0tY2xhc3NbNTo1Ml0tZlsvaG9tZS9hYmhheS8uLi5zdmVsdGVd" |
| 82 | +> |
| 83 | + <h1 data-svelte-trace="dGFnWzI6M10tY2xhc3NbLTE6LTFdLWZbL2hvbWUvYWJoYXkvLi4uc3ZlbHRlXQ=="> |
117 | 84 | Hello World |
118 | 85 | </h1> |
119 | 86 | </div> |
120 | 87 | ``` |
121 | 88 |
|
122 | | -The automatically injected client-side script listens for `Ctrl` + `Click` events, reads this attribute, and constructs a `vscode://` link to open the file instantly. |
| 89 | +## 📖 Decoding the Metadata |
123 | 90 |
|
124 | | -## 🎯 Advanced Use Case: Building Visual Editors |
| 91 | +Decode the `data-svelte-trace` value to access source information: |
125 | 92 |
|
126 | | -While the primary feature is the "Open in VS Code" workflow, the metadata added by svelte-trace is powerful. It creates a bridge that allows you to build sophisticated tools, such as: |
| 93 | +**Browser:** |
127 | 94 |
|
128 | | -- **Visual Website Builders:** Create Webflow-like editors for Svelte. |
129 | | -- **Client-Facing Edit Tools:** Let clients make content or style changes safely. |
130 | | -- **Enhanced DevTools:** Build custom debugging and development experiences. |
| 95 | +```js |
| 96 | +const el = document.querySelector("[data-svelte-trace]"); |
| 97 | +const decoded = atob(el.getAttribute("data-svelte-trace")); |
| 98 | +console.log(decoded); |
| 99 | +// Output: tag[1:1]-class[5:52]-f[/path/to/component.svelte] |
| 100 | +``` |
131 | 101 |
|
132 | | -To build these tools, you can disable the default click handler with openInCode: false and implement your own logic to parse the data-svelte-trace attributes. |
| 102 | +**Node.js:** |
133 | 103 |
|
134 | | -## 🤝 Contributing |
| 104 | +```js |
| 105 | +const encoded = "dGFnWzI6M10tY2xhc3NbLTE6LTFdLWZbL3BhdGgvc3ZlbHRlXQ=="; |
| 106 | +const decoded = Buffer.from(encoded, "base64").toString("utf8"); |
| 107 | +console.log(decoded); |
| 108 | +// Output: tag[2:3]-class[-1:-1]-f[/path/to/component.svelte] |
| 109 | +``` |
135 | 110 |
|
136 | | -We welcome contributions! This is beta software and needs testing across different Svelte applications. Please report issues, suggest features, or submit pull requests. |
| 111 | +**Token Format:** |
137 | 112 |
|
138 | | -## 📄 License |
| 113 | +- `tag[line:column]` — element position in source |
| 114 | +- `class[line:column]` — class attribute position (`-1:-1` if absent) |
| 115 | +- `f[filepath]` — source file path |
| 116 | + |
| 117 | +**Parse example:** |
| 118 | + |
| 119 | +```js |
| 120 | +const decoded = "tag[4:2]-class[-1:-1]-f[src/routes/+page.svelte]"; |
| 121 | +const m = decoded.match(/tag\[(.*?)\]-class\[(.*?)\]-f\[(.*)\]/); |
| 122 | +const [_, tagPos, classInfo, filePath] = m; |
| 123 | +``` |
| 124 | + |
| 125 | +## 💡 Use Cases |
| 126 | + |
| 127 | +- **DevTools:** Display source file + line when hovering elements. |
| 128 | +- **Visual Editors:** Map DOM selections back to component source (Webflow/Figma style). |
| 129 | +- **Automated Scripts:** Locate and transform source snippets programmatically. |
| 130 | +- **Custom Debugging:** Build tools that understand your Svelte component structure. |
| 131 | + |
| 132 | +### OPTIONAL: Enable "Open In VSCode" feature |
| 133 | + |
| 134 | +Set `openInCode: true` to use `Ctrl + Click` to open elements in VS Code during development. Below is the little preview: |
139 | 135 |
|
140 | | -MIT License - see [LICENSE](LICENSE) file for details. |
| 136 | +## ⚙️ Configuration |
| 137 | + |
| 138 | +```js |
| 139 | +svelteTrace({ |
| 140 | + openInCode: true, |
| 141 | +}); |
| 142 | +``` |
141 | 143 |
|
142 | | -## 🙏 Acknowledgments |
| 144 | +<p align="center"> |
| 145 | + <img src="https://beeimg.com/images/v89261247551.gif" alt="VS Code open-in-editor demo" width="800" /> |
| 146 | +</p> |
| 147 | + |
| 148 | +## 🤝 Contributing |
| 149 | + |
| 150 | +Please report issues and submit pull requests on [GitHub](https://github.com/Git002/svelte-trace). |
| 151 | + |
| 152 | +## 📄 License |
143 | 153 |
|
144 | | -Inspired by the need for better visual editing tools in the Svelte ecosystem. Special thanks to the Svelte team for creating an amazing framework. |
| 154 | +MIT License - see [LICENSE](LICENSE) file. |
145 | 155 |
|
146 | 156 | ## 📞 Support |
147 | 157 |
|
|
0 commit comments