UNPKG

@metagptx/vite-plugin-source-locator

Version:

A Vite plugin for source code location tracking

7 lines (6 loc) 2.61 kB
/*! * @metagptx/vite-plugin-source-locator v0.0.19 * (c) 2026 caowenyue@fuzhi.ai */ "use strict";var e=require("path"),t=require("@babel/types"),r=require("./utils-lqg7iSLU.js");function i(e){var t=Object.create(null);return e&&Object.keys(e).forEach(function(r){if("default"!==r){var i=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,i.get?i:{enumerable:!0,get:function(){return e[r]}})}}),t.default=e,Object.freeze(t)}require("util");var n=i(t);function s(e){let t,r=e[0],i=1;for(;i<e.length;){const n=e[i],s=e[i+1];if(i+=2,("optionalAccess"===n||"optionalCall"===n)&&null==r)return;"access"===n||"optionalAccess"===n?(t=r,r=s(r)):"call"!==n&&"optionalCall"!==n||(r=s((...e)=>r.call(t,...e)),t=void 0)}return r}const o="locator",a=["**/*.{jsx,tsx,js,ts}"],c=e=>Object.entries(e).map(([e,t])=>((e,t)=>{const r=n.isValidIdentifier(e)?n.identifier(e):n.stringLiteral(e);return n.objectProperty(r,n.stringLiteral(t))})(r.dataAttributeNameToDataSetKey(e),t)),u=(e,t)=>{if(e.value&&n.isJSXExpressionContainer(e.value))return n.isObjectExpression(e.value.expression)?void((e,t)=>{const r=e.properties.findIndex(e=>n.isSpreadElement(e));-1===r?e.properties.push(...t):e.properties.splice(r,0,...t)})(e.value.expression,t):void(e.value.expression=n.objectExpression([...t,n.spreadElement(e.value.expression)]));e.value=n.jsxExpressionContainer(n.objectExpression(t))};exports.rnSourceLocator=function(t={}){const{include:i=a,exclude:l=[],prefix:p=o,enable:d=!0,attributes:f,filterTag:b}=t;return function(){return{name:"@metagptx/source-locator-rn",visitor:{JSXOpeningElement(t,o){if(!d)return;const a=o.filename||s([o,"access",e=>e.file,"optionalAccess",e=>e.opts,"optionalAccess",e=>e.filename]),x=s([o,"access",e=>e.file,"optionalAccess",e=>e.code]);if(!a||!x)return;const j=e.relative(process.cwd(),a);if(!r.isIncludedFile(j,i,l))return;if(r.hasInjectedDataSetAttribute(t,p))return;if(r.isFragmentElement(t))return;if(r.isSvgChild(t))return;const v=r.getJSXTagName(t.node.name);if(!r.shouldInjectTag(v,b))return;const m=r.findWorkspaceRoot(process.cwd()),S=e.relative(m,a),g=r.createJsxLocatorAttributes({code:x,path:t,prefix:p,relativePath:S,attributes:f});if(!g)return;const A=c(g.attrs),E=t.node.attributes.find(e=>n.isJSXAttribute(e)&&n.isJSXIdentifier(e.name)&&"dataSet"===e.name.name);if(E&&n.isJSXAttribute(E))return void u(E,A);const h=t.node.attributes.findIndex(e=>n.isJSXSpreadAttribute(e)),O=n.jsxAttribute(n.jsxIdentifier("dataSet"),n.jsxExpressionContainer(n.objectExpression(A)));-1===h?t.node.attributes.push(O):t.node.attributes.splice(h,0,O)}}}}}; //# sourceMappingURL=rn.js.map