bippy 0.6.1-dev.61475ed → 0.6.1-dev.879f777
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +177 -500
- package/dist/core.cjs +1 -1
- package/dist/core.d.cts +45 -105
- package/dist/core.d.ts +45 -105
- package/dist/core.js +1 -1
- package/dist/core2.cjs +9 -0
- package/dist/core2.d.cts +11 -3
- package/dist/core2.d.ts +11 -3
- package/dist/core2.js +9 -0
- package/dist/errors.d.cts +410 -0
- package/dist/errors.d.ts +410 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +14 -3
- package/dist/index.d.ts +14 -3
- package/dist/index.js +1 -1
- package/dist/install-hook-only.cjs +1 -1
- package/dist/install-hook-only.d.cts +9 -1
- package/dist/install-hook-only.d.ts +9 -1
- package/dist/install-hook-only.js +1 -1
- package/dist/rdt-hook.cjs +1 -1
- package/dist/rdt-hook.js +1 -1
- package/dist/source.cjs +14 -5
- package/dist/source.d.cts +86 -69
- package/dist/source.d.ts +86 -69
- package/dist/source.js +14 -5
- package/package.json +26 -17
- package/src/core.ts +340 -673
- package/src/errors.ts +34 -0
- package/src/index.ts +1 -0
- package/src/install-hook-only.ts +2 -2
- package/src/rdt-hook.ts +160 -161
- package/src/react-internals/generated/react-work-tags.ts +318 -0
- package/src/react-internals/index.ts +67 -0
- package/src/react-internals/semver.ts +80 -0
- package/src/react-internals/types.ts +186 -0
- package/src/react.ts +76 -0
- package/src/source/constants.ts +1 -1
- package/src/source/error-stack.ts +11 -0
- package/src/source/get-display-name-from-source.ts +44 -40
- package/src/source/get-source.ts +43 -26
- package/src/source/index.ts +11 -1
- package/src/source/inspect-hooks.ts +220 -207
- package/src/source/owner-stack.ts +119 -174
- package/src/source/parse-debug-stack.ts +4 -4
- package/src/source/parse-hook-names.ts +14 -53
- package/src/source/parse-stack.ts +14 -31
- package/src/source/renderer-dispatchers.ts +30 -0
- package/src/source/symbolication.ts +709 -120
- package/dist/get-source.cjs +0 -19
- package/dist/get-source.js +0 -19
- package/dist/index.iife.js +0 -9
- package/dist/install-hook-only.iife.js +0 -9
- package/dist/react-refresh.cjs +0 -9
- package/dist/react-refresh.d.cts +0 -66
- package/dist/react-refresh.d.ts +0 -66
- package/dist/react-refresh.js +0 -9
- package/dist/unsubscribe.d.cts +0 -298
- package/dist/unsubscribe.d.ts +0 -298
- package/src/react-refresh/constants.ts +0 -9
- package/src/react-refresh/detect-hmr-transport.ts +0 -33
- package/src/react-refresh/index.ts +0 -173
- package/src/react-refresh/metro-hmr-transport.ts +0 -188
- package/src/react-refresh/next-webpack-hmr-transport.ts +0 -72
- package/src/react-refresh/normalize-hmr-file-path.ts +0 -24
- package/src/react-refresh/types.ts +0 -7
- package/src/react-refresh/vite-hmr-transport.ts +0 -116
- package/src/types.ts +0 -438
- package/src/unsubscribe.ts +0 -17
package/README.md
CHANGED
|
@@ -1,655 +1,332 @@
|
|
|
1
|
-
>
|
|
2
|
-
> ⚠️⚠️⚠️ **this project may break production apps and cause unexpected behavior** ⚠️⚠️⚠️
|
|
3
|
-
>
|
|
4
|
-
> this project uses react internals, which can change at any time. we don't recommend depending on internals unless you really, _really_ have to. by proceeding, you acknowledge the risk of breaking your own code or apps that use your code.
|
|
5
|
-
|
|
6
|
-
# <img src="https://github.com/aidenybai/bippy/blob/main/.github/public/bippy.png?raw=true" width="60" align="center" /> bippy
|
|
1
|
+
<h1><img src="./.github/public/bippy.png" width="48" alt="" align="middle" /> bippy</h1>
|
|
7
2
|
|
|
8
3
|
[](https://npmjs.com/package/bippy)
|
|
9
4
|
[](https://npmjs.com/package/bippy)
|
|
10
5
|
|
|
11
|
-
bippy
|
|
12
|
-
|
|
13
|
-
by default, you cannot access react internals. bippy bypasses this by “pretending” to be react devtools, giving you access to the fiber tree and other internals.
|
|
14
|
-
|
|
15
|
-
- works outside of react: no react code modification needed
|
|
16
|
-
- utility functions that work across modern react (v17-19)
|
|
17
|
-
- no prior react source code knowledge required
|
|
18
|
-
|
|
19
|
-
```jsx
|
|
20
|
-
import { instrument, traverseFiber } from "bippy"; // must be imported BEFORE react
|
|
21
|
-
|
|
22
|
-
instrument({
|
|
23
|
-
onCommitFiberRoot(rendererID, root) {
|
|
24
|
-
traverseFiber(root.current, (fiber) => {
|
|
25
|
-
// prints every fiber in the current React tree
|
|
26
|
-
console.log("fiber:", fiber);
|
|
27
|
-
});
|
|
28
|
-
},
|
|
29
|
-
});
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
## how it works & motivation
|
|
33
|
-
|
|
34
|
-
bippy allows you to **access** and **use** react fibers **outside** of react components.
|
|
35
|
-
|
|
36
|
-
a react fiber is a “unit of execution.” this means react will do something based on the data in a fiber. each fiber either represents a composite (function/class component) or a host (dom element).
|
|
37
|
-
|
|
38
|
-
> here is a [live visualization](https://jser.pro/ddir/rie?reactVersion=18.3.1&snippetKey=hq8jm2ylzb9u8eh468) of what the fiber tree looks like, and here is a [deep dive article](https://jser.dev/2023-07-18-how-react-rerenders/).
|
|
39
|
-
|
|
40
|
-
fibers are useful because they contain information about the react app (component props, state, contexts, etc.). a simplified version of a fiber looks roughly like this:
|
|
41
|
-
|
|
42
|
-
```typescript
|
|
43
|
-
interface Fiber {
|
|
44
|
-
// component type (function/class)
|
|
45
|
-
type: any;
|
|
46
|
-
|
|
47
|
-
child: Fiber | null;
|
|
48
|
-
sibling: Fiber | null;
|
|
49
|
-
|
|
50
|
-
// stateNode is the host fiber (e.g. DOM element)
|
|
51
|
-
stateNode: Node | null;
|
|
52
|
-
|
|
53
|
-
// parent fiber
|
|
54
|
-
return: Fiber | null;
|
|
55
|
-
|
|
56
|
-
// the previous or current version of the fiber
|
|
57
|
-
alternate: Fiber | null;
|
|
58
|
-
|
|
59
|
-
// saved props input
|
|
60
|
-
memoizedProps: any;
|
|
61
|
-
|
|
62
|
-
// state (useState, useReducer, useSES, etc.)
|
|
63
|
-
memoizedState: any;
|
|
64
|
-
|
|
65
|
-
// contexts (useContext)
|
|
66
|
-
dependencies: Dependencies | null;
|
|
67
|
-
|
|
68
|
-
// effects (useEffect, useLayoutEffect, etc.)
|
|
69
|
-
updateQueue: any;
|
|
70
|
-
}
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
here, the `child`, `sibling`, and `return` properties are pointers to other fibers in the tree.
|
|
74
|
-
|
|
75
|
-
additionally, `memoizedProps`, `memoizedState`, and `dependencies` are the fiber's props, state, and contexts.
|
|
76
|
-
|
|
77
|
-
while all of the information is there, it's awkward to work with, and changes frequently across different versions of react. bippy simplifies this by providing utility functions like:
|
|
78
|
-
|
|
79
|
-
- `traverseRenderedFibers` to detect renders and `traverseFiber` to traverse the overall fiber tree
|
|
80
|
-
- _(instead of `child`, `sibling`, and `return` pointers)_
|
|
81
|
-
- `traverseProps`, `traverseState`, and `traverseContexts` to traverse the fiber's props, state, and contexts
|
|
82
|
-
- _(instead of `memoizedProps`, `memoizedState`, and `dependencies`)_
|
|
83
|
-
|
|
84
|
-
however, react doesn't expose fibers to you directly. so, we have to hack our way around to access them.
|
|
85
|
-
|
|
86
|
-
luckily, react [reads from a property](https://github.com/facebook/react/blob/6a4b46cd70d2672bc4be59dcb5b8dede22ed0cef/packages/react-reconciler/src/ReactFiberDevToolsHook.js#L48) in the window object: `window.__REACT_DEVTOOLS_GLOBAL_HOOK__` and runs handlers on it when certain events happen. this property must exist before react's bundle is executed. this is intended for react devtools, but we can use it to our advantage.
|
|
87
|
-
|
|
88
|
-
here's what it roughly looks like:
|
|
89
|
-
|
|
90
|
-
```typescript
|
|
91
|
-
interface __REACT_DEVTOOLS_GLOBAL_HOOK__ {
|
|
92
|
-
// list of renderers (react-dom, react-native, etc.)
|
|
93
|
-
renderers: Map<RendererID, reactRenderer>;
|
|
94
|
-
|
|
95
|
-
// called when react has rendered everything for an update and the fiber tree is fully built and ready to
|
|
96
|
-
// apply changes to the host tree (e.g. DOM mutations)
|
|
97
|
-
onCommitFiberRoot: (rendererID: RendererID, root: FiberRoot, commitPriority?: number) => void;
|
|
98
|
-
|
|
99
|
-
// called when effects run
|
|
100
|
-
onPostCommitFiberRoot: (rendererID: RendererID, root: FiberRoot) => void;
|
|
101
|
-
|
|
102
|
-
// called when a specific fiber unmounts
|
|
103
|
-
onCommitFiberUnmount: (rendererID: RendererID, fiber: Fiber) => void;
|
|
104
|
-
}
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
bippy works by monkey-patching `window.__REACT_DEVTOOLS_GLOBAL_HOOK__` with our own custom handlers. bippy simplifies this by providing utility functions like:
|
|
6
|
+
bippy hacks into React internals.
|
|
108
7
|
|
|
109
|
-
|
|
110
|
-
- _(instead of directly mutating `onCommitFiberRoot`, …)_
|
|
111
|
-
- `traverseRenderedFibers` to traverse the fiber tree and determine which fibers have actually rendered
|
|
112
|
-
- _(instead of `child`, `sibling`, and `return` pointers)_
|
|
113
|
-
- `traverseFiber` to traverse the fiber tree, regardless of whether it has rendered
|
|
114
|
-
- _(instead of `child`, `sibling`, and `return` pointers)_
|
|
115
|
-
- `setFiberId` / `getFiberId` to set and get a fiber's id
|
|
116
|
-
- _(instead of anonymous fibers with no identity)_
|
|
8
|
+
React normally keeps its Fiber tree out of reach. bippy gets you in, so you can inspect components, track renders, and access the renderer directly.
|
|
117
9
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
10
|
+
> [!WARNING]
|
|
11
|
+
> ⚠️⚠️⚠️ **This project may break production apps and cause unexpected behavior.** ⚠️⚠️⚠️
|
|
12
|
+
>
|
|
13
|
+
> This project uses React internals, which can change at any time. We don't recommend depending on internals unless you really, _really_ have to. By proceeding, you acknowledge the risk of breaking your own code or apps that use your code.
|
|
14
|
+
|
|
15
|
+
## Table of contents
|
|
16
|
+
|
|
17
|
+
- [Install bippy](#install-bippy)
|
|
18
|
+
- [Next.js](#nextjs)
|
|
19
|
+
- [Vite](#vite)
|
|
20
|
+
- [React integration](#react-integration)
|
|
21
|
+
- [`useFiber`](#usefiber)
|
|
22
|
+
- [Instrumentation](#instrumentation)
|
|
23
|
+
- [`instrument`](#instrument)
|
|
24
|
+
- [`getRDTHook`](#getrdthook)
|
|
25
|
+
- [Fiber traversal](#fiber-traversal)
|
|
26
|
+
- [`traverseRenderedFibers`](#traverserenderedfibers)
|
|
27
|
+
- [`traverseFiber`](#traversefiber)
|
|
28
|
+
- [`didFiberRender` and `didFiberCommit`](#didfiberrender-and-didfibercommit)
|
|
29
|
+
- [Fiber inspection](#fiber-inspection)
|
|
30
|
+
- [`setFiberId` and `getFiberId`](#setfiberid-and-getfiberid)
|
|
31
|
+
- [Classification helpers](#classification-helpers)
|
|
32
|
+
- [`getDisplayName` and `getType`](#getdisplayname-and-gettype)
|
|
33
|
+
- [`getFiber`](#getfiber)
|
|
34
|
+
- [`getLatestFiber`](#getlatestfiber)
|
|
35
|
+
- [`getRenderer`](#getrenderer)
|
|
36
|
+
- [React internals](#react-internals)
|
|
37
|
+
- [Source inspection](#source-inspection)
|
|
38
|
+
- [`getSource`](#getsource)
|
|
39
|
+
- [`getOwnerStack` and `getParentStack`](#getownerstack-and-getparentstack)
|
|
40
|
+
- [Acknowledgements](#acknowledgements)
|
|
41
|
+
|
|
42
|
+
## Install bippy
|
|
43
|
+
|
|
44
|
+
Install bippy:
|
|
123
45
|
|
|
124
46
|
```shell
|
|
125
47
|
npm install bippy
|
|
126
48
|
```
|
|
127
49
|
|
|
128
|
-
|
|
50
|
+
Import bippy before React or any React renderer.
|
|
129
51
|
|
|
130
|
-
###
|
|
52
|
+
### Next.js
|
|
131
53
|
|
|
132
|
-
|
|
54
|
+
Next.js 15.3 and later can load bippy through [`instrumentation-client.ts`](https://nextjs.org/docs/app/api-reference/file-conventions/instrumentation-client). Create the file at the project root or in `src`:
|
|
133
55
|
|
|
134
56
|
```typescript
|
|
135
|
-
// instrumentation-client.ts
|
|
136
57
|
import "bippy";
|
|
137
58
|
```
|
|
138
59
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
### vite
|
|
60
|
+
### Vite
|
|
142
61
|
|
|
143
|
-
|
|
62
|
+
Import bippy at the top of your Vite entry point, before any React imports:
|
|
144
63
|
|
|
145
64
|
```typescript
|
|
146
|
-
// src/main.tsx
|
|
147
65
|
import "bippy";
|
|
148
66
|
import { StrictMode } from "react";
|
|
149
67
|
import { createRoot } from "react-dom/client";
|
|
150
|
-
|
|
151
|
-
// ... rest of your code
|
|
152
68
|
```
|
|
153
69
|
|
|
154
|
-
|
|
70
|
+
## React integration
|
|
155
71
|
|
|
156
|
-
|
|
72
|
+
### `useFiber`
|
|
157
73
|
|
|
158
|
-
|
|
159
|
-
> import "bippy/install-hook-only"; // only installs the hook
|
|
160
|
-
> import { getRDTHook, traverseFiber } from "bippy/core"; // import only what you need
|
|
161
|
-
> import * as React from "react"; // import react AFTER the hook is installed
|
|
162
|
-
>
|
|
163
|
-
> const hook = getRDTHook();
|
|
164
|
-
> // define your own utilities or use only specific ones
|
|
165
|
-
> ```
|
|
74
|
+
Returns the calling component’s Fiber. During server rendering it returns `undefined` because there is no client Fiber for the component.
|
|
166
75
|
|
|
167
|
-
|
|
76
|
+
```tsx
|
|
77
|
+
import { useFiber } from "bippy";
|
|
168
78
|
|
|
169
|
-
|
|
79
|
+
const Component = () => {
|
|
80
|
+
const fiber = useFiber();
|
|
81
|
+
console.log(fiber?.type);
|
|
82
|
+
return null;
|
|
83
|
+
};
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Instrumentation
|
|
87
|
+
|
|
88
|
+
Listen for React lifecycle events with `instrument`.
|
|
89
|
+
|
|
90
|
+
### `instrument`
|
|
91
|
+
|
|
92
|
+
Registers lifecycle handlers and returns an unsubscribe function.
|
|
170
93
|
|
|
171
|
-
|
|
94
|
+
Available handlers include:
|
|
172
95
|
|
|
173
|
-
|
|
96
|
+
- `onActive`: runs when instrumentation becomes active
|
|
97
|
+
- `onScheduleFiberRoot`: runs when React schedules a root
|
|
98
|
+
- `onCommitFiberRoot`: runs when React commits a root
|
|
99
|
+
- `onPostCommitFiberRoot`: runs after commit effects
|
|
100
|
+
- `onCommitFiberUnmount`: runs when React unmounts a Fiber
|
|
174
101
|
|
|
175
102
|
```typescript
|
|
176
|
-
import { instrument } from "bippy";
|
|
103
|
+
import { instrument } from "bippy";
|
|
177
104
|
import * as React from "react";
|
|
178
105
|
|
|
179
106
|
const unsubscribe = instrument({
|
|
180
107
|
onCommitFiberRoot(rendererID, root) {
|
|
181
|
-
console.log(
|
|
182
|
-
},
|
|
183
|
-
onPostCommitFiberRoot(rendererID, root) {
|
|
184
|
-
console.log("root with effects committed", root);
|
|
185
|
-
},
|
|
186
|
-
onCommitFiberUnmount(rendererID, fiber) {
|
|
187
|
-
console.log("fiber unmounted", fiber);
|
|
108
|
+
console.log(rendererID, root.current);
|
|
188
109
|
},
|
|
189
110
|
});
|
|
190
111
|
|
|
191
|
-
// later, stop listening (other instrument() subscribers keep working)
|
|
192
112
|
unsubscribe();
|
|
193
113
|
```
|
|
194
114
|
|
|
195
|
-
|
|
115
|
+
Call the returned function to unsubscribe those handlers.
|
|
116
|
+
|
|
117
|
+
### `getRDTHook`
|
|
196
118
|
|
|
197
|
-
|
|
119
|
+
Returns `globalThis.__REACT_DEVTOOLS_GLOBAL_HOOK__`. Use it when an integration needs the complete renderer registry or another low-level hook capability.
|
|
198
120
|
|
|
199
121
|
```typescript
|
|
200
122
|
import { getRDTHook } from "bippy";
|
|
201
123
|
|
|
202
124
|
const hook = getRDTHook();
|
|
203
|
-
console.log(hook);
|
|
125
|
+
console.log(hook.renderers);
|
|
204
126
|
```
|
|
205
127
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
not every fiber in the fiber tree renders. `traverseRenderedFibers` allows you to traverse the fiber tree and determine which fibers have actually rendered.
|
|
209
|
-
|
|
210
|
-
```typescript
|
|
211
|
-
import { instrument, traverseRenderedFibers } from "bippy"; // must be imported BEFORE react
|
|
212
|
-
import * as React from "react";
|
|
128
|
+
## Fiber traversal
|
|
213
129
|
|
|
214
|
-
|
|
215
|
-
onCommitFiberRoot(rendererID, root) {
|
|
216
|
-
traverseRenderedFibers(root, (fiber) => {
|
|
217
|
-
console.log("fiber rendered", fiber);
|
|
218
|
-
});
|
|
219
|
-
},
|
|
220
|
-
});
|
|
221
|
-
```
|
|
130
|
+
Traversal helpers walk a complete Fiber tree or select the Fibers involved in a commit.
|
|
222
131
|
|
|
223
|
-
###
|
|
132
|
+
### `traverseRenderedFibers`
|
|
224
133
|
|
|
225
|
-
|
|
134
|
+
Visits Fibers that mounted, updated, or unmounted in a commit. The callback receives the Fiber and its `mount`, `update`, or `unmount` phase.
|
|
226
135
|
|
|
227
136
|
```typescript
|
|
228
|
-
import { instrument,
|
|
137
|
+
import { instrument, traverseRenderedFibers } from "bippy";
|
|
229
138
|
import * as React from "react";
|
|
230
139
|
|
|
231
140
|
instrument({
|
|
232
141
|
onCommitFiberRoot(rendererID, root) {
|
|
233
|
-
|
|
234
|
-
console.log(fiber);
|
|
142
|
+
traverseRenderedFibers(root, (fiber, phase) => {
|
|
143
|
+
console.log(rendererID, phase, fiber);
|
|
235
144
|
});
|
|
236
145
|
},
|
|
237
146
|
});
|
|
238
147
|
```
|
|
239
148
|
|
|
240
|
-
|
|
149
|
+
Call it with the same root across commits so bippy can compare the current and previous trees.
|
|
241
150
|
|
|
242
|
-
|
|
151
|
+
### `traverseFiber`
|
|
243
152
|
|
|
244
|
-
|
|
245
|
-
import { traverseProps } from "bippy";
|
|
153
|
+
Walks down from a Fiber and calls a selector for each node. Return `true` to stop and return the selected Fiber. Pass `true` as the third argument to walk toward the root instead.
|
|
246
154
|
|
|
247
|
-
|
|
155
|
+
```typescript
|
|
156
|
+
import { isHostFiber, traverseFiber } from "bippy";
|
|
248
157
|
|
|
249
|
-
|
|
250
|
-
|
|
158
|
+
const buttonFiber = traverseFiber(root.current, (fiber) => {
|
|
159
|
+
return isHostFiber(fiber) && fiber.type === "button";
|
|
251
160
|
});
|
|
252
161
|
```
|
|
253
162
|
|
|
254
|
-
|
|
163
|
+
The selector can also return a promise. In that case, `traverseFiber` returns a promise for the selected Fiber.
|
|
255
164
|
|
|
256
|
-
|
|
165
|
+
### `didFiberRender` and `didFiberCommit`
|
|
257
166
|
|
|
258
|
-
|
|
259
|
-
import { traverseState } from "bippy";
|
|
167
|
+
Return whether a Fiber has rendered or committed. Use `traverseRenderedFibers` to inspect changes from a specific commit.
|
|
260
168
|
|
|
261
|
-
|
|
169
|
+
```typescript
|
|
170
|
+
import { didFiberCommit, didFiberRender } from "bippy";
|
|
262
171
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
});
|
|
172
|
+
console.log(didFiberRender(fiber));
|
|
173
|
+
console.log(didFiberCommit(fiber));
|
|
266
174
|
```
|
|
267
175
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
traverses the contexts (`useContext`) of a fiber.
|
|
271
|
-
|
|
272
|
-
```typescript
|
|
273
|
-
import { traverseContexts } from "bippy";
|
|
274
|
-
|
|
275
|
-
// ...
|
|
176
|
+
## Fiber inspection
|
|
276
177
|
|
|
277
|
-
|
|
278
|
-
console.log(next, prev);
|
|
279
|
-
});
|
|
280
|
-
```
|
|
178
|
+
Inspection helpers identify Fibers and read their component, host instance, and renderer metadata.
|
|
281
179
|
|
|
282
|
-
### setFiberId
|
|
180
|
+
### `setFiberId` and `getFiberId`
|
|
283
181
|
|
|
284
|
-
|
|
182
|
+
Assign and read a stable numeric identity across Fiber updates. `getFiberId` creates an identity when one does not exist.
|
|
285
183
|
|
|
286
184
|
```typescript
|
|
287
|
-
import {
|
|
185
|
+
import { getFiberId, setFiberId } from "bippy";
|
|
288
186
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
setFiberId(fiber);
|
|
292
|
-
console.log("unique id for fiber:", getFiberId(fiber));
|
|
187
|
+
setFiberId(fiber, 123);
|
|
188
|
+
console.log(getFiberId(fiber));
|
|
293
189
|
```
|
|
294
190
|
|
|
295
|
-
###
|
|
191
|
+
### Classification helpers
|
|
192
|
+
|
|
193
|
+
Use these predicates to narrow an unknown value or Fiber before reading renderer-specific fields:
|
|
296
194
|
|
|
297
|
-
|
|
195
|
+
| Helper | Result |
|
|
196
|
+
| ------------------ | ------------------------------------------------------- |
|
|
197
|
+
| `isFiber` | Performs a fast check for a Fiber-like object |
|
|
198
|
+
| `isValidFiber` | Checks the core fields required by a Fiber |
|
|
199
|
+
| `isHostFiber` | Narrows a Fiber to a host Fiber |
|
|
200
|
+
| `isCompositeFiber` | Finds function, class, memo, and other composite Fibers |
|
|
201
|
+
| `hasMemoCache` | Detects React Compiler memo cache data |
|
|
298
202
|
|
|
299
203
|
```typescript
|
|
300
204
|
import { isHostFiber } from "bippy";
|
|
301
205
|
|
|
302
206
|
if (isHostFiber(fiber)) {
|
|
303
|
-
console.log(
|
|
207
|
+
console.log(fiber.stateNode);
|
|
304
208
|
}
|
|
305
209
|
```
|
|
306
210
|
|
|
307
|
-
###
|
|
211
|
+
### `getDisplayName` and `getType`
|
|
308
212
|
|
|
309
|
-
|
|
213
|
+
`getDisplayName` reads a component name from a Fiber type. `getType` unwraps memo and forward-ref wrappers to return the underlying component definition.
|
|
310
214
|
|
|
311
215
|
```typescript
|
|
312
|
-
import {
|
|
216
|
+
import { getDisplayName, getType } from "bippy";
|
|
313
217
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
}
|
|
218
|
+
console.log(getDisplayName(fiber.type));
|
|
219
|
+
console.log(getType(fiber.type));
|
|
317
220
|
```
|
|
318
221
|
|
|
319
|
-
###
|
|
222
|
+
### `getFiber`
|
|
320
223
|
|
|
321
|
-
|
|
224
|
+
Returns the Fiber associated with a renderer host instance, such as a DOM element. The result is `null` when no registered renderer recognizes the instance.
|
|
322
225
|
|
|
323
226
|
```typescript
|
|
324
|
-
import {
|
|
325
|
-
|
|
326
|
-
console.log(getDisplayName(fiber));
|
|
327
|
-
```
|
|
328
|
-
|
|
329
|
-
### getType
|
|
330
|
-
|
|
331
|
-
returns the underlying type (the component definition) for a given fiber. for example, this could be a function component or class component.
|
|
332
|
-
|
|
333
|
-
```jsx
|
|
334
|
-
import { getType } from "bippy";
|
|
335
|
-
import { memo } from "react";
|
|
336
|
-
|
|
337
|
-
const RealComponent = () => {
|
|
338
|
-
return <div>hello</div>;
|
|
339
|
-
};
|
|
340
|
-
const MemoizedComponent = memo(() => {
|
|
341
|
-
return <div>hello</div>;
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
console.log(getType(fiberForMemoizedComponent) === RealComponent);
|
|
345
|
-
```
|
|
346
|
-
|
|
347
|
-
### getNearestHostFiber / getNearestHostFibers
|
|
348
|
-
|
|
349
|
-
`getNearestHostFiber` returns the closest host fiber above or below a given fiber. `getNearestHostFibers` returns all host fibers associated with the provided fiber and its subtree.
|
|
350
|
-
|
|
351
|
-
```jsx
|
|
352
|
-
import { getNearestHostFiber, getNearestHostFibers } from "bippy";
|
|
353
|
-
|
|
354
|
-
// ...
|
|
355
|
-
|
|
356
|
-
function Component() {
|
|
357
|
-
return (
|
|
358
|
-
<>
|
|
359
|
-
<div>hello</div>
|
|
360
|
-
<div>world</div>
|
|
361
|
-
</>
|
|
362
|
-
);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
console.log(getNearestHostFiber(fiberForComponent)); // <div>hello</div>
|
|
366
|
-
console.log(getNearestHostFibers(fiberForComponent)); // [<div>hello</div>, <div>world</div>]
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
### getTimings
|
|
370
|
-
|
|
371
|
-
returns the self and total render times for the fiber.
|
|
227
|
+
import { getFiber } from "bippy";
|
|
372
228
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
if (fiber.actualDuration !== undefined) {
|
|
376
|
-
const { selfTime, totalTime } = getTimings(fiber);
|
|
377
|
-
console.log(selfTime, totalTime);
|
|
378
|
-
}
|
|
229
|
+
const element = document.querySelector("button");
|
|
230
|
+
const fiber = getFiber(element);
|
|
379
231
|
```
|
|
380
232
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
returns an array representing the stack of fibers from the current fiber up to the root.
|
|
384
|
-
|
|
385
|
-
```typescript
|
|
386
|
-
[fiber, fiber.return, fiber.return.return, ...]
|
|
387
|
-
```
|
|
233
|
+
`getFiberFromHostInstance` remains available as an alias.
|
|
388
234
|
|
|
389
|
-
###
|
|
235
|
+
### `getLatestFiber`
|
|
390
236
|
|
|
391
|
-
|
|
237
|
+
Returns the latest version of a Fiber. Use it when you retain a Fiber across renders.
|
|
392
238
|
|
|
393
239
|
```typescript
|
|
394
|
-
import {
|
|
240
|
+
import { getFiber, getLatestFiber } from "bippy";
|
|
395
241
|
|
|
396
|
-
|
|
242
|
+
const fiber = getFiber(document.body);
|
|
243
|
+
const latestFiber = fiber ? getLatestFiber(fiber) : null;
|
|
397
244
|
```
|
|
398
245
|
|
|
399
|
-
###
|
|
246
|
+
### `getRenderer`
|
|
400
247
|
|
|
401
|
-
|
|
248
|
+
Returns the React renderer that owns a Fiber, or `null` when the renderer is unavailable.
|
|
402
249
|
|
|
403
250
|
```typescript
|
|
404
|
-
import {
|
|
251
|
+
import { getRenderer } from "bippy";
|
|
405
252
|
|
|
406
|
-
|
|
253
|
+
const renderer = getRenderer(fiber);
|
|
254
|
+
renderer?.overrideProps?.(fiber, ["title"], "new title");
|
|
255
|
+
renderer?.scheduleUpdate?.(fiber);
|
|
407
256
|
```
|
|
408
257
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
returns the fiber associated with a given host instance (e.g., a DOM element).
|
|
412
|
-
|
|
413
|
-
```typescript
|
|
414
|
-
import { getFiberFromHostInstance } from "bippy";
|
|
415
|
-
|
|
416
|
-
const fiber = getFiberFromHostInstance(document.querySelector("div"));
|
|
417
|
-
console.log(fiber);
|
|
418
|
-
```
|
|
258
|
+
Renderer capabilities are optional and vary by renderer version.
|
|
419
259
|
|
|
420
|
-
###
|
|
260
|
+
### React internals
|
|
421
261
|
|
|
422
|
-
|
|
262
|
+
The main `bippy` entry point exports the complete internals model used by its APIs. This includes Fiber, root, renderer, dispatcher, work-tag, flag, symbol, and build-type definitions.
|
|
423
263
|
|
|
424
264
|
```typescript
|
|
425
|
-
import {
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
265
|
+
import {
|
|
266
|
+
MutationMask,
|
|
267
|
+
ReactBuildType,
|
|
268
|
+
ReactFiberFlags,
|
|
269
|
+
ReactSymbols,
|
|
270
|
+
getReactWorkTags,
|
|
271
|
+
getReactWorkTagsForFiber,
|
|
272
|
+
getReactWorkTagsForRenderer,
|
|
273
|
+
} from "bippy";
|
|
274
|
+
import type {
|
|
275
|
+
Fiber,
|
|
276
|
+
FiberRoot,
|
|
277
|
+
ReactDevToolsGlobalHook,
|
|
278
|
+
ReactRenderer,
|
|
279
|
+
RendererDispatcherRef,
|
|
280
|
+
} from "bippy";
|
|
429
281
|
```
|
|
430
282
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
overrides component props at runtime by modifying the fiber's props.
|
|
283
|
+
These definitions follow React’s private implementation and may change between React versions.
|
|
434
284
|
|
|
435
|
-
|
|
436
|
-
import { overrideProps } from "bippy";
|
|
437
|
-
|
|
438
|
-
// override props on a fiber
|
|
439
|
-
overrideProps(fiber, {
|
|
440
|
-
title: "new title",
|
|
441
|
-
config: {
|
|
442
|
-
enabled: true,
|
|
443
|
-
count: 42,
|
|
444
|
-
},
|
|
445
|
-
});
|
|
446
|
-
```
|
|
285
|
+
## Source inspection
|
|
447
286
|
|
|
448
|
-
|
|
287
|
+
Source utilities resolve component locations, source maps, and component stacks. Import them from `bippy/source`.
|
|
449
288
|
|
|
450
|
-
###
|
|
289
|
+
### `getSource`
|
|
451
290
|
|
|
452
|
-
|
|
291
|
+
Returns the source location for a Fiber across DOM, native, terminal, canvas, PDF, and custom renderers.
|
|
453
292
|
|
|
454
293
|
```typescript
|
|
455
|
-
import {
|
|
456
|
-
|
|
457
|
-
// override the first hook (id: 0) with a new value
|
|
458
|
-
overrideHookState(fiber, 0, "new state value");
|
|
294
|
+
import { getSource } from "bippy/source";
|
|
459
295
|
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
user: {
|
|
463
|
-
name: "john",
|
|
464
|
-
age: 30,
|
|
465
|
-
},
|
|
466
|
-
});
|
|
296
|
+
const source = await getSource(fiber);
|
|
297
|
+
console.log(source);
|
|
467
298
|
```
|
|
468
299
|
|
|
469
|
-
|
|
300
|
+
Production builds may omit source information. Runtimes without `fetch` receive unsymbolicated locations.
|
|
470
301
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
overrides react context values at runtime by finding the appropriate context provider.
|
|
302
|
+
Pass a custom `SourceFetch` for packaged bundles, virtual filesystems, or renderer-specific URLs:
|
|
474
303
|
|
|
475
304
|
```typescript
|
|
476
|
-
import {
|
|
477
|
-
|
|
478
|
-
// override context value
|
|
479
|
-
overrideContext(fiber, MyContext, {
|
|
480
|
-
theme: "dark",
|
|
481
|
-
user: {
|
|
482
|
-
id: 123,
|
|
483
|
-
name: "jane",
|
|
484
|
-
},
|
|
485
|
-
});
|
|
486
|
-
|
|
487
|
-
// override with primitive value
|
|
488
|
-
overrideContext(fiber, ThemeContext, "dark");
|
|
489
|
-
```
|
|
490
|
-
|
|
491
|
-
the function traverses up the fiber tree to find the context provider matching the provided context type and overrides its value.
|
|
305
|
+
import { getSource, type SourceFetch } from "bippy/source";
|
|
492
306
|
|
|
493
|
-
|
|
307
|
+
const sourceFetch: SourceFetch = async (url, init) => {
|
|
308
|
+
const artifact = sourceArtifacts.get(url);
|
|
309
|
+
if (!artifact) return fetch(url, init);
|
|
494
310
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
// random fiber on the DOM
|
|
501
|
-
const hostFiber = getFiberFromHostInstance(document.querySelector("div"));
|
|
311
|
+
return new Response(artifact.content, {
|
|
312
|
+
headers: artifact.sourceMapUrl ? { SourceMap: artifact.sourceMapUrl } : undefined,
|
|
313
|
+
});
|
|
314
|
+
};
|
|
502
315
|
|
|
503
|
-
|
|
504
|
-
const compositeFiber = traverseFiber(
|
|
505
|
-
hostFiber,
|
|
506
|
-
(fiber) => {
|
|
507
|
-
if (isCompositeFiber(fiber)) {
|
|
508
|
-
return fiber;
|
|
509
|
-
}
|
|
510
|
-
},
|
|
511
|
-
true,
|
|
512
|
-
);
|
|
513
|
-
|
|
514
|
-
const source = await getSource(compositeFiber);
|
|
515
|
-
// {
|
|
516
|
-
// columnNumber: 12,
|
|
517
|
-
// fileName: 'path/to/file.tsx',
|
|
518
|
-
// lineNumber: 12,
|
|
519
|
-
// }
|
|
316
|
+
const source = await getSource(fiber, true, sourceFetch);
|
|
520
317
|
```
|
|
521
318
|
|
|
522
|
-
|
|
523
|
-
>
|
|
524
|
-
> - only available in dev mode
|
|
525
|
-
> - only works for composite fibers (function/class components)
|
|
526
|
-
> - captures the location where the component is _used_, not where it's _defined_
|
|
527
|
-
> - in react 18, resolves `_debugSource` directly (see [react#31981](https://github.com/facebook/react/issues/31981))
|
|
528
|
-
> - in react >18, `_debugSource` is not available for host fibers
|
|
529
|
-
|
|
530
|
-
### getOwnerStack / getParentStack
|
|
531
|
-
|
|
532
|
-
returns a symbolicated stack of components above a fiber.
|
|
533
|
-
|
|
534
|
-
`getOwnerStack` walks the chain of components that _created_ this fiber's JSX (react's `_debugOwner` chain), with exact creation-site locations on react 19, including server component owners. wrappers that merely render `{children}` don't appear. it automatically falls back to `getParentStack` when no usable owner frames exist (e.g. react <19).
|
|
319
|
+
### `getOwnerStack` and `getParentStack`
|
|
535
320
|
|
|
536
|
-
|
|
321
|
+
Both functions return symbolicated component stacks above a Fiber. `getOwnerStack` follows the components that created the Fiber’s JSX. `getParentStack` follows every ancestor in the Fiber return chain.
|
|
537
322
|
|
|
538
323
|
```typescript
|
|
539
324
|
import { getOwnerStack, getParentStack } from "bippy/source";
|
|
540
325
|
|
|
541
326
|
const ownerFrames = await getOwnerStack(fiber);
|
|
542
|
-
// [{ functionName: "Button", fileName: "src/button.tsx", lineNumber: 12, ... }, ...]
|
|
543
|
-
|
|
544
327
|
const parentFrames = await getParentStack(fiber);
|
|
545
|
-
// includes every wrapper between the fiber and the root
|
|
546
|
-
```
|
|
547
|
-
|
|
548
|
-
### instrumentReactRefresh
|
|
549
|
-
|
|
550
|
-
subscribes to fast refresh (HMR) updates from `bippy/react-refresh`. works with any bundler that uses react-refresh (vite, next.js webpack, next.js turbopack, metro) without bundler-specific code: bippy auto-detects the bundler's HMR transport and augments each update with the hot-updated source file paths.
|
|
551
|
-
|
|
552
|
-
the handler runs after react has re-rendered with the new component types, so `updatedFibers`/`staleFibers` are the mounted fibers matching the hot-swapped component types.
|
|
553
|
-
|
|
554
|
-
returns an unsubscribe function (a no-op during SSR, so no environment checks needed). the returned function is also a `Disposable`, so it works with `using`.
|
|
555
|
-
|
|
556
|
-
```typescript
|
|
557
|
-
import { instrumentReactRefresh } from "bippy/react-refresh";
|
|
558
|
-
import { getDisplayName } from "bippy";
|
|
559
|
-
|
|
560
|
-
const unsubscribe = instrumentReactRefresh({
|
|
561
|
-
onRefresh(update) {
|
|
562
|
-
for (const fiber of update.updatedFibers) {
|
|
563
|
-
console.log("hot updated:", getDisplayName(fiber.type));
|
|
564
|
-
}
|
|
565
|
-
console.log("changed files:", update.filePaths);
|
|
566
|
-
},
|
|
567
|
-
});
|
|
568
|
-
|
|
569
|
-
// later
|
|
570
|
-
unsubscribe();
|
|
571
328
|
```
|
|
572
329
|
|
|
573
|
-
##
|
|
574
|
-
|
|
575
|
-
here's a mini toy version of [`react-scan`](https://github.com/aidenybai/react-scan) that highlights renders in your app.
|
|
576
|
-
|
|
577
|
-
```javascript
|
|
578
|
-
import { instrument, getNearestHostFiber, traverseRenderedFibers } from "bippy"; // must be imported BEFORE react
|
|
579
|
-
|
|
580
|
-
const highlightFiber = (fiber) => {
|
|
581
|
-
if (!(fiber.stateNode instanceof HTMLElement)) return;
|
|
582
|
-
// fiber.stateNode is a DOM element
|
|
583
|
-
const rect = fiber.stateNode.getBoundingClientRect();
|
|
584
|
-
const highlight = document.createElement("div");
|
|
585
|
-
highlight.style.border = "1px solid red";
|
|
586
|
-
highlight.style.position = "fixed";
|
|
587
|
-
highlight.style.top = `${rect.top}px`;
|
|
588
|
-
highlight.style.left = `${rect.left}px`;
|
|
589
|
-
highlight.style.width = `${rect.width}px`;
|
|
590
|
-
highlight.style.height = `${rect.height}px`;
|
|
591
|
-
highlight.style.zIndex = "999999999";
|
|
592
|
-
document.documentElement.appendChild(highlight);
|
|
593
|
-
setTimeout(() => {
|
|
594
|
-
document.documentElement.removeChild(highlight);
|
|
595
|
-
}, 100);
|
|
596
|
-
};
|
|
597
|
-
|
|
598
|
-
/**
|
|
599
|
-
* `instrument` is a function that installs the react DevTools global
|
|
600
|
-
* hook and allows you to set up custom handlers for react fiber events.
|
|
601
|
-
*/
|
|
602
|
-
instrument({
|
|
603
|
-
/**
|
|
604
|
-
* `onCommitFiberRoot` is a handler that is called when react is
|
|
605
|
-
* ready to commit a fiber root. this means that react is has
|
|
606
|
-
* rendered your entire app and is ready to apply changes to
|
|
607
|
-
* the host tree (e.g. via DOM mutations).
|
|
608
|
-
*/
|
|
609
|
-
onCommitFiberRoot(rendererID, root) {
|
|
610
|
-
/**
|
|
611
|
-
* `traverseRenderedFibers` traverses the fiber tree and determines which
|
|
612
|
-
* fibers have actually rendered.
|
|
613
|
-
*
|
|
614
|
-
* A fiber tree contains many fibers that may have not rendered. this
|
|
615
|
-
* can be because it bailed out (e.g. `useMemo`) or because it wasn't
|
|
616
|
-
* actually rendered (if <Child> re-rendered, then <Parent> didn't
|
|
617
|
-
* actually render, but exists in the fiber tree).
|
|
618
|
-
*/
|
|
619
|
-
traverseRenderedFibers(root, (fiber) => {
|
|
620
|
-
/**
|
|
621
|
-
* `getNearestHostFiber` is a utility function that finds the
|
|
622
|
-
* nearest host fiber to a given fiber.
|
|
623
|
-
*
|
|
624
|
-
* a host fiber for `react-dom` is a fiber that has a DOM element
|
|
625
|
-
* as its `stateNode`.
|
|
626
|
-
*/
|
|
627
|
-
const hostFiber = getNearestHostFiber(fiber);
|
|
628
|
-
highlightFiber(hostFiber);
|
|
629
|
-
});
|
|
630
|
-
},
|
|
631
|
-
});
|
|
632
|
-
```
|
|
633
|
-
|
|
634
|
-
## glossary
|
|
635
|
-
|
|
636
|
-
- fiber: a “unit of execution” in react, representing a component or dom element
|
|
637
|
-
- commit: the process of applying changes to the host tree (e.g. DOM mutations)
|
|
638
|
-
- render: the process of building the fiber tree by executing component function/classes
|
|
639
|
-
- host tree: the tree of UI elements that react mutates (e.g. DOM elements)
|
|
640
|
-
- reconciler (or “renderer”): custom bindings for react, e.g. react-dom, react-native, react-three-fiber, etc to mutate the host tree
|
|
641
|
-
- `rendererID`: the id of the reconciler, starting at 1 (can be from multiple reconciler instances)
|
|
642
|
-
- `root`: a special `FiberRoot` type that contains the container fiber (the one you pass to `ReactDOM.createRoot`) in the `current` property
|
|
643
|
-
- `onCommitFiberRoot`: called when react is ready to commit a fiber root
|
|
644
|
-
- `onPostCommitFiberRoot`: called when react has committed a fiber root and effects have run
|
|
645
|
-
- `onCommitFiberUnmount`: called when a fiber unmounts
|
|
646
|
-
|
|
647
|
-
## misc
|
|
648
|
-
|
|
649
|
-
we initially created bippy for [react-scan](https://github.com/aidenybai/react-scan), which ships with safeguards so it only runs in development or error-guarded in production.
|
|
650
|
-
|
|
651
|
-
if you're seeking more robust solutions, you might consider [its-fine](https://github.com/pmndrs/its-fine) for accessing fibers within react using hooks, or [react-devtools-inline](https://www.npmjs.com/package/react-devtools-inline) for a headful interface.
|
|
652
|
-
|
|
653
|
-
if you plan to use this project beyond experimentation, please review [react-scan's source code](https://github.com/aidenybai/react-scan) to understand our safeguarding practices.
|
|
330
|
+
## Acknowledgements
|
|
654
331
|
|
|
655
332
|
the original bippy character is owned and created by [@dairyfreerice](https://www.instagram.com/dairyfreerice). this project is not related to the bippy brand, i just think the character is cute.
|