Skip to content

Commit 7d18604

Browse files
authored
add optimizeDeps.exclude note to readme (#53)
1 parent 591ba56 commit 7d18604

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

packages/vite-plugin-svelte/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ It supports all options from rollup-plugin-svelte and some additional options to
3838

3939
For more Information check [options.ts](src/utils/options.ts)
4040

41+
## Importing third-party Svelte libraries
42+
43+
When importing any third-party libraries that uses Svelte's lifecycle API, e.g. `onMount`, `setContext`, and others, they need to be excluded from Vite's dependency pre-bundling process:
44+
45+
```js
46+
// vite.config.js
47+
{
48+
plugins: [svelte()],
49+
optimizeDeps: {
50+
exclude: ['svelte-library']
51+
}
52+
}
53+
```
54+
55+
This is needed because Vite's dependency pre-bundling doesn't deduplicate the Svelte instance, resulting in multiple Svelte instance running at once, causing errors like `Function called outside component initialization`.
56+
57+
If you're unsure whether a library uses the lifecycle API, place it in `optimizeDeps.exclude` and you'll be fine. The team is working on removing this limitation soon.
58+
4159
## Integrations for other vite plugins
4260

4361
### Add an extra preprocessor

0 commit comments

Comments
 (0)