react-code-locator 0.1.13 → 0.1.14
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/README.md +9 -0
- package/dist/babel.d.cts +2 -2
- package/dist/babel.d.ts +2 -2
- package/dist/babelInjectComponentSource.d.cts +1 -1
- package/dist/babelInjectComponentSource.d.ts +1 -1
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +15 -1
- package/dist/client.d.ts +15 -1
- package/dist/esbuild.d.cts +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/index.cjs +2 -686
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -9
- package/dist/index.d.ts +1 -9
- package/dist/index.js +1 -673
- package/dist/index.js.map +1 -1
- package/dist/{sourceAdapter-DLWo_ABo.d.cts → sourceAdapter-dPr5CgLi.d.cts} +1 -2
- package/dist/{sourceAdapter-DLWo_ABo.d.ts → sourceAdapter-dPr5CgLi.d.ts} +1 -2
- package/dist/swc.d.cts +1 -1
- package/dist/swc.d.ts +1 -1
- package/dist/vite.d.cts +3 -3
- package/dist/vite.d.ts +3 -3
- package/package.json +1 -1
- package/dist/client-sm5wi0uT.d.cts +0 -15
- package/dist/client-sm5wi0uT.d.ts +0 -15
package/dist/index.cjs
CHANGED
|
@@ -20,20 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
-
babelInjectComponentSource: () => babelInjectComponentSource,
|
|
24
|
-
babelSourceAdapter: () => babelSourceAdapter,
|
|
25
|
-
createBabelSourceAdapter: () => createBabelSourceAdapter,
|
|
26
|
-
createEsbuildSourceAdapter: () => createEsbuildSourceAdapter,
|
|
27
|
-
createSwcSourceAdapter: () => createSwcSourceAdapter,
|
|
28
|
-
createViteClientInjector: () => createViteClientInjector,
|
|
29
|
-
createViteSourceAdapter: () => createViteSourceAdapter,
|
|
30
|
-
defineSourceAdapter: () => defineSourceAdapter,
|
|
31
23
|
enableReactComponentJump: () => enableReactComponentJump,
|
|
32
|
-
|
|
33
|
-
locateComponentSource: () => locateComponentSource,
|
|
34
|
-
reactComponentJump: () => createViteClientInjector,
|
|
35
|
-
swcSourceAdapter: () => swcSourceAdapter,
|
|
36
|
-
viteSourceAdapter: () => viteSourceAdapter
|
|
24
|
+
locateComponentSource: () => locateComponentSource
|
|
37
25
|
});
|
|
38
26
|
module.exports = __toCommonJS(index_exports);
|
|
39
27
|
|
|
@@ -70,15 +58,6 @@ function normalizeProjectRoot(projectRoot) {
|
|
|
70
58
|
}
|
|
71
59
|
return "";
|
|
72
60
|
}
|
|
73
|
-
function toRelativeSource(filename, loc, projectRoot) {
|
|
74
|
-
if (!filename || !loc) {
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
const root = normalizeProjectRoot(projectRoot);
|
|
78
|
-
const normalizedFilename = normalizeSlashes(filename);
|
|
79
|
-
const relPath = root && normalizedFilename.startsWith(`${root}/`) ? normalizedFilename.slice(root.length + 1) : root ? computeRelativePath(root, normalizedFilename) : normalizedFilename;
|
|
80
|
-
return `${relPath}:${loc.line}:${loc.column + 1}`;
|
|
81
|
-
}
|
|
82
61
|
function getSourceFile(source) {
|
|
83
62
|
if (!source) {
|
|
84
63
|
return null;
|
|
@@ -101,9 +80,6 @@ function isProjectLocalFile(filename, projectRoot) {
|
|
|
101
80
|
const relativePath = computeRelativePath(root, normalizedFilename);
|
|
102
81
|
return !relativePath.startsWith("../");
|
|
103
82
|
}
|
|
104
|
-
function isExternalToProjectRoot(filename, projectRoot) {
|
|
105
|
-
return !isProjectLocalFile(filename, projectRoot);
|
|
106
|
-
}
|
|
107
83
|
function isProjectLocalSource(source, projectRoot) {
|
|
108
84
|
const file = getSourceFile(source);
|
|
109
85
|
return isProjectLocalFile(file ?? void 0, projectRoot);
|
|
@@ -416,669 +392,9 @@ function enableReactComponentJump(options = {}) {
|
|
|
416
392
|
overlay?.remove();
|
|
417
393
|
};
|
|
418
394
|
}
|
|
419
|
-
|
|
420
|
-
// src/sourceAdapter.ts
|
|
421
|
-
function defineSourceAdapter(descriptor) {
|
|
422
|
-
return descriptor;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
// src/babelInjectComponentSource.ts
|
|
426
|
-
var import_core = require("@babel/core");
|
|
427
|
-
var SOURCE_PROP_LOCAL = "_componentSourceLoc";
|
|
428
|
-
var SOURCE_PROPS_REST = "__reactCodeLocatorProps";
|
|
429
|
-
function isComponentName(name) {
|
|
430
|
-
return /^[A-Z]/.test(name);
|
|
431
|
-
}
|
|
432
|
-
function isCustomComponentTag(name) {
|
|
433
|
-
if (import_core.types.isJSXIdentifier(name)) {
|
|
434
|
-
return isComponentName(name.name);
|
|
435
|
-
}
|
|
436
|
-
if (import_core.types.isJSXMemberExpression(name)) {
|
|
437
|
-
return true;
|
|
438
|
-
}
|
|
439
|
-
return false;
|
|
440
|
-
}
|
|
441
|
-
function isIntrinsicElementTag(name) {
|
|
442
|
-
return import_core.types.isJSXIdentifier(name) && /^[a-z]/.test(name.name);
|
|
443
|
-
}
|
|
444
|
-
function isElementFactoryIdentifier(name) {
|
|
445
|
-
return name === "jsx" || name === "jsxs" || name === "jsxDEV" || name === "_jsx" || name === "_jsxs" || name === "_jsxDEV" || name === "createElement";
|
|
446
|
-
}
|
|
447
|
-
function isReactElementFactoryCall(pathNode) {
|
|
448
|
-
const callee = pathNode.node.callee;
|
|
449
|
-
if (import_core.types.isIdentifier(callee)) {
|
|
450
|
-
return isElementFactoryIdentifier(callee.name);
|
|
451
|
-
}
|
|
452
|
-
return import_core.types.isMemberExpression(callee) && import_core.types.isIdentifier(callee.object, { name: "React" }) && import_core.types.isIdentifier(callee.property, { name: "createElement" });
|
|
453
|
-
}
|
|
454
|
-
function getRootJsxIdentifierName(name) {
|
|
455
|
-
if (import_core.types.isJSXIdentifier(name)) {
|
|
456
|
-
return name.name;
|
|
457
|
-
}
|
|
458
|
-
if (import_core.types.isJSXMemberExpression(name)) {
|
|
459
|
-
return getRootJsxIdentifierName(name.object);
|
|
460
|
-
}
|
|
461
|
-
return null;
|
|
462
|
-
}
|
|
463
|
-
function isStyledModuleImport(binding) {
|
|
464
|
-
if (!binding) {
|
|
465
|
-
return false;
|
|
466
|
-
}
|
|
467
|
-
if (!binding.path.isImportSpecifier() && !binding.path.isImportDefaultSpecifier() && !binding.path.isImportNamespaceSpecifier()) {
|
|
468
|
-
return false;
|
|
469
|
-
}
|
|
470
|
-
const source = binding.path.parentPath.isImportDeclaration() ? binding.path.parentPath.node.source.value : null;
|
|
471
|
-
if (typeof source !== "string") {
|
|
472
|
-
return false;
|
|
473
|
-
}
|
|
474
|
-
const normalized = source.replace(/\\/g, "/");
|
|
475
|
-
return normalized === "./styled" || normalized === "../styled" || normalized.endsWith("/styled");
|
|
476
|
-
}
|
|
477
|
-
function isSupportedComponentInit(node) {
|
|
478
|
-
if (!node) {
|
|
479
|
-
return false;
|
|
480
|
-
}
|
|
481
|
-
if (import_core.types.isArrowFunctionExpression(node) || import_core.types.isFunctionExpression(node)) {
|
|
482
|
-
return true;
|
|
483
|
-
}
|
|
484
|
-
if (!import_core.types.isCallExpression(node)) {
|
|
485
|
-
return false;
|
|
486
|
-
}
|
|
487
|
-
if (import_core.types.isIdentifier(node.callee) && (node.callee.name === "memo" || node.callee.name === "forwardRef")) {
|
|
488
|
-
return true;
|
|
489
|
-
}
|
|
490
|
-
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");
|
|
491
|
-
}
|
|
492
|
-
function hasSourcePropBinding(pattern) {
|
|
493
|
-
return pattern.properties.some((property) => {
|
|
494
|
-
if (!import_core.types.isObjectProperty(property)) {
|
|
495
|
-
return false;
|
|
496
|
-
}
|
|
497
|
-
return import_core.types.isIdentifier(property.key) && property.key.name === JSX_SOURCE_PROP;
|
|
498
|
-
});
|
|
499
|
-
}
|
|
500
|
-
function injectSourcePropBinding(pattern) {
|
|
501
|
-
if (hasSourcePropBinding(pattern)) {
|
|
502
|
-
return;
|
|
503
|
-
}
|
|
504
|
-
const sourceBinding = import_core.types.objectProperty(
|
|
505
|
-
import_core.types.identifier(JSX_SOURCE_PROP),
|
|
506
|
-
import_core.types.identifier(SOURCE_PROP_LOCAL),
|
|
507
|
-
false,
|
|
508
|
-
false
|
|
509
|
-
);
|
|
510
|
-
const restIndex = pattern.properties.findIndex(
|
|
511
|
-
(property) => import_core.types.isRestElement(property)
|
|
512
|
-
);
|
|
513
|
-
if (restIndex === -1) {
|
|
514
|
-
pattern.properties.push(sourceBinding);
|
|
515
|
-
return;
|
|
516
|
-
}
|
|
517
|
-
pattern.properties.splice(restIndex, 0, sourceBinding);
|
|
518
|
-
}
|
|
519
|
-
function injectSourcePropIntoIdentifierParam(node, param) {
|
|
520
|
-
if (!import_core.types.isBlockStatement(node.body)) {
|
|
521
|
-
node.body = import_core.types.blockStatement([import_core.types.returnStatement(node.body)]);
|
|
522
|
-
}
|
|
523
|
-
const alreadyInjected = node.body.body.some(
|
|
524
|
-
(statement) => import_core.types.isVariableDeclaration(statement) && statement.declarations.some(
|
|
525
|
-
(declaration) => import_core.types.isIdentifier(declaration.id) && declaration.id.name === SOURCE_PROPS_REST
|
|
526
|
-
)
|
|
527
|
-
);
|
|
528
|
-
if (alreadyInjected) {
|
|
529
|
-
return;
|
|
530
|
-
}
|
|
531
|
-
node.body.body.unshift(
|
|
532
|
-
import_core.types.variableDeclaration("const", [
|
|
533
|
-
import_core.types.variableDeclarator(
|
|
534
|
-
import_core.types.objectPattern([
|
|
535
|
-
import_core.types.objectProperty(
|
|
536
|
-
import_core.types.identifier(JSX_SOURCE_PROP),
|
|
537
|
-
import_core.types.identifier(SOURCE_PROP_LOCAL),
|
|
538
|
-
false,
|
|
539
|
-
false
|
|
540
|
-
),
|
|
541
|
-
import_core.types.restElement(import_core.types.identifier(SOURCE_PROPS_REST))
|
|
542
|
-
]),
|
|
543
|
-
param
|
|
544
|
-
)
|
|
545
|
-
]),
|
|
546
|
-
import_core.types.expressionStatement(
|
|
547
|
-
import_core.types.assignmentExpression(
|
|
548
|
-
"=",
|
|
549
|
-
import_core.types.identifier(param.name),
|
|
550
|
-
import_core.types.identifier(SOURCE_PROPS_REST)
|
|
551
|
-
)
|
|
552
|
-
)
|
|
553
|
-
);
|
|
554
|
-
}
|
|
555
|
-
function injectSourcePropIntoFunctionParams(node) {
|
|
556
|
-
const firstParam = node.params[0];
|
|
557
|
-
if (!firstParam) {
|
|
558
|
-
return;
|
|
559
|
-
}
|
|
560
|
-
if (import_core.types.isObjectPattern(firstParam)) {
|
|
561
|
-
injectSourcePropBinding(firstParam);
|
|
562
|
-
return;
|
|
563
|
-
}
|
|
564
|
-
if (import_core.types.isIdentifier(firstParam)) {
|
|
565
|
-
injectSourcePropIntoIdentifierParam(node, firstParam);
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
function injectSourcePropIntoExpression(node) {
|
|
569
|
-
if (!node) {
|
|
570
|
-
return;
|
|
571
|
-
}
|
|
572
|
-
if (import_core.types.isFunctionExpression(node) || import_core.types.isArrowFunctionExpression(node)) {
|
|
573
|
-
injectSourcePropIntoFunctionParams(node);
|
|
574
|
-
return;
|
|
575
|
-
}
|
|
576
|
-
if (!import_core.types.isCallExpression(node)) {
|
|
577
|
-
return;
|
|
578
|
-
}
|
|
579
|
-
const firstArg = node.arguments[0];
|
|
580
|
-
if (firstArg && !import_core.types.isSpreadElement(firstArg) && (import_core.types.isFunctionExpression(firstArg) || import_core.types.isArrowFunctionExpression(firstArg))) {
|
|
581
|
-
injectSourcePropIntoFunctionParams(firstArg);
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
function getSourceValue(state, loc, projectRoot) {
|
|
585
|
-
const filename = state.file?.opts?.filename;
|
|
586
|
-
if (!filename || !loc) {
|
|
587
|
-
return null;
|
|
588
|
-
}
|
|
589
|
-
return toRelativeSource(filename, loc, projectRoot);
|
|
590
|
-
}
|
|
591
|
-
function buildAssignment(name, sourceValue) {
|
|
592
|
-
return import_core.types.expressionStatement(
|
|
593
|
-
import_core.types.assignmentExpression(
|
|
594
|
-
"=",
|
|
595
|
-
import_core.types.memberExpression(import_core.types.identifier(name), import_core.types.identifier(SOURCE_PROP)),
|
|
596
|
-
import_core.types.stringLiteral(sourceValue)
|
|
597
|
-
)
|
|
598
|
-
);
|
|
599
|
-
}
|
|
600
|
-
function buildIntrinsicSourceHelper() {
|
|
601
|
-
return import_core.types.functionDeclaration(
|
|
602
|
-
import_core.types.identifier("_markIntrinsicElementSource"),
|
|
603
|
-
[import_core.types.identifier("element"), import_core.types.identifier("source")],
|
|
604
|
-
import_core.types.blockStatement([
|
|
605
|
-
import_core.types.variableDeclaration("const", [
|
|
606
|
-
import_core.types.variableDeclarator(
|
|
607
|
-
import_core.types.identifier("registryKey"),
|
|
608
|
-
import_core.types.callExpression(
|
|
609
|
-
import_core.types.memberExpression(import_core.types.identifier("Symbol"), import_core.types.identifier("for")),
|
|
610
|
-
[import_core.types.stringLiteral(JSX_SOURCE_REGISTRY_SYMBOL)]
|
|
611
|
-
)
|
|
612
|
-
)
|
|
613
|
-
]),
|
|
614
|
-
import_core.types.variableDeclaration("let", [
|
|
615
|
-
import_core.types.variableDeclarator(
|
|
616
|
-
import_core.types.identifier("registry"),
|
|
617
|
-
import_core.types.memberExpression(import_core.types.identifier("globalThis"), import_core.types.identifier("registryKey"), true)
|
|
618
|
-
)
|
|
619
|
-
]),
|
|
620
|
-
import_core.types.ifStatement(
|
|
621
|
-
import_core.types.unaryExpression(
|
|
622
|
-
"!",
|
|
623
|
-
import_core.types.binaryExpression("instanceof", import_core.types.identifier("registry"), import_core.types.identifier("WeakMap"))
|
|
624
|
-
),
|
|
625
|
-
import_core.types.blockStatement([
|
|
626
|
-
import_core.types.expressionStatement(
|
|
627
|
-
import_core.types.assignmentExpression(
|
|
628
|
-
"=",
|
|
629
|
-
import_core.types.identifier("registry"),
|
|
630
|
-
import_core.types.assignmentExpression(
|
|
631
|
-
"=",
|
|
632
|
-
import_core.types.memberExpression(import_core.types.identifier("globalThis"), import_core.types.identifier("registryKey"), true),
|
|
633
|
-
import_core.types.newExpression(import_core.types.identifier("WeakMap"), [])
|
|
634
|
-
)
|
|
635
|
-
)
|
|
636
|
-
)
|
|
637
|
-
])
|
|
638
|
-
),
|
|
639
|
-
import_core.types.ifStatement(
|
|
640
|
-
import_core.types.logicalExpression(
|
|
641
|
-
"&&",
|
|
642
|
-
import_core.types.identifier("element"),
|
|
643
|
-
import_core.types.logicalExpression(
|
|
644
|
-
"&&",
|
|
645
|
-
import_core.types.binaryExpression("===", import_core.types.unaryExpression("typeof", import_core.types.identifier("element")), import_core.types.stringLiteral("object")),
|
|
646
|
-
import_core.types.binaryExpression(
|
|
647
|
-
"===",
|
|
648
|
-
import_core.types.unaryExpression("typeof", import_core.types.memberExpression(import_core.types.identifier("element"), import_core.types.identifier("props"))),
|
|
649
|
-
import_core.types.stringLiteral("object")
|
|
650
|
-
)
|
|
651
|
-
)
|
|
652
|
-
),
|
|
653
|
-
import_core.types.blockStatement([
|
|
654
|
-
import_core.types.expressionStatement(
|
|
655
|
-
import_core.types.callExpression(
|
|
656
|
-
import_core.types.memberExpression(import_core.types.identifier("registry"), import_core.types.identifier("set")),
|
|
657
|
-
[import_core.types.memberExpression(import_core.types.identifier("element"), import_core.types.identifier("props")), import_core.types.identifier("source")]
|
|
658
|
-
)
|
|
659
|
-
)
|
|
660
|
-
])
|
|
661
|
-
),
|
|
662
|
-
import_core.types.returnStatement(import_core.types.identifier("element"))
|
|
663
|
-
])
|
|
664
|
-
);
|
|
665
|
-
}
|
|
666
|
-
function ensureIntrinsicSourceHelper(programPath, state) {
|
|
667
|
-
if (state.injectedIntrinsicHelper) {
|
|
668
|
-
return;
|
|
669
|
-
}
|
|
670
|
-
const alreadyExists = programPath.node.body.some(
|
|
671
|
-
(node) => import_core.types.isFunctionDeclaration(node) && import_core.types.isIdentifier(node.id, { name: "_markIntrinsicElementSource" })
|
|
672
|
-
);
|
|
673
|
-
if (!alreadyExists) {
|
|
674
|
-
programPath.unshiftContainer("body", buildIntrinsicSourceHelper());
|
|
675
|
-
}
|
|
676
|
-
state.injectedIntrinsicHelper = true;
|
|
677
|
-
}
|
|
678
|
-
function visitDeclaration(declarationPath, insertAfterPath, state, seen, projectRoot) {
|
|
679
|
-
if (declarationPath.isFunctionDeclaration() || declarationPath.isClassDeclaration()) {
|
|
680
|
-
const name = declarationPath.node.id?.name;
|
|
681
|
-
if (!name || !isComponentName(name) || seen.has(name)) {
|
|
682
|
-
return;
|
|
683
|
-
}
|
|
684
|
-
if (declarationPath.isFunctionDeclaration()) {
|
|
685
|
-
injectSourcePropIntoFunctionParams(declarationPath.node);
|
|
686
|
-
}
|
|
687
|
-
const sourceValue = getSourceValue(
|
|
688
|
-
state,
|
|
689
|
-
declarationPath.node.loc?.start,
|
|
690
|
-
projectRoot
|
|
691
|
-
);
|
|
692
|
-
if (!sourceValue) {
|
|
693
|
-
return;
|
|
694
|
-
}
|
|
695
|
-
seen.add(name);
|
|
696
|
-
insertAfterPath.insertAfter(buildAssignment(name, sourceValue));
|
|
697
|
-
return;
|
|
698
|
-
}
|
|
699
|
-
if (!declarationPath.isVariableDeclaration()) {
|
|
700
|
-
return;
|
|
701
|
-
}
|
|
702
|
-
const assignments = declarationPath.node.declarations.flatMap(
|
|
703
|
-
(declarator) => {
|
|
704
|
-
if (!import_core.types.isIdentifier(declarator.id) || !isComponentName(declarator.id.name) || seen.has(declarator.id.name)) {
|
|
705
|
-
return [];
|
|
706
|
-
}
|
|
707
|
-
if (!declarator.init) {
|
|
708
|
-
return [];
|
|
709
|
-
}
|
|
710
|
-
if (!isSupportedComponentInit(declarator.init)) {
|
|
711
|
-
return [];
|
|
712
|
-
}
|
|
713
|
-
injectSourcePropIntoExpression(declarator.init);
|
|
714
|
-
const sourceValue = getSourceValue(
|
|
715
|
-
state,
|
|
716
|
-
declarator.loc?.start ?? declarator.init.loc?.start,
|
|
717
|
-
projectRoot
|
|
718
|
-
);
|
|
719
|
-
if (!sourceValue) {
|
|
720
|
-
return [];
|
|
721
|
-
}
|
|
722
|
-
seen.add(declarator.id.name);
|
|
723
|
-
return [buildAssignment(declarator.id.name, sourceValue)];
|
|
724
|
-
}
|
|
725
|
-
);
|
|
726
|
-
if (assignments.length > 0) {
|
|
727
|
-
insertAfterPath.insertAfter(assignments);
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
function babelInjectComponentSource(options = {}) {
|
|
731
|
-
const {
|
|
732
|
-
injectJsxSource = true,
|
|
733
|
-
injectComponentSource = true,
|
|
734
|
-
projectRoot
|
|
735
|
-
} = options;
|
|
736
|
-
return {
|
|
737
|
-
name: "babel-inject-component-source",
|
|
738
|
-
visitor: {
|
|
739
|
-
CallExpression(pathNode, state) {
|
|
740
|
-
if (!injectJsxSource) {
|
|
741
|
-
return;
|
|
742
|
-
}
|
|
743
|
-
if (!isReactElementFactoryCall(pathNode)) {
|
|
744
|
-
return;
|
|
745
|
-
}
|
|
746
|
-
if (pathNode.parentPath.isCallExpression() && import_core.types.isIdentifier(pathNode.parentPath.node.callee, {
|
|
747
|
-
name: "_markIntrinsicElementSource"
|
|
748
|
-
})) {
|
|
749
|
-
return;
|
|
750
|
-
}
|
|
751
|
-
const sourceValue = getSourceValue(
|
|
752
|
-
state,
|
|
753
|
-
pathNode.node.loc?.start,
|
|
754
|
-
projectRoot
|
|
755
|
-
);
|
|
756
|
-
if (!sourceValue) {
|
|
757
|
-
return;
|
|
758
|
-
}
|
|
759
|
-
const programPath = pathNode.findParent((parent) => parent.isProgram());
|
|
760
|
-
if (!programPath || !programPath.isProgram()) {
|
|
761
|
-
return;
|
|
762
|
-
}
|
|
763
|
-
ensureIntrinsicSourceHelper(programPath, state);
|
|
764
|
-
pathNode.replaceWith(
|
|
765
|
-
import_core.types.callExpression(import_core.types.identifier("_markIntrinsicElementSource"), [
|
|
766
|
-
pathNode.node,
|
|
767
|
-
import_core.types.stringLiteral(sourceValue)
|
|
768
|
-
])
|
|
769
|
-
);
|
|
770
|
-
pathNode.skip();
|
|
771
|
-
},
|
|
772
|
-
JSXElement: {
|
|
773
|
-
exit(pathNode, state) {
|
|
774
|
-
if (!injectJsxSource) {
|
|
775
|
-
return;
|
|
776
|
-
}
|
|
777
|
-
if (!isIntrinsicElementTag(pathNode.node.openingElement.name)) {
|
|
778
|
-
return;
|
|
779
|
-
}
|
|
780
|
-
if (pathNode.parentPath.isCallExpression() && import_core.types.isIdentifier(pathNode.parentPath.node.callee, { name: "_markIntrinsicElementSource" })) {
|
|
781
|
-
return;
|
|
782
|
-
}
|
|
783
|
-
const sourceValue = getSourceValue(
|
|
784
|
-
state,
|
|
785
|
-
pathNode.node.openingElement.loc?.start,
|
|
786
|
-
projectRoot
|
|
787
|
-
);
|
|
788
|
-
if (!sourceValue) {
|
|
789
|
-
return;
|
|
790
|
-
}
|
|
791
|
-
const programPath = pathNode.findParent((parent) => parent.isProgram());
|
|
792
|
-
if (!programPath || !programPath.isProgram()) {
|
|
793
|
-
return;
|
|
794
|
-
}
|
|
795
|
-
ensureIntrinsicSourceHelper(programPath, state);
|
|
796
|
-
const wrappedNode = import_core.types.callExpression(import_core.types.identifier("_markIntrinsicElementSource"), [
|
|
797
|
-
pathNode.node,
|
|
798
|
-
import_core.types.stringLiteral(sourceValue)
|
|
799
|
-
]);
|
|
800
|
-
if (pathNode.parentPath.isJSXElement() || pathNode.parentPath.isJSXFragment()) {
|
|
801
|
-
pathNode.replaceWith(import_core.types.jsxExpressionContainer(wrappedNode));
|
|
802
|
-
return;
|
|
803
|
-
}
|
|
804
|
-
if (pathNode.parentPath.isJSXExpressionContainer()) {
|
|
805
|
-
pathNode.parentPath.replaceWith(import_core.types.jsxExpressionContainer(wrappedNode));
|
|
806
|
-
return;
|
|
807
|
-
}
|
|
808
|
-
pathNode.replaceWith(wrappedNode);
|
|
809
|
-
}
|
|
810
|
-
},
|
|
811
|
-
JSXOpeningElement(pathNode, state) {
|
|
812
|
-
if (!injectJsxSource) {
|
|
813
|
-
return;
|
|
814
|
-
}
|
|
815
|
-
if (!isCustomComponentTag(pathNode.node.name)) {
|
|
816
|
-
return;
|
|
817
|
-
}
|
|
818
|
-
const rootIdentifierName = getRootJsxIdentifierName(pathNode.node.name);
|
|
819
|
-
if (rootIdentifierName && isExternalToProjectRoot(state.file?.opts?.filename, projectRoot) && isStyledModuleImport(pathNode.scope.getBinding(rootIdentifierName))) {
|
|
820
|
-
return;
|
|
821
|
-
}
|
|
822
|
-
const hasSourceProp = pathNode.node.attributes.some(
|
|
823
|
-
(attr) => import_core.types.isJSXAttribute(attr) && import_core.types.isJSXIdentifier(attr.name) && attr.name.name === JSX_SOURCE_PROP
|
|
824
|
-
);
|
|
825
|
-
if (hasSourceProp) {
|
|
826
|
-
return;
|
|
827
|
-
}
|
|
828
|
-
const filename = state.file?.opts?.filename;
|
|
829
|
-
const loc = pathNode.node.loc?.start;
|
|
830
|
-
if (!filename || !loc) {
|
|
831
|
-
return;
|
|
832
|
-
}
|
|
833
|
-
pathNode.node.attributes.push(
|
|
834
|
-
import_core.types.jsxAttribute(
|
|
835
|
-
import_core.types.jsxIdentifier(JSX_SOURCE_PROP),
|
|
836
|
-
import_core.types.stringLiteral(
|
|
837
|
-
getSourceValue(state, loc, projectRoot) ?? `${filename.replace(/\\/g, "/")}:${loc.line}:${loc.column + 1}`
|
|
838
|
-
)
|
|
839
|
-
)
|
|
840
|
-
);
|
|
841
|
-
},
|
|
842
|
-
Program(programPath, state) {
|
|
843
|
-
if (!injectComponentSource) {
|
|
844
|
-
return;
|
|
845
|
-
}
|
|
846
|
-
const seen = /* @__PURE__ */ new Set();
|
|
847
|
-
for (const childPath of programPath.get("body")) {
|
|
848
|
-
if (childPath.isExportNamedDeclaration() || childPath.isExportDefaultDeclaration()) {
|
|
849
|
-
const declarationPath = childPath.get("declaration");
|
|
850
|
-
if (!Array.isArray(declarationPath) && declarationPath.node) {
|
|
851
|
-
visitDeclaration(declarationPath, childPath, state, seen, projectRoot);
|
|
852
|
-
}
|
|
853
|
-
continue;
|
|
854
|
-
}
|
|
855
|
-
visitDeclaration(childPath, childPath, state, seen, projectRoot);
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
}
|
|
859
|
-
};
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
// src/babel.ts
|
|
863
|
-
function createBabelSourceAdapter(options = {}) {
|
|
864
|
-
const resolvedOptions = {
|
|
865
|
-
projectRoot: process.cwd(),
|
|
866
|
-
...options
|
|
867
|
-
};
|
|
868
|
-
return defineSourceAdapter({
|
|
869
|
-
kind: "babel",
|
|
870
|
-
name: "react-code-locator/babel",
|
|
871
|
-
options: resolvedOptions,
|
|
872
|
-
config: {
|
|
873
|
-
plugins: [[babelInjectComponentSource, resolvedOptions]]
|
|
874
|
-
}
|
|
875
|
-
});
|
|
876
|
-
}
|
|
877
|
-
var babelSourceAdapter = createBabelSourceAdapter();
|
|
878
|
-
|
|
879
|
-
// src/sourceTransform.ts
|
|
880
|
-
var import_core2 = require("@babel/core");
|
|
881
|
-
async function transformSourceWithLocator(code, options) {
|
|
882
|
-
const { filename, sourceMaps = true, projectRoot = process.cwd(), ...pluginOptions } = options;
|
|
883
|
-
const result = await (0, import_core2.transformAsync)(code, {
|
|
884
|
-
filename,
|
|
885
|
-
babelrc: false,
|
|
886
|
-
configFile: false,
|
|
887
|
-
sourceMaps,
|
|
888
|
-
parserOpts: {
|
|
889
|
-
sourceType: "module",
|
|
890
|
-
plugins: ["jsx", "typescript"]
|
|
891
|
-
},
|
|
892
|
-
generatorOpts: {
|
|
893
|
-
retainLines: true
|
|
894
|
-
},
|
|
895
|
-
plugins: [[babelInjectComponentSource, { ...pluginOptions, projectRoot }]]
|
|
896
|
-
});
|
|
897
|
-
return {
|
|
898
|
-
code: result?.code ?? code,
|
|
899
|
-
map: result?.map ?? null
|
|
900
|
-
};
|
|
901
|
-
}
|
|
902
|
-
|
|
903
|
-
// src/viteClientInjector.ts
|
|
904
|
-
var VIRTUAL_CLIENT_MODULE_ID = "virtual:react-code-locator/client";
|
|
905
|
-
var RESOLVED_VIRTUAL_CLIENT_MODULE_ID = `\0${VIRTUAL_CLIENT_MODULE_ID}`;
|
|
906
|
-
function createClientInjector(locatorOptions = {}) {
|
|
907
|
-
const serialized = JSON.stringify(locatorOptions);
|
|
908
|
-
return {
|
|
909
|
-
name: "react-code-locator-client-injector",
|
|
910
|
-
apply: "serve",
|
|
911
|
-
resolveId(id) {
|
|
912
|
-
if (id === VIRTUAL_CLIENT_MODULE_ID) {
|
|
913
|
-
return RESOLVED_VIRTUAL_CLIENT_MODULE_ID;
|
|
914
|
-
}
|
|
915
|
-
return null;
|
|
916
|
-
},
|
|
917
|
-
load(id) {
|
|
918
|
-
if (id !== RESOLVED_VIRTUAL_CLIENT_MODULE_ID) {
|
|
919
|
-
return null;
|
|
920
|
-
}
|
|
921
|
-
return `
|
|
922
|
-
import { enableReactComponentJump } from "react-code-locator/client";
|
|
923
|
-
|
|
924
|
-
enableReactComponentJump(${serialized});
|
|
925
|
-
`;
|
|
926
|
-
},
|
|
927
|
-
transformIndexHtml() {
|
|
928
|
-
return [
|
|
929
|
-
{
|
|
930
|
-
tag: "script",
|
|
931
|
-
attrs: {
|
|
932
|
-
type: "module",
|
|
933
|
-
src: `/@id/__x00__${VIRTUAL_CLIENT_MODULE_ID}`
|
|
934
|
-
},
|
|
935
|
-
injectTo: "head"
|
|
936
|
-
}
|
|
937
|
-
];
|
|
938
|
-
}
|
|
939
|
-
};
|
|
940
|
-
}
|
|
941
|
-
function createViteClientInjector(options = {}) {
|
|
942
|
-
const { command = "serve", locator = {}, injectClient = true } = options;
|
|
943
|
-
const isServe = command === "serve";
|
|
944
|
-
return [isServe && injectClient ? createClientInjector(locator) : null].filter(Boolean);
|
|
945
|
-
}
|
|
946
|
-
|
|
947
|
-
// src/vite.ts
|
|
948
|
-
function shouldTransformSource(id) {
|
|
949
|
-
if (id.includes("/node_modules/") || id.startsWith("\0")) {
|
|
950
|
-
return false;
|
|
951
|
-
}
|
|
952
|
-
return /\.[mc]?[jt]sx?$/.test(id);
|
|
953
|
-
}
|
|
954
|
-
function viteSourceTransformPlugin(options = {}) {
|
|
955
|
-
return {
|
|
956
|
-
name: "react-code-locator-source-transform",
|
|
957
|
-
enforce: "pre",
|
|
958
|
-
async transform(code, id) {
|
|
959
|
-
if (!shouldTransformSource(id)) {
|
|
960
|
-
return null;
|
|
961
|
-
}
|
|
962
|
-
return transformSourceWithLocator(code, {
|
|
963
|
-
filename: id,
|
|
964
|
-
...options
|
|
965
|
-
});
|
|
966
|
-
}
|
|
967
|
-
};
|
|
968
|
-
}
|
|
969
|
-
function createViteSourceAdapter(options = {}) {
|
|
970
|
-
const { babel = {}, ...viteOptions } = options;
|
|
971
|
-
const resolvedBabelOptions = {
|
|
972
|
-
projectRoot: process.cwd(),
|
|
973
|
-
...babel
|
|
974
|
-
};
|
|
975
|
-
const plugins = [
|
|
976
|
-
viteSourceTransformPlugin(resolvedBabelOptions),
|
|
977
|
-
...createViteClientInjector(viteOptions)
|
|
978
|
-
].filter(Boolean);
|
|
979
|
-
return defineSourceAdapter({
|
|
980
|
-
kind: "vite",
|
|
981
|
-
name: "react-code-locator/vite",
|
|
982
|
-
options: {
|
|
983
|
-
...viteOptions,
|
|
984
|
-
babel: resolvedBabelOptions
|
|
985
|
-
},
|
|
986
|
-
config: {
|
|
987
|
-
plugins
|
|
988
|
-
}
|
|
989
|
-
});
|
|
990
|
-
}
|
|
991
|
-
var viteSourceAdapter = createViteSourceAdapter();
|
|
992
|
-
|
|
993
|
-
// src/esbuild.ts
|
|
994
|
-
var import_promises = require("fs/promises");
|
|
995
|
-
function getEsbuildLoader(filename) {
|
|
996
|
-
if (filename.endsWith(".tsx")) {
|
|
997
|
-
return "tsx";
|
|
998
|
-
}
|
|
999
|
-
if (filename.endsWith(".ts")) {
|
|
1000
|
-
return "ts";
|
|
1001
|
-
}
|
|
1002
|
-
if (filename.endsWith(".jsx")) {
|
|
1003
|
-
return "jsx";
|
|
1004
|
-
}
|
|
1005
|
-
return "js";
|
|
1006
|
-
}
|
|
1007
|
-
function esbuildSourceTransformPlugin(options = {}) {
|
|
1008
|
-
return {
|
|
1009
|
-
name: "react-code-locator-source-transform",
|
|
1010
|
-
setup(build) {
|
|
1011
|
-
build.onLoad({ filter: /\.[mc]?[jt]sx?$/ }, async ({ path }) => {
|
|
1012
|
-
if (path.includes("/node_modules/")) {
|
|
1013
|
-
return null;
|
|
1014
|
-
}
|
|
1015
|
-
const code = await (0, import_promises.readFile)(path, "utf8");
|
|
1016
|
-
const result = await transformSourceWithLocator(code, {
|
|
1017
|
-
filename: path,
|
|
1018
|
-
...options
|
|
1019
|
-
});
|
|
1020
|
-
return {
|
|
1021
|
-
contents: result.code,
|
|
1022
|
-
loader: getEsbuildLoader(path)
|
|
1023
|
-
};
|
|
1024
|
-
});
|
|
1025
|
-
}
|
|
1026
|
-
};
|
|
1027
|
-
}
|
|
1028
|
-
function createEsbuildSourceAdapter(options = {}) {
|
|
1029
|
-
const resolvedOptions = {
|
|
1030
|
-
projectRoot: process.cwd(),
|
|
1031
|
-
...options
|
|
1032
|
-
};
|
|
1033
|
-
return defineSourceAdapter({
|
|
1034
|
-
kind: "esbuild",
|
|
1035
|
-
name: "react-code-locator/esbuild",
|
|
1036
|
-
options: resolvedOptions,
|
|
1037
|
-
config: {
|
|
1038
|
-
plugins: [esbuildSourceTransformPlugin(resolvedOptions)]
|
|
1039
|
-
}
|
|
1040
|
-
});
|
|
1041
|
-
}
|
|
1042
|
-
var esbuildSourceAdapter = createEsbuildSourceAdapter();
|
|
1043
|
-
|
|
1044
|
-
// src/swc.ts
|
|
1045
|
-
async function transformSourceWithSwcLocator(code, options) {
|
|
1046
|
-
return transformSourceWithLocator(code, options);
|
|
1047
|
-
}
|
|
1048
|
-
function createSwcSourceAdapter(options = {}) {
|
|
1049
|
-
const resolvedOptions = {
|
|
1050
|
-
projectRoot: process.cwd(),
|
|
1051
|
-
...options
|
|
1052
|
-
};
|
|
1053
|
-
const transform = (code, transformOptions) => transformSourceWithSwcLocator(code, {
|
|
1054
|
-
...resolvedOptions,
|
|
1055
|
-
...transformOptions
|
|
1056
|
-
});
|
|
1057
|
-
return defineSourceAdapter({
|
|
1058
|
-
kind: "swc",
|
|
1059
|
-
name: "react-code-locator/swc",
|
|
1060
|
-
options: resolvedOptions,
|
|
1061
|
-
config: {
|
|
1062
|
-
transform
|
|
1063
|
-
}
|
|
1064
|
-
});
|
|
1065
|
-
}
|
|
1066
|
-
var swcSourceAdapter = createSwcSourceAdapter();
|
|
1067
395
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1068
396
|
0 && (module.exports = {
|
|
1069
|
-
babelInjectComponentSource,
|
|
1070
|
-
babelSourceAdapter,
|
|
1071
|
-
createBabelSourceAdapter,
|
|
1072
|
-
createEsbuildSourceAdapter,
|
|
1073
|
-
createSwcSourceAdapter,
|
|
1074
|
-
createViteClientInjector,
|
|
1075
|
-
createViteSourceAdapter,
|
|
1076
|
-
defineSourceAdapter,
|
|
1077
397
|
enableReactComponentJump,
|
|
1078
|
-
|
|
1079
|
-
locateComponentSource,
|
|
1080
|
-
reactComponentJump,
|
|
1081
|
-
swcSourceAdapter,
|
|
1082
|
-
viteSourceAdapter
|
|
398
|
+
locateComponentSource
|
|
1083
399
|
});
|
|
1084
400
|
//# sourceMappingURL=index.cjs.map
|