react-code-locator 0.1.9 → 0.1.12

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 (52) hide show
  1. package/README.md +49 -44
  2. package/dist/babel.cjs +427 -40
  3. package/dist/babel.cjs.map +1 -1
  4. package/dist/babel.d.cts +12 -1
  5. package/dist/babel.d.ts +12 -1
  6. package/dist/babel.js +424 -29
  7. package/dist/babel.js.map +1 -1
  8. package/dist/babelInjectComponentSource.cjs +403 -38
  9. package/dist/babelInjectComponentSource.cjs.map +1 -1
  10. package/dist/babelInjectComponentSource.d.cts +3 -4
  11. package/dist/babelInjectComponentSource.d.ts +3 -4
  12. package/dist/babelInjectComponentSource.js +403 -28
  13. package/dist/babelInjectComponentSource.js.map +1 -1
  14. package/dist/client-sm5wi0uT.d.cts +15 -0
  15. package/dist/client-sm5wi0uT.d.ts +15 -0
  16. package/dist/client.cjs +160 -28
  17. package/dist/client.cjs.map +1 -1
  18. package/dist/client.d.cts +1 -14
  19. package/dist/client.d.ts +1 -14
  20. package/dist/client.js +160 -28
  21. package/dist/client.js.map +1 -1
  22. package/dist/esbuild.cjs +615 -0
  23. package/dist/esbuild.cjs.map +1 -0
  24. package/dist/esbuild.d.cts +25 -0
  25. package/dist/esbuild.d.ts +25 -0
  26. package/dist/esbuild.js +588 -0
  27. package/dist/esbuild.js.map +1 -0
  28. package/dist/index.cjs +827 -30
  29. package/dist/index.cjs.map +1 -1
  30. package/dist/index.d.cts +9 -1
  31. package/dist/index.d.ts +9 -1
  32. package/dist/index.js +814 -29
  33. package/dist/index.js.map +1 -1
  34. package/dist/sourceAdapter-DLWo_ABo.d.cts +15 -0
  35. package/dist/sourceAdapter-DLWo_ABo.d.ts +15 -0
  36. package/dist/swc.cjs +588 -0
  37. package/dist/swc.cjs.map +1 -0
  38. package/dist/swc.d.cts +29 -0
  39. package/dist/swc.d.ts +29 -0
  40. package/dist/swc.js +559 -0
  41. package/dist/swc.js.map +1 -0
  42. package/dist/vite.cjs +525 -84
  43. package/dist/vite.cjs.map +1 -1
  44. package/dist/vite.d.cts +20 -6
  45. package/dist/vite.d.ts +20 -6
  46. package/dist/vite.js +520 -72
  47. package/dist/vite.js.map +1 -1
  48. package/dist/webpackRuntimeEntry.cjs +160 -28
  49. package/dist/webpackRuntimeEntry.cjs.map +1 -1
  50. package/dist/webpackRuntimeEntry.js +160 -28
  51. package/dist/webpackRuntimeEntry.js.map +1 -1
  52. package/package.json +12 -1
@@ -0,0 +1,615 @@
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/esbuild.ts
21
+ var esbuild_exports = {};
22
+ __export(esbuild_exports, {
23
+ createEsbuildSourceAdapter: () => createEsbuildSourceAdapter,
24
+ esbuildSourceAdapter: () => esbuildSourceAdapter,
25
+ esbuildSourceTransformPlugin: () => esbuildSourceTransformPlugin
26
+ });
27
+ module.exports = __toCommonJS(esbuild_exports);
28
+ var import_promises = require("fs/promises");
29
+
30
+ // src/sourceAdapter.ts
31
+ function defineSourceAdapter(descriptor) {
32
+ return descriptor;
33
+ }
34
+
35
+ // src/sourceTransform.ts
36
+ var import_core2 = require("@babel/core");
37
+
38
+ // src/babelInjectComponentSource.ts
39
+ var import_core = require("@babel/core");
40
+
41
+ // src/constants.ts
42
+ var SOURCE_PROP = "__componentSourceLoc";
43
+ var JSX_SOURCE_PROP = "$componentSourceLoc";
44
+ var JSX_SOURCE_REGISTRY_SYMBOL = "react-code-locator.jsxSourceRegistry";
45
+
46
+ // src/sourceMetadata.ts
47
+ function normalizeSlashes(value) {
48
+ return value.replace(/\\/g, "/");
49
+ }
50
+ function trimTrailingSlash(value) {
51
+ return value.replace(/\/+$/, "");
52
+ }
53
+ function splitPathSegments(value) {
54
+ return normalizeSlashes(value).split("/").filter(Boolean);
55
+ }
56
+ function computeRelativePath(fromPath, toPath) {
57
+ const fromSegments = splitPathSegments(fromPath);
58
+ const toSegments = splitPathSegments(toPath);
59
+ let sharedIndex = 0;
60
+ while (sharedIndex < fromSegments.length && sharedIndex < toSegments.length && fromSegments[sharedIndex] === toSegments[sharedIndex]) {
61
+ sharedIndex += 1;
62
+ }
63
+ const upSegments = new Array(Math.max(0, fromSegments.length - sharedIndex)).fill("..");
64
+ const downSegments = toSegments.slice(sharedIndex);
65
+ const relativeSegments = [...upSegments, ...downSegments];
66
+ return relativeSegments.length > 0 ? relativeSegments.join("/") : ".";
67
+ }
68
+ function normalizeProjectRoot(projectRoot) {
69
+ if (projectRoot) {
70
+ return trimTrailingSlash(normalizeSlashes(projectRoot));
71
+ }
72
+ if (typeof process !== "undefined" && typeof process.cwd === "function") {
73
+ return trimTrailingSlash(normalizeSlashes(process.cwd()));
74
+ }
75
+ return "";
76
+ }
77
+ function toRelativeSource(filename, loc, projectRoot) {
78
+ if (!filename || !loc) {
79
+ return null;
80
+ }
81
+ const root = normalizeProjectRoot(projectRoot);
82
+ const normalizedFilename = normalizeSlashes(filename);
83
+ const relPath = root && normalizedFilename.startsWith(`${root}/`) ? normalizedFilename.slice(root.length + 1) : root ? computeRelativePath(root, normalizedFilename) : normalizedFilename;
84
+ return `${relPath}:${loc.line}:${loc.column + 1}`;
85
+ }
86
+ function isProjectLocalFile(filename, projectRoot) {
87
+ if (!filename) {
88
+ return false;
89
+ }
90
+ const root = normalizeProjectRoot(projectRoot);
91
+ const normalizedFilename = normalizeSlashes(filename);
92
+ if (!root) {
93
+ return !normalizedFilename.startsWith("../") && !normalizedFilename.startsWith("/") && !/^[A-Za-z]:\//.test(normalizedFilename);
94
+ }
95
+ if (normalizedFilename.startsWith(`${root}/`) || normalizedFilename === root) {
96
+ return true;
97
+ }
98
+ const relativePath = computeRelativePath(root, normalizedFilename);
99
+ return !relativePath.startsWith("../");
100
+ }
101
+ function isExternalToProjectRoot(filename, projectRoot) {
102
+ return !isProjectLocalFile(filename, projectRoot);
103
+ }
104
+
105
+ // src/babelInjectComponentSource.ts
106
+ var SOURCE_PROP_LOCAL = "_componentSourceLoc";
107
+ var SOURCE_PROPS_REST = "__reactCodeLocatorProps";
108
+ function isComponentName(name) {
109
+ return /^[A-Z]/.test(name);
110
+ }
111
+ function isCustomComponentTag(name) {
112
+ if (import_core.types.isJSXIdentifier(name)) {
113
+ return isComponentName(name.name);
114
+ }
115
+ if (import_core.types.isJSXMemberExpression(name)) {
116
+ return true;
117
+ }
118
+ return false;
119
+ }
120
+ function isIntrinsicElementTag(name) {
121
+ return import_core.types.isJSXIdentifier(name) && /^[a-z]/.test(name.name);
122
+ }
123
+ function isElementFactoryIdentifier(name) {
124
+ return name === "jsx" || name === "jsxs" || name === "jsxDEV" || name === "_jsx" || name === "_jsxs" || name === "_jsxDEV" || name === "createElement";
125
+ }
126
+ function isReactElementFactoryCall(pathNode) {
127
+ const callee = pathNode.node.callee;
128
+ if (import_core.types.isIdentifier(callee)) {
129
+ return isElementFactoryIdentifier(callee.name);
130
+ }
131
+ return import_core.types.isMemberExpression(callee) && import_core.types.isIdentifier(callee.object, { name: "React" }) && import_core.types.isIdentifier(callee.property, { name: "createElement" });
132
+ }
133
+ function getRootJsxIdentifierName(name) {
134
+ if (import_core.types.isJSXIdentifier(name)) {
135
+ return name.name;
136
+ }
137
+ if (import_core.types.isJSXMemberExpression(name)) {
138
+ return getRootJsxIdentifierName(name.object);
139
+ }
140
+ return null;
141
+ }
142
+ function isStyledModuleImport(binding) {
143
+ if (!binding) {
144
+ return false;
145
+ }
146
+ if (!binding.path.isImportSpecifier() && !binding.path.isImportDefaultSpecifier() && !binding.path.isImportNamespaceSpecifier()) {
147
+ return false;
148
+ }
149
+ const source = binding.path.parentPath.isImportDeclaration() ? binding.path.parentPath.node.source.value : null;
150
+ if (typeof source !== "string") {
151
+ return false;
152
+ }
153
+ const normalized = source.replace(/\\/g, "/");
154
+ return normalized === "./styled" || normalized === "../styled" || normalized.endsWith("/styled");
155
+ }
156
+ function isSupportedComponentInit(node) {
157
+ if (!node) {
158
+ return false;
159
+ }
160
+ if (import_core.types.isArrowFunctionExpression(node) || import_core.types.isFunctionExpression(node)) {
161
+ return true;
162
+ }
163
+ if (!import_core.types.isCallExpression(node)) {
164
+ return false;
165
+ }
166
+ if (import_core.types.isIdentifier(node.callee) && (node.callee.name === "memo" || node.callee.name === "forwardRef")) {
167
+ return true;
168
+ }
169
+ return import_core.types.isMemberExpression(node.callee) && import_core.types.isIdentifier(node.callee.object, { name: "React" }) && import_core.types.isIdentifier(node.callee.property) && (node.callee.property.name === "memo" || node.callee.property.name === "forwardRef");
170
+ }
171
+ function hasSourcePropBinding(pattern) {
172
+ return pattern.properties.some((property) => {
173
+ if (!import_core.types.isObjectProperty(property)) {
174
+ return false;
175
+ }
176
+ return import_core.types.isIdentifier(property.key) && property.key.name === JSX_SOURCE_PROP;
177
+ });
178
+ }
179
+ function injectSourcePropBinding(pattern) {
180
+ if (hasSourcePropBinding(pattern)) {
181
+ return;
182
+ }
183
+ const sourceBinding = import_core.types.objectProperty(
184
+ import_core.types.identifier(JSX_SOURCE_PROP),
185
+ import_core.types.identifier(SOURCE_PROP_LOCAL),
186
+ false,
187
+ false
188
+ );
189
+ const restIndex = pattern.properties.findIndex(
190
+ (property) => import_core.types.isRestElement(property)
191
+ );
192
+ if (restIndex === -1) {
193
+ pattern.properties.push(sourceBinding);
194
+ return;
195
+ }
196
+ pattern.properties.splice(restIndex, 0, sourceBinding);
197
+ }
198
+ function injectSourcePropIntoIdentifierParam(node, param) {
199
+ if (!import_core.types.isBlockStatement(node.body)) {
200
+ node.body = import_core.types.blockStatement([import_core.types.returnStatement(node.body)]);
201
+ }
202
+ const alreadyInjected = node.body.body.some(
203
+ (statement) => import_core.types.isVariableDeclaration(statement) && statement.declarations.some(
204
+ (declaration) => import_core.types.isIdentifier(declaration.id) && declaration.id.name === SOURCE_PROPS_REST
205
+ )
206
+ );
207
+ if (alreadyInjected) {
208
+ return;
209
+ }
210
+ node.body.body.unshift(
211
+ import_core.types.variableDeclaration("const", [
212
+ import_core.types.variableDeclarator(
213
+ import_core.types.objectPattern([
214
+ import_core.types.objectProperty(
215
+ import_core.types.identifier(JSX_SOURCE_PROP),
216
+ import_core.types.identifier(SOURCE_PROP_LOCAL),
217
+ false,
218
+ false
219
+ ),
220
+ import_core.types.restElement(import_core.types.identifier(SOURCE_PROPS_REST))
221
+ ]),
222
+ param
223
+ )
224
+ ]),
225
+ import_core.types.expressionStatement(
226
+ import_core.types.assignmentExpression(
227
+ "=",
228
+ import_core.types.identifier(param.name),
229
+ import_core.types.identifier(SOURCE_PROPS_REST)
230
+ )
231
+ )
232
+ );
233
+ }
234
+ function injectSourcePropIntoFunctionParams(node) {
235
+ const firstParam = node.params[0];
236
+ if (!firstParam) {
237
+ return;
238
+ }
239
+ if (import_core.types.isObjectPattern(firstParam)) {
240
+ injectSourcePropBinding(firstParam);
241
+ return;
242
+ }
243
+ if (import_core.types.isIdentifier(firstParam)) {
244
+ injectSourcePropIntoIdentifierParam(node, firstParam);
245
+ }
246
+ }
247
+ function injectSourcePropIntoExpression(node) {
248
+ if (!node) {
249
+ return;
250
+ }
251
+ if (import_core.types.isFunctionExpression(node) || import_core.types.isArrowFunctionExpression(node)) {
252
+ injectSourcePropIntoFunctionParams(node);
253
+ return;
254
+ }
255
+ if (!import_core.types.isCallExpression(node)) {
256
+ return;
257
+ }
258
+ const firstArg = node.arguments[0];
259
+ if (firstArg && !import_core.types.isSpreadElement(firstArg) && (import_core.types.isFunctionExpression(firstArg) || import_core.types.isArrowFunctionExpression(firstArg))) {
260
+ injectSourcePropIntoFunctionParams(firstArg);
261
+ }
262
+ }
263
+ function getSourceValue(state, loc, projectRoot) {
264
+ const filename = state.file?.opts?.filename;
265
+ if (!filename || !loc) {
266
+ return null;
267
+ }
268
+ return toRelativeSource(filename, loc, projectRoot);
269
+ }
270
+ function buildAssignment(name, sourceValue) {
271
+ return import_core.types.expressionStatement(
272
+ import_core.types.assignmentExpression(
273
+ "=",
274
+ import_core.types.memberExpression(import_core.types.identifier(name), import_core.types.identifier(SOURCE_PROP)),
275
+ import_core.types.stringLiteral(sourceValue)
276
+ )
277
+ );
278
+ }
279
+ function buildIntrinsicSourceHelper() {
280
+ return import_core.types.functionDeclaration(
281
+ import_core.types.identifier("_markIntrinsicElementSource"),
282
+ [import_core.types.identifier("element"), import_core.types.identifier("source")],
283
+ import_core.types.blockStatement([
284
+ import_core.types.variableDeclaration("const", [
285
+ import_core.types.variableDeclarator(
286
+ import_core.types.identifier("registryKey"),
287
+ import_core.types.callExpression(
288
+ import_core.types.memberExpression(import_core.types.identifier("Symbol"), import_core.types.identifier("for")),
289
+ [import_core.types.stringLiteral(JSX_SOURCE_REGISTRY_SYMBOL)]
290
+ )
291
+ )
292
+ ]),
293
+ import_core.types.variableDeclaration("let", [
294
+ import_core.types.variableDeclarator(
295
+ import_core.types.identifier("registry"),
296
+ import_core.types.memberExpression(import_core.types.identifier("globalThis"), import_core.types.identifier("registryKey"), true)
297
+ )
298
+ ]),
299
+ import_core.types.ifStatement(
300
+ import_core.types.unaryExpression(
301
+ "!",
302
+ import_core.types.binaryExpression("instanceof", import_core.types.identifier("registry"), import_core.types.identifier("WeakMap"))
303
+ ),
304
+ import_core.types.blockStatement([
305
+ import_core.types.expressionStatement(
306
+ import_core.types.assignmentExpression(
307
+ "=",
308
+ import_core.types.identifier("registry"),
309
+ import_core.types.assignmentExpression(
310
+ "=",
311
+ import_core.types.memberExpression(import_core.types.identifier("globalThis"), import_core.types.identifier("registryKey"), true),
312
+ import_core.types.newExpression(import_core.types.identifier("WeakMap"), [])
313
+ )
314
+ )
315
+ )
316
+ ])
317
+ ),
318
+ import_core.types.ifStatement(
319
+ import_core.types.logicalExpression(
320
+ "&&",
321
+ import_core.types.identifier("element"),
322
+ import_core.types.logicalExpression(
323
+ "&&",
324
+ import_core.types.binaryExpression("===", import_core.types.unaryExpression("typeof", import_core.types.identifier("element")), import_core.types.stringLiteral("object")),
325
+ import_core.types.binaryExpression(
326
+ "===",
327
+ import_core.types.unaryExpression("typeof", import_core.types.memberExpression(import_core.types.identifier("element"), import_core.types.identifier("props"))),
328
+ import_core.types.stringLiteral("object")
329
+ )
330
+ )
331
+ ),
332
+ import_core.types.blockStatement([
333
+ import_core.types.expressionStatement(
334
+ import_core.types.callExpression(
335
+ import_core.types.memberExpression(import_core.types.identifier("registry"), import_core.types.identifier("set")),
336
+ [import_core.types.memberExpression(import_core.types.identifier("element"), import_core.types.identifier("props")), import_core.types.identifier("source")]
337
+ )
338
+ )
339
+ ])
340
+ ),
341
+ import_core.types.returnStatement(import_core.types.identifier("element"))
342
+ ])
343
+ );
344
+ }
345
+ function ensureIntrinsicSourceHelper(programPath, state) {
346
+ if (state.injectedIntrinsicHelper) {
347
+ return;
348
+ }
349
+ const alreadyExists = programPath.node.body.some(
350
+ (node) => import_core.types.isFunctionDeclaration(node) && import_core.types.isIdentifier(node.id, { name: "_markIntrinsicElementSource" })
351
+ );
352
+ if (!alreadyExists) {
353
+ programPath.unshiftContainer("body", buildIntrinsicSourceHelper());
354
+ }
355
+ state.injectedIntrinsicHelper = true;
356
+ }
357
+ function visitDeclaration(declarationPath, insertAfterPath, state, seen, projectRoot) {
358
+ if (declarationPath.isFunctionDeclaration() || declarationPath.isClassDeclaration()) {
359
+ const name = declarationPath.node.id?.name;
360
+ if (!name || !isComponentName(name) || seen.has(name)) {
361
+ return;
362
+ }
363
+ if (declarationPath.isFunctionDeclaration()) {
364
+ injectSourcePropIntoFunctionParams(declarationPath.node);
365
+ }
366
+ const sourceValue = getSourceValue(
367
+ state,
368
+ declarationPath.node.loc?.start,
369
+ projectRoot
370
+ );
371
+ if (!sourceValue) {
372
+ return;
373
+ }
374
+ seen.add(name);
375
+ insertAfterPath.insertAfter(buildAssignment(name, sourceValue));
376
+ return;
377
+ }
378
+ if (!declarationPath.isVariableDeclaration()) {
379
+ return;
380
+ }
381
+ const assignments = declarationPath.node.declarations.flatMap(
382
+ (declarator) => {
383
+ if (!import_core.types.isIdentifier(declarator.id) || !isComponentName(declarator.id.name) || seen.has(declarator.id.name)) {
384
+ return [];
385
+ }
386
+ if (!declarator.init) {
387
+ return [];
388
+ }
389
+ if (!isSupportedComponentInit(declarator.init)) {
390
+ return [];
391
+ }
392
+ injectSourcePropIntoExpression(declarator.init);
393
+ const sourceValue = getSourceValue(
394
+ state,
395
+ declarator.loc?.start ?? declarator.init.loc?.start,
396
+ projectRoot
397
+ );
398
+ if (!sourceValue) {
399
+ return [];
400
+ }
401
+ seen.add(declarator.id.name);
402
+ return [buildAssignment(declarator.id.name, sourceValue)];
403
+ }
404
+ );
405
+ if (assignments.length > 0) {
406
+ insertAfterPath.insertAfter(assignments);
407
+ }
408
+ }
409
+ function babelInjectComponentSource(options = {}) {
410
+ const {
411
+ injectJsxSource = true,
412
+ injectComponentSource = true,
413
+ projectRoot
414
+ } = options;
415
+ return {
416
+ name: "babel-inject-component-source",
417
+ visitor: {
418
+ CallExpression(pathNode, state) {
419
+ if (!injectJsxSource) {
420
+ return;
421
+ }
422
+ if (!isReactElementFactoryCall(pathNode)) {
423
+ return;
424
+ }
425
+ if (pathNode.parentPath.isCallExpression() && import_core.types.isIdentifier(pathNode.parentPath.node.callee, {
426
+ name: "_markIntrinsicElementSource"
427
+ })) {
428
+ return;
429
+ }
430
+ const sourceValue = getSourceValue(
431
+ state,
432
+ pathNode.node.loc?.start,
433
+ projectRoot
434
+ );
435
+ if (!sourceValue) {
436
+ return;
437
+ }
438
+ const programPath = pathNode.findParent((parent) => parent.isProgram());
439
+ if (!programPath || !programPath.isProgram()) {
440
+ return;
441
+ }
442
+ ensureIntrinsicSourceHelper(programPath, state);
443
+ pathNode.replaceWith(
444
+ import_core.types.callExpression(import_core.types.identifier("_markIntrinsicElementSource"), [
445
+ pathNode.node,
446
+ import_core.types.stringLiteral(sourceValue)
447
+ ])
448
+ );
449
+ pathNode.skip();
450
+ },
451
+ JSXElement: {
452
+ exit(pathNode, state) {
453
+ if (!injectJsxSource) {
454
+ return;
455
+ }
456
+ if (!isIntrinsicElementTag(pathNode.node.openingElement.name)) {
457
+ return;
458
+ }
459
+ if (pathNode.parentPath.isCallExpression() && import_core.types.isIdentifier(pathNode.parentPath.node.callee, { name: "_markIntrinsicElementSource" })) {
460
+ return;
461
+ }
462
+ const sourceValue = getSourceValue(
463
+ state,
464
+ pathNode.node.openingElement.loc?.start,
465
+ projectRoot
466
+ );
467
+ if (!sourceValue) {
468
+ return;
469
+ }
470
+ const programPath = pathNode.findParent((parent) => parent.isProgram());
471
+ if (!programPath || !programPath.isProgram()) {
472
+ return;
473
+ }
474
+ ensureIntrinsicSourceHelper(programPath, state);
475
+ const wrappedNode = import_core.types.callExpression(import_core.types.identifier("_markIntrinsicElementSource"), [
476
+ pathNode.node,
477
+ import_core.types.stringLiteral(sourceValue)
478
+ ]);
479
+ if (pathNode.parentPath.isJSXElement() || pathNode.parentPath.isJSXFragment()) {
480
+ pathNode.replaceWith(import_core.types.jsxExpressionContainer(wrappedNode));
481
+ return;
482
+ }
483
+ if (pathNode.parentPath.isJSXExpressionContainer()) {
484
+ pathNode.parentPath.replaceWith(import_core.types.jsxExpressionContainer(wrappedNode));
485
+ return;
486
+ }
487
+ pathNode.replaceWith(wrappedNode);
488
+ }
489
+ },
490
+ JSXOpeningElement(pathNode, state) {
491
+ if (!injectJsxSource) {
492
+ return;
493
+ }
494
+ if (!isCustomComponentTag(pathNode.node.name)) {
495
+ return;
496
+ }
497
+ const rootIdentifierName = getRootJsxIdentifierName(pathNode.node.name);
498
+ if (rootIdentifierName && isExternalToProjectRoot(state.file?.opts?.filename, projectRoot) && isStyledModuleImport(pathNode.scope.getBinding(rootIdentifierName))) {
499
+ return;
500
+ }
501
+ const hasSourceProp = pathNode.node.attributes.some(
502
+ (attr) => import_core.types.isJSXAttribute(attr) && import_core.types.isJSXIdentifier(attr.name) && attr.name.name === JSX_SOURCE_PROP
503
+ );
504
+ if (hasSourceProp) {
505
+ return;
506
+ }
507
+ const filename = state.file?.opts?.filename;
508
+ const loc = pathNode.node.loc?.start;
509
+ if (!filename || !loc) {
510
+ return;
511
+ }
512
+ pathNode.node.attributes.push(
513
+ import_core.types.jsxAttribute(
514
+ import_core.types.jsxIdentifier(JSX_SOURCE_PROP),
515
+ import_core.types.stringLiteral(
516
+ getSourceValue(state, loc, projectRoot) ?? `${filename.replace(/\\/g, "/")}:${loc.line}:${loc.column + 1}`
517
+ )
518
+ )
519
+ );
520
+ },
521
+ Program(programPath, state) {
522
+ if (!injectComponentSource) {
523
+ return;
524
+ }
525
+ const seen = /* @__PURE__ */ new Set();
526
+ for (const childPath of programPath.get("body")) {
527
+ if (childPath.isExportNamedDeclaration() || childPath.isExportDefaultDeclaration()) {
528
+ const declarationPath = childPath.get("declaration");
529
+ if (!Array.isArray(declarationPath) && declarationPath.node) {
530
+ visitDeclaration(declarationPath, childPath, state, seen, projectRoot);
531
+ }
532
+ continue;
533
+ }
534
+ visitDeclaration(childPath, childPath, state, seen, projectRoot);
535
+ }
536
+ }
537
+ }
538
+ };
539
+ }
540
+
541
+ // src/sourceTransform.ts
542
+ async function transformSourceWithLocator(code, options) {
543
+ const { filename, sourceMaps = true, ...pluginOptions } = options;
544
+ const result = await (0, import_core2.transformAsync)(code, {
545
+ filename,
546
+ babelrc: false,
547
+ configFile: false,
548
+ sourceMaps,
549
+ parserOpts: {
550
+ sourceType: "module",
551
+ plugins: ["jsx", "typescript"]
552
+ },
553
+ generatorOpts: {
554
+ retainLines: true
555
+ },
556
+ plugins: [[babelInjectComponentSource, pluginOptions]]
557
+ });
558
+ return {
559
+ code: result?.code ?? code,
560
+ map: result?.map ?? null
561
+ };
562
+ }
563
+
564
+ // src/esbuild.ts
565
+ function getEsbuildLoader(filename) {
566
+ if (filename.endsWith(".tsx")) {
567
+ return "tsx";
568
+ }
569
+ if (filename.endsWith(".ts")) {
570
+ return "ts";
571
+ }
572
+ if (filename.endsWith(".jsx")) {
573
+ return "jsx";
574
+ }
575
+ return "js";
576
+ }
577
+ function esbuildSourceTransformPlugin(options = {}) {
578
+ return {
579
+ name: "react-code-locator-source-transform",
580
+ setup(build) {
581
+ build.onLoad({ filter: /\.[mc]?[jt]sx?$/ }, async ({ path }) => {
582
+ if (path.includes("/node_modules/")) {
583
+ return null;
584
+ }
585
+ const code = await (0, import_promises.readFile)(path, "utf8");
586
+ const result = await transformSourceWithLocator(code, {
587
+ filename: path,
588
+ ...options
589
+ });
590
+ return {
591
+ contents: result.code,
592
+ loader: getEsbuildLoader(path)
593
+ };
594
+ });
595
+ }
596
+ };
597
+ }
598
+ function createEsbuildSourceAdapter(options = {}) {
599
+ return defineSourceAdapter({
600
+ kind: "esbuild",
601
+ name: "react-code-locator/esbuild",
602
+ options,
603
+ config: {
604
+ plugins: [esbuildSourceTransformPlugin(options)]
605
+ }
606
+ });
607
+ }
608
+ var esbuildSourceAdapter = createEsbuildSourceAdapter();
609
+ // Annotate the CommonJS export names for ESM import in node:
610
+ 0 && (module.exports = {
611
+ createEsbuildSourceAdapter,
612
+ esbuildSourceAdapter,
613
+ esbuildSourceTransformPlugin
614
+ });
615
+ //# sourceMappingURL=esbuild.cjs.map