Skip to content

Commit 0609865

Browse files
authored
fix: add auto-excluded svelte dependencies to ssr.noExternal (#147)
1 parent a453fb8 commit 0609865

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

.changeset/nasty-worms-admire.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
add automatically excluded svelte dependencies to ssr.noExternal

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

+35-16
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
// eslint-disable-next-line node/no-missing-import
1414
} from 'svelte/types/compiler/preprocess';
1515
import path from 'path';
16-
import { findRootSvelteDependencies } from './dependencies';
16+
import { findRootSvelteDependencies, SvelteDependency } from './dependencies';
1717
import { DepOptimizationOptions } from 'vite/src/node/optimizer/index';
1818

1919
const knownOptions = new Set([
@@ -181,8 +181,10 @@ export function buildExtraViteConfig(
181181
options: ResolvedOptions,
182182
config: UserConfig
183183
): Partial<UserConfig> {
184+
// extra handling for svelte dependencies in the project
185+
const svelteDeps = findRootSvelteDependencies(options.root);
184186
const extraViteConfig: Partial<UserConfig> = {
185-
optimizeDeps: buildOptimizeDepsForSvelte(options.root, config.optimizeDeps),
187+
optimizeDeps: buildOptimizeDepsForSvelte(svelteDeps, config.optimizeDeps),
186188
resolve: {
187189
mainFields: [...SVELTE_RESOLVE_MAIN_FIELDS],
188190
dedupe: [...SVELTE_IMPORTS, ...SVELTE_HMR_IMPORTS]
@@ -193,17 +195,8 @@ export function buildExtraViteConfig(
193195
// knownJsSrcExtensions: options.extensions
194196
};
195197

196-
if (options.isBuild && config.build?.ssr) {
197-
// add svelte to ssr.noExternal unless it is present in ssr.external
198-
// so we can resolve it with svelte/ssr
199-
// @ts-ignore
200-
if (!config.ssr?.external?.includes('svelte')) {
201-
// @ts-ignore
202-
extraViteConfig.ssr = {
203-
noExternal: ['svelte']
204-
};
205-
}
206-
}
198+
// @ts-ignore
199+
extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config);
207200

208201
if (options.experimental?.useVitePreprocess) {
209202
// needed to transform svelte files with component imports
@@ -220,7 +213,7 @@ export function buildExtraViteConfig(
220213
}
221214

222215
function buildOptimizeDepsForSvelte(
223-
root: string,
216+
svelteDeps: SvelteDependency[],
224217
optimizeDeps?: DepOptimizationOptions
225218
): DepOptimizationOptions {
226219
// include svelte imports for optimization unless explicitly excluded
@@ -237,8 +230,6 @@ function buildOptimizeDepsForSvelte(
237230
log.debug('"svelte" is excluded in optimizeDeps.exclude, skipped adding it to include.');
238231
}
239232

240-
// extra handling for svelte dependencies in the project
241-
const svelteDeps = findRootSvelteDependencies(root);
242233
const svelteDepsToExclude = Array.from(new Set(svelteDeps.map((dep) => dep.name))).filter(
243234
(dep) => !optimizeDeps?.include?.includes(dep)
244235
);
@@ -262,6 +253,34 @@ function buildOptimizeDepsForSvelte(
262253
return { include, exclude };
263254
}
264255

256+
function buildSSROptionsForSvelte(
257+
svelteDeps: SvelteDependency[],
258+
options: ResolvedOptions,
259+
config: UserConfig
260+
): any {
261+
const noExternal: string[] = [];
262+
263+
// add svelte to ssr.noExternal unless it is present in ssr.external
264+
// so we can resolve it with svelte/ssr
265+
if (options.isBuild && config.build?.ssr) {
266+
// @ts-ignore
267+
if (!config.ssr?.external?.includes('svelte')) {
268+
noExternal.push('svelte');
269+
}
270+
}
271+
272+
// add svelte dependencies to ssr.noExternal unless present in ssr.external or optimizeDeps.include
273+
noExternal.push(
274+
...Array.from(new Set(svelteDeps.map((s) => s.name))).filter((x) => {
275+
// @ts-ignore
276+
return !config.ssr?.external?.includes(x) && !config.optimizeDeps?.include?.includes(x);
277+
})
278+
);
279+
return {
280+
noExternal
281+
};
282+
}
283+
265284
export interface Options {
266285
// eslint-disable no-unused-vars
267286
/** path to svelte config file, either absolute or relative to vite root*/

0 commit comments

Comments
 (0)