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
+17Lines changed: 17 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -246,6 +246,23 @@ 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
+
263
+
> [!NOTE]
264
+
> This requires Svelte 5.5.0 or newer
265
+
249
266
## Programmatic snippets
250
267
251
268
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/07-misc/03-typescript.md
+17-7Lines changed: 17 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,26 +40,26 @@ If you want to use one of these features, you need to setup up a `script` prepro
40
40
41
41
To use non-type-only TypeScript features within Svelte components, you need to add a preprocessor that will turn TypeScript into JavaScript.
42
42
43
-
### Using SvelteKit or Vite
44
-
45
-
The easiest way to get started is scaffolding a new SvelteKit project by typing `npx sv create`, following the prompts and choosing the TypeScript option.
If you don't need or want all the features SvelteKit has to offer, you can scaffold a Svelte-flavoured Vite project instead by typing `npm create vite@latest` and selecting the `svelte-ts` option.
56
+
### Using SvelteKit or Vite
57
+
58
+
The easiest way to get started is scaffolding a new SvelteKit project by typing `npx sv create`, following the prompts and choosing the TypeScript option.
If you don't need or want all the features SvelteKit has to offer, you can scaffold a Svelte-flavoured Vite project instead by typing `npm create vite@latest` and selecting the `svelte-ts` option.
73
+
72
74
In both cases, a `svelte.config.js` with `vitePreprocess` will be added. Vite/SvelteKit will read from this config file.
73
75
74
76
### Other build tools
@@ -77,6 +79,14 @@ If you're using tools like Rollup or Webpack instead, install their respective S
77
79
78
80
> [!NOTE] If you're starting a new project, we recommend using SvelteKit or Vite instead
79
81
82
+
## tsconfig.json settings
83
+
84
+
When using TypeScript, make sure your `tsconfig.json` is setup correctly.
85
+
86
+
- Use a [`target`](https://www.typescriptlang.org/tsconfig/#target) of at least `ES2022`, or a `target` of at least `ES2015` alongside [`useDefineForClassFields`](https://www.typescriptlang.org/tsconfig/#useDefineForClassFields). This ensures that rune declarations on class fields are not messed with, which would break the Svelte compiler
87
+
- Set [`verbatimModuleSyntax`](https://www.typescriptlang.org/tsconfig/#verbatimModuleSyntax) to `true` so that imports are left as-is
88
+
- Set [`isolatedModules`](https://www.typescriptlang.org/tsconfig/#isolatedModules) to `true` so that each file is looked at in isolation. TypeScript has a few features which require cross-file analysis and compilation, which the Svelte compiler and tooling like Vite don't do.
89
+
80
90
## Typing `$props`
81
91
82
92
Type `$props` just like a regular object with certain properties.
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