Optimizing for server environments

#1181

Development PR

Issue

Open
E
eltigerchino
Jul 24, 2025, 5:21 AM

Describe the problem

When combined with @cloudflare/vite-plugin, most deps are prebundled before they are loaded by the workerd runtime during development. This means that the esbuild optimizer plugin should generate server code in these cases instead of always generating client code. https://github.com/sveltejs/vite-plugin-svelte/blob/3119a4b53f36835a5a8d28b19cfb7c47abb8bbbd/packages/vite-plugin-svelte/src/plugins/setup-optimizer.js#L199

This is important to enable SvelteKit to work with the Cloudflare Vite plugin in development, which currently receives client Svelte code when performing SSR.

Describe the proposed solution

The Svelte compiler generates client/server code depending on the Vite environment when optimizing dependencies.

EDIT: sapphi mentioned it's possible to set a different esbuild plugin during the configureEnvironment hook

Alternatives considered

If we waited for Rolldown to officially replace esbuild, we wouldn't need to bother with tweaking the esbuild plugin. But I think that might still be a long time from now.

We could also probably workaround this by ensuring SvelteKit doesn't get optimized during development.

Importance

I cannot use vite-plugin-svelte without it

Info

Assignees None
Labels enhancement
Milestone None