rerender-lens 0.2.0 → 0.3.0
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/CHANGELOG.md +69 -0
- package/README.md +146 -5
- package/dist/budget-COBu7jBU.d.cts +64 -0
- package/dist/budget-LkNjGtRc.d.ts +64 -0
- package/dist/cli.cjs +458 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +4 -0
- package/dist/cli.d.ts +4 -0
- package/dist/cli.js +452 -0
- package/dist/cli.js.map +1 -0
- package/dist/devtools-BHGUUo-p.d.ts +244 -0
- package/dist/devtools-DmhWiWcN.d.cts +244 -0
- package/dist/index.cjs +652 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +40 -336
- package/dist/index.d.ts +40 -336
- package/dist/index.js +636 -24
- package/dist/index.js.map +1 -1
- package/dist/notifiers-BGQtWKfX.d.cts +90 -0
- package/dist/notifiers-BjjGSHpp.d.ts +90 -0
- package/dist/playwright.cjs +196 -0
- package/dist/playwright.cjs.map +1 -0
- package/dist/playwright.d.cts +35 -0
- package/dist/playwright.d.ts +35 -0
- package/dist/playwright.js +184 -0
- package/dist/playwright.js.map +1 -0
- package/dist/relay.cjs +166 -0
- package/dist/relay.cjs.map +1 -0
- package/dist/relay.d.cts +41 -0
- package/dist/relay.d.ts +41 -0
- package/dist/relay.js +161 -0
- package/dist/relay.js.map +1 -0
- package/dist/rerender-lens.iife.js +1717 -0
- package/dist/setup.cjs +1510 -0
- package/dist/setup.cjs.map +1 -0
- package/dist/setup.d.cts +2 -0
- package/dist/setup.d.ts +2 -0
- package/dist/setup.js +1508 -0
- package/dist/setup.js.map +1 -0
- package/dist/types-BzUEVkxJ.d.cts +177 -0
- package/dist/types-BzUEVkxJ.d.ts +177 -0
- package/dist/vite.cjs +113 -0
- package/dist/vite.cjs.map +1 -0
- package/dist/vite.d.cts +84 -0
- package/dist/vite.d.ts +84 -0
- package/dist/vite.js +103 -0
- package/dist/vite.js.map +1 -0
- package/dist/vitest-setup.cjs +1299 -0
- package/dist/vitest-setup.cjs.map +1 -0
- package/dist/vitest-setup.d.cts +8 -0
- package/dist/vitest-setup.d.ts +8 -0
- package/dist/vitest-setup.js +1297 -0
- package/dist/vitest-setup.js.map +1 -0
- package/dist/vitest.cjs +1360 -0
- package/dist/vitest.cjs.map +1 -0
- package/dist/vitest.d.cts +52 -0
- package/dist/vitest.d.ts +52 -0
- package/dist/vitest.js +1351 -0
- package/dist/vitest.js.map +1 -0
- package/package.json +84 -4
- package/panel/panel.css +418 -0
- package/panel/panel.html +12 -0
- package/panel/panel.js +3325 -0
|
@@ -0,0 +1,1297 @@
|
|
|
1
|
+
import { afterAll, expect } from 'vitest';
|
|
2
|
+
import { appendFileSync } from 'fs';
|
|
3
|
+
import 'os';
|
|
4
|
+
import 'path';
|
|
5
|
+
|
|
6
|
+
// src/vitest-setup.ts
|
|
7
|
+
|
|
8
|
+
// src/diff.ts
|
|
9
|
+
var REACT_ELEMENT = /* @__PURE__ */ Symbol.for("react.element");
|
|
10
|
+
var REACT_TRANSITIONAL_ELEMENT = /* @__PURE__ */ Symbol.for("react.transitional.element");
|
|
11
|
+
function isReactElement(v) {
|
|
12
|
+
if (typeof v !== "object" || v === null) return false;
|
|
13
|
+
const t = v.$$typeof;
|
|
14
|
+
return t === REACT_ELEMENT || t === REACT_TRANSITIONAL_ELEMENT;
|
|
15
|
+
}
|
|
16
|
+
function isPlainObject(v) {
|
|
17
|
+
if (typeof v !== "object" || v === null) return false;
|
|
18
|
+
const proto = Object.getPrototypeOf(v);
|
|
19
|
+
return proto === Object.prototype || proto === null;
|
|
20
|
+
}
|
|
21
|
+
function remember(seen, a, b) {
|
|
22
|
+
let set = seen.get(a);
|
|
23
|
+
if (!set) {
|
|
24
|
+
set = /* @__PURE__ */ new Set();
|
|
25
|
+
seen.set(a, set);
|
|
26
|
+
}
|
|
27
|
+
if (set.has(b)) return true;
|
|
28
|
+
set.add(b);
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
function deepEqual(a, b, seen = /* @__PURE__ */ new Map()) {
|
|
32
|
+
if (Object.is(a, b)) return true;
|
|
33
|
+
if (typeof a !== typeof b) return false;
|
|
34
|
+
if (typeof a !== "object" || a === null || b === null) return false;
|
|
35
|
+
const objA = a;
|
|
36
|
+
const objB = b;
|
|
37
|
+
if (remember(seen, objA, objB)) return true;
|
|
38
|
+
if (a instanceof Date) return b instanceof Date && a.getTime() === b.getTime();
|
|
39
|
+
if (a instanceof RegExp) return b instanceof RegExp && a.source === b.source && a.flags === b.flags;
|
|
40
|
+
if (Array.isArray(a)) {
|
|
41
|
+
if (!Array.isArray(b) || a.length !== b.length) return false;
|
|
42
|
+
for (let i = 0; i < a.length; i++) if (!deepEqual(a[i], b[i], seen)) return false;
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
if (Array.isArray(b)) return false;
|
|
46
|
+
if (ArrayBuffer.isView(a)) {
|
|
47
|
+
if (!ArrayBuffer.isView(b) || a.constructor !== b.constructor) return false;
|
|
48
|
+
const ta = a;
|
|
49
|
+
const tb = b;
|
|
50
|
+
if (ta.length !== tb.length) return false;
|
|
51
|
+
for (let i = 0; i < ta.length; i++) if (ta[i] !== tb[i]) return false;
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
if (a instanceof Map) {
|
|
55
|
+
if (!(b instanceof Map) || a.size !== b.size) return false;
|
|
56
|
+
for (const [k, v] of a) {
|
|
57
|
+
if (!b.has(k) || !deepEqual(v, b.get(k), seen)) return false;
|
|
58
|
+
}
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
if (a instanceof Set) {
|
|
62
|
+
if (!(b instanceof Set) || a.size !== b.size) return false;
|
|
63
|
+
outer: for (const v of a) {
|
|
64
|
+
if (b.has(v)) continue;
|
|
65
|
+
for (const w of b) if (deepEqual(v, w, seen)) continue outer;
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
if (isReactElement(a)) {
|
|
71
|
+
if (!isReactElement(b)) return false;
|
|
72
|
+
return a.type === b.type && a.key === b.key && deepEqual(a.props, b.props, seen);
|
|
73
|
+
}
|
|
74
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
75
|
+
const ka = Object.keys(a);
|
|
76
|
+
const kb = Object.keys(b);
|
|
77
|
+
if (ka.length !== kb.length) return false;
|
|
78
|
+
for (const k of ka) {
|
|
79
|
+
if (!Object.prototype.hasOwnProperty.call(b, k)) return false;
|
|
80
|
+
if (!deepEqual(a[k], b[k], seen)) return false;
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
function classify(prev, next) {
|
|
87
|
+
if (typeof prev === "function" && typeof next === "function") {
|
|
88
|
+
return prev.name === next.name && prev.toString() === next.toString() ? "function" : "different";
|
|
89
|
+
}
|
|
90
|
+
if (isReactElement(prev) && isReactElement(next)) {
|
|
91
|
+
return deepEqual(prev, next) ? "element" : "different";
|
|
92
|
+
}
|
|
93
|
+
return deepEqual(prev, next) ? "deep-equal" : "different";
|
|
94
|
+
}
|
|
95
|
+
function joinPath(base, key) {
|
|
96
|
+
if (typeof key === "number") return `${base}[${key}]`;
|
|
97
|
+
if (base === "") return key;
|
|
98
|
+
return /^[A-Za-z_$][\w$]*$/.test(key) ? `${base}.${key}` : `${base}[${JSON.stringify(key)}]`;
|
|
99
|
+
}
|
|
100
|
+
function diffRecords(prev, next, basePath = "") {
|
|
101
|
+
const p = prev ?? {};
|
|
102
|
+
const n = next ?? {};
|
|
103
|
+
const changes = [];
|
|
104
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(p), ...Object.keys(n)]);
|
|
105
|
+
for (const key of keys) {
|
|
106
|
+
const inPrev = Object.prototype.hasOwnProperty.call(p, key);
|
|
107
|
+
const inNext = Object.prototype.hasOwnProperty.call(n, key);
|
|
108
|
+
const path = joinPath(basePath, key);
|
|
109
|
+
if (inPrev && !inNext) {
|
|
110
|
+
changes.push({ path, kind: "removed", prev: p[key], next: void 0 });
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (!inPrev && inNext) {
|
|
114
|
+
changes.push({ path, kind: "added", prev: void 0, next: n[key] });
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const a = p[key];
|
|
118
|
+
const b = n[key];
|
|
119
|
+
if (Object.is(a, b)) continue;
|
|
120
|
+
const kind = classify(a, b);
|
|
121
|
+
if (kind === "different") {
|
|
122
|
+
changes.push({ path: firstDifferentPath(a, b, path), kind, prev: a, next: b });
|
|
123
|
+
} else {
|
|
124
|
+
changes.push({ path, kind, prev: a, next: b });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return changes;
|
|
128
|
+
}
|
|
129
|
+
function firstDifferentPath(a, b, path, depth = 0) {
|
|
130
|
+
if (depth > 8) return path;
|
|
131
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
132
|
+
if (a.length !== b.length) return `${path}.length`;
|
|
133
|
+
for (let i = 0; i < a.length; i++) {
|
|
134
|
+
if (!deepEqual(a[i], b[i])) return firstDifferentPath(a[i], b[i], joinPath(path, i), depth + 1);
|
|
135
|
+
}
|
|
136
|
+
return path;
|
|
137
|
+
}
|
|
138
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
139
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
140
|
+
for (const k of keys) {
|
|
141
|
+
if (!deepEqual(a[k], b[k])) return firstDifferentPath(a[k], b[k], joinPath(path, k), depth + 1);
|
|
142
|
+
}
|
|
143
|
+
return path;
|
|
144
|
+
}
|
|
145
|
+
return path;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/report.ts
|
|
149
|
+
var now = () => typeof performance !== "undefined" && typeof performance.now === "function" ? performance.now() : Date.now();
|
|
150
|
+
var hookLabel = (c) => c.custom && c.custom.length ? `${c.custom.join(" \u203A ")} \u203A ${c.hook}#${c.index}` : `${c.hook} #${c.index}`;
|
|
151
|
+
function storeAdvice(custom) {
|
|
152
|
+
const chain = custom ?? [];
|
|
153
|
+
if (chain.some((n) => /^useSelector$/.test(n)))
|
|
154
|
+
return "the Redux selector returns a new object on every call: return a stored slice, pass shallowEqual as the equality function, or memoize it with createSelector";
|
|
155
|
+
if (chain.some((n) => /^useAppSelector$/.test(n)))
|
|
156
|
+
return "the selector returns a new object on every call: return a stored slice, pass shallowEqual, or memoize it with createSelector";
|
|
157
|
+
if (chain.some((n) => /^use[A-Z]\w*Store$/.test(n) || n === "useStore" || n === "useBoundStore"))
|
|
158
|
+
return "the store selector returns a new object on every call: select a primitive, or wrap the selector with useShallow (Zustand) / an equality function";
|
|
159
|
+
return "getSnapshot returns a new reference with the same contents: cache the snapshot in the store and return the same object while the data is unchanged";
|
|
160
|
+
}
|
|
161
|
+
var isGenuine = (c) => c.kind === "different" || c.kind === "added" || c.kind === "removed";
|
|
162
|
+
var isChildren = (change) => change.path === "children";
|
|
163
|
+
function fixFor(change) {
|
|
164
|
+
if (isChildren(change) && (change.kind === "element" || change.kind === "deep-equal")) {
|
|
165
|
+
return `lift the children out of the parent's render: memoize them with useMemo, hoist static elements to module scope, or render them from a component that does not re-render`;
|
|
166
|
+
}
|
|
167
|
+
switch (change.kind) {
|
|
168
|
+
case "function":
|
|
169
|
+
return `wrap it in useCallback (or hoist it out of the parent's render)`;
|
|
170
|
+
case "element":
|
|
171
|
+
return `memoize the element with useMemo, or pass it as children from a stable parent`;
|
|
172
|
+
case "deep-equal":
|
|
173
|
+
return Array.isArray(change.next) ? `memoize the array with useMemo (same items, new reference)` : `memoize the object with useMemo, or hoist it to module scope if it is constant`;
|
|
174
|
+
default:
|
|
175
|
+
return "";
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function describe(change) {
|
|
179
|
+
if (isChildren(change) && (change.kind === "element" || change.kind === "deep-equal")) {
|
|
180
|
+
return `children are new React elements with the same types and props on every render of the parent`;
|
|
181
|
+
}
|
|
182
|
+
switch (change.kind) {
|
|
183
|
+
case "deep-equal":
|
|
184
|
+
return `prop "${change.path}" is a new reference but deep-equal to the previous value`;
|
|
185
|
+
case "function":
|
|
186
|
+
return `prop "${change.path}" is a new function instance on every render`;
|
|
187
|
+
case "element":
|
|
188
|
+
return `prop "${change.path}" is a new React element with the same type and props`;
|
|
189
|
+
case "different":
|
|
190
|
+
return `prop "${change.path}" changed`;
|
|
191
|
+
case "added":
|
|
192
|
+
return `prop "${change.path}" was added`;
|
|
193
|
+
case "removed":
|
|
194
|
+
return `prop "${change.path}" was removed`;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
function buildReport(input) {
|
|
198
|
+
const stateChanges = input.stateChanges ?? [];
|
|
199
|
+
const hookChanges = input.hookChanges ?? [];
|
|
200
|
+
const isStateHook = (h) => h.hook === "useState" || h.hook === "useReducer";
|
|
201
|
+
const genuineProps = input.propChanges.some(isGenuine);
|
|
202
|
+
const genuineState = stateChanges.some(isGenuine) || hookChanges.some((h) => isStateHook(h) && isGenuine(h));
|
|
203
|
+
const genuineHooks = hookChanges.some((h) => !isStateHook(h) && isGenuine(h));
|
|
204
|
+
const causes = [genuineProps && "props", genuineState && "state", genuineHooks && "hooks"].filter(Boolean);
|
|
205
|
+
let trigger;
|
|
206
|
+
if (causes.length === 0) trigger = "parent";
|
|
207
|
+
else if (causes.length === 1) trigger = causes[0];
|
|
208
|
+
else trigger = "mixed";
|
|
209
|
+
const avoidable = trigger === "parent";
|
|
210
|
+
const reasons = [];
|
|
211
|
+
if (avoidable && input.propChanges.length === 0 && stateChanges.length === 0 && hookChanges.length === 0) {
|
|
212
|
+
const who = input.parent ? `<${input.parent.name}> re-rendered (${describeTrigger(input.parent.trigger)})` : "its parent re-rendered";
|
|
213
|
+
reasons.push(`re-rendered with identical props because ${who}. Wrap "${input.component}" in React.memo (or extend PureComponent).`);
|
|
214
|
+
} else if (avoidable && input.parent) {
|
|
215
|
+
reasons.push(`caused by <${input.parent.name}> re-rendering (${describeTrigger(input.parent.trigger)}).`);
|
|
216
|
+
}
|
|
217
|
+
for (const c of input.propChanges) {
|
|
218
|
+
const fix = fixFor(c);
|
|
219
|
+
reasons.push(fix ? `${describe(c)}: ${fix}.` : `${describe(c)}.`);
|
|
220
|
+
}
|
|
221
|
+
const memoized = input.memoized !== false;
|
|
222
|
+
if (avoidable && !memoized && input.propChanges.length > 0) {
|
|
223
|
+
reasons.push(
|
|
224
|
+
`"${input.component}" is not memoized, so fixing the props alone will not stop this re-render: also wrap it in React.memo (or extend PureComponent).`
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
for (const c of stateChanges) {
|
|
228
|
+
if (isGenuine(c)) reasons.push(`state "${c.path}" changed.`);
|
|
229
|
+
else reasons.push(`setState was called with a value deep-equal to the current "${c.path}" (new reference, same contents).`);
|
|
230
|
+
}
|
|
231
|
+
for (const c of hookChanges) {
|
|
232
|
+
if (c.hook === "useContext" && isGenuine(c)) {
|
|
233
|
+
const where = c.provider && c.provider.component ? ` (provided by <${c.provider.component}>)` : "";
|
|
234
|
+
const keys = c.changedKeys && typeof c.totalKeys === "number" && c.changedKeys.length > 0 && c.changedKeys.length < c.totalKeys ? `: only ${c.changedKeys.map((k) => `"${k}"`).join(", ")} of ${c.totalKeys} keys changed, yet every consumer re-renders. Split the context or memoize the slices consumers read` : "";
|
|
235
|
+
reasons.push(`${c.path} changed${where}${keys}.`);
|
|
236
|
+
} else if (isGenuine(c)) reasons.push(`${hookLabel(c)} changed.`);
|
|
237
|
+
else if (isStateHook(c))
|
|
238
|
+
reasons.push(`${hookLabel(c)} was set to a value deep-equal to the current one (new reference, same contents): reuse the existing object or bail out before calling the setter.`);
|
|
239
|
+
else if (c.hook === "useSyncExternalStore") reasons.push(`${hookLabel(c)} returned a new reference that is deep-equal to the previous value: ${storeAdvice(c.custom)}.`);
|
|
240
|
+
else reasons.push(`${hookLabel(c)} returned a new reference that is deep-equal to the previous value: memoize the context/store value where it is produced.`);
|
|
241
|
+
}
|
|
242
|
+
if (input.commitCause === "effect-after-commit") {
|
|
243
|
+
const who = input.updaters && input.updaters.length ? input.updaters.map((u) => `<${u}>`).join(", ") : "a component that rendered in it";
|
|
244
|
+
reasons.push(`this commit was scheduled right after commit #${input.afterCommit ?? "?"} by ${who}: an effect there set state (effect \u2192 setState loop). Derive the value during render or compute it before setting state.`);
|
|
245
|
+
} else if (input.commitCause === "suspense-resolved") {
|
|
246
|
+
reasons.push("this commit shows content that was suspended (a Suspense boundary resolved).");
|
|
247
|
+
}
|
|
248
|
+
const report = {
|
|
249
|
+
component: input.component,
|
|
250
|
+
instanceId: input.instanceId ?? 0,
|
|
251
|
+
commitId: input.commitId ?? 0,
|
|
252
|
+
renderCount: input.renderCount,
|
|
253
|
+
trigger,
|
|
254
|
+
avoidable,
|
|
255
|
+
memoized,
|
|
256
|
+
props: { prev: input.prevProps, next: input.nextProps },
|
|
257
|
+
propChanges: input.propChanges,
|
|
258
|
+
stateChanges,
|
|
259
|
+
hookChanges,
|
|
260
|
+
parent: input.parent ?? null,
|
|
261
|
+
owner: input.owner ?? null,
|
|
262
|
+
path: input.path ?? [],
|
|
263
|
+
reasons,
|
|
264
|
+
time: now()
|
|
265
|
+
};
|
|
266
|
+
if (input.selfDuration !== void 0) report.selfDuration = input.selfDuration;
|
|
267
|
+
if (input.treeDuration !== void 0) report.treeDuration = input.treeDuration;
|
|
268
|
+
if (input.commitPriority) report.commitPriority = input.commitPriority;
|
|
269
|
+
if (input.source) report.source = input.source;
|
|
270
|
+
if (input.hookState) report.hookState = input.hookState;
|
|
271
|
+
if (input.contexts) report.contexts = input.contexts;
|
|
272
|
+
if (input.state) report.state = input.state;
|
|
273
|
+
if (input.updaters && input.updaters.length) report.updaters = input.updaters;
|
|
274
|
+
if (input.commitCause) report.commitCause = input.commitCause;
|
|
275
|
+
if (input.afterCommit !== void 0) report.afterCommit = input.afterCommit;
|
|
276
|
+
if (input.key !== void 0) report.key = input.key;
|
|
277
|
+
return report;
|
|
278
|
+
}
|
|
279
|
+
function describeTrigger(t) {
|
|
280
|
+
switch (t) {
|
|
281
|
+
case "props":
|
|
282
|
+
return "its props changed";
|
|
283
|
+
case "state":
|
|
284
|
+
return "its state changed";
|
|
285
|
+
case "hooks":
|
|
286
|
+
return "a context or store it reads changed";
|
|
287
|
+
case "mixed":
|
|
288
|
+
return "its props and state changed";
|
|
289
|
+
default:
|
|
290
|
+
return "its own parent re-rendered";
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
var KIND_LABEL = {
|
|
294
|
+
"deep-equal": "equal by value",
|
|
295
|
+
function: "new function",
|
|
296
|
+
element: "equal element",
|
|
297
|
+
different: "changed",
|
|
298
|
+
added: "added",
|
|
299
|
+
removed: "removed"
|
|
300
|
+
};
|
|
301
|
+
function summarize(report) {
|
|
302
|
+
const counts2 = /* @__PURE__ */ new Map();
|
|
303
|
+
for (const c of [...report.propChanges, ...report.stateChanges, ...report.hookChanges]) {
|
|
304
|
+
counts2.set(c.kind, (counts2.get(c.kind) ?? 0) + 1);
|
|
305
|
+
}
|
|
306
|
+
const parts = [...counts2].map(([kind, n]) => `${n} ${KIND_LABEL[kind]}`);
|
|
307
|
+
if (parts.length === 0) parts.push("no changes");
|
|
308
|
+
return parts.join(", ");
|
|
309
|
+
}
|
|
310
|
+
function printReport(report, options) {
|
|
311
|
+
const c = options.console ?? console;
|
|
312
|
+
const open = options.collapse === false ? c.group : c.groupCollapsed;
|
|
313
|
+
const verdict = report.avoidable ? "avoidable re-render" : `re-render (${report.trigger})`;
|
|
314
|
+
open.call(c, `[rerender-lens] <${report.component}> ${verdict}: ${summarize(report)}`);
|
|
315
|
+
for (const r of report.reasons) c.log(`- ${r}`);
|
|
316
|
+
if (report.path.length) c.log(`at ${[...report.path, report.component].join(" > ")}`);
|
|
317
|
+
for (const ch of [...report.propChanges, ...report.stateChanges, ...report.hookChanges]) {
|
|
318
|
+
c.log(`${ch.path} (${KIND_LABEL[ch.kind]})`, { prev: ch.prev, next: ch.next });
|
|
319
|
+
}
|
|
320
|
+
c.log("props", report.props);
|
|
321
|
+
if (report.hookState && report.hookState.length) c.log("hooks", Object.fromEntries(report.hookState.map((h) => [h.path, h.value])));
|
|
322
|
+
if (report.contexts && report.contexts.length) c.log("contexts", Object.fromEntries(report.contexts.map((x) => [x.name, x.value])));
|
|
323
|
+
if (report.state) c.log("state", report.state);
|
|
324
|
+
c.groupEnd();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// src/state.ts
|
|
328
|
+
var KEY = /* @__PURE__ */ Symbol.for("rerender-lens.state");
|
|
329
|
+
function getState() {
|
|
330
|
+
const g = globalThis;
|
|
331
|
+
let s = g[KEY];
|
|
332
|
+
if (!s) {
|
|
333
|
+
s = { options: {}, enabled: false, printed: /* @__PURE__ */ new Map(), detach: null, nextInstanceId: 1, nextCommitId: 1, warnedOnce: /* @__PURE__ */ new Set(), scheduled: 0, commits: 0, overheadMs: 0, maxCommitMs: 0 };
|
|
334
|
+
g[KEY] = s;
|
|
335
|
+
}
|
|
336
|
+
return s;
|
|
337
|
+
}
|
|
338
|
+
function warnOnce(key, message) {
|
|
339
|
+
const s = getState();
|
|
340
|
+
if (s.warnedOnce.has(key)) return;
|
|
341
|
+
s.warnedOnce.add(key);
|
|
342
|
+
(s.options.console ?? console).warn(`[rerender-lens] ${message}`);
|
|
343
|
+
}
|
|
344
|
+
function dispatch(report, override) {
|
|
345
|
+
const s = getState();
|
|
346
|
+
const options = s.options;
|
|
347
|
+
if (!options.silent && (report.avoidable || options.logAll)) {
|
|
348
|
+
const max = options.maxReportsPerComponent ?? 0;
|
|
349
|
+
const n = (s.printed.get(report.component) ?? 0) + 1;
|
|
350
|
+
s.printed.set(report.component, n);
|
|
351
|
+
if (max <= 0 || n <= max) {
|
|
352
|
+
printReport(report, options);
|
|
353
|
+
} else if (n === max + 1) {
|
|
354
|
+
(options.console ?? console).log(
|
|
355
|
+
`[rerender-lens] <${report.component}> reached maxReportsPerComponent (${max}); further reports are not printed.`
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (options.notifier) {
|
|
360
|
+
try {
|
|
361
|
+
options.notifier(report);
|
|
362
|
+
} catch (err) {
|
|
363
|
+
(options.console ?? console).warn("[rerender-lens] notifier threw", err);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// src/types.ts
|
|
369
|
+
var MARKER = "rerenderLens";
|
|
370
|
+
|
|
371
|
+
// src/tracker.ts
|
|
372
|
+
var MEMO = /* @__PURE__ */ Symbol.for("react.memo");
|
|
373
|
+
var FORWARD_REF = /* @__PURE__ */ Symbol.for("react.forward_ref");
|
|
374
|
+
var isMemo = (t) => typeof t === "object" && t !== null && t.$$typeof === MEMO;
|
|
375
|
+
var isForwardRef = (t) => typeof t === "object" && t !== null && t.$$typeof === FORWARD_REF;
|
|
376
|
+
var isClass = (t) => typeof t === "function" && !!t.prototype?.isReactComponent;
|
|
377
|
+
var isPureClass = (t) => isClass(t) && !!t.prototype.isPureReactComponent;
|
|
378
|
+
var isComponentLike = (t) => typeof t === "function" || isMemo(t) || isForwardRef(t);
|
|
379
|
+
function getDisplayName(type) {
|
|
380
|
+
if (typeof type === "string") return type;
|
|
381
|
+
if (isMemo(type)) return type.displayName ?? getDisplayName(type.type);
|
|
382
|
+
if (isForwardRef(type)) return type.displayName ?? getDisplayName(type.render);
|
|
383
|
+
if (typeof type === "function") {
|
|
384
|
+
const t = type;
|
|
385
|
+
return t.displayName ?? (t.name || "Anonymous");
|
|
386
|
+
}
|
|
387
|
+
return "Anonymous";
|
|
388
|
+
}
|
|
389
|
+
function hasMarker(type) {
|
|
390
|
+
if (type === null || typeof type !== "function" && typeof type !== "object") return false;
|
|
391
|
+
if (type[MARKER] === true) return true;
|
|
392
|
+
if (isMemo(type)) return hasMarker(type.type);
|
|
393
|
+
if (isForwardRef(type)) return hasMarker(type.render);
|
|
394
|
+
return false;
|
|
395
|
+
}
|
|
396
|
+
function matches(m, name) {
|
|
397
|
+
if (typeof m === "string") return m === name;
|
|
398
|
+
if (m instanceof RegExp) return m.test(name);
|
|
399
|
+
return m(name);
|
|
400
|
+
}
|
|
401
|
+
function shouldTrack(type, o) {
|
|
402
|
+
if (!isComponentLike(type)) return false;
|
|
403
|
+
const name = getDisplayName(type);
|
|
404
|
+
if (o.exclude?.some((m) => matches(m, name))) return false;
|
|
405
|
+
if (hasMarker(type)) return true;
|
|
406
|
+
if (o.include?.some((m) => matches(m, name))) return true;
|
|
407
|
+
if (o.trackAllComponents) return true;
|
|
408
|
+
if (o.trackAllMemoized) return isMemo(type) || isPureClass(type);
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
function init(options = {}) {
|
|
412
|
+
const s = getState();
|
|
413
|
+
s.options = { ...options };
|
|
414
|
+
s.printed.clear();
|
|
415
|
+
if (!s.enabled) {
|
|
416
|
+
s.detach = attach();
|
|
417
|
+
s.enabled = true;
|
|
418
|
+
}
|
|
419
|
+
return disable;
|
|
420
|
+
}
|
|
421
|
+
function disable() {
|
|
422
|
+
const s = getState();
|
|
423
|
+
if (!s.enabled) return;
|
|
424
|
+
s.detach?.();
|
|
425
|
+
s.detach = null;
|
|
426
|
+
s.enabled = false;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// src/hookNames.ts
|
|
430
|
+
var noop = () => {
|
|
431
|
+
};
|
|
432
|
+
function customHooksFromStack(stack, componentName, marker) {
|
|
433
|
+
const names = [];
|
|
434
|
+
let past = false;
|
|
435
|
+
for (const line of stack.split("\n")) {
|
|
436
|
+
const m = /^\s*at\s+(?:async\s+)?([^\s(]+)|^([^@\s]+)@/.exec(line);
|
|
437
|
+
const name = m ? (m[1] || m[2] || "").replace(/^Object\./, "").replace(/^Proxy\./, "") : "";
|
|
438
|
+
if (!name) continue;
|
|
439
|
+
if (!past) {
|
|
440
|
+
if (name.includes(marker)) past = true;
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
if (name === componentName || name.endsWith("." + componentName)) break;
|
|
444
|
+
if (/^use[A-Z0-9_]/.test(name)) names.push(name);
|
|
445
|
+
if (names.length > 8) break;
|
|
446
|
+
}
|
|
447
|
+
return names;
|
|
448
|
+
}
|
|
449
|
+
var cache = /* @__PURE__ */ new WeakMap();
|
|
450
|
+
function makeDispatcher(fiber, componentName, out, marker) {
|
|
451
|
+
let node = fiber.memoizedState;
|
|
452
|
+
let index = 0;
|
|
453
|
+
const take = (count) => {
|
|
454
|
+
const first = node;
|
|
455
|
+
for (let i = 0; i < count && node; i++) node = node.next;
|
|
456
|
+
index += count;
|
|
457
|
+
return first;
|
|
458
|
+
};
|
|
459
|
+
const record = (primitive, count) => {
|
|
460
|
+
const stack = new Error().stack || "";
|
|
461
|
+
const chain = customHooksFromStack(stack, componentName, marker);
|
|
462
|
+
const start = index;
|
|
463
|
+
const first = take(count);
|
|
464
|
+
if (chain.length) for (let i = 0; i < count; i++) out.set(start + i, chain);
|
|
465
|
+
return first;
|
|
466
|
+
};
|
|
467
|
+
const memoized = (n) => n ? n.memoizedState : void 0;
|
|
468
|
+
const dispatcher = {};
|
|
469
|
+
const define = (name, impl) => {
|
|
470
|
+
const fn = { [marker + name]: (...a) => impl(...a) }[marker + name];
|
|
471
|
+
dispatcher[name] = fn;
|
|
472
|
+
};
|
|
473
|
+
define("useState", () => {
|
|
474
|
+
const n = record("useState", 1);
|
|
475
|
+
return [memoized(n), noop];
|
|
476
|
+
});
|
|
477
|
+
define("useReducer", () => {
|
|
478
|
+
const n = record("useReducer", 1);
|
|
479
|
+
return [memoized(n), noop];
|
|
480
|
+
});
|
|
481
|
+
define("useRef", (init2) => {
|
|
482
|
+
const n = record("useRef", 1);
|
|
483
|
+
return n ? n.memoizedState : { current: init2 };
|
|
484
|
+
});
|
|
485
|
+
define("useMemo", (fn) => {
|
|
486
|
+
const n = record("useMemo", 1);
|
|
487
|
+
const s = memoized(n);
|
|
488
|
+
return Array.isArray(s) ? s[0] : typeof fn === "function" ? fn() : void 0;
|
|
489
|
+
});
|
|
490
|
+
define("useCallback", (fn) => {
|
|
491
|
+
const n = record("useCallback", 1);
|
|
492
|
+
const s = memoized(n);
|
|
493
|
+
return Array.isArray(s) ? s[0] : fn;
|
|
494
|
+
});
|
|
495
|
+
for (const effect of ["useEffect", "useLayoutEffect", "useInsertionEffect", "useImperativeHandle"]) define(effect, () => void record(effect, 1));
|
|
496
|
+
define("useContext", (ctx) => {
|
|
497
|
+
record("useContext", 0);
|
|
498
|
+
const c = ctx;
|
|
499
|
+
return c ? c._currentValue : void 0;
|
|
500
|
+
});
|
|
501
|
+
define("useDebugValue", () => void record("useDebugValue", 0));
|
|
502
|
+
define("useSyncExternalStore", (_subscribe, getSnapshot) => {
|
|
503
|
+
const n = record("useSyncExternalStore", 2);
|
|
504
|
+
return n ? n.memoizedState : typeof getSnapshot === "function" ? getSnapshot() : void 0;
|
|
505
|
+
});
|
|
506
|
+
define("useTransition", () => {
|
|
507
|
+
record("useTransition", 2);
|
|
508
|
+
return [false, noop];
|
|
509
|
+
});
|
|
510
|
+
define("useDeferredValue", (v) => {
|
|
511
|
+
const n = record("useDeferredValue", 1);
|
|
512
|
+
return n ? n.memoizedState : v;
|
|
513
|
+
});
|
|
514
|
+
define("useId", () => {
|
|
515
|
+
const n = record("useId", 1);
|
|
516
|
+
return n ? n.memoizedState : "id";
|
|
517
|
+
});
|
|
518
|
+
define("useOptimistic", (v) => {
|
|
519
|
+
const n = record("useOptimistic", 1);
|
|
520
|
+
return [n ? n.memoizedState : v, noop];
|
|
521
|
+
});
|
|
522
|
+
for (const action of ["useActionState", "useFormState"]) {
|
|
523
|
+
define(action, (_fn, init2) => {
|
|
524
|
+
const n = record(action, 3);
|
|
525
|
+
return [n ? n.memoizedState : init2, noop, false];
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
define("useEffectEvent", (fn) => {
|
|
529
|
+
record("useEffectEvent", 1);
|
|
530
|
+
return fn;
|
|
531
|
+
});
|
|
532
|
+
define("useCacheRefresh", () => {
|
|
533
|
+
record("useCacheRefresh", 1);
|
|
534
|
+
return noop;
|
|
535
|
+
});
|
|
536
|
+
define("useHostTransitionStatus", () => ({ pending: false, data: null, method: null, action: null }));
|
|
537
|
+
define("useMemoCache", (size) => new Array(Number(size) || 0).fill(/* @__PURE__ */ Symbol.for("react.memo_cache_sentinel")));
|
|
538
|
+
define("use", (usable) => {
|
|
539
|
+
record("use", 0);
|
|
540
|
+
const u = usable;
|
|
541
|
+
if (u && typeof u.then === "function") {
|
|
542
|
+
if (u.status === "fulfilled") return u.value;
|
|
543
|
+
throw new Error("rerender-lens: cannot replay use() on a pending promise");
|
|
544
|
+
}
|
|
545
|
+
return u ? u._currentValue : void 0;
|
|
546
|
+
});
|
|
547
|
+
define("readContext", (ctx) => ctx?._currentValue);
|
|
548
|
+
return { dispatcher, nodesConsumed: () => index };
|
|
549
|
+
}
|
|
550
|
+
function renderFunctionOf(type) {
|
|
551
|
+
let t = type;
|
|
552
|
+
while (t && typeof t === "object" && t.type) t = t.type;
|
|
553
|
+
if (t && typeof t === "object" && typeof t.render === "function") return { fn: t.render, name: t.render.name || t.displayName || "Component" };
|
|
554
|
+
if (typeof t === "function") return { fn: t, name: t.name || "Component" };
|
|
555
|
+
return { fn: null, name: "Component" };
|
|
556
|
+
}
|
|
557
|
+
function resolveHookNames(fiber, ref) {
|
|
558
|
+
const type = fiber.type;
|
|
559
|
+
if (!type || !ref) return /* @__PURE__ */ new Map();
|
|
560
|
+
const cached = cache.get(type);
|
|
561
|
+
if (cached) return cached.byNode;
|
|
562
|
+
const result = { byNode: /* @__PURE__ */ new Map(), ok: false };
|
|
563
|
+
cache.set(type, result);
|
|
564
|
+
const { fn, name } = renderFunctionOf(type);
|
|
565
|
+
if (!fn) return result.byNode;
|
|
566
|
+
const useH = "H" in ref;
|
|
567
|
+
const marker = "__rl_";
|
|
568
|
+
const previous = useH ? ref.H : ref.current;
|
|
569
|
+
const { dispatcher, nodesConsumed } = makeDispatcher(fiber, name, result.byNode, marker);
|
|
570
|
+
const proxy = new Proxy(dispatcher, {
|
|
571
|
+
get(target, key) {
|
|
572
|
+
if (key in target) return target[key];
|
|
573
|
+
return () => void 0;
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
let total = 0;
|
|
577
|
+
for (let n = fiber.memoizedState; n && typeof n === "object" && "next" in n; n = n.next) total++;
|
|
578
|
+
try {
|
|
579
|
+
if (useH) ref.H = proxy;
|
|
580
|
+
else ref.current = proxy;
|
|
581
|
+
const secondArg = fiber.tag === 11 ? fiber.stateNode?.ref ?? null : void 0;
|
|
582
|
+
fn(fiber.memoizedProps ?? {}, secondArg);
|
|
583
|
+
result.ok = nodesConsumed() === total;
|
|
584
|
+
if (!result.ok) result.byNode.clear();
|
|
585
|
+
} catch {
|
|
586
|
+
result.byNode.clear();
|
|
587
|
+
} finally {
|
|
588
|
+
if (useH) ref.H = previous;
|
|
589
|
+
else ref.current = previous;
|
|
590
|
+
}
|
|
591
|
+
return result.byNode;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// src/fiber.ts
|
|
595
|
+
var FunctionComponent = 0;
|
|
596
|
+
var ClassComponent = 1;
|
|
597
|
+
var HostRoot = 3;
|
|
598
|
+
var ContextProvider = 10;
|
|
599
|
+
var ForwardRef = 11;
|
|
600
|
+
var SuspenseComponent = 13;
|
|
601
|
+
var MemoComponent = 14;
|
|
602
|
+
var SimpleMemoComponent = 15;
|
|
603
|
+
var PerformedWork = 1;
|
|
604
|
+
var HOOK = "__REACT_DEVTOOLS_GLOBAL_HOOK__";
|
|
605
|
+
var nowMs = () => typeof performance !== "undefined" && typeof performance.now === "function" ? performance.now() : Date.now();
|
|
606
|
+
function ensureDevtoolsHook() {
|
|
607
|
+
const g = globalThis;
|
|
608
|
+
let hook = g[HOOK];
|
|
609
|
+
if (!hook) {
|
|
610
|
+
let nextId = 0;
|
|
611
|
+
const renderers = /* @__PURE__ */ new Map();
|
|
612
|
+
hook = {
|
|
613
|
+
renderers,
|
|
614
|
+
supportsFiber: true,
|
|
615
|
+
inject(renderer) {
|
|
616
|
+
const id = ++nextId;
|
|
617
|
+
renderers.set(id, renderer);
|
|
618
|
+
return id;
|
|
619
|
+
},
|
|
620
|
+
onCommitFiberRoot() {
|
|
621
|
+
},
|
|
622
|
+
onCommitFiberUnmount() {
|
|
623
|
+
},
|
|
624
|
+
onScheduleFiberRoot() {
|
|
625
|
+
},
|
|
626
|
+
onPostCommitFiberRoot() {
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
g[HOOK] = hook;
|
|
630
|
+
}
|
|
631
|
+
return hook;
|
|
632
|
+
}
|
|
633
|
+
var capturedRenderers = /* @__PURE__ */ new Map();
|
|
634
|
+
var dispatcherRefs = /* @__PURE__ */ new Map();
|
|
635
|
+
var INJECT_WRAPPED = /* @__PURE__ */ Symbol.for("rerender-lens.injectWrapped");
|
|
636
|
+
var ATTACHED = /* @__PURE__ */ Symbol.for("rerender-lens.attached");
|
|
637
|
+
function pickRenderer(r) {
|
|
638
|
+
const info = r ?? {};
|
|
639
|
+
return { version: info.version, bundleType: info.bundleType, rendererPackageName: info.rendererPackageName };
|
|
640
|
+
}
|
|
641
|
+
function wrapInject(hook) {
|
|
642
|
+
const h = hook;
|
|
643
|
+
if (h[INJECT_WRAPPED] || typeof hook.inject !== "function") return;
|
|
644
|
+
const original = hook.inject;
|
|
645
|
+
hook.inject = function(renderer) {
|
|
646
|
+
const id = original.call(this, renderer);
|
|
647
|
+
const key = typeof id === "number" ? id : capturedRenderers.size + 1;
|
|
648
|
+
capturedRenderers.set(key, pickRenderer(renderer));
|
|
649
|
+
const ref = renderer?.currentDispatcherRef;
|
|
650
|
+
if (ref && typeof ref === "object") dispatcherRefs.set(key, ref);
|
|
651
|
+
return id;
|
|
652
|
+
};
|
|
653
|
+
h[INJECT_WRAPPED] = true;
|
|
654
|
+
}
|
|
655
|
+
function getDispatcherRef() {
|
|
656
|
+
for (const ref of dispatcherRefs.values()) return ref;
|
|
657
|
+
const g = globalThis;
|
|
658
|
+
const hook = g[HOOK];
|
|
659
|
+
let found = null;
|
|
660
|
+
if (hook && hook.renderers && typeof hook.renderers.forEach === "function") {
|
|
661
|
+
hook.renderers.forEach((r) => {
|
|
662
|
+
const ref = r?.currentDispatcherRef;
|
|
663
|
+
if (!found && ref && typeof ref === "object") found = ref;
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
return found;
|
|
667
|
+
}
|
|
668
|
+
function attach() {
|
|
669
|
+
const hook = ensureDevtoolsHook();
|
|
670
|
+
wrapInject(hook);
|
|
671
|
+
if (hook[ATTACHED]) return () => {
|
|
672
|
+
};
|
|
673
|
+
hook[ATTACHED] = true;
|
|
674
|
+
const previous = hook.onCommitFiberRoot;
|
|
675
|
+
const patched = function(id, root, priority, didError) {
|
|
676
|
+
const started = nowMs();
|
|
677
|
+
try {
|
|
678
|
+
onCommit(root, priorityLabel(priority));
|
|
679
|
+
} catch (err) {
|
|
680
|
+
warnOnce("commit", `failed to inspect a commit: ${String(err)}`);
|
|
681
|
+
} finally {
|
|
682
|
+
const spent = nowMs() - started;
|
|
683
|
+
const s = getState();
|
|
684
|
+
s.overheadMs += spent;
|
|
685
|
+
if (spent > s.maxCommitMs) s.maxCommitMs = spent;
|
|
686
|
+
}
|
|
687
|
+
if (typeof previous === "function") return previous.call(this, id, root, priority, didError);
|
|
688
|
+
};
|
|
689
|
+
hook.onCommitFiberRoot = patched;
|
|
690
|
+
const previousSchedule = hook.onScheduleFiberRoot;
|
|
691
|
+
const patchedSchedule = function(id, root, children) {
|
|
692
|
+
getState().scheduled++;
|
|
693
|
+
if (typeof previousSchedule === "function") return previousSchedule.call(this, id, root, children);
|
|
694
|
+
};
|
|
695
|
+
hook.onScheduleFiberRoot = patchedSchedule;
|
|
696
|
+
return () => {
|
|
697
|
+
hook[ATTACHED] = false;
|
|
698
|
+
if (hook.onCommitFiberRoot === patched) hook.onCommitFiberRoot = previous;
|
|
699
|
+
if (hook.onScheduleFiberRoot === patchedSchedule) hook.onScheduleFiberRoot = previousSchedule;
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
function priorityLabel(priority) {
|
|
703
|
+
switch (priority) {
|
|
704
|
+
case 1:
|
|
705
|
+
return "immediate";
|
|
706
|
+
case 2:
|
|
707
|
+
return "user-blocking";
|
|
708
|
+
case 3:
|
|
709
|
+
return "normal";
|
|
710
|
+
case 4:
|
|
711
|
+
return "low";
|
|
712
|
+
case 5:
|
|
713
|
+
return "idle";
|
|
714
|
+
default:
|
|
715
|
+
return void 0;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
var flagsOf = (f) => f.flags ?? f.effectTag ?? 0;
|
|
719
|
+
var isComponentTag = (tag) => tag === FunctionComponent || tag === ClassComponent || tag === ForwardRef || tag === MemoComponent || tag === SimpleMemoComponent;
|
|
720
|
+
function isMemoizedFiber(fiber) {
|
|
721
|
+
if (fiber.tag === MemoComponent || fiber.tag === SimpleMemoComponent) return true;
|
|
722
|
+
if (fiber.tag === ClassComponent) {
|
|
723
|
+
const inst = fiber.stateNode;
|
|
724
|
+
return !!(inst && inst.isPureReactComponent);
|
|
725
|
+
}
|
|
726
|
+
return false;
|
|
727
|
+
}
|
|
728
|
+
function durationsOf(fiber) {
|
|
729
|
+
if (typeof fiber.actualDuration !== "number") return null;
|
|
730
|
+
const tree = fiber.actualDuration;
|
|
731
|
+
let children = 0;
|
|
732
|
+
for (let c = fiber.child; c; c = c.sibling) if (typeof c.actualDuration === "number") children += c.actualDuration;
|
|
733
|
+
return { self: Math.max(0, tree - children), tree };
|
|
734
|
+
}
|
|
735
|
+
function fiberType(fiber) {
|
|
736
|
+
return fiber.elementType ?? fiber.type;
|
|
737
|
+
}
|
|
738
|
+
function fiberName(fiber) {
|
|
739
|
+
return getDisplayName(fiberType(fiber));
|
|
740
|
+
}
|
|
741
|
+
function didRender(fiber) {
|
|
742
|
+
return (flagsOf(fiber) & PerformedWork) !== 0;
|
|
743
|
+
}
|
|
744
|
+
function isHotSwapped(fiber, alt) {
|
|
745
|
+
return fiber.type !== alt.type;
|
|
746
|
+
}
|
|
747
|
+
var counts = /* @__PURE__ */ new WeakMap();
|
|
748
|
+
var ids = /* @__PURE__ */ new WeakMap();
|
|
749
|
+
var fibersById = /* @__PURE__ */ new Map();
|
|
750
|
+
var hasWeakRef = typeof WeakRef === "function";
|
|
751
|
+
function remember2(id, fiber) {
|
|
752
|
+
if (!hasWeakRef) return;
|
|
753
|
+
fibersById.set(id, new WeakRef(fiber));
|
|
754
|
+
if (fibersById.size > 5e3) {
|
|
755
|
+
for (const [k, ref] of fibersById) if (!ref.deref()) fibersById.delete(k);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
function instanceIdOf(fiber) {
|
|
759
|
+
return ids.get(fiber) ?? (fiber.alternate ? ids.get(fiber.alternate) : void 0);
|
|
760
|
+
}
|
|
761
|
+
function instanceId(fiber) {
|
|
762
|
+
const existing = instanceIdOf(fiber);
|
|
763
|
+
if (existing !== void 0) {
|
|
764
|
+
ids.set(fiber, existing);
|
|
765
|
+
remember2(existing, fiber);
|
|
766
|
+
return existing;
|
|
767
|
+
}
|
|
768
|
+
const s = getState();
|
|
769
|
+
const id = s.nextInstanceId++;
|
|
770
|
+
ids.set(fiber, id);
|
|
771
|
+
remember2(id, fiber);
|
|
772
|
+
return id;
|
|
773
|
+
}
|
|
774
|
+
function nearestComponent(fiber) {
|
|
775
|
+
let f = fiber;
|
|
776
|
+
while (f && f.tag !== HostRoot) {
|
|
777
|
+
if (isComponentTag(f.tag)) return f;
|
|
778
|
+
f = f.return;
|
|
779
|
+
}
|
|
780
|
+
return null;
|
|
781
|
+
}
|
|
782
|
+
var INTERNAL_FRAME = /react-dom|react_jsx|jsx-(dev-)?runtime|\/react\/|node_modules\/react|react-stack-bottom-frame|react_stack_bottom_frame|scheduler/;
|
|
783
|
+
function parseStackLocation(stack) {
|
|
784
|
+
for (const line of stack.split("\n")) {
|
|
785
|
+
const m = /(?:at\s+(?:.*?\s+)?\(?|@)?((?:https?|file|webpack|vite|blob):[^\s()]+?):(\d+):(\d+)\)?\s*$/.exec(line.trim());
|
|
786
|
+
if (!m || !m[1] || INTERNAL_FRAME.test(m[1])) continue;
|
|
787
|
+
return { fileName: m[1], lineNumber: Number(m[2]), columnNumber: Number(m[3]) };
|
|
788
|
+
}
|
|
789
|
+
return void 0;
|
|
790
|
+
}
|
|
791
|
+
function sourceOf(fiber) {
|
|
792
|
+
const src = fiber._debugSource;
|
|
793
|
+
if (src && typeof src.fileName === "string") {
|
|
794
|
+
const out = { fileName: src.fileName };
|
|
795
|
+
if (typeof src.lineNumber === "number") out.lineNumber = src.lineNumber;
|
|
796
|
+
if (typeof src.columnNumber === "number") out.columnNumber = src.columnNumber;
|
|
797
|
+
return out;
|
|
798
|
+
}
|
|
799
|
+
const st = fiber._debugStack;
|
|
800
|
+
const text = typeof st === "string" ? st : st && typeof st.stack === "string" ? st.stack : null;
|
|
801
|
+
return text ? parseStackLocation(text) : void 0;
|
|
802
|
+
}
|
|
803
|
+
function bumpCount(fiber) {
|
|
804
|
+
const prev = counts.get(fiber) ?? (fiber.alternate ? counts.get(fiber.alternate) : void 0) ?? 0;
|
|
805
|
+
const n = prev + 1;
|
|
806
|
+
counts.set(fiber, n);
|
|
807
|
+
return n;
|
|
808
|
+
}
|
|
809
|
+
function componentPath(fiber) {
|
|
810
|
+
const path = [];
|
|
811
|
+
let f = fiber.return;
|
|
812
|
+
while (f && f.tag !== HostRoot) {
|
|
813
|
+
if (isComponentTag(f.tag)) path.unshift(fiberName(f));
|
|
814
|
+
f = f.return;
|
|
815
|
+
}
|
|
816
|
+
return path;
|
|
817
|
+
}
|
|
818
|
+
function ownerName(fiber) {
|
|
819
|
+
const owner = fiber._debugOwner;
|
|
820
|
+
if (!owner) return null;
|
|
821
|
+
if (owner.type !== void 0) return getDisplayName(owner.type);
|
|
822
|
+
return typeof owner.name === "string" ? owner.name : null;
|
|
823
|
+
}
|
|
824
|
+
var isStateNode = (n) => !!n.queue && typeof n.queue.lastRenderedReducer === "function";
|
|
825
|
+
var isStoreNode = (n) => !!n.queue && typeof n.queue.getSnapshot === "function";
|
|
826
|
+
function hookLabel2(node) {
|
|
827
|
+
if (isStoreNode(node)) return "useSyncExternalStore";
|
|
828
|
+
if (isStateNode(node)) {
|
|
829
|
+
const r = node.queue.lastRenderedReducer;
|
|
830
|
+
return r.name === "basicStateReducer" ? "useState" : "useReducer";
|
|
831
|
+
}
|
|
832
|
+
return "state";
|
|
833
|
+
}
|
|
834
|
+
var NODELESS_HOOKS = /* @__PURE__ */ new Set(["useContext", "useDebugValue", "use"]);
|
|
835
|
+
function hookLabelsFor(fiber, first) {
|
|
836
|
+
const types = fiber._debugHookTypes?.filter((t) => !NODELESS_HOOKS.has(t));
|
|
837
|
+
let nodeCount = 0;
|
|
838
|
+
for (let n = first; n; n = n.next) nodeCount++;
|
|
839
|
+
return types && types.length === nodeCount ? types : null;
|
|
840
|
+
}
|
|
841
|
+
var isHookList = (v) => !!v && typeof v === "object" && "next" in v;
|
|
842
|
+
function snapshotHooks(fiber) {
|
|
843
|
+
const out = [];
|
|
844
|
+
if (fiber.tag === ClassComponent) return out;
|
|
845
|
+
const first = fiber.memoizedState;
|
|
846
|
+
if (!isHookList(first)) return out;
|
|
847
|
+
const labels = hookLabelsFor(fiber, first);
|
|
848
|
+
let i = 0;
|
|
849
|
+
for (let n = first; n; n = n.next, i++) {
|
|
850
|
+
if (!isStateNode(n) && !isStoreNode(n)) continue;
|
|
851
|
+
const hook = labels?.[i] ?? hookLabel2(n);
|
|
852
|
+
out.push({ path: `${hook}#${i}`, hook, index: i, value: n.memoizedState });
|
|
853
|
+
}
|
|
854
|
+
return out;
|
|
855
|
+
}
|
|
856
|
+
function snapshotContexts(fiber) {
|
|
857
|
+
const out = [];
|
|
858
|
+
for (let d = fiber.dependencies?.firstContext ?? null; d; d = d.next) out.push({ name: d.context.displayName ?? "Context", value: d.memoizedValue });
|
|
859
|
+
return out;
|
|
860
|
+
}
|
|
861
|
+
function diffHooks(fiber, alt) {
|
|
862
|
+
const out = [];
|
|
863
|
+
if (fiber.tag === ClassComponent) return out;
|
|
864
|
+
let a = alt.memoizedState;
|
|
865
|
+
let b = fiber.memoizedState;
|
|
866
|
+
if (!a || !b || typeof b !== "object" || !("next" in b)) return out;
|
|
867
|
+
const labels = hookLabelsFor(fiber, b);
|
|
868
|
+
let i = 0;
|
|
869
|
+
while (a && b) {
|
|
870
|
+
if ((isStateNode(b) || isStoreNode(b)) && !Object.is(a.memoizedState, b.memoizedState)) {
|
|
871
|
+
const hook = labels?.[i] ?? hookLabel2(b);
|
|
872
|
+
out.push({
|
|
873
|
+
path: `${hook}#${i}`,
|
|
874
|
+
hook,
|
|
875
|
+
index: i,
|
|
876
|
+
kind: classify(a.memoizedState, b.memoizedState),
|
|
877
|
+
prev: a.memoizedState,
|
|
878
|
+
next: b.memoizedState
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
a = a.next;
|
|
882
|
+
b = b.next;
|
|
883
|
+
i++;
|
|
884
|
+
}
|
|
885
|
+
return out;
|
|
886
|
+
}
|
|
887
|
+
function findProvider(fiber, context) {
|
|
888
|
+
let f = fiber.return;
|
|
889
|
+
while (f && f.tag !== HostRoot) {
|
|
890
|
+
if (f.tag === ContextProvider) {
|
|
891
|
+
const t = f.type;
|
|
892
|
+
if (t === context || t && typeof t === "object" && t._context === context) return f;
|
|
893
|
+
}
|
|
894
|
+
f = f.return;
|
|
895
|
+
}
|
|
896
|
+
return null;
|
|
897
|
+
}
|
|
898
|
+
var isPlainObject2 = (v) => typeof v === "object" && v !== null && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype;
|
|
899
|
+
function diffContexts(fiber, alt) {
|
|
900
|
+
const out = [];
|
|
901
|
+
let a = alt.dependencies?.firstContext ?? null;
|
|
902
|
+
let b = fiber.dependencies?.firstContext ?? null;
|
|
903
|
+
let i = 0;
|
|
904
|
+
while (a && b) {
|
|
905
|
+
if (a.context === b.context && !Object.is(a.memoizedValue, b.memoizedValue)) {
|
|
906
|
+
const name = b.context.displayName ?? "Context";
|
|
907
|
+
const change = {
|
|
908
|
+
path: `useContext(${name})`,
|
|
909
|
+
hook: "useContext",
|
|
910
|
+
index: i,
|
|
911
|
+
kind: classify(a.memoizedValue, b.memoizedValue),
|
|
912
|
+
prev: a.memoizedValue,
|
|
913
|
+
next: b.memoizedValue
|
|
914
|
+
};
|
|
915
|
+
const provider = findProvider(fiber, b.context);
|
|
916
|
+
if (provider) {
|
|
917
|
+
const path = componentPath(provider);
|
|
918
|
+
change.provider = { component: path[path.length - 1] ?? null, path };
|
|
919
|
+
}
|
|
920
|
+
if (isPlainObject2(a.memoizedValue) && isPlainObject2(b.memoizedValue)) {
|
|
921
|
+
const prev = a.memoizedValue;
|
|
922
|
+
const next = b.memoizedValue;
|
|
923
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(prev), ...Object.keys(next)]);
|
|
924
|
+
change.changedKeys = [...keys].filter((k) => !Object.is(prev[k], next[k]));
|
|
925
|
+
change.totalKeys = keys.size;
|
|
926
|
+
}
|
|
927
|
+
out.push(change);
|
|
928
|
+
}
|
|
929
|
+
a = a.next;
|
|
930
|
+
b = b.next;
|
|
931
|
+
i++;
|
|
932
|
+
}
|
|
933
|
+
return out;
|
|
934
|
+
}
|
|
935
|
+
var isGenuine2 = (c) => c.kind === "different" || c.kind === "added" || c.kind === "removed";
|
|
936
|
+
function analyze(fiber, alt, trackHooks) {
|
|
937
|
+
const prevProps = alt.memoizedProps ?? {};
|
|
938
|
+
const nextProps = fiber.memoizedProps ?? {};
|
|
939
|
+
const propChanges = prevProps === nextProps ? [] : diffRecords(prevProps, nextProps);
|
|
940
|
+
let stateChanges = [];
|
|
941
|
+
let hookChanges = [];
|
|
942
|
+
if (fiber.tag === ClassComponent) {
|
|
943
|
+
if (alt.memoizedState !== fiber.memoizedState) {
|
|
944
|
+
stateChanges = diffRecords(
|
|
945
|
+
alt.memoizedState ?? {},
|
|
946
|
+
fiber.memoizedState ?? {}
|
|
947
|
+
);
|
|
948
|
+
}
|
|
949
|
+
} else if (trackHooks) {
|
|
950
|
+
hookChanges = [...diffHooks(fiber, alt), ...diffContexts(fiber, alt)];
|
|
951
|
+
}
|
|
952
|
+
const isStateHook = (h) => h.hook !== "useContext" && h.hook !== "useSyncExternalStore";
|
|
953
|
+
const causes = [];
|
|
954
|
+
if (propChanges.some(isGenuine2)) causes.push("props");
|
|
955
|
+
if (stateChanges.some(isGenuine2) || hookChanges.some((h) => isStateHook(h) && isGenuine2(h))) causes.push("state");
|
|
956
|
+
if (hookChanges.some((h) => !isStateHook(h) && isGenuine2(h))) causes.push("hooks");
|
|
957
|
+
const trigger = causes.length === 0 ? "parent" : causes.length === 1 ? causes[0] : "mixed";
|
|
958
|
+
return { propChanges, stateChanges, hookChanges, trigger };
|
|
959
|
+
}
|
|
960
|
+
function nearestRenderedAncestor(fiber, cache2, trackHooks) {
|
|
961
|
+
let f = fiber.return;
|
|
962
|
+
while (f && f.tag !== HostRoot) {
|
|
963
|
+
if (isComponentTag(f.tag) && f.alternate && didRender(f)) {
|
|
964
|
+
let info = cache2.get(f);
|
|
965
|
+
if (info === void 0) {
|
|
966
|
+
info = { name: fiberName(f), trigger: analyze(f, f.alternate, trackHooks).trigger };
|
|
967
|
+
cache2.set(f, info);
|
|
968
|
+
}
|
|
969
|
+
return info;
|
|
970
|
+
}
|
|
971
|
+
f = f.return;
|
|
972
|
+
}
|
|
973
|
+
return null;
|
|
974
|
+
}
|
|
975
|
+
function updatersOf(root) {
|
|
976
|
+
const set = root.memoizedUpdaters;
|
|
977
|
+
if (!set || typeof set.forEach !== "function") return [];
|
|
978
|
+
const names = [];
|
|
979
|
+
set.forEach((f) => {
|
|
980
|
+
const comp = nearestComponent(f);
|
|
981
|
+
if (comp) {
|
|
982
|
+
const name = fiberName(comp);
|
|
983
|
+
if (!names.includes(name)) names.push(name);
|
|
984
|
+
}
|
|
985
|
+
});
|
|
986
|
+
return names;
|
|
987
|
+
}
|
|
988
|
+
var lastCommit = null;
|
|
989
|
+
var EFFECT_LOOP_WINDOW_MS = 50;
|
|
990
|
+
function onCommit(root, commitPriority) {
|
|
991
|
+
const s = getState();
|
|
992
|
+
if (!s.enabled) return;
|
|
993
|
+
s.commits++;
|
|
994
|
+
const o = s.options;
|
|
995
|
+
const trackHooks = o.trackHooks !== false;
|
|
996
|
+
const rendered = [];
|
|
997
|
+
const renderedNames = /* @__PURE__ */ new Set();
|
|
998
|
+
let hot = false;
|
|
999
|
+
let suspenseResolved = false;
|
|
1000
|
+
const stack = [root.current];
|
|
1001
|
+
while (stack.length) {
|
|
1002
|
+
const fiber = stack.pop();
|
|
1003
|
+
const alt = fiber.alternate;
|
|
1004
|
+
if (alt && isComponentTag(fiber.tag) && didRender(fiber)) {
|
|
1005
|
+
if (isHotSwapped(fiber, alt)) hot = true;
|
|
1006
|
+
renderedNames.add(fiberName(fiber));
|
|
1007
|
+
if (shouldTrack(fiberType(fiber), o)) rendered.push(fiber);
|
|
1008
|
+
}
|
|
1009
|
+
if (fiber.tag === SuspenseComponent && alt && alt.memoizedState !== null && fiber.memoizedState === null) suspenseResolved = true;
|
|
1010
|
+
if (!alt || fiber.child !== alt.child) {
|
|
1011
|
+
for (let c = fiber.child; c; c = c.sibling) stack.push(c);
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
if (hot && o.ignoreHotReload !== false) return;
|
|
1015
|
+
rendered.reverse();
|
|
1016
|
+
const at = nowMs();
|
|
1017
|
+
const updaters = updatersOf(root);
|
|
1018
|
+
let commitCause;
|
|
1019
|
+
let afterCommit;
|
|
1020
|
+
if (lastCommit && at - lastCommit.at < EFFECT_LOOP_WINDOW_MS && updaters.some((u) => lastCommit.rendered.has(u))) {
|
|
1021
|
+
commitCause = "effect-after-commit";
|
|
1022
|
+
afterCommit = lastCommit.id;
|
|
1023
|
+
} else if (suspenseResolved) commitCause = "suspense-resolved";
|
|
1024
|
+
if (rendered.length === 0) {
|
|
1025
|
+
lastCommit = { id: s.nextCommitId, at, rendered: renderedNames };
|
|
1026
|
+
return;
|
|
1027
|
+
}
|
|
1028
|
+
const commitId = s.nextCommitId++;
|
|
1029
|
+
lastCommit = { id: commitId, at, rendered: renderedNames };
|
|
1030
|
+
const dispatcherRef = o.resolveHookNames ? getDispatcherRef() : null;
|
|
1031
|
+
const parentCache = /* @__PURE__ */ new Map();
|
|
1032
|
+
for (const fiber of rendered) {
|
|
1033
|
+
const alt = fiber.alternate;
|
|
1034
|
+
const a = analyze(fiber, alt, trackHooks);
|
|
1035
|
+
const durations = durationsOf(fiber);
|
|
1036
|
+
let hookState = o.includeState !== false && trackHooks ? snapshotHooks(fiber) : void 0;
|
|
1037
|
+
if (dispatcherRef && fiber.tag !== ClassComponent && (a.hookChanges.length || hookState && hookState.length)) {
|
|
1038
|
+
const names = resolveHookNames(fiber, dispatcherRef);
|
|
1039
|
+
if (names.size) {
|
|
1040
|
+
for (const c of a.hookChanges) if (c.hook !== "useContext" && names.has(c.index)) c.custom = names.get(c.index);
|
|
1041
|
+
if (hookState) hookState = hookState.map((h) => names.has(h.index) ? { ...h, custom: names.get(h.index) } : h);
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
const report = buildReport({
|
|
1045
|
+
component: fiberName(fiber),
|
|
1046
|
+
instanceId: instanceId(fiber),
|
|
1047
|
+
renderCount: bumpCount(fiber),
|
|
1048
|
+
prevProps: alt.memoizedProps ?? {},
|
|
1049
|
+
nextProps: fiber.memoizedProps ?? {},
|
|
1050
|
+
propChanges: a.propChanges,
|
|
1051
|
+
stateChanges: a.stateChanges,
|
|
1052
|
+
hookChanges: a.hookChanges,
|
|
1053
|
+
...o.includeState !== false ? {
|
|
1054
|
+
hookState,
|
|
1055
|
+
contexts: trackHooks ? snapshotContexts(fiber) : void 0,
|
|
1056
|
+
state: fiber.tag === ClassComponent && fiber.memoizedState && typeof fiber.memoizedState === "object" ? fiber.memoizedState : void 0
|
|
1057
|
+
} : {},
|
|
1058
|
+
updaters,
|
|
1059
|
+
commitCause,
|
|
1060
|
+
afterCommit,
|
|
1061
|
+
key: fiber.key === null || fiber.key === void 0 ? null : String(fiber.key),
|
|
1062
|
+
parent: a.trigger === "parent" ? nearestRenderedAncestor(fiber, parentCache, trackHooks) : null,
|
|
1063
|
+
owner: ownerName(fiber),
|
|
1064
|
+
path: componentPath(fiber),
|
|
1065
|
+
memoized: isMemoizedFiber(fiber),
|
|
1066
|
+
selfDuration: durations ? durations.self : void 0,
|
|
1067
|
+
treeDuration: durations ? durations.tree : void 0,
|
|
1068
|
+
commitId,
|
|
1069
|
+
commitPriority,
|
|
1070
|
+
source: sourceOf(fiber)
|
|
1071
|
+
});
|
|
1072
|
+
dispatch(report);
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
// src/fixes.ts
|
|
1077
|
+
var AVOIDABLE = /* @__PURE__ */ new Set(["deep-equal", "function", "element"]);
|
|
1078
|
+
var rootOf = (path) => path.split(/[.[]/)[0] || path;
|
|
1079
|
+
function fixesFor(r) {
|
|
1080
|
+
const out = [];
|
|
1081
|
+
const owner = r.owner || r.parent && r.parent.name || "?";
|
|
1082
|
+
const changes = [...r.propChanges, ...r.stateChanges, ...r.hookChanges];
|
|
1083
|
+
if (r.avoidable && (changes.length === 0 || r.memoized === false)) {
|
|
1084
|
+
out.push({
|
|
1085
|
+
kind: "memo",
|
|
1086
|
+
owner: r.component,
|
|
1087
|
+
target: r.component,
|
|
1088
|
+
prop: null,
|
|
1089
|
+
label: `Wrap <${r.component}> in React.memo`,
|
|
1090
|
+
detail: changes.length === 0 ? `<${r.component}> re-rendered with identical props because <${r.parent && r.parent.name || "its parent"}> re-rendered.` : `<${r.component}> is not memoized: fixing its props alone will not stop the re-render.`
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
for (const c of r.propChanges) {
|
|
1094
|
+
if (!AVOIDABLE.has(c.kind)) continue;
|
|
1095
|
+
const root = rootOf(c.path);
|
|
1096
|
+
if (root === "children" && (c.kind === "element" || c.kind === "deep-equal")) {
|
|
1097
|
+
out.push({ kind: "children", owner, target: r.component, prop: "children", label: `memoize children of <${r.component}> in <${owner}>`, detail: `<${owner}> re-creates the children of <${r.component}> on every render.` });
|
|
1098
|
+
} else if (c.kind === "function") {
|
|
1099
|
+
out.push({ kind: "useCallback", owner, target: r.component, prop: root, label: `useCallback(${root}) in <${owner}>`, detail: `prop "${c.path}" of <${r.component}> is a new function on every render of <${owner}>.` });
|
|
1100
|
+
} else if (c.kind === "element") {
|
|
1101
|
+
out.push({ kind: "useMemoElement", owner, target: r.component, prop: root, label: `memoize element prop ${root} in <${owner}>`, detail: `prop "${c.path}" of <${r.component}> is a new element with the same type and props on every render of <${owner}>.` });
|
|
1102
|
+
} else {
|
|
1103
|
+
out.push({ kind: "useMemo", owner, target: r.component, prop: root, label: `useMemo(${root}) in <${owner}>`, detail: `prop "${c.path}" of <${r.component}> is a new ${Array.isArray(c.next) ? "array" : "object"} with the same contents on every render of <${owner}>.` });
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
for (const c of [...r.stateChanges, ...r.hookChanges]) {
|
|
1107
|
+
const isContext = c.hook === "useContext";
|
|
1108
|
+
const ctxName = isContext ? (/useContext\((.*)\)/.exec(c.path) || [])[1] || "Context" : "";
|
|
1109
|
+
const providerOwner = isContext && c.provider && c.provider.component ? c.provider.component : null;
|
|
1110
|
+
if (isContext && c.kind === "different" && c.changedKeys && typeof c.totalKeys === "number" && c.changedKeys.length > 0 && c.changedKeys.length < c.totalKeys) {
|
|
1111
|
+
out.push({
|
|
1112
|
+
kind: "splitContext",
|
|
1113
|
+
owner: providerOwner || `${ctxName}.Provider`,
|
|
1114
|
+
target: r.component,
|
|
1115
|
+
prop: ctxName,
|
|
1116
|
+
label: `split ${ctxName}${providerOwner ? ` in <${providerOwner}>` : ""}: only ${c.changedKeys.join(", ")} changed`,
|
|
1117
|
+
detail: `${c.changedKeys.length} of ${c.totalKeys} keys changed in ${ctxName}, yet every consumer re-rendered.`
|
|
1118
|
+
});
|
|
1119
|
+
continue;
|
|
1120
|
+
}
|
|
1121
|
+
if (!AVOIDABLE.has(c.kind)) continue;
|
|
1122
|
+
if (isContext) {
|
|
1123
|
+
out.push({ kind: "contextValue", owner: providerOwner || `${ctxName}.Provider`, target: r.component, prop: ctxName, label: `memoize the ${ctxName} provider value${providerOwner ? ` in <${providerOwner}>` : ""}`, detail: `${ctxName} produced a new value that is deep-equal to the previous one.` });
|
|
1124
|
+
} else if (c.hook === "useSyncExternalStore") {
|
|
1125
|
+
const chain = c.custom || [];
|
|
1126
|
+
const redux = chain.some((n) => /^use(App)?Selector$/.test(n));
|
|
1127
|
+
const zustand = !redux && chain.some((n) => /^use[A-Z]\w*Store$/.test(n) || n === "useStore" || n === "useBoundStore");
|
|
1128
|
+
out.push({ kind: "storeSnapshot", owner: r.component, target: r.component, prop: c.path, label: redux ? `memoize the selector in <${r.component}>` : zustand ? `useShallow in <${r.component}>` : `stable getSnapshot in <${r.component}>`, detail: storeAdvice(c.custom) });
|
|
1129
|
+
} else {
|
|
1130
|
+
out.push({ kind: "bailout", owner: r.component, target: r.component, prop: c.path, label: `bail out before setting ${c.path} in <${r.component}>`, detail: `${c.path} was set to a value deep-equal to the current one (new reference).` });
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
return out;
|
|
1134
|
+
}
|
|
1135
|
+
var fixKey = (f) => `${f.kind}|${f.owner}|${f.prop || f.target}`;
|
|
1136
|
+
function rankFixes(reports) {
|
|
1137
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
1138
|
+
for (const r of reports) {
|
|
1139
|
+
if (!r.avoidable) continue;
|
|
1140
|
+
for (const f of fixesFor(r)) {
|
|
1141
|
+
const key = fixKey(f);
|
|
1142
|
+
let agg = byKey.get(key);
|
|
1143
|
+
if (!agg) {
|
|
1144
|
+
agg = { ...f, key, count: 0, components: {} };
|
|
1145
|
+
byKey.set(key, agg);
|
|
1146
|
+
}
|
|
1147
|
+
agg.count++;
|
|
1148
|
+
agg.components[r.component] = (agg.components[r.component] || 0) + 1;
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
return [...byKey.values()].sort((a, b) => b.count - a.count || a.label.localeCompare(b.label));
|
|
1152
|
+
}
|
|
1153
|
+
function formatFixes(reports, limit = 10) {
|
|
1154
|
+
const ranked = rankFixes(reports);
|
|
1155
|
+
if (!ranked.length) return "No avoidable re-renders.";
|
|
1156
|
+
const lines = ranked.slice(0, limit).map((f, i) => `${String(i + 1).padStart(2)}. ${f.label} (removes ${f.count}: ${Object.entries(f.components).map(([c, n]) => `<${c}>${n > 1 ? " x" + n : ""}`).join(", ")})`);
|
|
1157
|
+
if (ranked.length > limit) lines.push(` \u2026 ${ranked.length - limit} more`);
|
|
1158
|
+
return lines.join("\n");
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
// src/sessions.ts
|
|
1162
|
+
function summarizeReports(reports, meta = {}) {
|
|
1163
|
+
var _a;
|
|
1164
|
+
const byComponent = {};
|
|
1165
|
+
let avoidable = 0;
|
|
1166
|
+
let wasted = 0;
|
|
1167
|
+
for (const r of reports) {
|
|
1168
|
+
const c = byComponent[_a = r.component] || (byComponent[_a] = { total: 0, avoidable: 0, wasted: 0 });
|
|
1169
|
+
c.total++;
|
|
1170
|
+
if (r.avoidable) {
|
|
1171
|
+
c.avoidable++;
|
|
1172
|
+
avoidable++;
|
|
1173
|
+
if (typeof r.selfDuration === "number") {
|
|
1174
|
+
c.wasted += r.selfDuration;
|
|
1175
|
+
wasted += r.selfDuration;
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
return {
|
|
1180
|
+
id: meta.id ?? `s${Date.now().toString(36)}`,
|
|
1181
|
+
name: meta.name ?? "session",
|
|
1182
|
+
startedAt: meta.startedAt ?? (reports[0]?.time ?? 0),
|
|
1183
|
+
endedAt: meta.endedAt ?? (reports[reports.length - 1]?.time ?? null),
|
|
1184
|
+
total: reports.length,
|
|
1185
|
+
avoidable,
|
|
1186
|
+
wasted,
|
|
1187
|
+
byComponent,
|
|
1188
|
+
fixes: rankFixes(reports).map((f) => ({ key: f.key, label: f.label, count: f.count }))
|
|
1189
|
+
};
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
// src/budget.ts
|
|
1193
|
+
function avoidableCounts(reports) {
|
|
1194
|
+
const counts2 = {};
|
|
1195
|
+
for (const r of reports) if (r.avoidable) counts2[r.component] = (counts2[r.component] || 0) + 1;
|
|
1196
|
+
return counts2;
|
|
1197
|
+
}
|
|
1198
|
+
function checkBudget(reports, budget) {
|
|
1199
|
+
const b = typeof budget === "number" ? { "*": budget } : budget;
|
|
1200
|
+
const fallback = typeof b["*"] === "number" ? b["*"] : 0;
|
|
1201
|
+
const counts2 = avoidableCounts(reports);
|
|
1202
|
+
const violations = [];
|
|
1203
|
+
const slack = [];
|
|
1204
|
+
const names = /* @__PURE__ */ new Set([...Object.keys(counts2), ...Object.keys(b).filter((k) => k !== "*")]);
|
|
1205
|
+
for (const component of names) {
|
|
1206
|
+
const allowed = typeof b[component] === "number" ? b[component] : fallback;
|
|
1207
|
+
const avoidable = counts2[component] || 0;
|
|
1208
|
+
if (avoidable > allowed) violations.push({ component, avoidable, allowed });
|
|
1209
|
+
else if (avoidable < allowed) slack.push({ component, avoidable, allowed });
|
|
1210
|
+
}
|
|
1211
|
+
violations.sort((x, y) => y.avoidable - y.allowed - (x.avoidable - x.allowed));
|
|
1212
|
+
return { ok: violations.length === 0, violations, slack, counts: counts2 };
|
|
1213
|
+
}
|
|
1214
|
+
function assertWithinBudget(reports, budget) {
|
|
1215
|
+
const result = checkBudget(reports, budget);
|
|
1216
|
+
if (result.ok) return result;
|
|
1217
|
+
const lines = result.violations.map((v) => ` <${v.component}>: ${v.avoidable} avoidable re-render${v.avoidable === 1 ? "" : "s"} (budget ${v.allowed})`);
|
|
1218
|
+
throw new Error(`Re-render budget exceeded:
|
|
1219
|
+
${lines.join("\n")}
|
|
1220
|
+
|
|
1221
|
+
Fixes, most impact first:
|
|
1222
|
+
${formatFixes(reports)}`);
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
// src/notifiers.ts
|
|
1226
|
+
function createCollector() {
|
|
1227
|
+
const reports = [];
|
|
1228
|
+
return {
|
|
1229
|
+
reports,
|
|
1230
|
+
get avoidable() {
|
|
1231
|
+
return reports.filter((r) => r.avoidable);
|
|
1232
|
+
},
|
|
1233
|
+
notifier: (r) => {
|
|
1234
|
+
reports.push(r);
|
|
1235
|
+
},
|
|
1236
|
+
clear: () => {
|
|
1237
|
+
reports.length = 0;
|
|
1238
|
+
},
|
|
1239
|
+
assertNoAvoidable() {
|
|
1240
|
+
const bad = reports.filter((r) => r.avoidable);
|
|
1241
|
+
if (bad.length === 0) return;
|
|
1242
|
+
const lines = bad.map((r) => ` <${r.component}> render #${r.renderCount}:
|
|
1243
|
+
${r.reasons.map((x) => ` - ${x}`).join("\n")}`);
|
|
1244
|
+
throw new Error(`${bad.length} avoidable re-render${bad.length === 1 ? "" : "s"} detected:
|
|
1245
|
+
${lines.join("\n")}
|
|
1246
|
+
|
|
1247
|
+
Fixes, most impact first:
|
|
1248
|
+
${formatFixes(reports)}`);
|
|
1249
|
+
},
|
|
1250
|
+
fixes: () => rankFixes(reports),
|
|
1251
|
+
summary: (name) => summarizeReports(reports, name ? { name } : {}),
|
|
1252
|
+
assertWithinBudget: (budget) => assertWithinBudget(reports, budget)
|
|
1253
|
+
};
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
// src/vitest.ts
|
|
1257
|
+
var OUT_ENV = "RERENDER_LENS_OUT";
|
|
1258
|
+
function setupRerenderLens(options = {}, hooks = {}) {
|
|
1259
|
+
ensureDevtoolsHook();
|
|
1260
|
+
const { failFast, ...rest } = options;
|
|
1261
|
+
const collector = createCollector();
|
|
1262
|
+
init({ trackAllMemoized: true, silent: true, ...rest, notifier: collector.notifier });
|
|
1263
|
+
const flush = () => {
|
|
1264
|
+
const reports = collector.reports.slice();
|
|
1265
|
+
if (!reports.length) return;
|
|
1266
|
+
const file = process.env[OUT_ENV];
|
|
1267
|
+
if (file) appendFileSync(file, JSON.stringify({ file: hooks.testPath?.() ?? "", reports: reports.map(slim) }) + "\n");
|
|
1268
|
+
collector.clear();
|
|
1269
|
+
if (failFast) {
|
|
1270
|
+
const avoidable = reports.filter((r) => r.avoidable);
|
|
1271
|
+
if (avoidable.length) throw new Error(`rerender-lens: ${avoidable.length} avoidable re-render${avoidable.length === 1 ? "" : "s"} in this file
|
|
1272
|
+
${formatFixes(avoidable, 5)}`);
|
|
1273
|
+
}
|
|
1274
|
+
};
|
|
1275
|
+
hooks.afterAll?.(flush);
|
|
1276
|
+
return { collector, flush };
|
|
1277
|
+
}
|
|
1278
|
+
function slim(r) {
|
|
1279
|
+
const value = (v) => Array.isArray(v) ? [] : typeof v === "function" ? `\u0192 ${v.name || ""}` : v && typeof v === "object" ? {} : v;
|
|
1280
|
+
return {
|
|
1281
|
+
...r,
|
|
1282
|
+
propChanges: r.propChanges.map((c) => ({ ...c, prev: value(c.prev), next: value(c.next) })),
|
|
1283
|
+
stateChanges: r.stateChanges.map((c) => ({ ...c, prev: value(c.prev), next: value(c.next) })),
|
|
1284
|
+
hookChanges: r.hookChanges.map((c) => ({ ...c, prev: value(c.prev), next: value(c.next) })),
|
|
1285
|
+
props: { prev: {}, next: {} },
|
|
1286
|
+
hookState: void 0,
|
|
1287
|
+
contexts: void 0,
|
|
1288
|
+
state: void 0
|
|
1289
|
+
};
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1292
|
+
// src/vitest-setup.ts
|
|
1293
|
+
var lens = setupRerenderLens({}, { afterAll, testPath: () => expect.getState().testPath });
|
|
1294
|
+
|
|
1295
|
+
export { lens };
|
|
1296
|
+
//# sourceMappingURL=vitest-setup.js.map
|
|
1297
|
+
//# sourceMappingURL=vitest-setup.js.map
|