|
1 | 1 | <p align="center"> |
2 | | - <img src="https://beeimg.com/images/p32116343591.png" alt="Svelte Trace Logo" width="200" /> |
| 2 | + <a href="https://github.com/Git002/svelte-trace"> |
| 3 | + <img src="https://beeimg.com/images/p32116343591.png" alt="Svelte Trace Logo" width="250" /> |
| 4 | + </a> |
3 | 5 | </p> |
4 | 6 |
|
5 | | -<h1 align="center">svelte-trace.js</h1> |
| 7 | +<h1 align="center">Svelte Trace</h1> |
6 | 8 |
|
7 | | -> **⚠️ Beta Stage**: This package is currently in beta and may not be stable. Use with caution. |
| 9 | +<p align="center"> |
| 10 | + <strong> |
| 11 | + Instantly jump from your browser to your Svelte code in VS Code. |
| 12 | + </strong> |
| 13 | + <br /> |
| 14 | + Supercharge your development workflow by <code>Ctrl + Clicking</code> any element to open its source. |
| 15 | +</p> |
8 | 16 |
|
9 | | -`svelte-trace` is a **Svelte 5 preprocessor** (unofficial) that enables visual editing by adding metadata to HTML elements. It creates a bridge between visual editors and your actual Svelte code, making it possible to build tools like visual website builders that directly modify your source files. |
| 17 | +<p align="center"> |
| 18 | +<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 | +<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 | +</p> |
10 | 21 |
|
11 | | -## 🚀 What does svelte-trace do? |
| 22 | +> ⚠️ BETA Stage: This package is currently in BETA stage, and may evolve. Things might break, and your feedback is most welcomed. Happy Coding! |
12 | 23 |
|
13 | | -It automatically adds `data-svelte-trace` attributes to all HTML elements in your Svelte components. These attributes contain metadata about the element's location in your source code, including: |
| 24 | +--- |
14 | 25 |
|
15 | | -- File path |
16 | | -- Class attribute position (start/end offsets) |
17 | | -- Line numbers and positioning data |
| 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!** |
18 | 27 |
|
19 | | -This enables the creation of visual editors that can: |
| 28 | +## 🚀 Key Features |
20 | 29 |
|
21 | | -- Click on any element in the browser |
22 | | -- Instantly know where it exists in your code |
23 | | -- Make real-time changes to your Svelte files |
24 | | -- Support Tailwind classes, inline styles, and more (in future) |
| 30 | +- **🖱️ 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. |
25 | 31 |
|
26 | | -## 🎯 Use Cases |
| 32 | +- **✨ 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. |
27 | 33 |
|
28 | | -- **Visual Website Builders**: Build Webflow-like editors for Svelte |
29 | | -- **Design Systems**: Allow designers to modify components visually |
30 | | -- **Rapid Prototyping**: Speed up development with visual editing tools |
31 | | -- **Client Editing**: Let clients make content/style changes without coding |
32 | | -- **Developer Tools**: Create better debugging and development experiences |
| 34 | +- **🛠️ 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. |
33 | 35 |
|
34 | 36 | ## 📦 Installation |
35 | 37 |
|
36 | 38 | ```bash |
37 | 39 | npm install svelte-trace --save-dev |
38 | 40 | ``` |
39 | 41 |
|
40 | | -## 🔧 Usage |
| 42 | +## 🔧 Getting Started in 3 Steps |
41 | 43 |
|
42 | | -### Basic Setup |
| 44 | +### Step 1: Update your svelte.config.js |
43 | 45 |
|
44 | | -Add SvelteTrace to your `svelte.config.js`: |
| 46 | +Add svelteTrace to your preprocessor array. It's that simple. |
45 | 47 |
|
46 | | -```javascript |
| 48 | +```js |
| 49 | +// Basic svelte.config.js |
47 | 50 | import adapter from "@sveltejs/adapter-auto"; |
48 | 51 | import { vitePreprocess } from "@sveltejs/vite-plugin-svelte"; |
49 | 52 | import { svelteTrace } from "svelte-trace"; |
50 | 53 |
|
51 | 54 | /** @type {import('@sveltejs/kit').Config} */ |
52 | 55 | const config = { |
| 56 | + // Add svelteTrace() to your preprocessors |
53 | 57 | preprocess: [vitePreprocess(), svelteTrace()], |
54 | | - |
55 | 58 | kit: { |
56 | | - // adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list. |
57 | | - // If your environment is not supported, or you settled on a specific environment, switch out the adapter. |
58 | | - // See https://svelte.dev/docs/kit/adapters for more information about adapters. |
59 | 59 | adapter: adapter(), |
60 | 60 | }, |
61 | | - |
62 | | - extensions: [".svelte"], |
63 | 61 | }; |
64 | 62 |
|
65 | 63 | export default config; |
66 | 64 | ``` |
67 | 65 |
|
68 | | -## 📋 Example Output |
| 66 | +### Step 2: Run your dev server |
69 | 67 |
|
70 | | -**Input:** |
71 | | - |
72 | | -```html |
73 | | -<div class="bg-blue-500 text-white"> |
74 | | - <h1 class="text-2xl font-bold">Hello World</h1> |
75 | | - <p>This is a paragraph</p> |
76 | | -</div> |
| 68 | +```bash |
| 69 | +npm run dev |
77 | 70 | ``` |
78 | 71 |
|
79 | | -**Output:** |
| 72 | +### Step 3: Ctrl + Click Anything! |
80 | 73 |
|
81 | | -```html |
82 | | -<div class="bg-blue-500 text-white" data-svelte-trace="loc[45,67]-f[src/App.svelte]"> |
83 | | - <h1 class="text-2xl font-bold" data-svelte-trace="loc[120,142]-f[src/App.svelte]">Hello World</h1> |
84 | | - <p data-svelte-trace="loc[-1,-1]-f[src/App.svelte]">This is a paragraph</p> |
85 | | -</div> |
86 | | -``` |
87 | | - |
88 | | -## 📊 Metadata Format |
| 74 | +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. |
89 | 75 |
|
90 | | -The `data-svelte-trace` attribute contains: |
| 76 | +## ⚙️ Configuration |
91 | 77 |
|
92 | | -- `loc[start,end]`: Character offsets of the class attribute in the file (-1,-1 if no class) |
93 | | -- `f[filepath]`: Relative path to the source file |
| 78 | +The preprocessor is designed to work **out-of-the-box**. However, you can customize its behavior. |
94 | 79 |
|
95 | | -**Future versions will use base64 encoding for more compact metadata.** |
| 80 | +```js |
| 81 | +// svelte.config.js |
| 82 | +import { svelteTrace } from "svelte-trace"; |
96 | 83 |
|
97 | | -## 🛠️ Building Visual Editors |
| 84 | +const config = { |
| 85 | + // The client-side "Open in VS Code" script is injected by default. |
| 86 | + // Set to false if you only want the metadata for building custom tools. |
| 87 | + preprocess: [svelteTrace({ openInCode: false })], |
| 88 | + // ... |
| 89 | +}; |
| 90 | +``` |
98 | 91 |
|
99 | | -With SvelteTrace, you can build editors that: |
| 92 | +## 🤔 How It Works |
100 | 93 |
|
101 | | -1. **Parse metadata**: Extract file paths and positions from DOM elements |
102 | | -2. **Locate source code**: Find exact locations in your Svelte files |
103 | | -3. **Make changes**: Modify classes, styles, or content programmatically |
104 | | -4. **Update files**: Write changes back to your source code in real-time |
| 94 | +`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. |
105 | 95 |
|
106 | | -Example of reading the metadata: |
| 96 | +Input Svelte Code: |
107 | 97 |
|
108 | | -```javascript |
109 | | -// Get element metadata |
110 | | -const element = document.querySelector("[data-svelte-trace]"); |
111 | | -const metadata = element.getAttribute("data-svelte-trace"); |
| 98 | +```html |
| 99 | +<div> |
| 100 | + <h1>Hello World</h1> |
| 101 | +</div> |
| 102 | +``` |
112 | 103 |
|
113 | | -// Parse: "loc[45,67]-f[src/App.svelte]" |
114 | | -const [locPart, filePart] = metadata.split("-f"); |
115 | | -const [start, end] = locPart.match(/\d+/g).map(Number); |
116 | | -const filepath = filePart.slice(1, -1); // Remove brackets |
| 104 | +Output HTML: |
117 | 105 |
|
118 | | -console.log({ start, end, filepath }); |
119 | | -// { start: 45, end: 67, filepath: "src/App.svelte" } |
| 106 | +```html |
| 107 | +<div data-svelte-trace="dGFnWzQ6Ml0tbG9jWy0xOi0xXS1mW3NyYy9yb3V0ZXMvK3BhZ2Uuc3ZlbHRlXQ=="> |
| 108 | + <h1 data-svelte-trace="dGFnWzU6NF0tbG9jWy0xOi0xXS1mW3NyYy9yb3V0ZXMvK3BhZ2Uuc3ZlbHRlXQ=="> |
| 109 | + Hello World |
| 110 | + </h1> |
| 111 | +</div> |
120 | 112 | ``` |
121 | 113 |
|
122 | | -## 🎨 Framework Compatibility |
123 | | - |
124 | | -- ✅ **Svelte 5**: Full support |
125 | | -- ✅ **SvelteKit**: Full support |
126 | | -- ✅ **Vite**: Full support |
127 | | -- ✅ **Tailwind CSS**: Works perfectly |
128 | | -- ⚠️ **Svelte 4**: Not tested (may work) |
| 114 | +The automatically injected client-side script listens for `Ctrl` + `Click` events, reads this attribute, and constructs a `vscode://` link to open the file instantly. |
129 | 115 |
|
130 | | -## 🚧 Current Limitations (Beta) |
| 116 | +## 🎯 Advanced Use Case: Building Visual Editors |
131 | 117 |
|
132 | | -- Metadata is not yet base64 encoded |
133 | | -- Limited testing with complex Svelte features |
134 | | -- No configuration options yet |
135 | | -- Performance not optimized for large applications |
136 | | -- May conflict with SSR in some edge cases |
| 118 | +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: |
137 | 119 |
|
138 | | -## 🗺️ Roadmap |
| 120 | +- **Visual Website Builders:** Create Webflow-like editors for Svelte. |
| 121 | +- **Client-Facing Edit Tools:** Let clients make content or style changes safely. |
| 122 | +- **Enhanced DevTools:** Build custom debugging and development experiences. |
139 | 123 |
|
140 | | -- [ ] Base64 encoding for metadata |
141 | | -- [ ] Configuration options |
142 | | -- [ ] Performance optimizations |
143 | | -- [ ] Better error handling |
144 | | -- [ ] Support for more Svelte features |
145 | | -- [ ] Official visual editor |
146 | | -- [ ] React/Vue preprocessors |
| 124 | +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. |
147 | 125 |
|
148 | 126 | ## 🤝 Contributing |
149 | 127 |
|
150 | | -We welcome contributions! This is beta software and needs testing across different Svelte applications. |
| 128 | +We welcome contributions! This is beta software and needs testing across different Svelte applications. Please report issues, suggest features, or submit pull requests. |
151 | 129 |
|
152 | 130 | ## 📄 License |
153 | 131 |
|
|
0 commit comments