Skip to content

Commit 6b9eb2b

Browse files
author
Brian Vaughn
committed
Refactored inspected element plugins type/format
Moved StyleX plugin type to be shared. Replaced Array-of-plugins type with object type, since the frontend already needs to know about which types it supports anyway (so no need to do unnecessary filtering).
1 parent 5425364 commit 6b9eb2b

File tree

7 files changed

+33
-38
lines changed

7 files changed

+33
-38
lines changed

packages/react-devtools-shared/src/backend/StyleX/utils.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77
* @flow
88
*/
99

10-
export type StyleXData = {|
11-
sources: Array<string>,
12-
resolvedStyles: Object,
13-
|};
10+
import type {StyleXPlugin} from 'react-devtools-shared/src/types';
1411

1512
const cachedStyleNameToValueMap: Map<string, string> = new Map();
1613

17-
export function getStyleXData(data: any): StyleXData {
14+
export function getStyleXData(data: any): StyleXPlugin {
1815
const sources = new Set();
1916
const resolvedStyles = {};
2017

packages/react-devtools-shared/src/backend/legacy/renderer.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,9 @@ export function attach(
839839
rendererPackageName: null,
840840
rendererVersion: null,
841841

842-
plugins: [],
842+
plugins: {
843+
stylex: null,
844+
},
843845
};
844846
}
845847

packages/react-devtools-shared/src/backend/renderer.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ import type {
102102
NativeType,
103103
PathFrame,
104104
PathMatch,
105-
Plugin,
106105
ProfilingDataBackend,
107106
ProfilingDataForRootBackend,
108107
ReactRenderer,
@@ -113,6 +112,7 @@ import type {
113112
import type {
114113
ComponentFilter,
115114
ElementType,
115+
Plugins,
116116
} from 'react-devtools-shared/src/types';
117117

118118
type getDisplayNameForFiberType = (fiber: Fiber) => string | null;
@@ -3239,14 +3239,13 @@ export function attach(
32393239
targetErrorBoundaryID = getNearestErrorBoundaryID(fiber);
32403240
}
32413241

3242-
const plugins: Array<Plugin> = [];
3242+
const plugins: Plugins = {
3243+
stylex: null,
3244+
};
32433245

32443246
if (enableStyleXFeatures) {
32453247
if (memoizedProps.hasOwnProperty('xstyle')) {
3246-
plugins.push({
3247-
type: 'stylex',
3248-
data: getStyleXData(memoizedProps.xstyle),
3249-
});
3248+
plugins.stylex = getStyleXData(memoizedProps.xstyle);
32503249
}
32513250
}
32523251

packages/react-devtools-shared/src/backend/types.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
1313
import type {
1414
ComponentFilter,
1515
ElementType,
16+
Plugins,
1617
} from 'react-devtools-shared/src/types';
1718
import type {ResolveNativeStyle} from 'react-devtools-shared/src/backend/NativeStyleEditor/setupNativeStyleEditor';
1819

@@ -213,14 +214,6 @@ export type OwnersList = {|
213214
owners: Array<SerializedElement> | null,
214215
|};
215216

216-
// For now, let's only support a hard-coded set of plugins.
217-
type PluginType = 'stylex';
218-
219-
export type Plugin = {
220-
type: PluginType,
221-
data: any,
222-
};
223-
224217
export type InspectedElement = {|
225218
id: number,
226219

@@ -274,8 +267,8 @@ export type InspectedElement = {|
274267
rendererPackageName: string | null,
275268
rendererVersion: string | null,
276269

277-
// Array of UI plugins/visualizations for the inspected element.
278-
plugins: Array<Plugin>,
270+
// UI plugins/visualizations for the inspected element.
271+
plugins: Plugins,
279272
|};
280273

281274
export const InspectElementErrorType = 'error';

packages/react-devtools-shared/src/devtools/views/Components/InspectedElementStyleXPlugin.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import KeyValue from './KeyValue';
1212
import Store from '../../store';
1313
import sharedStyles from './InspectedElementSharedStyles.css';
1414
import styles from './InspectedElementStyleXPlugin.css';
15+
import {enableStyleXFeatures} from 'react-devtools-feature-flags';
1516

1617
import type {InspectedElement} from './types';
1718
import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
@@ -30,14 +31,16 @@ export default function InspectedElementStyleXPlugin({
3031
inspectedElement,
3132
store,
3233
}: Props) {
33-
const styleXPlugin = inspectedElement.plugins.find(
34-
({type}) => type === 'stylex',
35-
);
36-
if (styleXPlugin == null || styleXPlugin.data == null) {
34+
if (!enableStyleXFeatures) {
35+
return null;
36+
}
37+
38+
const styleXPlugin = inspectedElement.plugins.stylex;
39+
if (styleXPlugin == null) {
3740
return null;
3841
}
3942

40-
const {resolvedStyles, sources} = styleXPlugin.data;
43+
const {resolvedStyles, sources} = styleXPlugin;
4144

4245
return (
4346
<div className={sharedStyles.InspectedElementTree}>

packages/react-devtools-shared/src/devtools/views/Components/types.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
Dehydrated,
1313
Unserializable,
1414
} from 'react-devtools-shared/src/hydration';
15-
import type {ElementType} from 'react-devtools-shared/src/types';
15+
import type {ElementType, Plugins} from 'react-devtools-shared/src/types';
1616

1717
// Each element on the frontend corresponds to a Fiber on the backend.
1818
// Some of its information (e.g. id, type, displayName) come from the backend.
@@ -64,14 +64,6 @@ export type InspectedElementResponseType =
6464
| 'no-change'
6565
| 'not-found';
6666

67-
// For now, let's only support a hard-coded set of plugins.
68-
type PluginType = 'stylex';
69-
70-
export type Plugin = {
71-
type: PluginType,
72-
data: any,
73-
};
74-
7567
export type InspectedElement = {|
7668
id: number,
7769

@@ -123,8 +115,8 @@ export type InspectedElement = {|
123115
rendererPackageName: string | null,
124116
rendererVersion: string | null,
125117

126-
// Array of UI plugins/visualizations for the inspected element.
127-
plugins: Array<Plugin>,
118+
// UI plugins/visualizations for the inspected element.
119+
plugins: Plugins,
128120
|};
129121

130122
// TODO: Add profiling type

packages/react-devtools-shared/src/types.js

+9
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,12 @@ export type LRUCache<K, V> = {|
9191
reset: () => void,
9292
set: (key: K, value: V) => void,
9393
|};
94+
95+
export type StyleXPlugin = {|
96+
sources: Array<string>,
97+
resolvedStyles: Object,
98+
|};
99+
100+
export type Plugins = {|
101+
stylex: StyleXPlugin | null,
102+
|};

0 commit comments

Comments
 (0)