@@ -14,6 +14,7 @@ import {
14
14
ComponentFilterElementType ,
15
15
ComponentFilterHOC ,
16
16
ComponentFilterLocation ,
17
+ ComponentFilterEnvironmentName ,
17
18
ElementTypeClass ,
18
19
ElementTypeContext ,
19
20
ElementTypeFunction ,
@@ -721,6 +722,11 @@ export function getInternalReactConstants(version: string): {
721
722
} ;
722
723
}
723
724
725
+ // All environment names we've seen so far. This lets us create a list of filters to apply.
726
+ // This should ideally include env of filtered Components too so that you can add those as
727
+ // filters at the same time as removing some other filter.
728
+ const knownEnvironmentNames : Set < string > = new Set ( ) ;
729
+
724
730
// Map of one or more Fibers in a pair to their unique id number.
725
731
// We track both Fibers to support Fast Refresh,
726
732
// which may forcefully replace one of the pair as part of hot reloading.
@@ -1099,6 +1105,7 @@ export function attach(
1099
1105
const hideElementsWithDisplayNames : Set < RegExp > = new Set();
1100
1106
const hideElementsWithPaths: Set< RegExp > = new Set();
1101
1107
const hideElementsWithTypes: Set< ElementType > = new Set();
1108
+ const hideElementsWithEnvs: Set< string > = new Set();
1102
1109
1103
1110
// Highlight updates
1104
1111
let traceUpdatesEnabled: boolean = false;
@@ -1108,6 +1115,7 @@ export function attach(
1108
1115
hideElementsWithTypes . clear ( ) ;
1109
1116
hideElementsWithDisplayNames . clear ( ) ;
1110
1117
hideElementsWithPaths . clear ( ) ;
1118
+ hideElementsWithEnvs . clear ( ) ;
1111
1119
1112
1120
componentFilters . forEach ( componentFilter => {
1113
1121
if ( ! componentFilter . isEnabled ) {
@@ -1133,6 +1141,9 @@ export function attach(
1133
1141
case ComponentFilterHOC :
1134
1142
hideElementsWithDisplayNames . add ( new RegExp ( '\\(' ) ) ;
1135
1143
break ;
1144
+ case ComponentFilterEnvironmentName :
1145
+ hideElementsWithEnvs . add ( componentFilter . value ) ;
1146
+ break ;
1136
1147
default :
1137
1148
console . warn (
1138
1149
`Invalid component filter type "${ componentFilter . type } "` ,
@@ -1215,7 +1226,14 @@ export function attach(
1215
1226
flushPendingEvents ( ) ;
1216
1227
}
1217
1228
1218
- function shouldFilterVirtual ( data : ReactComponentInfo ) : boolean {
1229
+ function getEnvironmentNames ( ) : Array < string > {
1230
+ return Array . from ( knownEnvironmentNames ) ;
1231
+ }
1232
+
1233
+ function shouldFilterVirtual(
1234
+ data: ReactComponentInfo,
1235
+ secondaryEnv: null | string,
1236
+ ): boolean {
1219
1237
// For purposes of filtering Server Components are always Function Components.
1220
1238
// Environment will be used to filter Server vs Client.
1221
1239
// Technically they can be forwardRef and memo too but those filters will go away
@@ -1236,6 +1254,14 @@ export function attach(
1236
1254
}
1237
1255
}
1238
1256
1257
+ if (
1258
+ (data.env == null || hideElementsWithEnvs.has(data.env)) &&
1259
+ ( secondaryEnv === null || hideElementsWithEnvs . has ( secondaryEnv ) )
1260
+ ) {
1261
+ // If a Component has two environments, you have to filter both for it not to appear.
1262
+ return true ;
1263
+ }
1264
+
1239
1265
return false;
1240
1266
}
1241
1267
@@ -1294,6 +1320,26 @@ export function attach(
1294
1320
}
1295
1321
}
1296
1322
1323
+ if (hideElementsWithEnvs.has('Client')) {
1324
+ // If we're filtering out the Client environment we should filter out all
1325
+ // "Client Components". Technically that also includes the built-ins but
1326
+ // since that doesn't actually include any additional code loading it's
1327
+ // useful to not filter out the built-ins. Those can be filtered separately.
1328
+ // There's no other way to filter out just Function components on the Client.
1329
+ // Therefore, this only filters Class and Function components.
1330
+ switch ( tag ) {
1331
+ case ClassComponent :
1332
+ case IncompleteClassComponent :
1333
+ case IncompleteFunctionComponent :
1334
+ case FunctionComponent :
1335
+ case IndeterminateComponent :
1336
+ case ForwardRef :
1337
+ case MemoComponent :
1338
+ case SimpleMemoComponent :
1339
+ return true ;
1340
+ }
1341
+ }
1342
+
1297
1343
/* DISABLED: https://github.com/facebook/react/pull/28417
1298
1344
if (hideElementsWithPaths.size > 0) {
1299
1345
const source = getSourceForFiber(fiber);
@@ -2489,7 +2535,14 @@ export function attach(
2489
2535
}
2490
2536
// Scan up until the next Component to see if this component changed environment.
2491
2537
const componentInfo : ReactComponentInfo = ( debugEntry : any ) ;
2492
- if ( shouldFilterVirtual ( componentInfo ) ) {
2538
+ const secondaryEnv = getSecondaryEnvironmentName ( fiber . _debugInfo , i ) ;
2539
+ if ( componentInfo . env != null ) {
2540
+ knownEnvironmentNames . add ( componentInfo . env ) ;
2541
+ }
2542
+ if ( secondaryEnv !== null ) {
2543
+ knownEnvironmentNames . add ( secondaryEnv ) ;
2544
+ }
2545
+ if ( shouldFilterVirtual ( componentInfo , secondaryEnv ) ) {
2493
2546
// Skip.
2494
2547
continue ;
2495
2548
}
@@ -2511,10 +2564,6 @@ export function attach(
2511
2564
) ;
2512
2565
}
2513
2566
previousVirtualInstance = createVirtualInstance ( componentInfo ) ;
2514
- const secondaryEnv = getSecondaryEnvironmentName (
2515
- fiber . _debugInfo ,
2516
- i ,
2517
- ) ;
2518
2567
recordVirtualMount (
2519
2568
previousVirtualInstance ,
2520
2569
reconcilingParent ,
@@ -2919,7 +2968,17 @@ export function attach(
2919
2968
continue ;
2920
2969
}
2921
2970
const componentInfo : ReactComponentInfo = ( debugEntry : any ) ;
2922
- if ( shouldFilterVirtual ( componentInfo ) ) {
2971
+ const secondaryEnv = getSecondaryEnvironmentName (
2972
+ nextChild . _debugInfo ,
2973
+ i ,
2974
+ ) ;
2975
+ if ( componentInfo . env != null ) {
2976
+ knownEnvironmentNames . add ( componentInfo . env ) ;
2977
+ }
2978
+ if ( secondaryEnv !== null ) {
2979
+ knownEnvironmentNames . add ( secondaryEnv ) ;
2980
+ }
2981
+ if ( shouldFilterVirtual ( componentInfo , secondaryEnv ) ) {
2923
2982
continue ;
2924
2983
}
2925
2984
if ( level === virtualLevel ) {
@@ -2983,10 +3042,6 @@ export function attach(
2983
3042
} else {
2984
3043
// Otherwise we create a new instance.
2985
3044
const newVirtualInstance = createVirtualInstance ( componentInfo ) ;
2986
- const secondaryEnv = getSecondaryEnvironmentName (
2987
- nextChild . _debugInfo ,
2988
- i ,
2989
- ) ;
2990
3045
recordVirtualMount (
2991
3046
newVirtualInstance ,
2992
3047
reconcilingParent ,
@@ -3925,7 +3980,7 @@ export function attach(
3925
3980
owner = ownerFiber . _debugOwner ;
3926
3981
} else {
3927
3982
const ownerInfo : ReactComponentInfo = ( owner : any ) ; // Refined
3928
- if ( ! shouldFilterVirtual ( ownerInfo ) ) {
3983
+ if ( ! shouldFilterVirtual ( ownerInfo , null ) ) {
3929
3984
return ownerInfo ;
3930
3985
}
3931
3986
owner = ownerInfo . owner ;
@@ -5750,5 +5805,6 @@ export function attach(
5750
5805
storeAsGlobal ,
5751
5806
unpatchConsoleForStrictMode ,
5752
5807
updateComponentFilters ,
5808
+ getEnvironmentNames ,
5753
5809
} ;
5754
5810
}
0 commit comments