@@ -32,7 +32,8 @@ export function setupOptimizer(api) {
32
32
return {
33
33
name : 'vite-plugin-svelte:setup-optimizer' ,
34
34
apply : 'serve' ,
35
- config ( ) {
35
+ configEnvironment ( _name , config ) {
36
+ const consumer = config . consumer ?? 'client' ;
36
37
/** @type {import('vite').UserConfig['optimizeDeps'] } */
37
38
const optimizeDeps = {
38
39
// Experimental Vite API to allow these extensions to be scanned and prebundled
@@ -43,43 +44,25 @@ export function setupOptimizer(api) {
43
44
// the added plugins are patched in configResolved below
44
45
if ( rolldownVersion ) {
45
46
//@ts -ignore rolldown types not finished
47
+
46
48
optimizeDeps . rollupOptions = {
47
49
plugins : [
48
- placeholderRolldownOptimizerPlugin ( optimizeSveltePluginName ) ,
49
- placeholderRolldownOptimizerPlugin ( optimizeSvelteModulePluginName )
50
+ rolldownOptimizerPlugin ( api , consumer , true ) ,
51
+ rolldownOptimizerPlugin ( api , consumer , false )
50
52
]
51
53
} ;
52
54
} else {
53
55
optimizeDeps . esbuildOptions = {
54
56
plugins : [
55
- { name : optimizeSveltePluginName , setup : ( ) => { } } ,
56
- { name : optimizeSvelteModulePluginName , setup : ( ) => { } }
57
+ eSBuildOptimizerPlugin ( api , consumer , true ) ,
58
+ eSBuildOptimizerPlugin ( api , consumer , false )
57
59
]
58
60
} ;
59
61
}
60
62
return { optimizeDeps } ;
61
63
} ,
62
64
configResolved ( c ) {
63
65
viteConfig = c ;
64
- const optimizeDeps = c . optimizeDeps ;
65
- if ( rolldownVersion ) {
66
- const plugins =
67
- // @ts -expect-error not typed
68
- optimizeDeps . rollupOptions ?. plugins ?. filter ( ( p ) =>
69
- [ optimizeSveltePluginName , optimizeSvelteModulePluginName ] . includes ( p . name )
70
- ) ?? [ ] ;
71
- for ( const plugin of plugins ) {
72
- patchRolldownOptimizerPlugin ( plugin , api . options ) ;
73
- }
74
- } else {
75
- const plugins =
76
- optimizeDeps . esbuildOptions ?. plugins ?. filter ( ( p ) =>
77
- [ optimizeSveltePluginName , optimizeSvelteModulePluginName ] . includes ( p . name )
78
- ) ?? [ ] ;
79
- for ( const plugin of plugins ) {
80
- patchESBuildOptimizerPlugin ( plugin , api . options ) ;
81
- }
82
- }
83
66
} ,
84
67
async buildStart ( ) {
85
68
if ( ! api . options . prebundleSvelteLibraries ) return ;
@@ -94,54 +77,72 @@ export function setupOptimizer(api) {
94
77
}
95
78
96
79
/**
97
- * @param {EsbuildPlugin } plugin
98
- * @param {import('../types/options.d.ts').ResolvedOptions } options
80
+ * @param {import('../types/plugin-api.d.ts').PluginAPI } api
81
+ * @param {'server'|'client' } consumer
82
+ * @param {boolean } components
83
+ * @return {EsbuildPlugin }
99
84
*/
100
- function patchESBuildOptimizerPlugin ( plugin , options ) {
101
- const components = plugin . name === optimizeSveltePluginName ;
85
+ function eSBuildOptimizerPlugin ( api , consumer , components ) {
86
+ const name = components ? optimizeSveltePluginName : optimizeSvelteModulePluginName ;
102
87
const compileFn = components ? compileSvelte : compileSvelteModule ;
103
88
const statsName = components ? 'prebundle library components' : 'prebundle library modules' ;
104
89
const filter = components ? / \. s v e l t e (?: \? .* ) ? $ / : / \. s v e l t e \. [ j t ] s (?: \? .* ) ? $ / ;
105
- plugin . setup = ( build ) => {
106
- if ( build . initialOptions . plugins ?. some ( ( v ) => v . name === 'vite:dep-scan' ) ) return ;
90
+ const generate = consumer === 'server' ? 'server' : 'client' ;
107
91
108
- /** @type {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection | undefined } */
109
- let statsCollection ;
110
- build . onStart ( ( ) => {
111
- statsCollection = options . stats ?. startCollection ( statsName , {
112
- logResult : ( c ) => c . stats . length > 1
92
+ return {
93
+ name,
94
+ setup ( build ) {
95
+ if ( build . initialOptions . plugins ?. some ( ( v ) => v . name === 'vite:dep-scan' ) ) return ;
96
+
97
+ /** @type {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection | undefined } */
98
+ let statsCollection ;
99
+ build . onStart ( ( ) => {
100
+ statsCollection = api . options . stats ?. startCollection ( statsName , {
101
+ logResult : ( c ) => c . stats . length > 1
102
+ } ) ;
113
103
} ) ;
114
- } ) ;
115
- build . onLoad ( { filter } , async ( { path : filename } ) => {
116
- const code = readFileSync ( filename , 'utf8' ) ;
117
- try {
118
- const result = await compileFn ( options , { filename, code } , statsCollection ) ;
119
- const contents = result . map
120
- ? result . code + '//# sourceMappingURL=' + result . map . toUrl ( )
121
- : result . code ;
122
- return { contents } ;
123
- } catch ( e ) {
124
- return { errors : [ toESBuildError ( e , options ) ] } ;
125
- }
126
- } ) ;
127
- build . onEnd ( ( ) => {
128
- statsCollection ?. finish ( ) ;
129
- } ) ;
104
+ build . onLoad ( { filter } , async ( { path : filename } ) => {
105
+ const code = readFileSync ( filename , 'utf8' ) ;
106
+ try {
107
+ const result = await compileFn (
108
+ api . options ,
109
+ { filename, code } ,
110
+ generate ,
111
+ statsCollection
112
+ ) ;
113
+ const contents = result . map
114
+ ? result . code + '//# sourceMappingURL=' + result . map . toUrl ( )
115
+ : result . code ;
116
+ return { contents } ;
117
+ } catch ( e ) {
118
+ return { errors : [ toESBuildError ( e , api . options ) ] } ;
119
+ }
120
+ } ) ;
121
+ build . onEnd ( ( ) => {
122
+ statsCollection ?. finish ( ) ;
123
+ } ) ;
124
+ }
130
125
} ;
131
126
}
132
127
133
128
/**
134
- * @param {RollupPlugin } plugin
135
- * @param {import('../types/options.d.ts').ResolvedOptions } options
129
+ * @param {import('../types/plugin-api.d.ts').PluginAPI } api
130
+ * @param {'server'|'client' } consumer
131
+ * @param {boolean } components
132
+ * @return {import('vite').Rollup.Plugin }
136
133
*/
137
- function patchRolldownOptimizerPlugin ( plugin , options ) {
138
- const components = plugin . name === optimizeSveltePluginName ;
134
+ function rolldownOptimizerPlugin ( api , consumer , components ) {
135
+ const name = components ? optimizeSveltePluginName : optimizeSvelteModulePluginName ;
139
136
const compileFn = components ? compileSvelte : compileSvelteModule ;
140
137
const statsName = components ? 'prebundle library components' : 'prebundle library modules' ;
141
138
const includeRe = components ? / ^ [ ^ ? # ] + \. s v e l t e (?: [ ? # ] | $ ) / : / ^ [ ^ ? # ] + \. s v e l t e \. [ j t ] s (?: [ ? # ] | $ ) / ;
139
+ const generate = consumer === 'server' ? 'server' : 'client' ;
142
140
/** @type {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection | undefined } */
143
141
let statsCollection ;
144
-
142
+ /**@type {import('vite').Rollup.Plugin }*/
143
+ const plugin = {
144
+ name
145
+ } ;
145
146
plugin . options = ( opts ) => {
146
147
// @ts -expect-error plugins is an array here
147
148
const isScanner = opts . plugins . some (
@@ -160,14 +161,14 @@ function patchRolldownOptimizerPlugin(plugin, options) {
160
161
*/
161
162
async handler ( code , filename ) {
162
163
try {
163
- return await compileFn ( options , { filename, code } , statsCollection ) ;
164
+ return await compileFn ( api . options , { filename, code } , generate , statsCollection ) ;
164
165
} catch ( e ) {
165
- throw toRollupError ( e , options ) ;
166
+ throw toRollupError ( e , api . options ) ;
166
167
}
167
168
}
168
169
} ;
169
170
plugin . buildStart = ( ) => {
170
- statsCollection = options . stats ?. startCollection ( statsName , {
171
+ statsCollection = api . options . stats ?. startCollection ( statsName , {
171
172
logResult : ( c ) => c . stats . length > 1
172
173
} ) ;
173
174
} ;
@@ -176,15 +177,17 @@ function patchRolldownOptimizerPlugin(plugin, options) {
176
177
} ;
177
178
}
178
179
} ;
180
+ return plugin ;
179
181
}
180
182
181
183
/**
182
184
* @param {import('../types/options.d.ts').ResolvedOptions } options
183
185
* @param {{ filename: string, code: string } } input
186
+ * @param {'client'|'server' } generate
184
187
* @param {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection } [statsCollection]
185
188
* @returns {Promise<import('../types/compile.d.ts').Code> }
186
189
*/
187
- async function compileSvelte ( options , { filename, code } , statsCollection ) {
190
+ async function compileSvelte ( options , { filename, code } , generate , statsCollection ) {
188
191
let css = options . compilerOptions . css ;
189
192
if ( css !== 'injected' ) {
190
193
// TODO ideally we'd be able to externalize prebundled styles too, but for now always put them in the js
@@ -196,7 +199,7 @@ async function compileSvelte(options, { filename, code }, statsCollection) {
196
199
...options . compilerOptions ,
197
200
css,
198
201
filename,
199
- generate : 'client'
202
+ generate
200
203
} ;
201
204
202
205
if ( compileOptions . hmr && options . emitCss ) {
@@ -252,15 +255,16 @@ async function compileSvelte(options, { filename, code }, statsCollection) {
252
255
/**
253
256
* @param {import('../types/options.d.ts').ResolvedOptions } options
254
257
* @param {{ filename: string; code: string } } input
258
+ * @param {'client'|'server' } generate
255
259
* @param {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection } [statsCollection]
256
260
* @returns {Promise<import('../types/compile.d.ts').Code> }
257
261
*/
258
- async function compileSvelteModule ( options , { filename, code } , statsCollection ) {
262
+ async function compileSvelteModule ( options , { filename, code } , generate , statsCollection ) {
259
263
const endStat = statsCollection ?. start ( filename ) ;
260
264
const compiled = svelte . compileModule ( code , {
261
265
dev : options . compilerOptions ?. dev ?? true , // default to dev: true because prebundling is only used in dev
262
266
filename,
263
- generate : 'client'
267
+ generate
264
268
} ) ;
265
269
if ( endStat ) {
266
270
endStat ( ) ;
@@ -311,21 +315,6 @@ async function svelteMetadataChanged(cacheDir, options) {
311
315
return currentSvelteMetadata !== existingSvelteMetadata ;
312
316
}
313
317
314
- /**
315
- *
316
- * @param {string } name
317
- * @returns {import('vite').Rollup.Plugin }
318
- */
319
- function placeholderRolldownOptimizerPlugin ( name ) {
320
- return {
321
- name,
322
- options ( ) { } ,
323
- buildStart ( ) { } ,
324
- buildEnd ( ) { } ,
325
- transform : { filter : { id : / ^ $ / } , handler ( ) { } }
326
- } ;
327
- }
328
-
329
318
/**
330
319
* @param {import('../types/options.d.ts').ResolvedOptions } options
331
320
* @returns {Partial<import('../types/options.d.ts').ResolvedOptions> }
0 commit comments