Skip to content

Commit 31e1446

Browse files
committed
Updated README
1 parent dc72322 commit 31e1446

File tree

5 files changed

+111
-101
lines changed

5 files changed

+111
-101
lines changed

README.md

Lines changed: 81 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,141 +7,151 @@
77
<h1 align="center">Svelte Trace</h1>
88

99
<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>
1311
<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.
1513
</p>
1614

1715
<p align="center">
1816
<a href="https://www.npmjs.com/package/svelte-trace"><img src="https://img.shields.io/npm/v/svelte-trace.svg" alt="NPM Version"></a>
1917
<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>
2018
</p>
2119

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-
2420
---
2521

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?
3723

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.
3925

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
4127

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.
4332

4433
## 📦 Installation
4534

4635
```bash
4736
npm install svelte-trace --save-dev
4837
```
4938

50-
## 🔧 Getting Started in 3 Steps
51-
52-
### Step 1: Update your svelte.config.js
39+
## 🚀 Quick Start
5340

54-
Add svelteTrace to your preprocessor array. It's that simple.
41+
### 1. Update `svelte.config.js`
5542

5643
```js
57-
// Basic svelte.config.js
5844
import adapter from "@sveltejs/adapter-auto";
5945
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";
46+
// Import svelteTrace
6047
import { svelteTrace } from "svelte-trace";
6148

62-
/** @type {import('@sveltejs/kit').Config} */
49+
// Then add it to your preprocess list
6350
const config = {
64-
// Add svelteTrace() to your preprocessors
6551
preprocess: [vitePreprocess(), svelteTrace()],
66-
kit: {
67-
adapter: adapter(),
68-
},
52+
kit: { adapter: adapter() },
6953
};
7054

7155
export default config;
7256
```
7357

74-
### Step 2: Run your dev server
58+
### 2. Run your dev server
7559

7660
```bash
7761
npm run dev
7862
```
7963

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
8365

84-
## ⚙️ Configuration
66+
Every element gets a base64-encoded `data-svelte-trace` attribute:
8567

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:**
10569

10670
```html
107-
<div>
71+
<div class="container">
10872
<h1>Hello World</h1>
10973
</div>
11074
```
11175

112-
Output HTML:
76+
**Output:**
11377

11478
```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==">
11784
Hello World
11885
</h1>
11986
</div>
12087
```
12188

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
12390

124-
## 🎯 Advanced Use Case: Building Visual Editors
91+
Decode the `data-svelte-trace` value to access source information:
12592

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:**
12794

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+
```
131101

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:**
133103

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+
```
135110

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:**
137112

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:
139135

140-
MIT License - see [LICENSE](LICENSE) file for details.
136+
## ⚙️ Configuration
137+
138+
```js
139+
svelteTrace({
140+
openInCode: true,
141+
});
142+
```
141143

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
143153

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.
145155

146156
## 📞 Support
147157

package-lock.json

Lines changed: 24 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-trace",
3-
"version": "0.1.6-beta.2",
3+
"version": "0.1.7",
44
"description": "svelte-trace is a Svelte 5 preprocessor that enables 'click-to-open VS Code' functionality by adding metadata to HTML elements.",
55
"type": "module",
66
"main": "./dist/index.js",

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ interface MarkupParams {
1616
/**
1717
* A preprocessor for Svelte files for tracking element's metadata. This function returns an object containing the preprocessor name, and a markup function.
1818
*
19-
* Set `openInCode` parameter to `false` if you don't want "Open in VSCode" functionality.
19+
* Set `openInCode` parameter to `true` if you want "Open in VSCode".
2020
*/
21-
export function svelteTrace(openInCode: boolean = true) {
21+
export function svelteTrace(openInCode: boolean = false) {
2222
return {
2323
name: "svelte-trace",
2424
markup: ({ content, filename }: MarkupParams): PreprocessorResult | undefined => {
@@ -28,9 +28,9 @@ export function svelteTrace(openInCode: boolean = true) {
2828
let ast: AST.Root = parse(content, { modern: true });
2929
let processedContent = content;
3030

31-
// If file is "src/routes/+layout.svelte"
31+
// If the file is "src/routes/+layout.svelte"
3232
if (isRootLayoutFile(filename)) {
33-
// If enabled, we have to inject the script to allow "openInCode" to work
33+
// If openInCode is enabled, we inject a script to allow this feature
3434
if (openInCode) {
3535
processedContent = injectIntoHead(processedContent, ast);
3636
}

src/processors/core_processor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function processNodes(filepath: string, content: string, ast: AST.Root):
1919
const magicString = new MagicString(content);
2020

2121
/**
22-
* Adds unique data attribute to an HTML element
22+
* Adds a unique `data` attribute to an HTML element
2323
*/
2424
const addDataAttribute = (node: AST.RegularElement): void => {
2525
if (node.type !== "RegularElement") return;

0 commit comments

Comments
 (0)