Skip to content

Commit 9a44775

Browse files
authored
docs: add faq to explain hmr [skip ci] (#89)
* docs: add faq to explain hmr [skip ci] * docs: explain lang=ts in faq [skip ci]
1 parent 76ed865 commit 9a44775

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

packages/vite-plugin-svelte/README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,54 @@ const vitePluginCoolCss = {
8686

8787
Check out [windicss](https://github.com/windicss/vite-plugin-windicss/blob/517eca0cebc879d931c6578a08accadfb112157c/packages/vite-plugin-windicss/src/index.ts#L167)
8888

89+
## FAQ
90+
91+
### Why is component state reset on hmr update?
92+
93+
Preservation of local component state after js updates is disabled to avoid unpredictable and errorprone behavior. You can read more about it [here](https://github.com/rixo/svelte-hmr#preservation-of-local-state).
94+
95+
Please note that if you only edit the `style` node, a separate css update can be applied where component state is 100% preserved.
96+
97+
### What is the recommended node order for svelte sfc files?
98+
99+
The `<style>` node should be last to ensure optimal hmr results.
100+
This is also the default order with [prettier-plugin-svelte](https://github.com/sveltejs/prettier-plugin-svelte)
101+
102+
Good:
103+
104+
```sveltehtml
105+
<script></script>
106+
<div></div>
107+
<style></style>
108+
```
109+
110+
Bad:
111+
112+
```sveltehtml
113+
<script></script>
114+
<style></style>
115+
<!-- this template element is below the style node and may cause extra js hmr updates -->
116+
<div></div>
117+
```
118+
119+
### Why isn't vite detecting my imports correctly in `.svelte` files with typescript?
120+
121+
You have to use the `lang="ts"` attribute for vite to parse it. Never `lang="typescript"` or `type="text/typescript"`
122+
123+
Good:
124+
125+
```sveltehtml
126+
<script lang="ts"></script>
127+
```
128+
129+
Bad:
130+
131+
```sveltehtml
132+
<!-- these are not detected by vite -->
133+
<script lang="typescript"></script>
134+
<script type="text/typescript"></script>
135+
```
136+
89137
## License
90138

91-
MIT
139+
[MIT](./LICENSE)

0 commit comments

Comments
 (0)