@@ -40,17 +40,18 @@ import type {
40
40
FunctionPluginHooks ,
41
41
InputOptions ,
42
42
LoadResult ,
43
- MinimalPluginContext ,
44
43
ModuleInfo ,
45
44
ModuleOptions ,
46
45
NormalizedInputOptions ,
47
46
OutputOptions ,
48
47
ParallelPluginHooks ,
49
48
PartialNull ,
50
49
PartialResolvedId ,
50
+ PluginContextMeta ,
51
51
ResolvedId ,
52
52
RollupError ,
53
53
RollupLog ,
54
+ MinimalPluginContext as RollupMinimalPluginContext ,
54
55
PluginContext as RollupPluginContext ,
55
56
TransformPluginContext as RollupTransformPluginContext ,
56
57
SourceDescription ,
@@ -88,8 +89,6 @@ import type {
88
89
EnvironmentModuleNode ,
89
90
} from './moduleGraph'
90
91
91
- const noop = ( ) => { }
92
-
93
92
// same default value of "moduleInfo.meta" as in Rollup
94
93
const EMPTY_OBJECT = Object . freeze ( { } )
95
94
@@ -105,6 +104,9 @@ const debugPluginResolve = createDebugger('vite:plugin-resolve', {
105
104
const debugPluginTransform = createDebugger ( 'vite:plugin-transform' , {
106
105
onlyWhenFocused : 'vite:plugin' ,
107
106
} )
107
+ const debugPluginContainerContext = createDebugger (
108
+ 'vite:plugin-container-context' ,
109
+ )
108
110
109
111
export const ERR_CLOSED_SERVER = 'ERR_CLOSED_SERVER'
110
112
@@ -182,18 +184,10 @@ class EnvironmentPluginContainer {
182
184
public plugins : Plugin [ ] ,
183
185
public watcher ?: FSWatcher ,
184
186
) {
185
- this . minimalContext = {
186
- meta : {
187
- rollupVersion,
188
- watchMode : true ,
189
- } ,
190
- debug : noop ,
191
- info : noop ,
192
- warn : noop ,
193
- // @ts -expect-error noop
194
- error : noop ,
187
+ this . minimalContext = new MinimalPluginContext (
188
+ { rollupVersion, watchMode : true } ,
195
189
environment ,
196
- }
190
+ )
197
191
const utils = createPluginHookUtils ( plugins )
198
192
this . getSortedPlugins = utils . getSortedPlugins
199
193
this . getSortedPluginHooks = utils . getSortedPluginHooks
@@ -545,22 +539,63 @@ class EnvironmentPluginContainer {
545
539
}
546
540
}
547
541
548
- class PluginContext implements Omit < RollupPluginContext , 'cache' > {
542
+ class MinimalPluginContext implements RollupMinimalPluginContext {
543
+ constructor (
544
+ public meta : PluginContextMeta ,
545
+ public environment : Environment ,
546
+ ) { }
547
+
548
+ debug ( rawLog : string | RollupLog | ( ( ) => string | RollupLog ) ) : void {
549
+ const log = this . _normalizeRawLog ( rawLog )
550
+ const msg = buildErrorMessage ( log , [ `debug: ${ log . message } ` ] , false )
551
+ debugPluginContainerContext ?.( msg )
552
+ }
553
+
554
+ info ( rawLog : string | RollupLog | ( ( ) => string | RollupLog ) ) : void {
555
+ const log = this . _normalizeRawLog ( rawLog )
556
+ const msg = buildErrorMessage ( log , [ `info: ${ log . message } ` ] , false )
557
+ this . environment . logger . info ( msg , { clear : true , timestamp : true } )
558
+ }
559
+
560
+ warn ( rawLog : string | RollupLog | ( ( ) => string | RollupLog ) ) : void {
561
+ const log = this . _normalizeRawLog ( rawLog )
562
+ const msg = buildErrorMessage (
563
+ log ,
564
+ [ colors . yellow ( `warning: ${ log . message } ` ) ] ,
565
+ false ,
566
+ )
567
+ this . environment . logger . warn ( msg , { clear : true , timestamp : true } )
568
+ }
569
+
570
+ error ( e : string | RollupError ) : never {
571
+ const err = ( typeof e === 'string' ? new Error ( e ) : e ) as RollupError
572
+ throw err
573
+ }
574
+
575
+ private _normalizeRawLog (
576
+ rawLog : string | RollupLog | ( ( ) => string | RollupLog ) ,
577
+ ) : RollupLog {
578
+ const logValue = typeof rawLog === 'function' ? rawLog ( ) : rawLog
579
+ return typeof logValue === 'string' ? new Error ( logValue ) : logValue
580
+ }
581
+ }
582
+
583
+ class PluginContext
584
+ extends MinimalPluginContext
585
+ implements Omit < RollupPluginContext , 'cache' >
586
+ {
549
587
ssr = false
550
588
_scan = false
551
589
_activeId : string | null = null
552
590
_activeCode : string | null = null
553
591
_resolveSkips ?: Set < Plugin >
554
592
_resolveSkipCalls ?: readonly SkipInformation [ ]
555
- meta : RollupPluginContext [ 'meta' ]
556
- environment : Environment
557
593
558
594
constructor (
559
595
public _plugin : Plugin ,
560
596
public _container : EnvironmentPluginContainer ,
561
597
) {
562
- this . environment = this . _container . environment
563
- this . meta = this . _container . minimalContext . meta
598
+ super ( _container . minimalContext . meta , _container . environment )
564
599
}
565
600
566
601
parse ( code : string , opts : any ) {
@@ -684,39 +719,41 @@ class PluginContext implements Omit<RollupPluginContext, 'cache'> {
684
719
return ''
685
720
}
686
721
687
- warn (
688
- e : string | RollupLog | ( ( ) => string | RollupLog ) ,
722
+ override debug ( log : string | RollupLog | ( ( ) => string | RollupLog ) ) : void {
723
+ const err = this . _formatLog ( typeof log === 'function' ? log ( ) : log )
724
+ super . debug ( err )
725
+ }
726
+
727
+ override info ( log : string | RollupLog | ( ( ) => string | RollupLog ) ) : void {
728
+ const err = this . _formatLog ( typeof log === 'function' ? log ( ) : log )
729
+ super . info ( err )
730
+ }
731
+
732
+ override warn (
733
+ log : string | RollupLog | ( ( ) => string | RollupLog ) ,
689
734
position ?: number | { column : number ; line : number } ,
690
735
) : void {
691
- const err = this . _formatError ( typeof e === 'function' ? e ( ) : e , position )
692
- const msg = buildErrorMessage (
693
- err ,
694
- [ colors . yellow ( `warning: ${ err . message } ` ) ] ,
695
- false ,
736
+ const err = this . _formatLog (
737
+ typeof log === 'function' ? log ( ) : log ,
738
+ position ,
696
739
)
697
- this . environment . logger . warn ( msg , {
698
- clear : true ,
699
- timestamp : true ,
700
- } )
740
+ super . warn ( err )
701
741
}
702
742
703
- error (
743
+ override error (
704
744
e : string | RollupError ,
705
745
position ?: number | { column : number ; line : number } ,
706
746
) : never {
707
747
// error thrown here is caught by the transform middleware and passed on
708
748
// the the error middleware.
709
- throw this . _formatError ( e , position )
749
+ throw this . _formatLog ( e , position )
710
750
}
711
751
712
- debug = noop
713
- info = noop
714
-
715
- private _formatError (
716
- e : string | RollupError ,
717
- position : number | { column : number ; line : number } | undefined ,
718
- ) : RollupError {
719
- const err = ( typeof e === 'string' ? new Error ( e ) : e ) as RollupError
752
+ private _formatLog < E extends RollupLog > (
753
+ e : string | E ,
754
+ position ?: number | { column : number ; line : number } | undefined ,
755
+ ) : E {
756
+ const err = ( typeof e === 'string' ? new Error ( e ) : e ) as E
720
757
if ( err . pluginCode ) {
721
758
return err // The plugin likely called `this.error`
722
759
}
0 commit comments