@@ -13,7 +13,7 @@ import {
13
13
// eslint-disable-next-line node/no-missing-import
14
14
} from 'svelte/types/compiler/preprocess' ;
15
15
import path from 'path' ;
16
- import { findRootSvelteDependencies } from './dependencies' ;
16
+ import { findRootSvelteDependencies , SvelteDependency } from './dependencies' ;
17
17
import { DepOptimizationOptions } from 'vite/src/node/optimizer/index' ;
18
18
19
19
const knownOptions = new Set ( [
@@ -181,8 +181,10 @@ export function buildExtraViteConfig(
181
181
options : ResolvedOptions ,
182
182
config : UserConfig
183
183
) : Partial < UserConfig > {
184
+ // extra handling for svelte dependencies in the project
185
+ const svelteDeps = findRootSvelteDependencies ( options . root ) ;
184
186
const extraViteConfig : Partial < UserConfig > = {
185
- optimizeDeps : buildOptimizeDepsForSvelte ( options . root , config . optimizeDeps ) ,
187
+ optimizeDeps : buildOptimizeDepsForSvelte ( svelteDeps , config . optimizeDeps ) ,
186
188
resolve : {
187
189
mainFields : [ ...SVELTE_RESOLVE_MAIN_FIELDS ] ,
188
190
dedupe : [ ...SVELTE_IMPORTS , ...SVELTE_HMR_IMPORTS ]
@@ -193,17 +195,8 @@ export function buildExtraViteConfig(
193
195
// knownJsSrcExtensions: options.extensions
194
196
} ;
195
197
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 ) ;
207
200
208
201
if ( options . experimental ?. useVitePreprocess ) {
209
202
// needed to transform svelte files with component imports
@@ -220,7 +213,7 @@ export function buildExtraViteConfig(
220
213
}
221
214
222
215
function buildOptimizeDepsForSvelte (
223
- root : string ,
216
+ svelteDeps : SvelteDependency [ ] ,
224
217
optimizeDeps ?: DepOptimizationOptions
225
218
) : DepOptimizationOptions {
226
219
// include svelte imports for optimization unless explicitly excluded
@@ -237,8 +230,6 @@ function buildOptimizeDepsForSvelte(
237
230
log . debug ( '"svelte" is excluded in optimizeDeps.exclude, skipped adding it to include.' ) ;
238
231
}
239
232
240
- // extra handling for svelte dependencies in the project
241
- const svelteDeps = findRootSvelteDependencies ( root ) ;
242
233
const svelteDepsToExclude = Array . from ( new Set ( svelteDeps . map ( ( dep ) => dep . name ) ) ) . filter (
243
234
( dep ) => ! optimizeDeps ?. include ?. includes ( dep )
244
235
) ;
@@ -262,6 +253,34 @@ function buildOptimizeDepsForSvelte(
262
253
return { include, exclude } ;
263
254
}
264
255
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
+
265
284
export interface Options {
266
285
// eslint-disable no-unused-vars
267
286
/** path to svelte config file, either absolute or relative to vite root*/
0 commit comments