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
* feat: enable prebundleSvelteLibraries by default
* docs: add information about switching from deep imports to package imports for prebundled
* docs: update prebundling faq
* docs: update changeset to remove outdated advice
* docs: Apply suggestions from code review
Co-authored-by: Ben McCann <[email protected]>
* docs: improve recommendations
* fix: make sure prebundleSvelteLibraries is not used for build or ssr
* docs: Apply suggestions from code review
Co-authored-by: Ben McCann <[email protected]>
* docs: Apply suggestions from code review
Co-authored-by: Bjorn Lu <[email protected]>
* docs: update follow-up wording after previous changes
* fix: only enable prebundleSvelteLibraries for dev, log warnings if it is enabled during build or ssr
* docs: update default value in docs
* fix: remove warning for ssr.optimizeDeps, we don't add the svelte esbuild plugin for that so it does whatever, but won't break because of prebundleSvelteLibraries
* Apply suggestions from code review
Co-authored-by: Bjorn Lu <[email protected]>
Co-authored-by: Ben McCann <[email protected]>
Co-authored-by: Bjorn Lu <[email protected]>
enable `prebundleSvelteLibraries` during dev by default to improve page loading for the dev server.
6
+
7
+
see the [FAQ](https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/faq.md#what-is-going-on-with-vite-and-pre-bundling-dependencies) for more information about `prebundleSvelteLibraries` and how to tune it.
Copy file name to clipboardExpand all lines: docs/config.md
+6-2Lines changed: 6 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -204,9 +204,13 @@ A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patt
204
204
### prebundleSvelteLibraries
205
205
206
206
-**Type:**`boolean`
207
-
-**Default:**`false`
207
+
-**Default:**`true` for dev, `false` for build
208
+
209
+
Enable [Vite's dependency prebundling](https://vitejs.dev/guide/dep-pre-bundling.html) for Svelte libraries.
210
+
211
+
This option improves page loading for the dev server in most applications when using Svelte component libraries.
208
212
209
-
Force Vite to pre-bundle Svelte libraries. Setting this `true` should improve initial page load performance, especially when using large Svelte libraries. See the [FAQ](./faq.md#what-is-going-on-with-vite-and-pre-bundling-dependencies) for details of the pre-bundling implementation.
213
+
See the [FAQ](./faq.md#what-is-going-on-with-vite-and-pre-bundling-dependencies) for details and how to fine-tune it for huge libraries.
Copy file name to clipboardExpand all lines: docs/faq.md
+78-1Lines changed: 78 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,4 +99,81 @@ For reference, check out [windicss](https://github.com/windicss/vite-plugin-wind
99
99
100
100
### What is going on with Vite and `Pre-bundling dependencies:`?
101
101
102
-
Pre-bundling dependencies is an [optimization in Vite](https://vitejs.dev/guide/dep-pre-bundling.html). It is required for CJS dependencies, as Vite's development server only works with ES modules on the client side. Importantly for Svelte libraries and ESM modules, prebundling combines component libraries into a single file to speed up the initial page load. Try setting the [`prebundleSvelteLibraries`](./config.md#prebundleSvelteLibraries) option to `true` to speed things up. This will likely be enabled by default in future version of the plugin.
102
+
Prebundling dependencies is an [optimization in Vite](https://vitejs.dev/guide/dep-pre-bundling.html).
103
+
104
+
> We only use prebundling during **development**, the following does not apply to or describe the built application
105
+
106
+
It is required for CJS dependencies, as Vite's development server only works with ES modules on the client side.
107
+
Importantly for Svelte libraries and ES modules, it also reduces the number of http requests when you load a page from the dev server and caches files so subsequent starts are even faster.
108
+
109
+
The way prebundling Svelte libraries affects your dev-server load times depends on the import style you use, index or deep:
110
+
111
+
#### Index imports
112
+
113
+
Offers better DX but can cause noticable delays on your machine, especially for libraries with many files.
114
+
115
+
```diff
116
+
import { SomeComponent } from 'some-library'
117
+
+ only one request per library
118
+
+ intellisense for the whole library after first import
119
+
- compiles the whole library even if you only use a few components
120
+
- slower build and dev-server ssr
121
+
```
122
+
123
+
#### Deep imports
124
+
125
+
Offers snappier dev and faster builds for libraries with many files at the expense of some DX
126
+
127
+
```diff
128
+
import SomeComponent from 'some-library/src/SomeComponent.svelte'
129
+
+ compiles only the components you import
130
+
+ faster build and dev-server ssr
131
+
- one request per import can slow down initial load if you use a lot of components
132
+
- intellisense only for imported components
133
+
```
134
+
135
+
#### Rewriting imports with plugins or preprocessors
136
+
137
+
**Do not use it in combination with prebundling!**
138
+
139
+
Prebundling works by reading your `.svelte` files from disk and scanning them for imports. It cannot detect
140
+
added/changed/removed imports and these then cause extra requests, delays and render the prebundled files from the initial scan moot.
141
+
If you prefer to use these tools, please exclude the libraries you use them with from prebundling.
142
+
143
+
#### Excluding libraries from prebundling
144
+
145
+
If you want to disable prebundling for a single library, use `optimizeDeps.exclude`
146
+
147
+
```js
148
+
// vite.config.js
149
+
exportdefaultdefineConfig({
150
+
optimizeDeps: {
151
+
exclude: ['some-library'] // do not pre-bundle some-library
152
+
}
153
+
});
154
+
```
155
+
156
+
Or disable it for all Svelte libraries
157
+
158
+
```js
159
+
// svelte.config.js
160
+
exportdefault {
161
+
vitePlugin: {
162
+
prebundleSvelteLibraries:false
163
+
}
164
+
};
165
+
```
166
+
167
+
#### Recommendations
168
+
169
+
There is no golden rule, but you can follow these recommendations:
170
+
171
+
1.**Never** combine plugins or preprocessors that rewrite imports with prebundling
172
+
2. Start with index imports and if your dev-server or build process feels slow, check compile stats to see if switching to deep imports can improve the experience.
173
+
3. Do not mix deep and index imports for the same library, use one style consistently.
174
+
4. Use different import styles for different libraries where it helps. E.g. deep imports for the few icons of that one huge icon library, but index import for the component library that is heavily used.
175
+
176
+
#### I get a warning `Incompatible options: prebundleSvelteLibraries ...`
177
+
178
+
This warning only occurs if you use non-default settings in your vite config that can cause problems in combination with prebundleSvelteLibraries.
179
+
You should not use prebundleSvelteLibraries during build or for ssr, disable one of the incompatible options to make that warning (and subsequent errors) go away.
0 commit comments