* New FAQ, new renderer
* Push blog stuff
* Fix blog posts
* Add tutorial to be rendered
* Update documentation/content/blog/2023-03-09-zero-config-type-safety.md
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
* Update documentation/content/blog/2023-03-09-zero-config-type-safety.md
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
* Revamp a lot of renderer, make it (soft) compatible with sveltekit
* Remove markdown types
* Clean up faq +page
* Document stuff
* Make the options more explicity
* chore(site-2): Restructure docs pt 2 (#8604)
* Push
* Update readme
* Push
* inor accessibility fix
* minr stuff
* Add prepare
* Run prettier
* Remove test script
* pnpm update
* Update sites/svelte.dev/src/routes/examples/[slug]/+page.svelte
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
* Update sites/svelte.dev/package.json
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
---------
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
---------
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
@ -48,7 +48,8 @@ To see the full set of changes, consult the [svelte-upgrade README](https://gith
Another thing that people often found confusing about Svelte is the way computed properties work. To recap, if you had a component with this...
Another thing that people often found confusing about Svelte is the way computed properties work. To recap, if you had a component with this...
```js
```ts
// @noErrors
export default {
export default {
computed: {
computed: {
d: (a, b, c) => (a = b + c)
d: (a, b, c) => (a = b + c)
@ -60,7 +61,8 @@ export default {
In v2, we use [destructuring](http://www.jstips.co/en/javascript/use-destructuring-in-function-parameters/) instead:
In v2, we use [destructuring](http://www.jstips.co/en/javascript/use-destructuring-in-function-parameters/) instead:
```js
```ts
// @noErrors
export default {
export default {
computed: {
computed: {
d: ({ a, b, c }) => (a = b + c)
d: ({ a, b, c }) => (a = b + c)
@ -83,6 +85,7 @@ If you need to support IE11 and friends, you will need to use a transpiler like
In addition to `oncreate` and `ondestroy`, Svelte v2 adds two more [lifecycle hooks](https://v2.svelte.dev/guide#lifecycle-hooks) for responding to state changes:
In addition to `oncreate` and `ondestroy`, Svelte v2 adds two more [lifecycle hooks](https://v2.svelte.dev/guide#lifecycle-hooks) for responding to state changes:
```js
```js
// @noErrors
export default {
export default {
onstate({ changed, current, previous }) {
onstate({ changed, current, previous }) {
// this fires before oncreate, and
// this fires before oncreate, and
@ -100,6 +103,7 @@ export default {
You can also listen to those events programmatically:
You can also listen to those events programmatically:
With the new lifecycle hooks, we no longer need the `component.observe(...)` method:
With the new lifecycle hooks, we no longer need the `component.observe(...)` method:
```js
```js
// @noErrors
// before
// before
export default {
export default {
oncreate() {
oncreate() {
@ -134,6 +139,7 @@ This shrinks the amount of code Svelte needs to generate, and gives you more fle
However, if you prefer to use `component.observe(...)`, then you can install it from [svelte-extras](https://github.com/sveltejs/svelte-extras):
However, if you prefer to use `component.observe(...)`, then you can install it from [svelte-extras](https://github.com/sveltejs/svelte-extras):
```js
```js
// @noErrors
import { observe } from 'svelte-extras';
import { observe } from 'svelte-extras';
export default {
export default {
@ -148,6 +154,7 @@ export default {
This method no longer takes an optional `key` argument — instead, it always returns the entire state object:
This method no longer takes an optional `key` argument — instead, it always returns the entire state object:
```js
```js
// @noErrors
// before
// before
const foo = this.get('foo');
const foo = this.get('foo');
const bar = this.get('bar');
const bar = this.get('bar');
@ -181,6 +188,7 @@ That causes unexpected behaviour, and has been changed: if you need to pass a li
In most cases you'll never need to deal with the compiler directly, so this shouldn't require any action on your part. It's worth noting anyway: the compiler API has changed. Instead of an object with a mish-mash of properties, the compiler now returns `js`, `css`, `ast` and `stats`:
In most cases you'll never need to deal with the compiler directly, so this shouldn't require any action on your part. It's worth noting anyway: the compiler API has changed. Instead of an object with a mish-mash of properties, the compiler now returns `js`, `css`, `ast` and `stats`:
@ -43,6 +45,7 @@ Misunderstood claims about virtual DOM performance date back to the launch of Re
But hang on a minute! The virtual DOM operations are _in addition to_ the eventual operations on the real DOM. The only way it could be faster is if we were comparing it to a less efficient framework (there were plenty to go around back in 2013!), or arguing against a straw man — that the alternative is to do something no-one actually does:
But hang on a minute! The virtual DOM operations are _in addition to_ the eventual operations on the real DOM. The only way it could be faster is if we were comparing it to a less efficient framework (there were plenty to go around back in 2013!), or arguing against a straw man — that the alternative is to do something no-one actually does:
```js
```js
// @noErrors
onEveryStateChange(() => {
onEveryStateChange(() => {
document.body.innerHTML = renderMyApp();
document.body.innerHTML = renderMyApp();
});
});
@ -73,6 +76,7 @@ Most obviously, [diffing isn't free](https://twitter.com/pcwalton/status/1015694
Of these three steps, only the third has value in this case, since — as is the case in the vast majority of updates — the basic structure of the app is unchanged. It would be much more efficient if we could skip straight to step 3:
Of these three steps, only the third has value in this case, since — as is the case in the vast majority of updates — the basic structure of the app is unchanged. It would be much more efficient if we could skip straight to step 3:
```js
```js
// @noErrors
if (changed.name) {
if (changed.name) {
text.data = name;
text.data = name;
}
}
@ -85,6 +89,7 @@ if (changed.name) {
The diffing algorithms used by React and other virtual DOM frameworks are fast. Arguably, the greater overhead is in the components themselves. You wouldn't write code like this...
The diffing algorithms used by React and other virtual DOM frameworks are fast. Arguably, the greater overhead is in the components themselves. You wouldn't write code like this...
```js
```js
// @noErrors
function StrawManComponent(props) {
function StrawManComponent(props) {
const value = expensivelyCalculateValue(props.foo);
const value = expensivelyCalculateValue(props.foo);
@ -95,6 +100,7 @@ function StrawManComponent(props) {
...because you'd be carelessly recalculating `value` on every update, regardless of whether `props.foo` had changed. But it's extremely common to do unnecessary computation and allocation in ways that seem much more benign:
...because you'd be carelessly recalculating `value` on every update, regardless of whether `props.foo` had changed. But it's extremely common to do unnecessary computation and allocation in ways that seem much more benign:
@ -36,6 +36,7 @@ To make that possible we first needed to rethink the concept at the heart of mod
In old Svelte, you would tell the computer that some state had changed by calling the `this.set` method:
In old Svelte, you would tell the computer that some state had changed by calling the `this.set` method:
```js
```js
// @errors: 7017
const { count } = this.get();
const { count } = this.get();
this.set({
this.set({
count: count + 1
count: count + 1
@ -45,6 +46,7 @@ this.set({
That would cause the component to _react_. Speaking of which, `this.set` is almost identical to the `this.setState` method used in classical (pre-hooks) React:
That would cause the component to _react_. Speaking of which, `this.set` is almost identical to the `this.setState` method used in classical (pre-hooks) React:
```js
```js
// @errors: 7017
const { count } = this.state;
const { count } = this.state;
this.setState({
this.setState({
count: count + 1
count: count + 1
@ -61,13 +63,20 @@ That all changed with the advent of [hooks](https://reactjs.org/docs/hooks-intro
So we took a step back and asked ourselves what kind of API would work for us... and realised that the best API is no API at all. We can just _use the language_. Updating some `count` value — and all the things that depend on it — should be as simple as this:
So we took a step back and asked ourselves what kind of API would work for us... and realised that the best API is no API at all. We can just _use the language_. Updating some `count` value — and all the things that depend on it — should be as simple as this:
```js
```ts
let count: number = 10;
// ---cut---
count += 1;
count += 1;
```
```
Since we're a compiler, we can do that by instrumenting assignments behind the scenes:
Since we're a compiler, we can do that by instrumenting assignments behind the scenes:
import type { ServerLoadEvent } from '@sveltejs/kit';
import type { ServerLoadEvent } from '@sveltejs/kit';
@ -71,6 +77,7 @@ After we have loaded our data, we want to display it in our `+page.svelte`. The
When running the dev server or the build, types are auto-generated. Thanks to the file-system based routing, SvelteKit is able to infer things like the correct parameters or parent data by traversing the route tree. The result is outputted into one `$types.d.ts` file for each route, which looks roughly like this:
When running the dev server or the build, types are auto-generated. Thanks to the file-system based routing, SvelteKit is able to infer things like the correct parameters or parent data by traversing the route tree. The result is outputted into one `$types.d.ts` file for each route, which looks roughly like this: