Skip to content

Commit 6fa5cc1

Browse files
committed
Merge branch 'main' into add-cli-docs
2 parents 74ac31e + c88afe8 commit 6fa5cc1

File tree

597 files changed

+14833
-11301
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

597 files changed

+14833
-11301
lines changed

apps/svelte.dev/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
/src/routes/_home/Supporters/contributors.js
77
/src/routes/_home/Supporters/donors.jpg
88
/src/routes/_home/Supporters/donors.js
9-
/static/svelte-app.json
109

1110
# git-repositories of synced docs go here
1211
/repos/

apps/svelte.dev/content/blog/2020-07-17-svelte-and-typescript.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,18 @@ You first need to set up [`svelte-preprocess`](https://github.com/sveltejs/svelt
6666

6767
In a Rollup project, that would look like this — note that we also need to install `@rollup/plugin-typescript` so that Rollup can handle `.ts` files:
6868

69-
```diff
70-
+ import autoPreprocess from 'svelte-preprocess';
71-
+ import typescript from '@rollup/plugin-typescript';
69+
```js
70+
+++import autoPreprocess from 'svelte-preprocess';
71+
import typescript from '@rollup/plugin-typescript';+++
7272

7373
export default {
74-
...,
75-
plugins: [
76-
svelte({
77-
+ preprocess: autoPreprocess()
78-
}),
79-
+ typescript({ sourceMap: !production })
80-
]
74+
// ...,
75+
plugins: [
76+
svelte({
77+
+++preprocess: autoPreprocess()+++
78+
}),
79+
+++typescript({ sourceMap: !production })+++
80+
]
8181
}
8282
```
8383

apps/svelte.dev/content/blog/2023-03-09-zero-config-type-safety.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const database = {
2828
}
2929
};
3030
// ---cut---
31-
// src/routes/blog/[slug]/+page.server.ts
31+
/// file: src/routes/blog/[slug]/+page.server.ts
3232
import type { ServerLoadEvent } from '@sveltejs/kit';
3333

3434
export async function load(event: ServerLoadEvent) {
@@ -42,16 +42,16 @@ This works, but we can do better. Notice that we accidentally wrote `event.param
4242

4343
This is where our automatic type generation comes in. Every route directory has a hidden `$types.d.ts` file with route-specific types:
4444

45-
```diff
46-
// src/routes/blog/[slug]/+page.server.ts
47-
-import type { ServerLoadEvent } from '@sveltejs/kit';
48-
+import type { PageServerLoadEvent } from './$types';
45+
```js
46+
/// file: src/routes/blog/[slug]/+page.server.ts
47+
---import type { ServerLoadEvent } from '@sveltejs/kit';---
48+
+++import type { PageServerLoadEvent } from './$types';+++
4949

5050
export async function load(event: PageServerLoadEvent) {
51-
return {
52-
- post: await database.getPost(event.params.post)
53-
+ post: await database.getPost(event.params.slug)
54-
};
51+
return {
52+
---post: await database.getPost(event.params.post)---
53+
+++post: await database.getPost(event.params.slug)+++
54+
};
5555
}
5656
```
5757

@@ -60,7 +60,7 @@ This reveals our typo, as it now errors on the `params.post` property access. Be
6060
After we have loaded our data, we want to display it in our `+page.svelte`. The same type generation mechanism ensures that the type of `data` is correct:
6161

6262
```svelte
63-
<!-- src/routes/blog/[slug]/+page.svelte -->
63+
/// file: src/routes/blog/[slug]/+page.svelte
6464
<script lang="ts">
6565
import type { PageData } from './$types';
6666
@@ -78,7 +78,7 @@ When running the dev server or the build, types are auto-generated. Thanks to th
7878

7979
```ts
8080
// @errors: 2344 2694 2307
81-
// $types.d.ts
81+
/// file: $types.d.ts
8282
import type * as Kit from '@sveltejs/kit';
8383

8484
// types inferred from the routing tree
@@ -100,7 +100,7 @@ export type PageData = Kit.ReturnType<
100100

101101
We don't actually write `$types.d.ts` into your `src` directory — that would be messy, and no-one likes messy code. Instead, we use a TypeScript feature called [`rootDirs`](https://www.typescriptlang.org/tsconfig#rootDirs), which lets us map ‘virtual’ directories to real ones. By setting `rootDirs` to the project root (the default) and additionally to `.svelte-kit/types` (the output folder of all the generated types) and then mirroring the route structure inside it we get the desired behavior:
102102

103-
```
103+
```tree
104104
// on disk:
105105
.svelte-kit/
106106
├ types/
@@ -115,8 +115,9 @@ src/
115115
│ │ ├ [slug]/
116116
│ │ │ ├ +page.server.ts
117117
│ │ │ └ +page.svelte
118+
```
118119

119-
120+
```tree
120121
// what TypeScript sees:
121122
src/
122123
├ routes/
@@ -131,24 +132,23 @@ src/
131132

132133
Thanks to the automatic type generation we get advanced type safety. Wouldn't it be great though if we could just omit writing the types at all? As of today you can do exactly that:
133134

134-
```diff
135-
// src/routes/blog/[slug]/+page.server.ts
136-
-import type { PageServerLoadEvent } from './$types';
135+
```js
136+
/// file: src/routes/blog/[slug]/+page.server.ts
137+
---import type { PageServerLoadEvent } from './$types';---
137138

138-
-export async function load(event: PageServerLoadEvent) {
139-
+export async function load(event) {
140-
return {
141-
post: await database.getPost(event.params.post)
142-
};
139+
export async function load(event---: PageServerLoadEvent---) {
140+
return {
141+
post: await database.getPost(event.params.post)
142+
};
143143
}
144144
```
145145

146-
```diff
147-
<!-- src/routes/blog/[slug]/+page.svelte -->
146+
```svelte
147+
/// file: src/routes/blog/[slug]/+page.svelte
148148
<script lang="ts">
149-
- import type { PageData } from './$types';
150-
- export let data: PageData;
151-
+ export let data;
149+
---import type { PageData } from './$types';---
150+
---export let data: PageData;---
151+
+++export let data;+++
152152
</script>
153153
```
154154

apps/svelte.dev/content/blog/2023-08-31-view-transitions.md

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ However, until now, you couldn’t easily use this API in a SvelteKit app, since
1414
You can trigger a view transition by calling `document.startViewTransition` and passing a callback that updates the DOM somehow. For our purposes today, SvelteKit will update the DOM as the user navigates. Once the callback finishes, the browser will transition to the new page state — by default, it does a crossfade between the old and the new states.
1515

1616
```js
17-
// @errors: 2339
17+
// @errors: 2339 2304
1818
const domUpdate = async () => {};
1919
// ---cut---
2020
document.startViewTransition(async () => {
@@ -76,32 +76,28 @@ With that, every navigation that occurs will trigger a view transition. You can
7676

7777
<video src="https://sveltejs.github.io/assets/video/vt-demo-1.mp4" controls muted playsinline></video>
7878

79-
<details>
80-
<summary>How the code works</summary>
81-
82-
This code may look a bit intimidating – if you're curious, I can break it down line-by-line, but for now it’s enough to know that adding it will allow you to interact with the view transitions API during navigation.
83-
84-
As mentioned above, the `onNavigate` callback will run immediately before the new page is rendered after a navigation. Inside the callback, we check if `document.startViewTransition` exists. If it doesn’t (i.e. the browser doesn’t support it), we exit early.
85-
86-
We then return a promise to delay completing the navigation until the view transition has started. We use a [promise constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise) so that we can control when the promise resolves.
87-
88-
```js
89-
// @errors: 1108
90-
return new Promise((resolve) => {
91-
document.startViewTransition(async () => {
92-
resolve();
93-
await navigation.complete;
94-
});
95-
});
96-
```
97-
98-
Inside the promise constructor, we start the view transition. Inside the view transition callback we resolve the promise we just returned, which indicates to SvelteKit that it should finish the navigation. It’s important that the navigation waits to finish until _after_ we start the view transition – the browser needs to snapshot the old state so it can transition to the new state.
99-
100-
Finally, inside the view transition callback we wait for SvelteKit to finish the navigation by awaiting `navigation.complete`. Once `navigation.complete` resolves, the new page has been loaded into the DOM and the browser can animate between the two states.
101-
102-
It’s a bit of a mouthful, but by not abstracting it we allow you to interact with the view transition directly and make any customizations you require.
103-
104-
</details>
79+
> [!DETAILS] How the code works
80+
> This code may look a bit intimidating – if you're curious, I can break it down line-by-line, but for now it’s enough to know that adding it will allow you to interact with the view transitions API during navigation.
81+
>
82+
> As mentioned above, the `onNavigate` callback will run immediately before the new page is rendered after a navigation. Inside the callback, we check if `document.startViewTransition` exists. If it doesn’t (i.e. the browser doesn’t support it), we exit early.
83+
>
84+
> We then return a promise to delay completing the navigation until the view transition has started. We use a [promise constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise) so that we can control when the promise resolves.
85+
>
86+
> ```js
87+
> // @errors: 1108
88+
> return new Promise((resolve) => {
89+
> document.startViewTransition(async () => {
90+
> resolve();
91+
> await navigation.complete;
92+
> });
93+
> });
94+
> ```
95+
>
96+
> Inside the promise constructor, we start the view transition. Inside the view transition callback we resolve the promise we just returned, which indicates to SvelteKit that it should finish the navigation. It’s important that the navigation waits to finish until > _after_ we start the view transition – the browser needs to snapshot the old state so it can transition to the new state.
97+
>
98+
> Finally, inside the view transition callback we wait for SvelteKit to finish the navigation by awaiting `navigation.complete`. Once `navigation.complete` resolves, the new page has been loaded into the DOM and the browser can animate between the two states.
99+
>
100+
> It’s a bit of a mouthful, but by not abstracting it we allow you to interact with the view transition directly and make any customizations you require.
105101
106102
## Customizing the transition with CSS
107103
@@ -163,38 +159,34 @@ Now, the header will not transition in and out on navigation, but the rest of th
163159

164160
<video src="https://sveltejs.github.io/assets/video/vt-demo-2.mp4" controls muted playsinline></video>
165161

166-
<details>
167-
<summary>Fixing the types</summary>
168-
169-
Since `startViewTransition` is not supported by all browsers, your IDE may not know that it exists. To make the errors go away and get the correct typings, add the following to your `app.d.ts`:
170-
171-
```ts
172-
declare global {
173-
// preserve any customizations you have here
174-
namespace App {
175-
// interface Error {}
176-
// interface Locals {}
177-
// interface PageData {}
178-
// interface Platform {}
179-
}
180-
181-
// add these lines
182-
interface ViewTransition {
183-
updateCallbackDone: Promise<void>;
184-
ready: Promise<void>;
185-
finished: Promise<void>;
186-
skipTransition: () => void;
187-
}
188-
189-
interface Document {
190-
startViewTransition(updateCallback: () => Promise<void>): ViewTransition;
191-
}
192-
}
193-
194-
export {};
195-
```
196-
197-
</details>
162+
> [!DETAILS] Fixing the types
163+
> Since `startViewTransition` is not supported by all browsers, your IDE may not know that it exists. To make the errors go away and get the correct typings, add the following to your `app.d.ts`:
164+
>
165+
> ```dts
166+
> declare global {
167+
> // preserve any customizations you have here
168+
> namespace App {
169+
> // interface Error {}
170+
> // interface Locals {}
171+
> // interface PageData {}
172+
> // interface Platform {}
173+
> }
174+
>
175+
> // add these lines
176+
> interface ViewTransition {
177+
> updateCallbackDone: Promise<void>;
178+
> ready: Promise<void>;
179+
> finished: Promise<void>;
180+
> skipTransition: () => void;
181+
> }
182+
>
183+
> interface Document {
184+
> startViewTransition(updateCallback: () => Promise<void>): ViewTransition;
185+
> }
186+
> }
187+
>
188+
> export {};
189+
> ```
198190
199191
## Transitioning individual elements
200192

apps/svelte.dev/content/blog/2023-09-20-runes.md

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ We don't yet have a release date for Svelte 5. What we're showing you here is a
4646

4747
## What are runes?
4848

49-
> **rune** /ro͞on/ _noun_
49+
> [!NOTE] **rune** /ro͞on/ _noun_
5050
>
5151
> A letter or mark used as a mystical or magic symbol.
5252
5353
Runes are symbols that influence the Svelte compiler. Whereas Svelte today uses `let`, `=`, the `export` keyword and the `$:` label to mean specific things, runes use _function syntax_ to achieve the same things and more.
5454

5555
For example, to declare a piece of reactive state, we can use the `$state` rune:
5656

57-
```diff
57+
```svelte
5858
<!--- file: App.svelte --->
5959
<script>
60-
- let count = 0;
61-
+ let count = $state(0);
60+
---let count = 0;---
61+
+++let count = $state(0);+++
6262
6363
function increment() {
6464
count += 1;
@@ -94,64 +94,63 @@ export function createCounter() {
9494

9595
Because this implements the _store contract_ — the returned value has a `subscribe` method — we can reference the store value by prefixing the store name with `$`:
9696

97-
```diff
97+
```svelte
9898
<!--- file: App.svelte --->
9999
<script>
100100
/// file: App.svelte
101-
+ import { createCounter } from './counter.js';
102-
+
103-
+ const counter = createCounter();
104-
- let count = 0;
105-
-
106-
- function increment() {
107-
- count += 1;
108-
- }
101+
+++ import { createCounter } from './counter.js';
102+
103+
const counter = createCounter();+++
104+
--- let count = 0;
105+
106+
function increment() {
107+
count += 1;
108+
}---
109109
</script>
110110
111-
-<button on:click={increment}>
112-
- clicks: {count}
113-
+<button on:click={counter.increment}>
114-
+ clicks: {$counter}
111+
---<button on:click={increment}>
112+
clicks: {count}---
113+
+++<button on:click={counter.increment}>
114+
clicks: {$counter}+++
115115
</button>
116116
```
117117

118118
This works, but it's pretty weird! We've found that the store API can get rather unwieldy when you start doing more complex things.
119119

120120
With runes, things get much simpler:
121121

122-
```diff
122+
```js
123123
/// file: counter.svelte.js
124-
-import { writable } from 'svelte/store';
124+
---import { writable } from 'svelte/store';---
125125

126126
export function createCounter() {
127-
- const { subscribe, update } = writable(0);
128-
+ let count = $state(0);
127+
---const { subscribe, update } = writable(0);---
128+
+++let count = $state(0);+++
129129

130130
return {
131-
- subscribe,
132-
- increment: () => update((n) => n + 1)
133-
+ get count() { return count },
134-
+ increment: () => count += 1
135-
};
131+
---subscribe,---
132+
---increment: () => update((n) => n + 1)---
133+
+++get count() { return count },+++
134+
+++increment: () => count += 1+++
135+
};
136136
}
137137
```
138138

139-
```diff
139+
```svelte
140140
<!--- file: App.svelte --->
141141
<script>
142-
- import { createCounter } from './counter.js';
143-
+ import { createCounter } from './counter.svelte.js';
142+
import { createCounter } from './counter+++.svelte+++.js';
144143
145144
const counter = createCounter();
146145
</script>
147146
148147
<button on:click={counter.increment}>
149-
- clicks: {$counter}
150-
+ clicks: {counter.count}
148+
---clicks: {$counter}---
149+
+++clicks: {counter.count}+++
151150
</button>
152151
```
153152

154-
> Outside `.svelte` components, runes can only be used in `.svelte.js` and `.svelte.ts` modules.
153+
> [!NOTE] Outside `.svelte` components, runes can only be used in `.svelte.js` and `.svelte.ts` modules.
155154
156155
Note that we're using a [get property](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get) in the returned object, so that `counter.count` always refers to the current value rather than the value at the time the function was called.
157156

0 commit comments

Comments
 (0)