@@ -395,6 +395,42 @@ export async function resolveConfig(
395
395
mode = inlineConfig . mode || config . mode || mode
396
396
configEnv . mode = mode
397
397
398
+ // Some plugins that aren't intended to work in the bundling of workers (doing post-processing at build time for example).
399
+ // And Plugins may also have cached that could be corrupted by being used in these extra rollup calls.
400
+ // So we need to separate the worker plugin from the plugin that vite needs to run.
401
+ const rawWorkerUserPlugins = (
402
+ ( await asyncFlatten ( config . worker ?. plugins || [ ] ) ) as Plugin [ ]
403
+ ) . filter ( ( p ) => {
404
+ if ( ! p ) {
405
+ return false
406
+ } else if ( ! p . apply ) {
407
+ return true
408
+ } else if ( typeof p . apply === 'function' ) {
409
+ return p . apply ( { ...config , mode } , configEnv )
410
+ } else {
411
+ return p . apply === command
412
+ }
413
+ } )
414
+
415
+ let workerConfig = mergeConfig ( { } , config )
416
+ const [ workerPrePlugins , workerNormalPlugins , workerPostPlugins ] =
417
+ sortUserPlugins ( rawWorkerUserPlugins )
418
+
419
+ // run config hooks
420
+ const workerUserPlugins = [
421
+ ...workerPrePlugins ,
422
+ ...workerNormalPlugins ,
423
+ ...workerPostPlugins
424
+ ]
425
+ for ( const p of workerUserPlugins ) {
426
+ if ( p . config ) {
427
+ const res = await p . config ( workerConfig , configEnv )
428
+ if ( res ) {
429
+ workerConfig = mergeConfig ( workerConfig , res )
430
+ }
431
+ }
432
+ }
433
+
398
434
// resolve plugins
399
435
const rawUserPlugins = (
400
436
( await asyncFlatten ( config . plugins || [ ] ) ) as Plugin [ ]
@@ -412,13 +448,6 @@ export async function resolveConfig(
412
448
const [ prePlugins , normalPlugins , postPlugins ] =
413
449
sortUserPlugins ( rawUserPlugins )
414
450
415
- // resolve worker
416
- const resolvedWorkerOptions : ResolveWorkerOptions = {
417
- format : config . worker ?. format || 'iife' ,
418
- plugins : [ ] ,
419
- rollupOptions : config . worker ?. rollupOptions || { }
420
- }
421
-
422
451
// run config hooks
423
452
const userPlugins = [ ...prePlugins , ...normalPlugins , ...postPlugins ]
424
453
for ( const p of userPlugins ) {
@@ -577,8 +606,14 @@ export async function resolveConfig(
577
606
578
607
const BASE_URL = resolvedBase
579
608
580
- const resolved : ResolvedConfig = {
581
- ...config ,
609
+ // resolve worker
610
+ const resolvedWorkerOptions : ResolveWorkerOptions = {
611
+ format : workerConfig . worker ?. format || 'iife' ,
612
+ plugins : [ ] ,
613
+ rollupOptions : workerConfig . worker ?. rollupOptions || { }
614
+ }
615
+
616
+ const resolvedConfig : ResolvedConfig = {
582
617
configFile : configFile ? normalizePath ( configFile ) : undefined ,
583
618
configFileDependencies : configFileDependencies . map ( ( name ) =>
584
619
normalizePath ( path . resolve ( name ) )
@@ -628,6 +663,7 @@ export async function resolveConfig(
628
663
...config . experimental
629
664
}
630
665
}
666
+ const resolved : ResolvedConfig = Object . assign ( config , resolvedConfig )
631
667
632
668
if ( middlewareMode === 'ssr' ) {
633
669
logger . warn (
@@ -659,35 +695,34 @@ export async function resolveConfig(
659
695
)
660
696
}
661
697
662
- // Some plugins that aren't intended to work in the bundling of workers (doing post-processing at build time for example).
663
- // And Plugins may also have cached that could be corrupted by being used in these extra rollup calls.
664
- // So we need to separate the worker plugin from the plugin that vite needs to run.
665
- const [ workerPrePlugins , workerNormalPlugins , workerPostPlugins ] =
666
- sortUserPlugins ( config . worker ?. plugins as Plugin [ ] )
667
- const workerResolved : ResolvedConfig = {
668
- ...resolved ,
669
- isWorker : true ,
670
- mainConfig : resolved
671
- }
672
- resolved . worker . plugins = await resolvePlugins (
673
- workerResolved ,
674
- workerPrePlugins ,
675
- workerNormalPlugins ,
676
- workerPostPlugins
677
- )
678
- // call configResolved worker plugins hooks
679
- await Promise . all (
680
- resolved . worker . plugins . map ( ( p ) => p . configResolved ?.( workerResolved ) )
681
- )
682
698
; ( resolved . plugins as Plugin [ ] ) = await resolvePlugins (
683
699
resolved ,
684
700
prePlugins ,
685
701
normalPlugins ,
686
702
postPlugins
687
703
)
688
704
705
+ const workerResolved : ResolvedConfig = Object . assign (
706
+ workerConfig ,
707
+ resolvedConfig ,
708
+ {
709
+ isWorker : true ,
710
+ mainConfig : resolved
711
+ }
712
+ )
713
+ resolved . worker . plugins = await resolvePlugins (
714
+ workerResolved ,
715
+ workerPrePlugins ,
716
+ workerNormalPlugins ,
717
+ workerPostPlugins
718
+ )
719
+
689
720
// call configResolved hooks
690
- await Promise . all ( userPlugins . map ( ( p ) => p . configResolved ?.( resolved ) ) )
721
+ await Promise . all (
722
+ userPlugins
723
+ . map ( ( p ) => p . configResolved ?.( resolved ) )
724
+ . concat ( workerUserPlugins . map ( ( p ) => p . configResolved ?.( workerResolved ) ) )
725
+ )
691
726
692
727
if ( process . env . DEBUG ) {
693
728
debug ( `using resolved config: %O` , {
0 commit comments