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: README.md
+54-5Lines changed: 54 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,55 @@ On the other hand, if you are really developing a plugin... Sorry, no docs for n
16
16
17
17
- inject CSS instead of doing a full replace when only the component's CSS has changed, with compatible HMR APIs (`rollup-plugin-hot`, Nollup, and Snowpack for now)
18
18
19
+
## Options
20
+
21
+
Those are the HMR options that are implemented by `svelte-hmr` itself, and so should be supported by any plugin listed bellow (especially if they include a link pointing to this section). How to pass those options is specific to each plugins, so refer to their specific docs on this point.
22
+
23
+
#### noReload
24
+
25
+
Type: `bool`<br>
26
+
Default: `false`
27
+
28
+
By default, `svelte-hmr` will trigger a full browser reload when it detects an error that will prevent subsequent HMR updates to be applied correctly. Set this to `true` to prevent automatic reloads. Note that Svelte Native does _not_ execute in a browser, and so this option has no effect there.
29
+
30
+
#### noPreserveState
31
+
32
+
Type: `bool`<br>
33
+
Default: `false`
34
+
35
+
Prevent preserving the state of the component (i.e. value of props and `let` variables) across HMR updates.
36
+
37
+
Note that, independently of this option, the state of child components of a component that is impacted by a HMR update will never be preserved beyond what is re-injected by props, and the state of parent / sibling components will always be preserved (more accurately: parent and siblings are not affected by HMR updates). Read the HMR explanation bellow if you want to understand why.
38
+
39
+
#### noPreserveStateKey
40
+
41
+
Type: `string`<br>
42
+
Default: `'@!hmr'`
43
+
44
+
Escape hatch from preservation of local state. There are some situation where preservation of state gets in the way, typically when you want to change the initial / default value of a prop or local variable. If this string appears anywhere in the component's code, then state won't be preserved for this update.
45
+
46
+
You'd generally use it with a quick comment right where you are currently editing the code, and remove it just after saving the file:
47
+
48
+
```js
49
+
let answer =42// @!hmr
50
+
```
51
+
52
+
But you can also make it more permanent if you find that some of your components don't play with state preservation. Maybe use a noop string to clearly manifest your intention?
53
+
54
+
```svelte
55
+
<script>
56
+
'@!hmr'
57
+
...
58
+
</script>
59
+
```
60
+
61
+
#### optimistic
62
+
63
+
Type: `bool`<br>
64
+
Default: `true`
65
+
66
+
Set this to `false` to consider runtime errors during component init (i.e. when your `<script>` code is run) as fatal to HMR (hence worthy of a full reload if `noReload` option is not set). By default, `svelte-hmr` will try to render the next version of the component in the place of the one that has crashed.
67
+
19
68
## What's HMR, by the way?
20
69
21
70
> **NOTE** To avoid repetition, the following text only mentions HMR in the context of browsers, but it can also be used in other platforms. For example `svelte-hmr` is also used in Svelte Native.
@@ -73,14 +122,14 @@ Local state is preserved by Svelte HMR, that is any state that Svelte itself tra
73
122
74
123
This means that in code like this:
75
124
76
-
~~~svelte
125
+
```svelte
77
126
<script>
78
127
let x = 1
79
128
x++ // x is now 2
80
129
</script>
81
130
82
131
<p>{x}</p>
83
-
~~~
132
+
```
84
133
85
134
If you replace `let x = 1` by `let x = 10` and save, the previous value of `x` will be preserved. That is, `x` will be 2 and not 10. The restoration of previous state happens _after_ the init code of the component has run, so the value will not be 11 either, despite the `x++` that is still here.
86
135
@@ -122,7 +171,7 @@ Some initial work has also been made on supporting Sapper with Rollup, and basic
122
171
### Svelte Native
123
172
124
173
The official [Svelte Native template](svelte-native-template)
125
-
already includes HMR support.
174
+
already includes HMR support.
126
175
127
176
### Vite
128
177
@@ -133,9 +182,9 @@ The official [Svelte Native template](svelte-native-template)
133
182
134
183
Official [Snowpack plugin for Svelte](snowpack/plugin-svelte) has HMR support via `svelte-hmr`. Use [create-snowpack-app] with [app-template-svelte] to get started quickly:
0 commit comments