react-code-locator 0.1.17 → 0.2.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.
Files changed (60) hide show
  1. package/README.md +89 -266
  2. package/dist/client.cjs +1 -399
  3. package/dist/client.cjs.map +1 -1
  4. package/dist/client.d.cts +1 -1
  5. package/dist/client.d.ts +1 -1
  6. package/dist/client.js +1 -371
  7. package/dist/client.js.map +1 -1
  8. package/dist/esbuild.cjs +29 -7149
  9. package/dist/esbuild.cjs.map +1 -1
  10. package/dist/esbuild.d.cts +1 -1
  11. package/dist/esbuild.d.ts +1 -1
  12. package/dist/esbuild.js +29 -7122
  13. package/dist/esbuild.js.map +1 -1
  14. package/dist/index.cjs +42 -398
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.cts +6 -0
  17. package/dist/index.d.ts +6 -0
  18. package/dist/index.js +42 -370
  19. package/dist/index.js.map +1 -1
  20. package/dist/swc.cjs +29 -7119
  21. package/dist/swc.cjs.map +1 -1
  22. package/dist/swc.d.cts +1 -1
  23. package/dist/swc.d.ts +1 -1
  24. package/dist/swc.js +29 -7090
  25. package/dist/swc.js.map +1 -1
  26. package/dist/{transform-CXh-m5Ez.d.cts → transform-z2n_1yc5.d.cts} +2 -2
  27. package/dist/{transform-CXh-m5Ez.d.ts → transform-z2n_1yc5.d.ts} +2 -2
  28. package/dist/unplugin.cjs +32 -7135
  29. package/dist/unplugin.cjs.map +1 -1
  30. package/dist/unplugin.d.cts +16 -5
  31. package/dist/unplugin.d.ts +16 -5
  32. package/dist/unplugin.js +32 -7106
  33. package/dist/unplugin.js.map +1 -1
  34. package/dist/webpack.cjs +36 -88
  35. package/dist/webpack.cjs.map +1 -1
  36. package/dist/webpack.d.cts +11 -51
  37. package/dist/webpack.d.ts +11 -51
  38. package/dist/webpack.js +36 -58
  39. package/dist/webpack.js.map +1 -1
  40. package/package.json +10 -34
  41. package/dist/core/transform-cjs.cjs +0 -7014
  42. package/dist/core/webpackPitchLoader-cjs.cjs +0 -7032
  43. package/dist/vite.cjs +0 -7200
  44. package/dist/vite.cjs.map +0 -1
  45. package/dist/vite.d.cts +0 -52
  46. package/dist/vite.d.ts +0 -52
  47. package/dist/vite.js +0 -7169
  48. package/dist/vite.js.map +0 -1
  49. package/dist/webpackLoader.cjs +0 -7115
  50. package/dist/webpackLoader.cjs.map +0 -1
  51. package/dist/webpackLoader.d.cts +0 -15
  52. package/dist/webpackLoader.d.ts +0 -15
  53. package/dist/webpackLoader.js +0 -7092
  54. package/dist/webpackLoader.js.map +0 -1
  55. package/dist/webpackRuntimeEntry.cjs +0 -375
  56. package/dist/webpackRuntimeEntry.cjs.map +0 -1
  57. package/dist/webpackRuntimeEntry.d.cts +0 -2
  58. package/dist/webpackRuntimeEntry.d.ts +0 -2
  59. package/dist/webpackRuntimeEntry.js +0 -373
  60. package/dist/webpackRuntimeEntry.js.map +0 -1
package/dist/client.cjs CHANGED
@@ -1,400 +1,2 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/client.ts
21
- var client_exports = {};
22
- __export(client_exports, {
23
- enableReactComponentJump: () => enableReactComponentJump,
24
- locateComponentSource: () => locateComponentSource
25
- });
26
- module.exports = __toCommonJS(client_exports);
27
-
28
- // src/constants.ts
29
- var SOURCE_PROP = "__componentSourceLoc";
30
- var JSX_SOURCE_PROP = "$componentSourceLoc";
31
- var JSX_SOURCE_REGISTRY_SYMBOL = "react-code-locator.jsxSourceRegistry";
32
-
33
- // src/sourceMetadata.ts
34
- function normalizeSlashes(value) {
35
- return value.replace(/\\/g, "/");
36
- }
37
- function trimTrailingSlash(value) {
38
- return value.replace(/\/+$/, "");
39
- }
40
- function splitPathSegments(value) {
41
- return normalizeSlashes(value).split("/").filter(Boolean);
42
- }
43
- function computeRelativePath(fromPath, toPath) {
44
- const fromSegments = splitPathSegments(fromPath);
45
- const toSegments = splitPathSegments(toPath);
46
- let sharedIndex = 0;
47
- while (sharedIndex < fromSegments.length && sharedIndex < toSegments.length && fromSegments[sharedIndex] === toSegments[sharedIndex]) {
48
- sharedIndex += 1;
49
- }
50
- const upSegments = new Array(Math.max(0, fromSegments.length - sharedIndex)).fill("..");
51
- const downSegments = toSegments.slice(sharedIndex);
52
- const relativeSegments = [...upSegments, ...downSegments];
53
- return relativeSegments.length > 0 ? relativeSegments.join("/") : ".";
54
- }
55
- function normalizeProjectRoot(projectRoot) {
56
- if (projectRoot) {
57
- return trimTrailingSlash(normalizeSlashes(projectRoot));
58
- }
59
- return "";
60
- }
61
- function getSourceFile(source) {
62
- if (!source) {
63
- return null;
64
- }
65
- const match = source.match(/^(.*):\d+:\d+$/);
66
- return match?.[1] ?? null;
67
- }
68
- function isProjectLocalFile(filename, projectRoot) {
69
- if (!filename) {
70
- return false;
71
- }
72
- const root = normalizeProjectRoot(projectRoot);
73
- const normalizedFilename = normalizeSlashes(filename);
74
- if (!root) {
75
- return !normalizedFilename.startsWith("../") && !normalizedFilename.startsWith("/") && !/^[A-Za-z]:\//.test(normalizedFilename);
76
- }
77
- if (normalizedFilename.startsWith(`${root}/`) || normalizedFilename === root) {
78
- return true;
79
- }
80
- const relativePath = computeRelativePath(root, normalizedFilename);
81
- return !relativePath.startsWith("../");
82
- }
83
- function isProjectLocalSource(source, projectRoot) {
84
- const file = getSourceFile(source);
85
- return isProjectLocalFile(file ?? void 0, projectRoot);
86
- }
87
-
88
- // src/runtime.ts
89
- function isTriggerPressed(event, triggerKey) {
90
- if (triggerKey === "none") {
91
- return true;
92
- }
93
- if (triggerKey === "alt") {
94
- return event.altKey;
95
- }
96
- if (triggerKey === "meta") {
97
- return event.metaKey;
98
- }
99
- if (triggerKey === "ctrl") {
100
- return event.ctrlKey;
101
- }
102
- return event.shiftKey;
103
- }
104
- function getReactFiberKey(element) {
105
- return Object.keys(element).find((key) => key.startsWith("__reactFiber$") || key.startsWith("__reactInternalInstance$"));
106
- }
107
- function getClosestReactFiber(target) {
108
- let current = target;
109
- while (current) {
110
- const fiberKey = getReactFiberKey(current);
111
- if (fiberKey) {
112
- return current[fiberKey];
113
- }
114
- current = current.parentElement;
115
- }
116
- return null;
117
- }
118
- function getSourceFromType(type) {
119
- if (!type) {
120
- return null;
121
- }
122
- if (typeof type === "function") {
123
- const source2 = type[SOURCE_PROP];
124
- return typeof source2 === "string" ? source2 : null;
125
- }
126
- if (typeof type !== "object") {
127
- return null;
128
- }
129
- const record = type;
130
- const source = record[SOURCE_PROP] ?? record.type?.[SOURCE_PROP] ?? record.render?.[SOURCE_PROP];
131
- return typeof source === "string" ? source : null;
132
- }
133
- function getSourceFromProps(props) {
134
- if (props && typeof props === "object") {
135
- const registry = globalThis[Symbol.for(JSX_SOURCE_REGISTRY_SYMBOL)];
136
- if (registry instanceof WeakMap) {
137
- const intrinsicSource = registry.get(props);
138
- if (typeof intrinsicSource === "string") {
139
- return intrinsicSource;
140
- }
141
- }
142
- }
143
- const source = props?.[JSX_SOURCE_PROP];
144
- return typeof source === "string" ? source : null;
145
- }
146
- function resolveComponentSourceFromFiber(fiber) {
147
- let current = fiber;
148
- while (current) {
149
- const source = getSourceFromType(current.type) ?? getSourceFromType(current.elementType);
150
- if (source) {
151
- return source;
152
- }
153
- current = current.return ?? null;
154
- }
155
- return null;
156
- }
157
- function getDirectDebugSource(fiber) {
158
- const debugSource = fiber?._debugSource;
159
- if (debugSource?.fileName && typeof debugSource.lineNumber === "number") {
160
- return `${debugSource.fileName.replace(/\\/g, "/")}:${debugSource.lineNumber}:${debugSource.columnNumber ?? 1}`;
161
- }
162
- return null;
163
- }
164
- function resolveSourceCandidates(fiber) {
165
- let current = fiber;
166
- const jsxCandidates = [];
167
- const componentCandidates = [];
168
- while (current) {
169
- const jsxSource = getSourceFromProps(current.pendingProps) ?? getSourceFromProps(current.memoizedProps) ?? getDirectDebugSource(current);
170
- if (jsxSource) {
171
- const file = getSourceFile(jsxSource);
172
- if (file && !jsxCandidates.some((candidate) => candidate.source === jsxSource)) {
173
- jsxCandidates.push({ source: jsxSource, file });
174
- }
175
- }
176
- const componentSource = getSourceFromType(current.type) ?? getSourceFromType(current.elementType);
177
- if (componentSource) {
178
- const file = getSourceFile(componentSource);
179
- if (file && !componentCandidates.some((candidate) => candidate.source === componentSource)) {
180
- componentCandidates.push({ source: componentSource, file });
181
- }
182
- }
183
- current = current.return ?? null;
184
- }
185
- const direct = jsxCandidates[0]?.source ?? null;
186
- const nearestProjectLocalComponentFile = componentCandidates.find((candidate) => isProjectLocalSource(candidate.source))?.file;
187
- let screen = null;
188
- if (nearestProjectLocalComponentFile) {
189
- const matchingJsxCandidate = jsxCandidates.find((candidate) => candidate.file === nearestProjectLocalComponentFile);
190
- if (matchingJsxCandidate) {
191
- screen = matchingJsxCandidate.source;
192
- } else {
193
- const matchingComponentCandidate = componentCandidates.find(
194
- (candidate) => candidate.file === nearestProjectLocalComponentFile
195
- );
196
- if (matchingComponentCandidate) {
197
- screen = matchingComponentCandidate.source;
198
- }
199
- }
200
- }
201
- const implementationComponentCandidate = componentCandidates.find((candidate) => !isProjectLocalSource(candidate.source))?.source ?? null;
202
- const implementationJsxCandidate = jsxCandidates.find((candidate) => !isProjectLocalSource(candidate.source))?.source ?? null;
203
- const projectLocalJsxCandidate = jsxCandidates.find((candidate) => isProjectLocalSource(candidate.source))?.source ?? null;
204
- const screenFallback = screen ?? projectLocalJsxCandidate ?? componentCandidates.find((candidate) => isProjectLocalSource(candidate.source))?.source ?? null;
205
- return {
206
- direct: direct ?? screenFallback,
207
- screen: screenFallback,
208
- implementation: implementationComponentCandidate ?? implementationJsxCandidate ?? screenFallback
209
- };
210
- }
211
- function getModeDescription(mode) {
212
- if (mode === "direct") {
213
- return "Direct JSX";
214
- }
215
- if (mode === "screen") {
216
- return "Screen source";
217
- }
218
- return "Implementation source";
219
- }
220
- function createStatusOverlay(triggerKey) {
221
- if (typeof document === "undefined") {
222
- return null;
223
- }
224
- const element = document.createElement("div");
225
- let copyValue = null;
226
- let currentMode = "screen";
227
- let hideTimer = null;
228
- element.setAttribute("data-react-code-locator", "true");
229
- Object.assign(element.style, {
230
- position: "fixed",
231
- right: "12px",
232
- bottom: "12px",
233
- zIndex: "2147483647",
234
- padding: "8px 10px",
235
- borderRadius: "8px",
236
- background: "rgba(17, 24, 39, 0.92)",
237
- color: "#fff",
238
- fontSize: "12px",
239
- lineHeight: "1.4",
240
- fontFamily: "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace",
241
- boxShadow: "0 8px 30px rgba(0, 0, 0, 0.25)",
242
- pointerEvents: "auto",
243
- cursor: "pointer",
244
- maxWidth: "min(70vw, 720px)",
245
- wordBreak: "break-all",
246
- opacity: "0",
247
- transition: "opacity 120ms ease"
248
- });
249
- const show = (message, tone) => {
250
- element.textContent = message;
251
- element.style.background = tone === "success" ? "rgba(6, 95, 70, 0.92)" : tone === "error" ? "rgba(153, 27, 27, 0.94)" : "rgba(17, 24, 39, 0.92)";
252
- element.style.opacity = "1";
253
- element.style.pointerEvents = "auto";
254
- if (hideTimer) {
255
- clearTimeout(hideTimer);
256
- }
257
- hideTimer = setTimeout(() => {
258
- element.style.opacity = "0";
259
- element.style.pointerEvents = "none";
260
- }, 2e3);
261
- };
262
- element.addEventListener("click", async () => {
263
- if (!copyValue) {
264
- return;
265
- }
266
- try {
267
- await navigator.clipboard.writeText(copyValue);
268
- show(`[react-code-locator] copied`, "success");
269
- } catch {
270
- show(`[react-code-locator] copy failed`, "error");
271
- }
272
- });
273
- show(`[react-code-locator] enabled (${triggerKey}+click, alt+1/2/3 to switch mode)`, "idle");
274
- const mount = () => {
275
- if (!element.isConnected && document.body) {
276
- document.body.appendChild(element);
277
- }
278
- };
279
- if (document.body) {
280
- mount();
281
- } else {
282
- document.addEventListener("DOMContentLoaded", mount, { once: true });
283
- }
284
- return {
285
- setStatus(message, tone = "idle") {
286
- show(message, tone);
287
- },
288
- setCopyValue(value) {
289
- copyValue = value;
290
- },
291
- setMode(mode) {
292
- currentMode = mode;
293
- show(`[react-code-locator] ${getModeDescription(mode)}`, "idle");
294
- },
295
- remove() {
296
- if (hideTimer) {
297
- clearTimeout(hideTimer);
298
- }
299
- element.remove();
300
- }
301
- };
302
- }
303
- function locateComponentSource(target, mode = "screen") {
304
- const elementTarget = target instanceof Element ? target : target instanceof Node ? target.parentElement : null;
305
- const fiber = getClosestReactFiber(elementTarget);
306
- if (!fiber) {
307
- return null;
308
- }
309
- const candidates = resolveSourceCandidates(fiber);
310
- const source = candidates[mode] ?? candidates.screen ?? candidates.direct ?? candidates.implementation;
311
- if (source) {
312
- return {
313
- source,
314
- mode
315
- };
316
- }
317
- const componentSource = resolveComponentSourceFromFiber(fiber);
318
- if (!componentSource) {
319
- return null;
320
- }
321
- return {
322
- source: componentSource,
323
- mode
324
- };
325
- }
326
- function enableReactComponentJump(options = {}) {
327
- const overlay = createStatusOverlay(options.triggerKey ?? "shift");
328
- let currentMode = "screen";
329
- const {
330
- triggerKey = "shift",
331
- onLocate = (result) => {
332
- console.log(`[react-code-locator] ${result.source}`);
333
- overlay?.setCopyValue(result.source);
334
- overlay?.setStatus(`[react-code-locator] ${result.source}`, "success");
335
- },
336
- onError = (error) => {
337
- console.error("[react-code-locator]", error);
338
- const message = error instanceof Error ? error.message : String(error);
339
- overlay?.setCopyValue(null);
340
- overlay?.setStatus(`[react-code-locator] ${message}`, "error");
341
- }
342
- } = options;
343
- console.log("[react-code-locator] enabled", { triggerKey });
344
- const keyHandler = (event) => {
345
- if (!event.altKey) {
346
- return;
347
- }
348
- if (event.code === "Digit1") {
349
- currentMode = "direct";
350
- overlay?.setMode(currentMode);
351
- event.preventDefault();
352
- return;
353
- }
354
- if (event.code === "Digit2") {
355
- currentMode = "screen";
356
- overlay?.setMode(currentMode);
357
- event.preventDefault();
358
- return;
359
- }
360
- if (event.code === "Digit3") {
361
- currentMode = "implementation";
362
- overlay?.setMode(currentMode);
363
- event.preventDefault();
364
- }
365
- };
366
- const handler = (event) => {
367
- console.log("[react-code-locator] click", {
368
- triggerKey,
369
- shiftKey: event.shiftKey,
370
- altKey: event.altKey,
371
- ctrlKey: event.ctrlKey,
372
- metaKey: event.metaKey,
373
- target: event.target
374
- });
375
- if (!isTriggerPressed(event, triggerKey)) {
376
- return;
377
- }
378
- const result = locateComponentSource(event.target, currentMode);
379
- if (!result) {
380
- onError(new Error("No React component source metadata found for clicked element."));
381
- return;
382
- }
383
- event.preventDefault();
384
- event.stopPropagation();
385
- onLocate(result);
386
- };
387
- document.addEventListener("click", handler, true);
388
- document.addEventListener("keydown", keyHandler, true);
389
- return () => {
390
- document.removeEventListener("click", handler, true);
391
- document.removeEventListener("keydown", keyHandler, true);
392
- overlay?.remove();
393
- };
394
- }
395
- // Annotate the CommonJS export names for ESM import in node:
396
- 0 && (module.exports = {
397
- enableReactComponentJump,
398
- locateComponentSource
399
- });
1
+ "use strict";var h=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var O=(t,e)=>{for(var n in e)h(t,n,{get:e[n],enumerable:!0})},T=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let c of P(e))!M.call(t,c)&&c!==n&&h(t,c,{get:()=>e[c],enumerable:!(o=k(e,c))||o.enumerable});return t};var F=t=>T(h({},"__esModule",{value:!0}),t);var X={};O(X,{enableReactComponentJump:()=>v,locateComponentSource:()=>x});module.exports=F(X);var p="__componentSourceLoc",C="$componentSourceLoc",E="react-code-locator.jsxSourceRegistry";function R(t){return t.replace(/\\/g,"/")}function _(t){return t.replace(/\/+$/,"")}function L(t){return R(t).split("/").filter(Boolean)}function K(t,e){let n=L(t),o=L(e),c=0;for(;c<n.length&&c<o.length&&n[c]===o[c];)c+=1;let s=new Array(Math.max(0,n.length-c)).fill(".."),l=o.slice(c),u=[...s,...l];return u.length>0?u.join("/"):"."}function $(t){return t?_(R(t)):""}function g(t){return t?t.match(/^(.*):\d+:\d+$/)?.[1]??null:null}function j(t,e){if(!t)return!1;let n=$(e),o=R(t);return n?o.startsWith(`${n}/`)||o===n?!0:!K(n,o).startsWith("../"):!o.startsWith("../")&&!o.startsWith("/")&&!/^[A-Za-z]:\//.test(o)}function f(t,e){let n=g(t);return j(n??void 0,e)}function D(t,e){return e==="none"?!0:e==="alt"?t.altKey:e==="meta"?t.metaKey:e==="ctrl"?t.ctrlKey:t.shiftKey}function N(t){return Object.keys(t).find(e=>e.startsWith("__reactFiber$")||e.startsWith("__reactInternalInstance$"))}function J(t){let e=t;for(;e;){let n=N(e);if(n)return e[n];e=e.parentElement}return null}function y(t){if(!t)return null;if(typeof t=="function"){let o=t[p];return typeof o=="string"?o:null}if(typeof t!="object")return null;let e=t,n=e[p]??e.type?.[p]??e.render?.[p];return typeof n=="string"?n:null}function w(t){if(t&&typeof t=="object"){let n=globalThis[Symbol.for(E)];if(n instanceof WeakMap){let o=n.get(t);if(typeof o=="string")return o}}let e=t?.[C];return typeof e=="string"?e:null}function z(t){let e=t;for(;e;){let n=y(e.type)??y(e.elementType);if(n)return n;e=e.return??null}return null}function W(t){let e=t?._debugSource;return e?.fileName&&typeof e.lineNumber=="number"?`${e.fileName.replace(/\\/g,"/")}:${e.lineNumber}:${e.columnNumber??1}`:null}function I(t){let e=t,n=[],o=[];for(;e;){let i=w(e.pendingProps)??w(e.memoizedProps)??W(e);if(i){let d=g(i);d&&!n.some(b=>b.source===i)&&n.push({source:i,file:d})}let a=y(e.type)??y(e.elementType);if(a){let d=g(a);d&&!o.some(b=>b.source===a)&&o.push({source:a,file:d})}e=e.return??null}let c=n[0]?.source??null,s=o.find(i=>f(i.source))?.file,l=null;if(s){let i=n.find(a=>a.file===s);if(i)l=i.source;else{let a=o.find(d=>d.file===s);a&&(l=a.source)}}let u=o.find(i=>!f(i.source))?.source??null,r=n.find(i=>!f(i.source))?.source??null,m=n.find(i=>f(i.source))?.source??null,S=l??m??o.find(i=>f(i.source))?.source??null;return{direct:c??S,screen:S,implementation:u??r??S}}function U(t){return t==="direct"?"Direct JSX":t==="screen"?"Screen source":"Implementation source"}function V(t){if(typeof document>"u")return null;let e=document.createElement("div"),n=null,o="screen",c=null;e.setAttribute("data-react-code-locator","true"),Object.assign(e.style,{position:"fixed",right:"12px",bottom:"12px",zIndex:"2147483647",padding:"8px 10px",borderRadius:"8px",background:"rgba(17, 24, 39, 0.92)",color:"#fff",fontSize:"12px",lineHeight:"1.4",fontFamily:"ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace",boxShadow:"0 8px 30px rgba(0, 0, 0, 0.25)",pointerEvents:"auto",cursor:"pointer",maxWidth:"min(70vw, 720px)",wordBreak:"break-all",opacity:"0",transition:"opacity 120ms ease"});let s=(u,r)=>{e.textContent=u,e.style.background=r==="success"?"rgba(6, 95, 70, 0.92)":r==="error"?"rgba(153, 27, 27, 0.94)":"rgba(17, 24, 39, 0.92)",e.style.opacity="1",e.style.pointerEvents="auto",c&&clearTimeout(c),c=setTimeout(()=>{e.style.opacity="0",e.style.pointerEvents="none"},2e3)};e.addEventListener("click",async()=>{if(n)try{await navigator.clipboard.writeText(n),s("[react-code-locator] copied","success")}catch{s("[react-code-locator] copy failed","error")}}),s(`[react-code-locator] enabled (${t}+click, alt+1/2/3 to switch mode)`,"idle");let l=()=>{!e.isConnected&&document.body&&document.body.appendChild(e)};return document.body?l():document.addEventListener("DOMContentLoaded",l,{once:!0}),{setStatus(u,r="idle"){s(u,r)},setCopyValue(u){n=u},setMode(u){o=u,s(`[react-code-locator] ${U(u)}`,"idle")},remove(){c&&clearTimeout(c),e.remove()}}}function x(t,e="screen"){let n=t instanceof Element?t:t instanceof Node?t.parentElement:null,o=J(n);if(!o)return null;let c=I(o),s=c[e]??c.screen??c.direct??c.implementation;if(s)return{source:s,mode:e};let l=z(o);return l?{source:l,mode:e}:null}function v(t={}){if(process.env.NODE_ENV!=="development")return;let e=V(t.triggerKey??"shift"),n="screen",{triggerKey:o="shift",onLocate:c=r=>{console.log(`[react-code-locator] ${r.source}`),e?.setCopyValue(r.source),e?.setStatus(`[react-code-locator] ${r.source}`,"success")},onError:s=r=>{console.error("[react-code-locator]",r);let m=r instanceof Error?r.message:String(r);e?.setCopyValue(null),e?.setStatus(`[react-code-locator] ${m}`,"error")}}=t;console.log("[react-code-locator] enabled",{triggerKey:o});let l=r=>{if(r.altKey){if(r.code==="Digit1"){n="direct",e?.setMode(n),r.preventDefault();return}if(r.code==="Digit2"){n="screen",e?.setMode(n),r.preventDefault();return}r.code==="Digit3"&&(n="implementation",e?.setMode(n),r.preventDefault())}},u=r=>{if(console.log("[react-code-locator] click",{triggerKey:o,shiftKey:r.shiftKey,altKey:r.altKey,ctrlKey:r.ctrlKey,metaKey:r.metaKey,target:r.target}),!D(r,o))return;let m=x(r.target,n);if(!m){s(new Error("No React component source metadata found for clicked element."));return}r.preventDefault(),r.stopPropagation(),c(m)};return document.addEventListener("click",u,!0),document.addEventListener("keydown",l,!0),()=>{document.removeEventListener("click",u,!0),document.removeEventListener("keydown",l,!0),e?.remove()}}0&&(module.exports={enableReactComponentJump,locateComponentSource});
400
2
  //# sourceMappingURL=client.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/client.ts","../src/constants.ts","../src/sourceMetadata.ts","../src/runtime.ts"],"sourcesContent":["export { enableReactComponentJump, locateComponentSource } from \"./runtime\";\nexport type { LocatorMode, LocatorOptions, LocatorResult, TriggerKey } from \"./runtime\";\n","export const SOURCE_PROP = \"__componentSourceLoc\";\nexport const JSX_SOURCE_PROP = \"$componentSourceLoc\";\nexport const JSX_SOURCE_REGISTRY_SYMBOL = \"react-code-locator.jsxSourceRegistry\";\n","export type SourceLocation = {\n line: number;\n column: number;\n};\n\nfunction normalizeSlashes(value: string) {\n return value.replace(/\\\\/g, \"/\");\n}\n\nfunction trimTrailingSlash(value: string) {\n return value.replace(/\\/+$/, \"\");\n}\n\nfunction splitPathSegments(value: string) {\n return normalizeSlashes(value).split(\"/\").filter(Boolean);\n}\n\nfunction computeRelativePath(fromPath: string, toPath: string) {\n const fromSegments = splitPathSegments(fromPath);\n const toSegments = splitPathSegments(toPath);\n\n let sharedIndex = 0;\n while (\n sharedIndex < fromSegments.length &&\n sharedIndex < toSegments.length &&\n fromSegments[sharedIndex] === toSegments[sharedIndex]\n ) {\n sharedIndex += 1;\n }\n\n const upSegments = new Array(Math.max(0, fromSegments.length - sharedIndex)).fill(\"..\");\n const downSegments = toSegments.slice(sharedIndex);\n const relativeSegments = [...upSegments, ...downSegments];\n return relativeSegments.length > 0 ? relativeSegments.join(\"/\") : \".\";\n}\n\nexport function normalizeProjectRoot(projectRoot?: string) {\n if (projectRoot) {\n return trimTrailingSlash(normalizeSlashes(projectRoot));\n }\n return \"\";\n}\n\nexport function toRelativeSource(\n filename: string | undefined,\n loc: SourceLocation | null | undefined,\n projectRoot?: string,\n) {\n if (!filename || !loc) {\n return null;\n }\n\n const root = normalizeProjectRoot(projectRoot);\n const normalizedFilename = normalizeSlashes(filename);\n const relPath =\n root && normalizedFilename.startsWith(`${root}/`)\n ? normalizedFilename.slice(root.length + 1)\n : root\n ? computeRelativePath(root, normalizedFilename)\n : normalizedFilename;\n return `${relPath}:${loc.line}:${loc.column + 1}`;\n}\n\nexport function getSourceFile(source: string | null) {\n if (!source) {\n return null;\n }\n\n const match = source.match(/^(.*):\\d+:\\d+$/);\n return match?.[1] ?? null;\n}\n\nexport function isProjectLocalFile(filename: string | undefined, projectRoot?: string) {\n if (!filename) {\n return false;\n }\n\n const root = normalizeProjectRoot(projectRoot);\n const normalizedFilename = normalizeSlashes(filename);\n\n if (!root) {\n return (\n !normalizedFilename.startsWith(\"../\") &&\n !normalizedFilename.startsWith(\"/\") &&\n !/^[A-Za-z]:\\//.test(normalizedFilename)\n );\n }\n\n if (normalizedFilename.startsWith(`${root}/`) || normalizedFilename === root) {\n return true;\n }\n\n const relativePath = computeRelativePath(root, normalizedFilename);\n return !relativePath.startsWith(\"../\");\n}\n\nexport function isExternalToProjectRoot(filename: string | undefined, projectRoot?: string) {\n return !isProjectLocalFile(filename, projectRoot);\n}\n\nexport function isProjectLocalSource(source: string, projectRoot?: string) {\n const file = getSourceFile(source);\n return isProjectLocalFile(file ?? undefined, projectRoot);\n}\n","import {\n JSX_SOURCE_PROP,\n JSX_SOURCE_REGISTRY_SYMBOL,\n SOURCE_PROP,\n} from \"./constants\";\nimport { getSourceFile, isProjectLocalSource } from \"./sourceMetadata\";\n\nexport type TriggerKey = \"alt\" | \"meta\" | \"ctrl\" | \"shift\" | \"none\";\nexport type LocatorMode = \"direct\" | \"screen\" | \"implementation\";\n\ntype ReactFiber = {\n return?: ReactFiber | null;\n type?: unknown;\n elementType?: unknown;\n pendingProps?: Record<string, unknown> | null;\n memoizedProps?: Record<string, unknown> | null;\n _debugOwner?: ReactFiber | null;\n _debugSource?: {\n fileName?: string;\n lineNumber?: number;\n columnNumber?: number;\n } | null;\n};\n\nexport type LocatorResult = {\n source: string;\n mode: LocatorMode;\n};\n\nexport type LocatorOptions = {\n triggerKey?: TriggerKey;\n onLocate?: (result: LocatorResult) => void;\n onError?: (error: unknown) => void;\n};\n\ntype StatusOverlay = {\n setStatus: (message: string, tone?: \"idle\" | \"success\" | \"error\") => void;\n setCopyValue: (value: string | null) => void;\n setMode: (mode: LocatorMode) => void;\n remove: () => void;\n};\n\nfunction isTriggerPressed(event: MouseEvent, triggerKey: TriggerKey) {\n if (triggerKey === \"none\") {\n return true;\n }\n\n if (triggerKey === \"alt\") {\n return event.altKey;\n }\n\n if (triggerKey === \"meta\") {\n return event.metaKey;\n }\n\n if (triggerKey === \"ctrl\") {\n return event.ctrlKey;\n }\n\n return event.shiftKey;\n}\n\nfunction getReactFiberKey(element: Element) {\n return Object.keys(element).find((key) => key.startsWith(\"__reactFiber$\") || key.startsWith(\"__reactInternalInstance$\"));\n}\n\nfunction getClosestReactFiber(target: Element | null) {\n let current = target;\n\n while (current) {\n const fiberKey = getReactFiberKey(current);\n if (fiberKey) {\n return (current as unknown as Record<string, unknown>)[fiberKey] as ReactFiber;\n }\n\n current = current.parentElement;\n }\n\n return null;\n}\n\nfunction getSourceFromType(type: unknown) {\n if (!type) {\n return null;\n }\n\n if (typeof type === \"function\") {\n const source = (type as unknown as Record<string, unknown>)[SOURCE_PROP];\n return typeof source === \"string\" ? source : null;\n }\n\n if (typeof type !== \"object\") {\n return null;\n }\n\n const record = type as {\n type?: Record<string, unknown>;\n render?: Record<string, unknown>;\n [SOURCE_PROP]?: unknown;\n };\n\n const source = record[SOURCE_PROP] ?? record.type?.[SOURCE_PROP] ?? record.render?.[SOURCE_PROP];\n return typeof source === \"string\" ? source : null;\n}\n\nfunction getSourceFromProps(props: Record<string, unknown> | null | undefined) {\n if (props && typeof props === \"object\") {\n const registry = (globalThis as Record<symbol, unknown>)[\n Symbol.for(JSX_SOURCE_REGISTRY_SYMBOL)\n ];\n if (registry instanceof WeakMap) {\n const intrinsicSource = registry.get(props as object);\n if (typeof intrinsicSource === \"string\") {\n return intrinsicSource;\n }\n }\n }\n\n const source = props?.[JSX_SOURCE_PROP];\n return typeof source === \"string\" ? source : null;\n}\n\nfunction resolveComponentSourceFromFiber(fiber: ReactFiber | null) {\n let current = fiber;\n\n while (current) {\n const source = getSourceFromType(current.type) ?? getSourceFromType(current.elementType);\n if (source) {\n return source;\n }\n\n current = current.return ?? null;\n }\n\n return null;\n}\n\nfunction getDirectDebugSource(fiber: ReactFiber | null) {\n const debugSource = fiber?._debugSource;\n if (debugSource?.fileName && typeof debugSource.lineNumber === \"number\") {\n return `${debugSource.fileName.replace(/\\\\/g, \"/\")}:${debugSource.lineNumber}:${debugSource.columnNumber ?? 1}`;\n }\n\n return null;\n}\n\ntype SourceCandidate = {\n source: string;\n file: string;\n};\n\ntype ResolvedCandidates = {\n direct: string | null;\n screen: string | null;\n implementation: string | null;\n};\n\nfunction resolveSourceCandidates(fiber: ReactFiber | null): ResolvedCandidates {\n let current = fiber;\n const jsxCandidates: SourceCandidate[] = [];\n const componentCandidates: SourceCandidate[] = [];\n\n while (current) {\n const jsxSource =\n getSourceFromProps(current.pendingProps) ?? getSourceFromProps(current.memoizedProps) ?? getDirectDebugSource(current);\n if (jsxSource) {\n const file = getSourceFile(jsxSource);\n if (file && !jsxCandidates.some((candidate) => candidate.source === jsxSource)) {\n jsxCandidates.push({ source: jsxSource, file });\n }\n }\n\n const componentSource = getSourceFromType(current.type) ?? getSourceFromType(current.elementType);\n if (componentSource) {\n const file = getSourceFile(componentSource);\n if (file && !componentCandidates.some((candidate) => candidate.source === componentSource)) {\n componentCandidates.push({ source: componentSource, file });\n }\n }\n\n current = current.return ?? null;\n }\n\n const direct = jsxCandidates[0]?.source ?? null;\n const nearestProjectLocalComponentFile = componentCandidates.find((candidate) => isProjectLocalSource(candidate.source))?.file;\n let screen: string | null = null;\n if (nearestProjectLocalComponentFile) {\n const matchingJsxCandidate = jsxCandidates.find((candidate) => candidate.file === nearestProjectLocalComponentFile);\n if (matchingJsxCandidate) {\n screen = matchingJsxCandidate.source;\n } else {\n const matchingComponentCandidate = componentCandidates.find(\n (candidate) => candidate.file === nearestProjectLocalComponentFile,\n );\n if (matchingComponentCandidate) {\n screen = matchingComponentCandidate.source;\n }\n }\n }\n\n const implementationComponentCandidate =\n componentCandidates.find((candidate) => !isProjectLocalSource(candidate.source))?.source ?? null;\n const implementationJsxCandidate =\n jsxCandidates.find((candidate) => !isProjectLocalSource(candidate.source))?.source ?? null;\n\n const projectLocalJsxCandidate = jsxCandidates.find((candidate) => isProjectLocalSource(candidate.source))?.source ?? null;\n const screenFallback = screen ?? projectLocalJsxCandidate ?? componentCandidates.find((candidate) => isProjectLocalSource(candidate.source))?.source ?? null;\n\n return {\n direct: direct ?? screenFallback,\n screen: screenFallback,\n implementation: implementationComponentCandidate ?? implementationJsxCandidate ?? screenFallback,\n };\n}\n\nfunction getModeDescription(mode: LocatorMode) {\n if (mode === \"direct\") {\n return \"Direct JSX\";\n }\n\n if (mode === \"screen\") {\n return \"Screen source\";\n }\n\n return \"Implementation source\";\n}\n\nfunction createStatusOverlay(triggerKey: TriggerKey): StatusOverlay | null {\n if (typeof document === \"undefined\") {\n return null;\n }\n\n const element = document.createElement(\"div\");\n let copyValue: string | null = null;\n let currentMode: LocatorMode = \"screen\";\n let hideTimer: ReturnType<typeof setTimeout> | null = null;\n element.setAttribute(\"data-react-code-locator\", \"true\");\n Object.assign(element.style, {\n position: \"fixed\",\n right: \"12px\",\n bottom: \"12px\",\n zIndex: \"2147483647\",\n padding: \"8px 10px\",\n borderRadius: \"8px\",\n background: \"rgba(17, 24, 39, 0.92)\",\n color: \"#fff\",\n fontSize: \"12px\",\n lineHeight: \"1.4\",\n fontFamily: \"ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace\",\n boxShadow: \"0 8px 30px rgba(0, 0, 0, 0.25)\",\n pointerEvents: \"auto\",\n cursor: \"pointer\",\n maxWidth: \"min(70vw, 720px)\",\n wordBreak: \"break-all\",\n opacity: \"0\",\n transition: \"opacity 120ms ease\",\n });\n\n const show = (message: string, tone: \"idle\" | \"success\" | \"error\") => {\n element.textContent = message;\n element.style.background =\n tone === \"success\"\n ? \"rgba(6, 95, 70, 0.92)\"\n : tone === \"error\"\n ? \"rgba(153, 27, 27, 0.94)\"\n : \"rgba(17, 24, 39, 0.92)\";\n element.style.opacity = \"1\";\n element.style.pointerEvents = \"auto\";\n\n if (hideTimer) {\n clearTimeout(hideTimer);\n }\n\n hideTimer = setTimeout(() => {\n element.style.opacity = \"0\";\n element.style.pointerEvents = \"none\";\n }, 2000);\n };\n\n element.addEventListener(\"click\", async () => {\n if (!copyValue) {\n return;\n }\n\n try {\n await navigator.clipboard.writeText(copyValue);\n show(`[react-code-locator] copied`, \"success\");\n } catch {\n show(`[react-code-locator] copy failed`, \"error\");\n }\n });\n\n show(`[react-code-locator] enabled (${triggerKey}+click, alt+1/2/3 to switch mode)`, \"idle\");\n\n const mount = () => {\n if (!element.isConnected && document.body) {\n document.body.appendChild(element);\n }\n };\n\n if (document.body) {\n mount();\n } else {\n document.addEventListener(\"DOMContentLoaded\", mount, { once: true });\n }\n\n return {\n setStatus(message, tone = \"idle\") {\n show(message, tone);\n },\n setCopyValue(value) {\n copyValue = value;\n },\n setMode(mode) {\n currentMode = mode;\n show(`[react-code-locator] ${getModeDescription(mode)}`, \"idle\");\n },\n remove() {\n if (hideTimer) {\n clearTimeout(hideTimer);\n }\n element.remove();\n },\n };\n}\n\nexport function locateComponentSource(target: EventTarget | null, mode: LocatorMode = \"screen\"): LocatorResult | null {\n const elementTarget =\n target instanceof Element ? target : target instanceof Node ? target.parentElement : null;\n const fiber = getClosestReactFiber(elementTarget);\n if (!fiber) {\n return null;\n }\n\n const candidates = resolveSourceCandidates(fiber);\n const source = candidates[mode] ?? candidates.screen ?? candidates.direct ?? candidates.implementation;\n if (source) {\n return {\n source,\n mode,\n };\n }\n\n const componentSource = resolveComponentSourceFromFiber(fiber);\n if (!componentSource) {\n return null;\n }\n\n return {\n source: componentSource,\n mode,\n };\n}\n\nexport function enableReactComponentJump(options: LocatorOptions = {}) {\n const overlay = createStatusOverlay(options.triggerKey ?? \"shift\");\n let currentMode: LocatorMode = \"screen\";\n const {\n triggerKey = \"shift\",\n onLocate = (result) => {\n console.log(`[react-code-locator] ${result.source}`);\n overlay?.setCopyValue(result.source);\n overlay?.setStatus(`[react-code-locator] ${result.source}`, \"success\");\n },\n onError = (error) => {\n console.error(\"[react-code-locator]\", error);\n const message = error instanceof Error ? error.message : String(error);\n overlay?.setCopyValue(null);\n overlay?.setStatus(`[react-code-locator] ${message}`, \"error\");\n },\n } = options;\n\n console.log(\"[react-code-locator] enabled\", { triggerKey });\n\n const keyHandler = (event: KeyboardEvent) => {\n if (!event.altKey) {\n return;\n }\n\n if (event.code === \"Digit1\") {\n currentMode = \"direct\";\n overlay?.setMode(currentMode);\n event.preventDefault();\n return;\n }\n\n if (event.code === \"Digit2\") {\n currentMode = \"screen\";\n overlay?.setMode(currentMode);\n event.preventDefault();\n return;\n }\n\n if (event.code === \"Digit3\") {\n currentMode = \"implementation\";\n overlay?.setMode(currentMode);\n event.preventDefault();\n }\n };\n\n const handler = (event: MouseEvent) => {\n console.log(\"[react-code-locator] click\", {\n triggerKey,\n shiftKey: event.shiftKey,\n altKey: event.altKey,\n ctrlKey: event.ctrlKey,\n metaKey: event.metaKey,\n target: event.target,\n });\n\n if (!isTriggerPressed(event, triggerKey)) {\n return;\n }\n\n const result = locateComponentSource(event.target, currentMode);\n if (!result) {\n onError(new Error(\"No React component source metadata found for clicked element.\"));\n return;\n }\n\n event.preventDefault();\n event.stopPropagation();\n onLocate(result);\n };\n\n document.addEventListener(\"click\", handler, true);\n document.addEventListener(\"keydown\", keyHandler, true);\n\n return () => {\n document.removeEventListener(\"click\", handler, true);\n document.removeEventListener(\"keydown\", keyHandler, true);\n overlay?.remove();\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,cAAc;AACpB,IAAM,kBAAkB;AACxB,IAAM,6BAA6B;;;ACG1C,SAAS,iBAAiB,OAAe;AACvC,SAAO,MAAM,QAAQ,OAAO,GAAG;AACjC;AAEA,SAAS,kBAAkB,OAAe;AACxC,SAAO,MAAM,QAAQ,QAAQ,EAAE;AACjC;AAEA,SAAS,kBAAkB,OAAe;AACxC,SAAO,iBAAiB,KAAK,EAAE,MAAM,GAAG,EAAE,OAAO,OAA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B,WAAW,YAAY,IAAI;AAErD,SAAO,MAAM;AACX,aAAS,oBAAoB,SAAS,SAAS,IAAI;AACnD,aAAS,oBAAoB,WAAW,YAAY,IAAI;AACxD,aAAS,OAAO;AAAA,EAClB;AACF;","names":["source"]}
1
+ {"version":3,"sources":["../src/client.ts","../src/constants.ts","../src/sourceMetadata.ts","../src/runtime.ts"],"sourcesContent":["export { enableReactComponentJump, locateComponentSource } from \"./runtime\";\nexport type { LocatorMode, LocatorOptions, LocatorResult, TriggerKey } from \"./runtime\";\n","export const SOURCE_PROP = \"__componentSourceLoc\";\nexport const JSX_SOURCE_PROP = \"$componentSourceLoc\";\nexport const JSX_SOURCE_REGISTRY_SYMBOL = \"react-code-locator.jsxSourceRegistry\";\n","export type SourceLocation = {\n line: number;\n column: number;\n};\n\nfunction normalizeSlashes(value: string) {\n return value.replace(/\\\\/g, \"/\");\n}\n\nfunction trimTrailingSlash(value: string) {\n return value.replace(/\\/+$/, \"\");\n}\n\nfunction splitPathSegments(value: string) {\n return normalizeSlashes(value).split(\"/\").filter(Boolean);\n}\n\nfunction computeRelativePath(fromPath: string, toPath: string) {\n const fromSegments = splitPathSegments(fromPath);\n const toSegments = splitPathSegments(toPath);\n\n let sharedIndex = 0;\n while (\n sharedIndex < fromSegments.length &&\n sharedIndex < toSegments.length &&\n fromSegments[sharedIndex] === toSegments[sharedIndex]\n ) {\n sharedIndex += 1;\n }\n\n const upSegments = new Array(Math.max(0, fromSegments.length - sharedIndex)).fill(\"..\");\n const downSegments = toSegments.slice(sharedIndex);\n const relativeSegments = [...upSegments, ...downSegments];\n return relativeSegments.length > 0 ? relativeSegments.join(\"/\") : \".\";\n}\n\nexport function normalizeProjectRoot(projectRoot?: string) {\n if (projectRoot) {\n return trimTrailingSlash(normalizeSlashes(projectRoot));\n }\n return \"\";\n}\n\nexport function toRelativeSource(\n filename: string | undefined,\n loc: SourceLocation | null | undefined,\n projectRoot?: string,\n) {\n if (!filename || !loc) {\n return null;\n }\n\n const root = normalizeProjectRoot(projectRoot);\n const normalizedFilename = normalizeSlashes(filename);\n const relPath =\n root && normalizedFilename.startsWith(`${root}/`)\n ? normalizedFilename.slice(root.length + 1)\n : root\n ? computeRelativePath(root, normalizedFilename)\n : normalizedFilename;\n return `${relPath}:${loc.line}:${loc.column + 1}`;\n}\n\nexport function getSourceFile(source: string | null) {\n if (!source) {\n return null;\n }\n\n const match = source.match(/^(.*):\\d+:\\d+$/);\n return match?.[1] ?? null;\n}\n\nexport function isProjectLocalFile(filename: string | undefined, projectRoot?: string) {\n if (!filename) {\n return false;\n }\n\n const root = normalizeProjectRoot(projectRoot);\n const normalizedFilename = normalizeSlashes(filename);\n\n if (!root) {\n return (\n !normalizedFilename.startsWith(\"../\") &&\n !normalizedFilename.startsWith(\"/\") &&\n !/^[A-Za-z]:\\//.test(normalizedFilename)\n );\n }\n\n if (normalizedFilename.startsWith(`${root}/`) || normalizedFilename === root) {\n return true;\n }\n\n const relativePath = computeRelativePath(root, normalizedFilename);\n return !relativePath.startsWith(\"../\");\n}\n\nexport function isExternalToProjectRoot(filename: string | undefined, projectRoot?: string) {\n return !isProjectLocalFile(filename, projectRoot);\n}\n\nexport function isProjectLocalSource(source: string, projectRoot?: string) {\n const file = getSourceFile(source);\n return isProjectLocalFile(file ?? undefined, projectRoot);\n}\n","import {\n JSX_SOURCE_PROP,\n JSX_SOURCE_REGISTRY_SYMBOL,\n SOURCE_PROP,\n} from \"./constants\";\nimport { getSourceFile, isProjectLocalSource } from \"./sourceMetadata\";\n\nexport type TriggerKey = \"alt\" | \"meta\" | \"ctrl\" | \"shift\" | \"none\";\nexport type LocatorMode = \"direct\" | \"screen\" | \"implementation\";\n\ntype ReactFiber = {\n return?: ReactFiber | null;\n type?: unknown;\n elementType?: unknown;\n pendingProps?: Record<string, unknown> | null;\n memoizedProps?: Record<string, unknown> | null;\n _debugOwner?: ReactFiber | null;\n _debugSource?: {\n fileName?: string;\n lineNumber?: number;\n columnNumber?: number;\n } | null;\n};\n\nexport type LocatorResult = {\n source: string;\n mode: LocatorMode;\n};\n\nexport type LocatorOptions = {\n triggerKey?: TriggerKey;\n onLocate?: (result: LocatorResult) => void;\n onError?: (error: unknown) => void;\n};\n\ntype StatusOverlay = {\n setStatus: (message: string, tone?: \"idle\" | \"success\" | \"error\") => void;\n setCopyValue: (value: string | null) => void;\n setMode: (mode: LocatorMode) => void;\n remove: () => void;\n};\n\nfunction isTriggerPressed(event: MouseEvent, triggerKey: TriggerKey) {\n if (triggerKey === \"none\") {\n return true;\n }\n\n if (triggerKey === \"alt\") {\n return event.altKey;\n }\n\n if (triggerKey === \"meta\") {\n return event.metaKey;\n }\n\n if (triggerKey === \"ctrl\") {\n return event.ctrlKey;\n }\n\n return event.shiftKey;\n}\n\nfunction getReactFiberKey(element: Element) {\n return Object.keys(element).find((key) => key.startsWith(\"__reactFiber$\") || key.startsWith(\"__reactInternalInstance$\"));\n}\n\nfunction getClosestReactFiber(target: Element | null) {\n let current = target;\n\n while (current) {\n const fiberKey = getReactFiberKey(current);\n if (fiberKey) {\n return (current as unknown as Record<string, unknown>)[fiberKey] as ReactFiber;\n }\n\n current = current.parentElement;\n }\n\n return null;\n}\n\nfunction getSourceFromType(type: unknown) {\n if (!type) {\n return null;\n }\n\n if (typeof type === \"function\") {\n const source = (type as unknown as Record<string, unknown>)[SOURCE_PROP];\n return typeof source === \"string\" ? source : null;\n }\n\n if (typeof type !== \"object\") {\n return null;\n }\n\n const record = type as {\n type?: Record<string, unknown>;\n render?: Record<string, unknown>;\n [SOURCE_PROP]?: unknown;\n };\n\n const source = record[SOURCE_PROP] ?? record.type?.[SOURCE_PROP] ?? record.render?.[SOURCE_PROP];\n return typeof source === \"string\" ? source : null;\n}\n\nfunction getSourceFromProps(props: Record<string, unknown> | null | undefined) {\n if (props && typeof props === \"object\") {\n const registry = (globalThis as Record<symbol, unknown>)[\n Symbol.for(JSX_SOURCE_REGISTRY_SYMBOL)\n ];\n if (registry instanceof WeakMap) {\n const intrinsicSource = registry.get(props as object);\n if (typeof intrinsicSource === \"string\") {\n return intrinsicSource;\n }\n }\n }\n\n const source = props?.[JSX_SOURCE_PROP];\n return typeof source === \"string\" ? source : null;\n}\n\nfunction resolveComponentSourceFromFiber(fiber: ReactFiber | null) {\n let current = fiber;\n\n while (current) {\n const source = getSourceFromType(current.type) ?? getSourceFromType(current.elementType);\n if (source) {\n return source;\n }\n\n current = current.return ?? null;\n }\n\n return null;\n}\n\nfunction getDirectDebugSource(fiber: ReactFiber | null) {\n const debugSource = fiber?._debugSource;\n if (debugSource?.fileName && typeof debugSource.lineNumber === \"number\") {\n return `${debugSource.fileName.replace(/\\\\/g, \"/\")}:${debugSource.lineNumber}:${debugSource.columnNumber ?? 1}`;\n }\n\n return null;\n}\n\ntype SourceCandidate = {\n source: string;\n file: string;\n};\n\ntype ResolvedCandidates = {\n direct: string | null;\n screen: string | null;\n implementation: string | null;\n};\n\nfunction resolveSourceCandidates(fiber: ReactFiber | null): ResolvedCandidates {\n let current = fiber;\n const jsxCandidates: SourceCandidate[] = [];\n const componentCandidates: SourceCandidate[] = [];\n\n while (current) {\n const jsxSource =\n getSourceFromProps(current.pendingProps) ?? getSourceFromProps(current.memoizedProps) ?? getDirectDebugSource(current);\n if (jsxSource) {\n const file = getSourceFile(jsxSource);\n if (file && !jsxCandidates.some((candidate) => candidate.source === jsxSource)) {\n jsxCandidates.push({ source: jsxSource, file });\n }\n }\n\n const componentSource = getSourceFromType(current.type) ?? getSourceFromType(current.elementType);\n if (componentSource) {\n const file = getSourceFile(componentSource);\n if (file && !componentCandidates.some((candidate) => candidate.source === componentSource)) {\n componentCandidates.push({ source: componentSource, file });\n }\n }\n\n current = current.return ?? null;\n }\n\n const direct = jsxCandidates[0]?.source ?? null;\n const nearestProjectLocalComponentFile = componentCandidates.find((candidate) => isProjectLocalSource(candidate.source))?.file;\n let screen: string | null = null;\n if (nearestProjectLocalComponentFile) {\n const matchingJsxCandidate = jsxCandidates.find((candidate) => candidate.file === nearestProjectLocalComponentFile);\n if (matchingJsxCandidate) {\n screen = matchingJsxCandidate.source;\n } else {\n const matchingComponentCandidate = componentCandidates.find(\n (candidate) => candidate.file === nearestProjectLocalComponentFile,\n );\n if (matchingComponentCandidate) {\n screen = matchingComponentCandidate.source;\n }\n }\n }\n\n const implementationComponentCandidate =\n componentCandidates.find((candidate) => !isProjectLocalSource(candidate.source))?.source ?? null;\n const implementationJsxCandidate =\n jsxCandidates.find((candidate) => !isProjectLocalSource(candidate.source))?.source ?? null;\n\n const projectLocalJsxCandidate = jsxCandidates.find((candidate) => isProjectLocalSource(candidate.source))?.source ?? null;\n const screenFallback = screen ?? projectLocalJsxCandidate ?? componentCandidates.find((candidate) => isProjectLocalSource(candidate.source))?.source ?? null;\n\n return {\n direct: direct ?? screenFallback,\n screen: screenFallback,\n implementation: implementationComponentCandidate ?? implementationJsxCandidate ?? screenFallback,\n };\n}\n\nfunction getModeDescription(mode: LocatorMode) {\n if (mode === \"direct\") {\n return \"Direct JSX\";\n }\n\n if (mode === \"screen\") {\n return \"Screen source\";\n }\n\n return \"Implementation source\";\n}\n\nfunction createStatusOverlay(triggerKey: TriggerKey): StatusOverlay | null {\n if (typeof document === \"undefined\") {\n return null;\n }\n\n const element = document.createElement(\"div\");\n let copyValue: string | null = null;\n let currentMode: LocatorMode = \"screen\";\n let hideTimer: ReturnType<typeof setTimeout> | null = null;\n element.setAttribute(\"data-react-code-locator\", \"true\");\n Object.assign(element.style, {\n position: \"fixed\",\n right: \"12px\",\n bottom: \"12px\",\n zIndex: \"2147483647\",\n padding: \"8px 10px\",\n borderRadius: \"8px\",\n background: \"rgba(17, 24, 39, 0.92)\",\n color: \"#fff\",\n fontSize: \"12px\",\n lineHeight: \"1.4\",\n fontFamily: \"ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace\",\n boxShadow: \"0 8px 30px rgba(0, 0, 0, 0.25)\",\n pointerEvents: \"auto\",\n cursor: \"pointer\",\n maxWidth: \"min(70vw, 720px)\",\n wordBreak: \"break-all\",\n opacity: \"0\",\n transition: \"opacity 120ms ease\",\n });\n\n const show = (message: string, tone: \"idle\" | \"success\" | \"error\") => {\n element.textContent = message;\n element.style.background =\n tone === \"success\"\n ? \"rgba(6, 95, 70, 0.92)\"\n : tone === \"error\"\n ? \"rgba(153, 27, 27, 0.94)\"\n : \"rgba(17, 24, 39, 0.92)\";\n element.style.opacity = \"1\";\n element.style.pointerEvents = \"auto\";\n\n if (hideTimer) {\n clearTimeout(hideTimer);\n }\n\n hideTimer = setTimeout(() => {\n element.style.opacity = \"0\";\n element.style.pointerEvents = \"none\";\n }, 2000);\n };\n\n element.addEventListener(\"click\", async () => {\n if (!copyValue) {\n return;\n }\n\n try {\n await navigator.clipboard.writeText(copyValue);\n show(`[react-code-locator] copied`, \"success\");\n } catch {\n show(`[react-code-locator] copy failed`, \"error\");\n }\n });\n\n show(`[react-code-locator] enabled (${triggerKey}+click, alt+1/2/3 to switch mode)`, \"idle\");\n\n const mount = () => {\n if (!element.isConnected && document.body) {\n document.body.appendChild(element);\n }\n };\n\n if (document.body) {\n mount();\n } else {\n document.addEventListener(\"DOMContentLoaded\", mount, { once: true });\n }\n\n return {\n setStatus(message, tone = \"idle\") {\n show(message, tone);\n },\n setCopyValue(value) {\n copyValue = value;\n },\n setMode(mode) {\n currentMode = mode;\n show(`[react-code-locator] ${getModeDescription(mode)}`, \"idle\");\n },\n remove() {\n if (hideTimer) {\n clearTimeout(hideTimer);\n }\n element.remove();\n },\n };\n}\n\nexport function locateComponentSource(target: EventTarget | null, mode: LocatorMode = \"screen\"): LocatorResult | null {\n const elementTarget =\n target instanceof Element ? target : target instanceof Node ? target.parentElement : null;\n const fiber = getClosestReactFiber(elementTarget);\n if (!fiber) {\n return null;\n }\n\n const candidates = resolveSourceCandidates(fiber);\n const source = candidates[mode] ?? candidates.screen ?? candidates.direct ?? candidates.implementation;\n if (source) {\n return {\n source,\n mode,\n };\n }\n\n const componentSource = resolveComponentSourceFromFiber(fiber);\n if (!componentSource) {\n return null;\n }\n\n return {\n source: componentSource,\n mode,\n };\n}\n\nexport function enableReactComponentJump(options: LocatorOptions = {}) {\n if (process.env.NODE_ENV !== \"development\") return;\n const overlay = createStatusOverlay(options.triggerKey ?? \"shift\");\n let currentMode: LocatorMode = \"screen\";\n const {\n triggerKey = \"shift\",\n onLocate = (result) => {\n console.log(`[react-code-locator] ${result.source}`);\n overlay?.setCopyValue(result.source);\n overlay?.setStatus(`[react-code-locator] ${result.source}`, \"success\");\n },\n onError = (error) => {\n console.error(\"[react-code-locator]\", error);\n const message = error instanceof Error ? error.message : String(error);\n overlay?.setCopyValue(null);\n overlay?.setStatus(`[react-code-locator] ${message}`, \"error\");\n },\n } = options;\n\n console.log(\"[react-code-locator] enabled\", { triggerKey });\n\n const keyHandler = (event: KeyboardEvent) => {\n if (!event.altKey) {\n return;\n }\n\n if (event.code === \"Digit1\") {\n currentMode = \"direct\";\n overlay?.setMode(currentMode);\n event.preventDefault();\n return;\n }\n\n if (event.code === \"Digit2\") {\n currentMode = \"screen\";\n overlay?.setMode(currentMode);\n event.preventDefault();\n return;\n }\n\n if (event.code === \"Digit3\") {\n currentMode = \"implementation\";\n overlay?.setMode(currentMode);\n event.preventDefault();\n }\n };\n\n const handler = (event: MouseEvent) => {\n console.log(\"[react-code-locator] click\", {\n triggerKey,\n shiftKey: event.shiftKey,\n altKey: event.altKey,\n ctrlKey: event.ctrlKey,\n metaKey: event.metaKey,\n target: event.target,\n });\n\n if (!isTriggerPressed(event, triggerKey)) {\n return;\n }\n\n const result = locateComponentSource(event.target, currentMode);\n if (!result) {\n onError(new Error(\"No React component source metadata found for clicked element.\"));\n return;\n }\n\n event.preventDefault();\n event.stopPropagation();\n onLocate(result);\n };\n\n document.addEventListener(\"click\", handler, true);\n document.addEventListener(\"keydown\", keyHandler, true);\n\n return () => {\n document.removeEventListener(\"click\", handler, true);\n document.removeEventListener(\"keydown\", keyHandler, true);\n overlay?.remove();\n };\n}\n"],"mappings":"yaAAA,IAAAA,EA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,CAAW,EAC5B9C,EAAM,eAAe,GAEzB,EAEMgE,EAAWhE,GAAsB,CAUrC,GATA,QAAQ,IAAI,6BAA8B,CACxC,WAAAC,EACA,SAAUD,EAAM,SAChB,OAAQA,EAAM,OACd,QAASA,EAAM,QACf,QAASA,EAAM,QACf,OAAQA,EAAM,MAChB,CAAC,EAEG,CAACD,EAAiBC,EAAOC,CAAU,EACrC,OAGF,IAAM2D,EAASP,EAAsBrD,EAAM,OAAQ8C,CAAW,EAC9D,GAAI,CAACc,EAAQ,CACXC,EAAQ,IAAI,MAAM,+DAA+D,CAAC,EAClF,MACF,CAEA7D,EAAM,eAAe,EACrBA,EAAM,gBAAgB,EACtB2D,EAASC,CAAM,CACjB,EAEA,gBAAS,iBAAiB,QAASI,EAAS,EAAI,EAChD,SAAS,iBAAiB,UAAWD,EAAY,EAAI,EAE9C,IAAM,CACX,SAAS,oBAAoB,QAASC,EAAS,EAAI,EACnD,SAAS,oBAAoB,UAAWD,EAAY,EAAI,EACxDL,GAAS,OAAO,CAClB,CACF","names":["client_exports","__export","enableReactComponentJump","locateComponentSource","__toCommonJS","SOURCE_PROP","JSX_SOURCE_PROP","JSX_SOURCE_REGISTRY_SYMBOL","normalizeSlashes","value","trimTrailingSlash","splitPathSegments","computeRelativePath","fromPath","toPath","fromSegments","toSegments","sharedIndex","upSegments","downSegments","relativeSegments","normalizeProjectRoot","projectRoot","getSourceFile","source","isProjectLocalFile","filename","projectRoot","root","normalizeProjectRoot","normalizedFilename","normalizeSlashes","computeRelativePath","isProjectLocalSource","source","projectRoot","file","getSourceFile","isProjectLocalFile","isTriggerPressed","event","triggerKey","getReactFiberKey","element","key","getClosestReactFiber","target","current","fiberKey","getSourceFromType","type","source","SOURCE_PROP","record","getSourceFromProps","props","registry","JSX_SOURCE_REGISTRY_SYMBOL","intrinsicSource","JSX_SOURCE_PROP","resolveComponentSourceFromFiber","fiber","getDirectDebugSource","debugSource","resolveSourceCandidates","jsxCandidates","componentCandidates","jsxSource","file","getSourceFile","candidate","componentSource","direct","nearestProjectLocalComponentFile","isProjectLocalSource","screen","matchingJsxCandidate","matchingComponentCandidate","implementationComponentCandidate","implementationJsxCandidate","projectLocalJsxCandidate","screenFallback","getModeDescription","mode","createStatusOverlay","copyValue","currentMode","hideTimer","show","message","tone","mount","value","locateComponentSource","elementTarget","candidates","enableReactComponentJump","options","overlay","onLocate","result","onError","error","keyHandler","handler"]}
package/dist/client.d.cts CHANGED
@@ -10,6 +10,6 @@ type LocatorOptions = {
10
10
  onError?: (error: unknown) => void;
11
11
  };
12
12
  declare function locateComponentSource(target: EventTarget | null, mode?: LocatorMode): LocatorResult | null;
13
- declare function enableReactComponentJump(options?: LocatorOptions): () => void;
13
+ declare function enableReactComponentJump(options?: LocatorOptions): (() => void) | undefined;
14
14
 
15
15
  export { type LocatorMode, type LocatorOptions, type LocatorResult, type TriggerKey, enableReactComponentJump, locateComponentSource };
package/dist/client.d.ts CHANGED
@@ -10,6 +10,6 @@ type LocatorOptions = {
10
10
  onError?: (error: unknown) => void;
11
11
  };
12
12
  declare function locateComponentSource(target: EventTarget | null, mode?: LocatorMode): LocatorResult | null;
13
- declare function enableReactComponentJump(options?: LocatorOptions): () => void;
13
+ declare function enableReactComponentJump(options?: LocatorOptions): (() => void) | undefined;
14
14
 
15
15
  export { type LocatorMode, type LocatorOptions, type LocatorResult, type TriggerKey, enableReactComponentJump, locateComponentSource };