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