Skip to content

Commit e306c3d

Browse files
authored
fix: improve deduplication support for svelte during dev (#137)
* fix: improve deduplication support by adding svelte itself to optimizeDeps.include by default * docs: improve changeset description
1 parent 429708d commit e306c3d

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

.changeset/seven-cows-leave.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': major
3+
---
4+
5+
automatically include svelte in vite config optimizeDeps.include
6+
7+
Previously, svelte was automatically excluded. We include it now by default to improve deduplication.
8+
9+
As a result, svelte is pre-bundled by vite during dev, which it logs when starting the devserver
10+
11+
```shell
12+
Pre-bundling dependencies:
13+
svelte/animate
14+
svelte/easing
15+
svelte/internal
16+
svelte/motion
17+
svelte/store
18+
(...and 2 more)
19+
(this will be run only when your dependencies or config have changed)
20+
```
21+
22+
And it's also visible in the browsers network tab, where requests for svelte imports now start with `node_modules/.vite/` during dev.
23+
24+
Check out the [vite pre-bundling documentation](https://vitejs.dev/guide/dep-pre-bundling.html) for more information.
25+
26+
To get the old behavior back, add the following to your vite config
27+
28+
```js
29+
optimizeDeps: {
30+
exclude: ['svelte'];
31+
}
32+
```

packages/vite-plugin-svelte/src/utils/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const SVELTE_IMPORTS = [
77
'svelte/easing',
88
'svelte/internal',
99
'svelte/motion',
10+
'svelte/ssr',
1011
'svelte/store',
1112
'svelte/transition',
1213
'svelte'

packages/vite-plugin-svelte/src/utils/options.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,24 @@ export function buildExtraViteConfig(
179179
options: ResolvedOptions,
180180
config: UserConfig
181181
): Partial<UserConfig> {
182-
const allSvelteImports = [...SVELTE_IMPORTS, ...SVELTE_HMR_IMPORTS];
183-
184-
// exclude svelte imports from optimization unless explicitly included
185-
const excludeFromOptimize = allSvelteImports.filter(
186-
(x) => !config.optimizeDeps?.include?.includes(x)
187-
);
188-
182+
// include svelte imports for optimization unless explicitly excluded
183+
const include: string[] = [];
184+
const exclude: string[] = ['svelte-hmr'];
185+
const isSvelteExcluded = config.optimizeDeps?.exclude?.includes('svelte');
186+
if (!isSvelteExcluded) {
187+
log.debug(`adding bare svelte packages to optimizeDeps.include: ${SVELTE_IMPORTS.join(', ')} `);
188+
include.push(...SVELTE_IMPORTS);
189+
} else {
190+
log.debug('"svelte" is excluded in optimizeDeps.exclude, skipped adding it to include.');
191+
}
189192
const extraViteConfig: Partial<UserConfig> = {
190193
optimizeDeps: {
191-
exclude: excludeFromOptimize
194+
include,
195+
exclude
192196
},
193197
resolve: {
194198
mainFields: [...SVELTE_RESOLVE_MAIN_FIELDS],
195-
dedupe: allSvelteImports
199+
dedupe: [...SVELTE_IMPORTS, ...SVELTE_HMR_IMPORTS]
196200
}
197201
// this option is still awaiting a PR in vite to be supported
198202
// see https://github.com/sveltejs/vite-plugin-svelte/issues/60

0 commit comments

Comments
 (0)