@@ -14,6 +14,7 @@ import type {
14
14
RollupLog ,
15
15
RollupOptions ,
16
16
RollupOutput ,
17
+ PluginContext as RollupPluginContext ,
17
18
RollupWatcher ,
18
19
WatcherOptions ,
19
20
} from 'rollup'
@@ -1040,10 +1041,10 @@ function wrapEnvironmentResolveId(
1040
1041
const fn = getHookHandler ( hook )
1041
1042
const handler : Plugin [ 'resolveId' ] = function ( id , importer , options ) {
1042
1043
return fn . call (
1043
- this ,
1044
+ injectEnvironmentInContext ( this ) ,
1044
1045
id ,
1045
1046
importer ,
1046
- injectEnvironmentFlag ( options , environment ) ,
1047
+ injectSsrFlag ( options , environment ) ,
1047
1048
)
1048
1049
}
1049
1050
@@ -1065,8 +1066,12 @@ function wrapEnvironmentLoad(
1065
1066
1066
1067
const fn = getHookHandler ( hook )
1067
1068
const handler : Plugin [ 'load' ] = function ( id , ...args ) {
1068
- // @ts -expect-error: Receiving options param to be future-proof if Rollup adds it
1069
- return fn . call ( this , id , injectEnvironmentFlag ( args [ 0 ] , environment ) )
1069
+ return fn . call (
1070
+ injectEnvironmentInContext ( this , environment ) ,
1071
+ id ,
1072
+ // @ts -expect-error: Receiving options param to be future-proof if Rollup adds it
1073
+ injectSsrFlag ( args [ 0 ] , environment ) ,
1074
+ )
1070
1075
}
1071
1076
1072
1077
if ( 'handler' in hook ) {
@@ -1088,11 +1093,11 @@ function wrapEnvironmentTransform(
1088
1093
const fn = getHookHandler ( hook )
1089
1094
const handler : Plugin [ 'transform' ] = function ( code , importer , ...args ) {
1090
1095
return fn . call (
1091
- this ,
1096
+ injectEnvironmentInContext ( this , environment ) ,
1092
1097
code ,
1093
1098
importer ,
1094
1099
// @ts -expect-error: Receiving options param to be future-proof if Rollup adds it
1095
- injectEnvironmentFlag ( args [ 0 ] , environment ) ,
1100
+ injectSsrFlag ( args [ 0 ] , environment ) ,
1096
1101
)
1097
1102
}
1098
1103
@@ -1106,14 +1111,27 @@ function wrapEnvironmentTransform(
1106
1111
}
1107
1112
}
1108
1113
1109
- function injectEnvironmentFlag < T extends Record < string , any > > (
1114
+ function injectEnvironmentInContext (
1115
+ context : RollupPluginContext ,
1116
+ environment ?: BuildEnvironment ,
1117
+ ) {
1118
+ return new Proxy ( context , {
1119
+ get ( target , prop , receiver ) {
1120
+ if ( prop === 'environment' ) {
1121
+ return environment
1122
+ }
1123
+ return Reflect . get ( target , prop , receiver )
1124
+ } ,
1125
+ } )
1126
+ }
1127
+
1128
+ function injectSsrFlag < T extends Record < string , any > > (
1110
1129
options ?: T ,
1111
1130
environment ?: BuildEnvironment ,
1112
- ) : T & { ssr ?: boolean ; environment ?: BuildEnvironment } {
1131
+ ) : T & { ssr ?: boolean } {
1113
1132
const ssr = environment ? environment . name !== 'client' : true
1114
- return { ...( options ?? { } ) , ssr, environment } as T & {
1133
+ return { ...( options ?? { } ) , ssr } as T & {
1115
1134
ssr ?: boolean
1116
- environment ?: BuildEnvironment
1117
1135
}
1118
1136
}
1119
1137
0 commit comments