Skip to content

Commit 3d22f89

Browse files
authored
fix: externalize transitive deps during dev ssr (#289)
* fix: externalize transitive cjs only deps during dev ssr * fix: kindly tell ts we know about ssr * fix: move generating external * fix: unconditionally externalize all transitive deps of svelte libraries during dev * fix: omit dependencies present in noExternal or already in external from generated list of externals * fix: formatting and null checks * chore: tidy up
1 parent 65219d1 commit 3d22f89

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

.changeset/cold-pumpkins-enjoy.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
improve handling of transitive cjs dependencies of svelte libraries during dev ssr

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
5555
// @ts-expect-error temporarily lend the options variable until fixed in configResolved
5656
options = await preResolveOptions(inlineOptions, config, configEnv);
5757
// extra vite config
58-
const extraViteConfig = buildExtraViteConfig(options, config, configEnv);
58+
const extraViteConfig = buildExtraViteConfig(options, config);
5959
log.debug('additional vite config', extraViteConfig);
6060
return extraViteConfig;
6161
},

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

+23-7
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ function resolveViteRoot(viteConfig: UserConfig): string | undefined {
184184

185185
export function buildExtraViteConfig(
186186
options: PreResolvedOptions,
187-
config: UserConfig,
188-
configEnv: ConfigEnv
187+
config: UserConfig
189188
): Partial<UserConfig> {
190189
// extra handling for svelte dependencies in the project
191190
const svelteDeps = findRootSvelteDependencies(options.root);
@@ -200,7 +199,7 @@ export function buildExtraViteConfig(
200199
// knownJsSrcExtensions: options.extensions
201200
};
202201

203-
if (configEnv.command === 'serve') {
202+
if (options.isServe) {
204203
extraViteConfig.optimizeDeps = buildOptimizeDepsForSvelte(
205204
svelteDeps,
206205
options,
@@ -209,7 +208,7 @@ export function buildExtraViteConfig(
209208
}
210209

211210
// @ts-ignore
212-
extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config);
211+
extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config, extraViteConfig);
213212

214213
return extraViteConfig;
215214
}
@@ -294,7 +293,7 @@ function buildSSROptionsForSvelte(
294293
// add svelte to ssr.noExternal unless it is present in ssr.external
295294
// so we can resolve it with svelte/ssr
296295
if (options.isBuild && config.build?.ssr) {
297-
// @ts-ignore
296+
// @ts-expect-error ssr still flagged in vite
298297
if (!config.ssr?.external?.includes('svelte')) {
299298
noExternal.push('svelte');
300299
}
@@ -309,13 +308,30 @@ function buildSSROptionsForSvelte(
309308
// add svelte dependencies to ssr.noExternal unless present in ssr.external or optimizeDeps.include
310309
noExternal.push(
311310
...Array.from(new Set(svelteDeps.map((s) => s.name))).filter((x) => {
312-
// @ts-ignore
311+
// @ts-expect-error ssr still flagged in vite
313312
return !config.ssr?.external?.includes(x) && !config.optimizeDeps?.include?.includes(x);
314313
})
315314
);
316-
return {
315+
const ssr = {
317316
noExternal
318317
};
318+
319+
if (options.isServe) {
320+
// during dev, we have to externalize transitive dependencies, see https://github.com/sveltejs/vite-plugin-svelte/issues/281
321+
// @ts-expect-error ssr still flagged in vite
322+
ssr.external = Array.from(
323+
new Set(svelteDeps.flatMap((dep) => Object.keys(dep.pkg.dependencies || {})))
324+
).filter(
325+
(dep) =>
326+
!ssr.noExternal.includes(dep) &&
327+
// @ts-expect-error ssr still flagged in vite
328+
!config.ssr?.noExternal?.includes(dep) &&
329+
// @ts-expect-error ssr still flagged in vite
330+
!config.ssr?.external?.includes(dep)
331+
);
332+
}
333+
334+
return ssr;
319335
}
320336

321337
export function patchResolvedViteConfig(viteConfig: ResolvedConfig, options: ResolvedOptions) {

0 commit comments

Comments
 (0)