Skip to content

Commit 48ed571

Browse files
benmccannbluwy
andauthored
feat: bring prebundleSvelteLibraries out of experimental (#476)
Co-authored-by: bluwy <[email protected]>
1 parent 74fd16d commit 48ed571

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

.changeset/twelve-islands-jump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
Bring `prebundleSvelteLibraries` out of experimental, it is now a top-level option

docs/config.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patt
201201

202202
> This is currently required for hybrid packages like Routify, that export both Node and browser code.
203203
204+
### prebundleSvelteLibraries
205+
206+
- **Type:** `boolean`
207+
- **Default:** `false`
208+
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.
210+
204211
## Experimental options
205212

206213
These options are considered experimental and breaking changes to them can occur in any release! Specify them under the `experimental` option.
@@ -240,13 +247,6 @@ export default {
240247

241248
Use extra preprocessors that delegate style and TypeScript preprocessing to native Vite plugins. TypeScript will be transformed with esbuild. Styles will be transformed using [Vite's CSS plugin](https://vitejs.dev/guide/features.html#css), which handles `@imports`, `url()` references, PostCSS, CSS Modules, and `.scss`/`.sass`/`.less`/`.styl`/`.stylus` files. Do not use together with TypeScript or style preprocessors from `svelte-preprocess` as attempts to transform the content twice will fail!
242249

243-
### prebundleSvelteLibraries
244-
245-
- **Type:** `boolean`
246-
- **Default:** `false`
247-
248-
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.
249-
250250
### generateMissingPreprocessorSourcemaps
251251

252252
- **Type:** `boolean`

docs/faq.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,4 @@ For reference, check out [windicss](https://github.com/windicss/vite-plugin-wind
9999

100100
### What is going on with Vite and `Pre-bundling dependencies:`?
101101

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.
103-
104-
Historically, Svelte components had issues being pre-bundled, causing [deduplication issues](https://github.com/vitejs/vite/issues/3910) and [CJS interoperability issues](https://github.com/vitejs/vite/issues/3024). Since Vite 2.5.2, [a new API in Vite](https://github.com/vitejs/vite/pull/4634) allowed us to [automatically handle Svelte library pre-bundling](https://github.com/sveltejs/vite-plugin-svelte/pull/157) for you.
105-
106-
This feature had served us well, however a caveat remained that large Svelte component libraries often slows down the initial page load. If this affects you, try setting [experimental.prebundleSvelteLibraries](./config.md#prebundleSvelteLibraries) option to `true` to speed things up.
107-
108-
In case you still run into errors like `The requested module 'xxx' does not provide an export named 'yyy'`, please check our [open issues](https://github.com/sveltejs/vite-plugin-svelte/issues).
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.

packages/vite-plugin-svelte/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
8585
},
8686

8787
async buildStart() {
88-
if (!options.experimental?.prebundleSvelteLibraries) return;
88+
if (!options.prebundleSvelteLibraries) return;
8989
const isSvelteMetadataChanged = await saveSvelteMetadata(viteConfig.cacheDir, options);
9090
if (isSvelteMetadataChanged) {
9191
// Force Vite to optimize again. Although we mutate the config here, it works because

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const allowedPluginOptions = new Set([
3434
'hot',
3535
'ignorePluginPreprocessors',
3636
'disableDependencyReinclusion',
37+
'prebundleSvelteLibraries',
3738
'experimental'
3839
]);
3940

@@ -188,6 +189,7 @@ export function resolveOptions(
188189
const merged = mergeConfigs<ResolvedOptions>(defaultOptions, preResolveOptions, extraOptions);
189190

190191
removeIgnoredOptions(merged);
192+
handleDeprecatedOptions(merged);
191193
addSvelteKitOptions(merged);
192194
addExtraPreprocessors(merged, viteConfig);
193195
enforceOptionsForHmr(merged);
@@ -288,6 +290,15 @@ function addSvelteKitOptions(options: ResolvedOptions) {
288290
}
289291
}
290292

293+
function handleDeprecatedOptions(options: ResolvedOptions) {
294+
if ((options.experimental as any)?.prebundleSvelteLibraries) {
295+
options.prebundleSvelteLibraries = (options.experimental as any)?.prebundleSvelteLibraries;
296+
log.warn(
297+
'experimental.prebundleSvelteLibraries is no longer experimental and has moved to prebundleSvelteLibraries'
298+
);
299+
}
300+
}
301+
291302
// vite passes unresolved `root`option to config hook but we need the resolved value, so do it here
292303
// https://github.com/sveltejs/vite-plugin-svelte/issues/113
293304
// https://github.com/vitejs/vite/blob/43c957de8a99bb326afd732c962f42127b0a4d1e/packages/vite/src/node/config.ts#L293
@@ -318,7 +329,7 @@ export function buildExtraViteConfig(
318329
config.optimizeDeps
319330
);
320331

321-
if (options.experimental?.prebundleSvelteLibraries) {
332+
if (options.prebundleSvelteLibraries) {
322333
extraViteConfig.optimizeDeps = {
323334
...extraViteConfig.optimizeDeps,
324335
// Experimental Vite API to allow these extensions to be scanned and prebundled
@@ -378,7 +389,7 @@ function buildOptimizeDepsForSvelte(
378389
}
379390

380391
// If we prebundle svelte libraries, we can skip the whole prebundling dance below
381-
if (options.experimental?.prebundleSvelteLibraries) {
392+
if (options.prebundleSvelteLibraries) {
382393
return { include, exclude };
383394
}
384395

@@ -540,6 +551,13 @@ export interface PluginOptions {
540551
*/
541552
disableDependencyReinclusion?: boolean | string[];
542553

554+
/**
555+
* Force Vite to pre-bundle Svelte libraries
556+
*
557+
* @default false
558+
*/
559+
prebundleSvelteLibraries?: boolean;
560+
543561
/**
544562
* These options are considered experimental and breaking changes to them can occur in any release
545563
*/
@@ -594,13 +612,6 @@ export interface ExperimentalOptions {
594612
*/
595613
useVitePreprocess?: boolean;
596614

597-
/**
598-
* Force Vite to pre-bundle Svelte libraries
599-
*
600-
* @default false
601-
*/
602-
prebundleSvelteLibraries?: boolean;
603-
604615
/**
605616
* If a preprocessor does not provide a sourcemap, a best-effort fallback sourcemap will be provided.
606617
* This option requires `diff-match-patch` to be installed as a peer dependency.

0 commit comments

Comments
 (0)