Skip to content

Commit 38d0c49

Browse files
committed
start documenting options
1 parent 3793076 commit 38d0c49

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

README.md

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,55 @@ On the other hand, if you are really developing a plugin... Sorry, no docs for n
1616

1717
- 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)
1818

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+
1968
## What's HMR, by the way?
2069

2170
> **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
73122

74123
This means that in code like this:
75124

76-
~~~svelte
125+
```svelte
77126
<script>
78127
let x = 1
79128
x++ // x is now 2
80129
</script>
81130
82131
<p>{x}</p>
83-
~~~
132+
```
84133

85134
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.
86135

@@ -122,7 +171,7 @@ Some initial work has also been made on supporting Sapper with Rollup, and basic
122171
### Svelte Native
123172

124173
The official [Svelte Native template](svelte-native-template)
125-
already includes HMR support.
174+
already includes HMR support.
126175

127176
### Vite
128177

@@ -133,9 +182,9 @@ The official [Svelte Native template](svelte-native-template)
133182

134183
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:
135184

136-
~~~bash
185+
```bash
137186
npx create-snowpack-app new-dir --template @snowpack/app-template-svelte [--use-yarn | --use-pnpm | --no-install]
138-
~~~
187+
```
139188

140189
## License
141190

lib/make-hot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const defaultHotOptions = {
1212
// don't reload on fatal error
1313
noReload: false,
1414
// try to recover after runtime errors during component init
15-
optimistic: false,
15+
optimistic: true,
1616
// auto accept modules of components that have named exports (i.e. exports
1717
// from context="module")
1818
acceptNamedExports: true,

0 commit comments

Comments
 (0)