@@ -19,11 +19,11 @@ import { normalize } from './id.js';
19
19
* name: string,
20
20
* options: ()=>void,
21
21
* transform?:{
22
- * filter: any
22
+ * filter?: {id?:{include?:Array<RegExp>,exclude?:Array<RegExp>}}
23
23
* handler: (code: string,id: string)=>Promise<any>
24
24
* },
25
- * buildStart?:()=>void,
26
- * buildEnd?:()=>void
25
+ * buildStart?:{handler: ()=>void,}
26
+ * buildEnd?:{handler: ()=>void,}
27
27
* }} RolldownPlugin
28
28
*/
29
29
export const facadeOptimizeSveltePluginName = 'vite-plugin-svelte:facade' ;
@@ -70,56 +70,80 @@ export function esbuildSveltePlugin(options) {
70
70
71
71
/**
72
72
* @param {import('../types/options.d.ts').ResolvedOptions } options
73
+ * @param {boolean } components
73
74
* @returns {RolldownPlugin }
74
75
*/
75
- export function rolldownOptimizeSveltePlugin ( options ) {
76
+ function createOptimizerPlugin ( options , components = true ) {
77
+ const compileFn = components ? compileSvelte : compileSvelteModule ;
78
+ const name = components
79
+ ? 'vite-plugin-svelte:prebundle-components'
80
+ : 'vite-plugin-svelte:prebundle-modules' ;
81
+ const statsName = components ? 'prebundle library components' : 'prebundle library modules' ;
82
+ const includeRe = components ? / ^ [ ^ ? # ] + \. s v e l t e (?: [ ? # ] | $ ) / : / ^ [ ^ ? # ] + \. s v e l t e \. [ j t ] s (?: [ ? # ] | $ ) / ;
76
83
/** @type {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection | undefined } */
77
84
let statsCollection ;
85
+ /** @type boolean */
86
+ let isScanner ;
78
87
/** @type {RolldownPlugin } */
79
88
const plugin = {
80
- name : 'vite-plugin-svelte:rolldown-optimize-svelte' ,
89
+ name,
81
90
// @ts -expect-error not typed in rolldown yet
82
91
options ( opts ) {
83
- if (
84
- opts . plugins ?. some ( ( /** @type {{ name: string; } } */ p ) =>
85
- p . name . startsWith ( 'vite:dep-scan' )
86
- )
87
- ) {
88
- delete plugin . transform ;
89
- delete plugin . buildStart ;
90
- delete plugin . buildEnd ;
91
- }
92
+ // workaround to not run this plugin in scanner, only define hooks in options if there's no scanner plugins in the pipeline
93
+ isScanner = opts . plugins ?. some (
94
+ ( /** @type {{ name: string; } } */ p ) => p . name === 'vite:dep-scan:resolve'
95
+ ) ;
92
96
} ,
93
97
transform : {
94
98
filter : {
95
- // TODO: remove excludes once above options hook works
96
- id : { include : [ / ^ [ ^ ? # ] + \. s v e l t e (?: [ ? # ] | $ ) / ] , exclude : [ / ^ v i r t u a l - m o d u l e : / ] } ,
97
- code : { exclude : [ / (?: i m p o r t | e x p o r t ) [ ^ " \n ] + " v i r t u a l - m o d u l e : / ] }
99
+ id : {
100
+ get include ( ) {
101
+ return isScanner ? [ / ^ $ / ] : [ includeRe ] ;
102
+ }
103
+ }
98
104
} ,
99
105
/**
100
106
* @param {string } code
101
107
* @param {string } filename
102
108
*/
103
109
async handler ( code , filename ) {
104
110
try {
105
- return await compileSvelte ( options , { filename, code } , statsCollection ) ;
111
+ return await compileFn ( options , { filename, code } , statsCollection ) ;
106
112
} catch ( e ) {
107
113
throw toRollupError ( e , options ) ;
108
114
}
109
115
}
110
116
} ,
111
- buildStart ( ) {
112
- statsCollection = options . stats ?. startCollection ( 'prebundle library components' , {
113
- logResult : ( c ) => c . stats . length > 1
114
- } ) ;
117
+ buildStart : {
118
+ handler : ( ) => {
119
+ if ( isScanner ) {
120
+ return ;
121
+ }
122
+ statsCollection = options . stats ?. startCollection ( statsName , {
123
+ logResult : ( c ) => c . stats . length > 1
124
+ } ) ;
125
+ }
115
126
} ,
116
- buildEnd ( ) {
117
- statsCollection ?. finish ( ) ;
127
+ buildEnd : {
128
+ handler : ( ) => {
129
+ if ( isScanner ) {
130
+ return ;
131
+ }
132
+ statsCollection ?. finish ( ) ;
133
+ }
118
134
}
119
135
} ;
120
136
return plugin ;
121
137
}
122
138
139
+ /**
140
+ * @param {import('../types/options.d.ts').ResolvedOptions } options
141
+ * @returns {RolldownPlugin }
142
+ */
143
+ export function rolldownOptimizeSveltePlugin ( options ) {
144
+ return createOptimizerPlugin ( options , true ) ;
145
+ }
146
+
123
147
/**
124
148
* @param {import('../types/options.d.ts').ResolvedOptions } options
125
149
* @param {{ filename: string, code: string } } input
@@ -235,51 +259,7 @@ export function esbuildSvelteModulePlugin(options) {
235
259
* @returns {RolldownPlugin }
236
260
*/
237
261
export function rolldownOptimizeSvelteModulePlugin ( options ) {
238
- /** @type {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection | undefined } */
239
- let statsCollection ;
240
- /** @type {RolldownPlugin } */
241
- const plugin = {
242
- name : 'vite-plugin-svelte:rolldown-optimize-svelte-module' ,
243
- // @ts -expect-error not typed in rolldown yet
244
- options ( opts ) {
245
- if (
246
- opts . plugins ?. some ( ( /** @type {{ name: string; } } */ p ) =>
247
- p . name . startsWith ( 'vite:dep-scan' )
248
- )
249
- ) {
250
- delete plugin . transform ;
251
- delete plugin . buildStart ;
252
- delete plugin . buildEnd ;
253
- }
254
- } ,
255
- transform : {
256
- filter : {
257
- // TODO: remove excludes once above options hook works
258
- id : { include : [ / ^ [ ^ ? # ] + \. s v e l t e \. j s (?: [ ? # ] | $ ) / ] , exclude : [ / ^ v i r t u a l - m o d u l e : / ] } ,
259
- code : { exclude : [ / (?: i m p o r t | e x p o r t ) [ ^ " \n ] + " v i r t u a l - m o d u l e : / ] }
260
- } ,
261
- /**
262
- * @param {string } code
263
- * @param {string } filename
264
- */
265
- async handler ( code , filename ) {
266
- try {
267
- return await compileSvelteModule ( options , { filename, code } , statsCollection ) ;
268
- } catch ( e ) {
269
- throw toRollupError ( e , options ) ;
270
- }
271
- }
272
- } ,
273
- buildStart ( ) {
274
- statsCollection = options . stats ?. startCollection ( 'prebundle library components' , {
275
- logResult : ( c ) => c . stats . length > 1
276
- } ) ;
277
- } ,
278
- buildEnd ( ) {
279
- statsCollection ?. finish ( ) ;
280
- }
281
- } ;
282
- return plugin ;
262
+ return createOptimizerPlugin ( options , false ) ;
283
263
}
284
264
285
265
/**
0 commit comments