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
Copy file name to clipboardExpand all lines: documentation/docs/03-template-syntax/06-snippet.md
+14Lines changed: 14 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -246,6 +246,20 @@ We can tighten things up further by declaring a generic, so that `data` and `row
246
246
</script>
247
247
```
248
248
249
+
## Exporting snippets
250
+
251
+
Snippets declared at the top level of a `.svelte` file can be exported from a `<script module>` for use in other components, provided they don't reference any declarations in a non-module `<script>` (whether directly or indirectly, via other snippets) ([demo](/playground/untitled#H4sIAAAAAAAAE3WPwY7CMAxEf8UyB1hRgdhjl13Bga8gHFJipEqtGyUGFUX5dxJUtEB3b9bYM_MckHVLWOKut50TMuC5tpbEY4GnuiGP5T6gXG0-ykLSB8vW2oW_UCNZq7Snv_Rjx0Kc4kpc-6OrrfwoVlK3uQ4CaGMgwsl1LUwXy0f54J9-KV4vf20cNo7YkMu22aqAz4-oOLUI9YKluDPF4h_at-hX5PFyzA1tZ84N3fGpf8YfUU6GvDumLqDKmEqCjjCHUEX4hqDTWCU5PJ6Or38c4g1cPu9tnAEAAA==)):
252
+
253
+
```svelte
254
+
<script module>
255
+
export { add };
256
+
</script>
257
+
258
+
{#snippet add(a, b)}
259
+
{a} + {b} = {a + b}
260
+
{/snippet}
261
+
```
262
+
249
263
## Programmatic snippets
250
264
251
265
Snippets can be created programmatically with the [`createRawSnippet`](svelte#createRawSnippet) API. This is intended for advanced use cases.
Copy file name to clipboardExpand all lines: documentation/docs/98-reference/.generated/compile-errors.md
+30Lines changed: 30 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -400,6 +400,12 @@ Expected token %token%
400
400
Expected whitespace
401
401
```
402
402
403
+
### export_undefined
404
+
405
+
```
406
+
`%name%` is not defined
407
+
```
408
+
403
409
### global_reference_invalid
404
410
405
411
```
@@ -694,6 +700,30 @@ Cannot use `<slot>` syntax and `{@render ...}` tags in the same component. Migra
694
700
Cannot use explicit children snippet at the same time as implicit children content. Remove either the non-whitespace content or the children snippet block
695
701
```
696
702
703
+
### snippet_invalid_export
704
+
705
+
```
706
+
An exported snippet can only reference things declared in a `<script module>`, or other exportable snippets
707
+
```
708
+
709
+
It's possible to export a snippet from a `<script module>` block, but only if it doesn't reference anything defined inside a non-module-level `<script>`. For example you can't do this...
710
+
711
+
```svelte
712
+
<script module>
713
+
export { greeting };
714
+
</script>
715
+
716
+
<script>
717
+
let message = 'hello';
718
+
</script>
719
+
720
+
{#snippet greeting(name)}
721
+
<p>{message} {name}!</p>
722
+
{/snippet}
723
+
```
724
+
725
+
...because `greeting` references `message`, which is defined in the second `<script>`.
Copy file name to clipboardExpand all lines: packages/svelte/messages/compile-errors/script.md
+26Lines changed: 26 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,6 +38,10 @@
38
38
39
39
> `$effect()` can only be used as an expression statement
40
40
41
+
## export_undefined
42
+
43
+
> `%name%` is not defined
44
+
41
45
## global_reference_invalid
42
46
43
47
> `%name%` is an illegal variable name. To reference a global variable called `%name%`, use `globalThis.%name%`
@@ -134,6 +138,28 @@
134
138
135
139
> %name% cannot be used in runes mode
136
140
141
+
## snippet_invalid_export
142
+
143
+
> An exported snippet can only reference things declared in a `<script module>`, or other exportable snippets
144
+
145
+
It's possible to export a snippet from a `<script module>` block, but only if it doesn't reference anything defined inside a non-module-level `<script>`. For example you can't do this...
146
+
147
+
```svelte
148
+
<script module>
149
+
export { greeting };
150
+
</script>
151
+
152
+
<script>
153
+
let message = 'hello';
154
+
</script>
155
+
156
+
{#snippet greeting(name)}
157
+
<p>{message} {name}!</p>
158
+
{/snippet}
159
+
```
160
+
161
+
...because `greeting` references `message`, which is defined in the second `<script>`.
0 commit comments