@@ -19,11 +19,11 @@ import { normalize } from './id.js';
1919 * name: string,
2020 * options: ()=>void,
2121 * transform?:{
22- * filter: any
22+ * filter?: {id?:{include?:Array<RegExp>,exclude?:Array<RegExp>}}
2323 * handler: (code: string,id: string)=>Promise<any>
2424 * },
25- * buildStart?:()=>void,
26- * buildEnd?:()=>void
25+ * buildStart?:{handler: ()=>void,}
26+ * buildEnd?:{handler: ()=>void,}
2727 * }} RolldownPlugin
2828 */
2929export const facadeOptimizeSveltePluginName = 'vite-plugin-svelte:facade' ;
@@ -70,56 +70,80 @@ export function esbuildSveltePlugin(options) {
7070
7171/**
7272 * @param {import('../types/options.d.ts').ResolvedOptions } options
73+ * @param {boolean } components
7374 * @returns {RolldownPlugin }
7475 */
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 (?: [ ? # ] | $ ) / ;
7683 /** @type {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection | undefined } */
7784 let statsCollection ;
85+ /** @type boolean */
86+ let isScanner ;
7887 /** @type {RolldownPlugin } */
7988 const plugin = {
80- name : 'vite-plugin-svelte:rolldown-optimize-svelte' ,
89+ name,
8190 // @ts -expect-error not typed in rolldown yet
8291 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+ ) ;
9296 } ,
9397 transform : {
9498 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+ }
98104 } ,
99105 /**
100106 * @param {string } code
101107 * @param {string } filename
102108 */
103109 async handler ( code , filename ) {
104110 try {
105- return await compileSvelte ( options , { filename, code } , statsCollection ) ;
111+ return await compileFn ( options , { filename, code } , statsCollection ) ;
106112 } catch ( e ) {
107113 throw toRollupError ( e , options ) ;
108114 }
109115 }
110116 } ,
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+ }
115126 } ,
116- buildEnd ( ) {
117- statsCollection ?. finish ( ) ;
127+ buildEnd : {
128+ handler : ( ) => {
129+ if ( isScanner ) {
130+ return ;
131+ }
132+ statsCollection ?. finish ( ) ;
133+ }
118134 }
119135 } ;
120136 return plugin ;
121137}
122138
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+
123147/**
124148 * @param {import('../types/options.d.ts').ResolvedOptions } options
125149 * @param {{ filename: string, code: string } } input
@@ -235,51 +259,7 @@ export function esbuildSvelteModulePlugin(options) {
235259 * @returns {RolldownPlugin }
236260 */
237261export 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 ) ;
283263}
284264
285265/**
0 commit comments