You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update package version to 0.0.8, enhance README with a new section on global components, and improve inline Svelte plugin to support global component declarations and better script content injection.
You can define components inside a special `/* svelte:globals */` fence to make them automatically available to all other inline components in the same file. This is perfect for defining shared UI elements or mocks without manual imports.
64
+
65
+
```typescript
66
+
/* svelte:globals
67
+
// Any component defined here is "global" to this file.
// ✅ GlobalButton is now available here automatically.
72
+
const Page =html`
73
+
<section>
74
+
<p>Welcome to the page.</p>
75
+
<GlobalButton />
76
+
</section>
77
+
`;
78
+
```
79
+
80
+
---
81
+
62
82
## 🧩 Named Exports & Snippets
63
83
64
84
Just like regular Svelte files, you can use `<script context="module">` (or `<script module>`) to export values from an inline component. This is especially useful for **Svelte 5 snippets**.
|`tags`|`string[]`|`["html", "svelte"]`| Tag names to be treated as inline Svelte markup. |
211
+
|`fenceStart`|`string`|`/* svelte:imports`| The comment that starts a standard import fence. |
212
+
|`globalsFenceStart`|`string`|`/* svelte:globals`| The comment that starts a global components fence. |
213
+
|`fenceEnd`|`string`|`*/`| The comment that ends any fence. |
195
214
196
215
---
197
216
198
217
## 🧐 How it works (nutshell)
199
218
200
-
1.**Scan** each user source file (`.js`, `.ts`, `.jsx`, `.tsx`).
201
-
2.**Match** template literals whose tag name is in `options.tags`.
202
-
3.**Hash** the template content (including any fenced imports) to create a stable virtual module ID.
203
-
4.**Replace** the literal with a variable that imports the virtual module. Named exports (like snippets) are merged onto the default component export using `Object.assign`.
204
-
5.**Compile** the markup with Svelte when Vite requests the virtual module ID.
219
+
The plugin uses a multi-stage process to transform your code:
220
+
221
+
1.**Scan for Fences:** The plugin first looks for `/* svelte:globals */` and `/* svelte:imports */` fences to identify shared components and imports.
222
+
2.**Process Globals:** It compiles any components found inside the `globals` fence.
223
+
3.**Process Locals:** It then compiles the remaining "local" components, injecting the standard imports and the compiled global components into each one's script scope.
224
+
4.**Replace Literals:** Finally, it replaces all the original `html\`...\`\` literals in your code with variables that point to the newly created virtual components.
205
225
206
-
The result behaves just like a normal Svelte component import, so SSR, hydration, and Hot Module Replacement work as expected.
226
+
The result behaves just like a normal Svelte component import.
207
227
208
228
---
209
229
210
230
## ⚠️ Caveats
211
231
212
232
- The plugin only transforms your application's source code, ignoring anything inside `node_modules`.
213
233
- The template content must be valid Svelte component markup. Syntax errors will surface during compilation.
214
-
- Because each inline component gets a unique hash, HMR will re‑render the whole component tree containing it. Keep inline components small for best performance.
234
+
- Because each inline component gets a unique hash, HMR will re-render the whole component tree containing it. Keep inline components small for best performance.
0 commit comments