@vcp-dev/site-edit-engine 0.11.11 → 0.11.13
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 +4 -0
- package/dist/index.js +11 -11
- package/dist/site-edit-instrumentation-loader.cjs +1 -1
- package/dist/webpack-loader.cjs +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var k=require("@babel/generator"),ee=require("@babel/parser"),L=require("@babel/traverse"),r=require("@babel/types"),U=require("node:fs"),d=require("node:path"),te=typeof k=="function"?k:k.default,F=typeof L=="function"?L:L.default,Z={key:"data-vcp-edit-key",parentKey:"data-vcp-edit-parent-key",movable:"data-vcp-movable",removable:"data-vcp-removable",flags:"data-vcp-flags"},ne=new Set(["base","body","head","html","link","meta","noscript","script","style","template","title"]);var re="suppressHydrationWarning",ie=new Set(["SiteEditBootstrap"]),se="data-vcp-ui-component",J="components/ui";function oe(e,t){this.cacheable?.();let n=this.async();try{if(!ce(this.resourcePath)){n(null,e,t);return}let i=this.getOptions?.()??{},s=H({source:e,file:this.resourcePath,projectRoot:i.projectRoot,cmsPreviewModulePath:ae(i.projectRoot)});n(null,s.code,s.code===e?t:null)}catch(i){n(i)}}function ae(e){try{let t=require.resolve("@vcp-dev/site-edit-bridge/auto-bootstrap",{paths:[e||process.cwd()]});for(let n of[d.dirname(t),d.resolve(d.dirname(t),"../dist")]){let i=d.join(n,"cms-preview-record.js");if(U.existsSync(i)&&U.existsSync(d.join(n,"cms-preview-probe.js")))return i}return}catch{return}}function ce(e){return typeof e=="string"&&/\.(jsx|tsx)$/.test(e)&&!e.includes("/node_modules/")&&!e.endsWith(".d.ts")}function H(e){let t=Ee(e.file,e.projectRoot);if(!me(e.source)||X(t))return{code:e.source,elements:[]};let n=e.attributes??Z,i=ee.parse(e.source,{sourceType:"module",plugins:["jsx","typescript"]}),s=[],o=be(i,t);F(i,{JSXElement(l){let c=l.node.openingElement,E=c.name;if(!ve(E)||$(c,n.key))return;let b=Ce(l)??"default",w=we(l),S=ge(t,b,w),x=P(E)??"unknown",f=Se(E)?"jsx":"component-call";if(f==="component-call"&&!Pe(l.node.children)&&ie.has(x))return;let a=f==="component-call"&&he(x,o);Ie(c,n.key,S),a&&q(c,se),fe(l)&&q(c,re),s.push({key:S,tag:x,component_name:b,source_file:t,source_kind:f})}});let p=e.cmsPreviewModulePath?ue(i,e.cmsPreviewModulePath,t,e.file,e.projectRoot):!1;return s.length===0&&!p?{code:e.source,elements:s}:{code:te(i,{jsescOption:{minimal:!0}}).code,elements:s}}function W(e,t,n){if(!d.isAbsolute(e))return e;let i=d.isAbsolute(t)?t:d.resolve(n||process.cwd(),t),s=d.relative(d.dirname(i),e).split(d.sep).join("/");return s.startsWith(".")?s:`./${s}`}function ue(e,t,n,i,s){let o;if(F(e,{Program(a){o=a,a.stop()}}),!o)return!1;let p=o.scope.generateUidIdentifier("vcpAttachCmsPreviewRecord"),l=o.scope.generateUidIdentifier("vcpStripCmsPreviewMetadata"),c=o.scope.generateUidIdentifier("vcpReadCmsPreviewCandidate"),E=o.scope.generateUidIdentifier("VcpCmsPreviewProbe"),b=!1,w=!1,S=!1,x=/(?:^|\/)(?:[^/]+-)?(?:list|detail)-view\.[jt]sx$/.test(n)||e.program.body.some(a=>r.isImportDeclaration(a)&&(a.source.value==="@/lib/cms"||/^@\/lib\/cms\/generated\//.test(a.source.value)));F(e,{CallExpression(a){let u=a.node;if(r.isMemberExpression(u.callee)&&!u.callee.computed&&r.isIdentifier(u.callee.property,{name:"map"})&&u.arguments.length===1&&r.isIdentifier(u.arguments[0])){let I=D(a,u.arguments[0].name),C=de(u.callee.object),B=C&&z(a,C);if(I&&B?.operation==="list"&&B.slug===I.slug){let R=a.scope.generateUidIdentifier("vcpCmsRecord");u.arguments[0]=r.arrowFunctionExpression([R],r.callExpression(p,[r.callExpression(r.cloneNode(u.arguments[0]),[r.cloneNode(R)]),r.cloneNode(R),r.stringLiteral(I.slug),r.stringLiteral("list")])),b=!0,a.skip()}return}if(!r.isIdentifier(u.callee)||u.arguments.length!==1)return;let m=D(a,u.callee.name),y=r.isIdentifier(u.arguments[0]),v=y?u.arguments[0].name:N(u.arguments[0],["data"]),h=v&&z(a,v);if(!m||h?.operation!=="detail"||(y?h.slug!==m.slug:h.slug!==void 0))return;let M=r.cloneNode(u,!0),A=r.cloneNode(u.arguments[0],!0);a.replaceWith(r.callExpression(p,[M,A,r.stringLiteral(m.slug),r.stringLiteral("detail")])),b=!0,a.skip()},JSXSpreadAttribute(a){a.node.argument=r.callExpression(l,[a.node.argument]),w=!0},JSXElement:{exit(a){if(!x)return;let u=a.node.openingElement;if(!$(u,Z.key))return;let m=le(a);if(!m)return;let y=u.attributes.findIndex(C=>r.isJSXAttribute(C)&&r.isJSXIdentifier(C.name,{name:"key"})),v=y<0?null:u.attributes.splice(y,1)[0],h=[r.identifier(m.base),r.stringLiteral(m.field)];m.index&&h.push(r.cloneNode(m.index,!0));let M=[...v?[v]:[],r.jsxAttribute(r.jsxIdentifier("candidate"),r.jsxExpressionContainer(r.callExpression(c,h)))],A=r.jsxIdentifier(E.name),I=r.jsxElement(r.jsxOpeningElement(r.cloneNode(A),M,!1),r.jsxClosingElement(r.cloneNode(A)),[a.node]);a.replaceWith(I),S=!0,a.skip()}}});let f=[];return b&&f.push(r.importSpecifier(p,r.identifier("attachCmsPreviewRecord"))),w&&f.push(r.importSpecifier(l,r.identifier("stripCmsPreviewMetadata"))),S&&f.push(r.importSpecifier(c,r.identifier("readCmsPreviewCandidate"))),f.length&&e.program.body.unshift(r.importDeclaration(f,r.stringLiteral(W(t,i,s)))),S&&e.program.body.unshift(r.importDeclaration([r.importSpecifier(E,r.identifier("CmsPreviewProbe"))],r.stringLiteral(W(d.join(d.dirname(t),"cms-preview-probe.js"),i,s)))),f.length>0||S}function le(e){let t=e.node,n=t.openingElement.name,i=P(n);if(!i)return null;let s=t.openingElement.attributes.find(c=>r.isJSXAttribute(c)&&r.isJSXIdentifier(c.name,{name:"src"})),o=t.openingElement.attributes.find(c=>r.isJSXAttribute(c)&&r.isJSXIdentifier(c.name,{name:"dangerouslySetInnerHTML"})),p=i==="img"||/^[A-Z]/.test(i)&&s,l=p?r.isJSXAttribute(s)&&r.isJSXExpressionContainer(s.value)&&r.isExpression(s.value.expression)?[s.value.expression]:[]:r.isJSXAttribute(o)&&r.isJSXExpressionContainer(o.value)&&r.isExpression(o.value.expression)?[o.value.expression]:t.children.flatMap(c=>r.isJSXExpressionContainer(c)&&r.isExpression(c.expression)?[c.expression]:[]);return l.length!==1?null:p&&r.isIdentifier(l[0])&&e.scope.hasBinding(l[0].name)?{base:l[0].name,field:"url"}:pe(l[0],e)}function pe(e,t){let n=e,i=[];for(;r.isMemberExpression(n)||r.isOptionalMemberExpression(n);){if(!n.computed&&r.isIdentifier(n.property))i.unshift({property:n.property.name});else if(n.computed&&r.isNumericLiteral(n.property)&&Number.isSafeInteger(n.property.value)&&n.property.value>=0)i.unshift({index:n.property});else if(n.computed&&r.isIdentifier(n.property)&&t.scope.hasBinding(n.property.name))i.unshift({index:n.property});else return null;n=n.object}if(!r.isIdentifier(n)||!t.scope.hasBinding(n.name)||(i[0]?.property==="fields"&&i.shift(),!i[0]?.property||i.slice(2).some(o=>o.index)))return null;let s=i[1];return{base:n.name,field:i[0].property,...s?.index?{index:s.index}:{}}}function D(e,t){let n=e.scope.getBinding(t);if(!n?.constant||!n.path.isImportSpecifier())return null;let i=n.path.node;if(!r.isIdentifier(i.imported)||!/^to[A-Za-z0-9_$]+View$/.test(i.imported.name))return null;let s=n.path.parentPath.node.source.value,o=typeof s=="string"&&s.match(/^@\/lib\/cms\/generated\/([a-z0-9][a-z0-9-]*)$/);return o?{slug:o[1]}:null}function N(e,t){let n=e;for(let i of[...t].reverse()){if(!r.isMemberExpression(n)||n.computed||!r.isIdentifier(n.property,{name:i}))return null;n=n.object}return r.isIdentifier(n)?n.name:null}function de(e){if(r.isConditionalExpression(e)){let t=N(e.consequent,["data","items"]);return!t||!r.isArrayExpression(e.alternate)||e.alternate.elements.length!==0||!r.isBinaryExpression(e.test,{operator:"==="})||N(e.test.left,["kind"])!==t||!r.isStringLiteral(e.test.right,{value:"ok"})?null:t}return N(e,["data","items"])}function z(e,t){let n=e.scope.getBinding(t);if(!n?.constant||!n.path.isVariableDeclarator())return null;let i=n.path.node.init,s=r.isAwaitExpression(i)?i.argument:null;if(!r.isCallExpression(s)||!r.isMemberExpression(s.callee)||s.callee.computed||!r.isIdentifier(s.callee.property)||!r.isMemberExpression(s.callee.object)||s.callee.object.computed||!r.isIdentifier(s.callee.object.property,{name:"collections"})||!r.isIdentifier(s.callee.object.object))return null;let o=e.scope.getBinding(s.callee.object.object.name);return!o?.constant||!o.path.isImportSpecifier()||!r.isIdentifier(o.path.node.imported,{name:"cms"})||o.path.parentPath.node.source.value!=="@/lib/cms"?null:s.callee.property.name==="listStrict"&&r.isStringLiteral(s.arguments[0])?{operation:"list",slug:s.arguments[0].value}:s.callee.property.name==="getByUrlStrict"?{operation:"detail"}:s.callee.property.name==="get"&&r.isStringLiteral(s.arguments[0])?{operation:"detail",slug:s.arguments[0].value}:null}function fe(e){return!e.findParent(t=>t.isJSXElement())}function me(e){if(typeof e!="string")throw new TypeError("site edit instrumentation source must be a string");return/(?:^|[=(:,?;\[{>&|\n\r]|return\s+)\s*<(?:[A-Za-z][\w.:-]*|>)(?:\s|>|\/)/m.test(e)}function ge(e,t,n){return`${e}::${t}::${n}`}function Ee(e,t){let n=e.replace(/\\/g,"/").replace(/^\.\//,""),i=t?.replace(/\\/g,"/").replace(/\/+$/,"");if(i&&n.startsWith(`${i}/`))return n.slice(i.length+1);for(let[o,p]of[["/src/app/",1],["/src/components/",1],["/app/",1],["/components/",1]]){let l=n.indexOf(o);if(l>=0)return n.slice(l+p)}return n.match(/(?:^|\/)((?:src\/app|src\/components|app|components)\/.+)$/)?.[1]??n}function Se(e){return r.isJSXIdentifier(e)&&/^[a-z]/.test(e.name)}function P(e){return r.isJSXIdentifier(e)?e.name:r.isJSXMemberExpression(e)?G(e):null}function be(e,t){let n=new Set;for(let i of e.program.body)if(!(!r.isImportDeclaration(i)||!xe(i.source.value,t)))for(let s of i.specifiers)n.add(s.local.name);return n}function he(e,t){let n=e.split(".")[0]??e;return t.has(n)}function X(e){if(!e)return!1;let t=O(e);return t===J||t.startsWith(`${J}/`)||t.includes(`/${J}/`)||t.endsWith(`/${J}`)}function xe(e,t){return e?e.startsWith("@/")||e.startsWith("~/")?X(e.slice(2)):e.startsWith("./")||e.startsWith("../")?X(O(`${ye(t)}/${e}`)):X(e):!1}function O(e){let n=(e.split("?")[0]??e).replace(/\\/g,"/").replace(/^\.\//,""),i=[];for(let s of n.split("/"))if(!(!s||s===".")){if(s===".."){i.pop();continue}i.push(s)}return i.join("/")}function ye(e){let t=O(e),n=t.lastIndexOf("/");return n>=0?t.slice(0,n):""}function ve(e){let t=P(e);return!t||t==="Fragment"||t.endsWith(".Fragment")?!1:!ne.has(t)}function $(e,t){return e.attributes.some(n=>r.isJSXAttribute(n)&&r.isJSXIdentifier(n.name,{name:t}))}function Ie(e,t,n){Q(e,r.jsxAttribute(r.jsxIdentifier(t),r.stringLiteral(n)))}function q(e,t){$(e,t)||Q(e,r.jsxAttribute(r.jsxIdentifier(t),null))}function Q(e,t){let n=e.attributes.findIndex(i=>r.isJSXSpreadAttribute(i));if(n>=0){e.attributes.splice(n,0,t);return}e.attributes.push(t)}function G(e){return`${r.isJSXIdentifier(e.object)?e.object.name:G(e.object)}.${e.property.name}`}function Ce(e){let t=e;for(;t;){if(t.isFunctionDeclaration()||t.isClassDeclaration())return t.node.id?.name??null;if(t.isVariableDeclarator()&&r.isIdentifier(t.node.id)){let n=t.get("init");if(!Array.isArray(n)&&(n.isArrowFunctionExpression()||n.isFunctionExpression()))return t.node.id.name}t=t.parentPath}return null}function Pe(e){return e.some(t=>r.isJSXElement(t)||r.isJSXFragment(t))}var K=new WeakMap,j=new WeakMap,_=new WeakMap;function we(e){let t=K.get(e.node);if(t!==void 0)return t;let n=[],i=e;for(;i;){if(i.isJSXElement()){let o=Ae(i);o&&n.unshift(o)}i=i.parentPath}let s=n.join("/");return K.set(e.node,s),s}function Ae(e){let t=P(e.node.openingElement.name);return!t||t==="Fragment"||t.endsWith(".Fragment")?null:/^[A-Z]/.test(t)?`component:${t}#${V(e,t)}`:`${t}#${V(e,t)}`}function V(e,t){let n=Y(e)??e.getFunctionParent();if(!n)return 0;let i=_.get(n.node)?.get(t)?.get(e.node);if(i!==void 0)return i;let s=_.get(n.node);s||(s=new Map,_.set(n.node,s));let o=s.get(t);o||(o=new WeakMap,s.set(t,o));let p=0,l=n.isJSXElement()||n.isJSXFragment()?T(n):Je(n);for(let c of l)if(P(c.node.openingElement.name)===t){if(o.set(c.node,p),c.node===e.node)return p;p+=1}return o.get(e.node)??0}function Je(e){let t=j.get(e.node);if(t)return t;let n=[];return e.traverse({JSXElement(i){i.getFunctionParent()?.node===e.node&&!Y(i)&&n.push(i)}}),j.set(e.node,n),n}function Y(e){let t=e.parentPath,n=null;for(;t;){if(t.isJSXFragment()){n=t,t=t.parentPath;continue}if(t.isJSXElement())return t;t=t.parentPath}return n}function T(e){let t=j.get(e.node);if(t)return t;let n=[],i=e.get("children");for(let s of i)if(!Array.isArray(s)){if(s.isJSXElement()){n.push(s);continue}if(s.isJSXFragment()){n.push(...T(s));continue}s.isJSXExpressionContainer()&&n.push(...g(s.get("expression")))}return j.set(e.node,n),n}function g(e){return e.isJSXElement()?[e]:e.isJSXFragment()?T(e):e.isParenthesizedExpression()?g(e.get("expression")):e.isLogicalExpression()&&e.node.operator==="&&"?g(e.get("right")):e.isConditionalExpression()?[...g(e.get("consequent")),...g(e.get("alternate"))]:e.isCallExpression()&&Xe(e.node)?Ne(e.get("arguments")[0]):[]}function Ne(e){if(!e?.isArrowFunctionExpression()&&!e?.isFunctionExpression())return[];let t=e.get("body");if(t.isJSXElement())return[t];if(t.isJSXFragment())return T(t);if(t.isParenthesizedExpression())return g(t.get("expression"));if(!t.isBlockStatement())return[];let n=[];for(let i of t.get("body")){if(i.isVariableDeclaration())for(let o of i.get("declarations")){let p=o.get("init");(p.isJSXElement()||p.isJSXFragment())&&n.push(...g(p))}if(!i.isReturnStatement())continue;let s=i.get("argument");return s.node&&n.push(...g(s)),n}return n}function Xe(e){return r.isMemberExpression(e.callee)&&r.isIdentifier(e.callee.property)&&e.callee.property.name==="map"}module.exports=oe;module.exports.instrumentSiteEditRuntimeAttributes=H;
|
|
1
|
+
"use strict";var k=require("@babel/generator"),ee=require("@babel/parser"),L=require("@babel/traverse"),r=require("@babel/types"),U=require("node:fs"),d=require("node:path"),te=typeof k=="function"?k:k.default,F=typeof L=="function"?L:L.default,K={key:"data-vcp-edit-key",parentKey:"data-vcp-edit-parent-key",movable:"data-vcp-movable",removable:"data-vcp-removable",flags:"data-vcp-flags"},ne=new Set(["base","body","head","html","link","meta","noscript","script","style","template","title"]);var re="suppressHydrationWarning",ie=new Set(["SiteEditBootstrap"]),se="data-vcp-ui-component",J="components/ui";function oe(e,t){this.cacheable?.();let n=this.async();try{if(!ce(this.resourcePath)){n(null,e,t);return}let s=this.getOptions?.()??{},i=Q({source:e,file:this.resourcePath,projectRoot:s.projectRoot,cmsPreviewModulePath:ae(s.projectRoot)});n(null,i.code,i.code===e?t:null)}catch(s){n(s)}}function ae(e){try{let t=require.resolve("@vcp-dev/site-edit-bridge/auto-bootstrap",{paths:[e||process.cwd()]});for(let n of[d.dirname(t),d.resolve(d.dirname(t),"../dist")]){let s=d.join(n,"cms-preview-record.js");if(U.existsSync(s)&&U.existsSync(d.join(n,"cms-preview-probe.js")))return s}return}catch{return}}function ce(e){return typeof e=="string"&&/\.(jsx|tsx)$/.test(e)&&!e.includes("/node_modules/")&&!e.endsWith(".d.ts")}function Q(e){let t=be(e.file,e.projectRoot);if(!me(e.source)||X(t))return{code:e.source,elements:[]};let n=e.attributes??K,s=ee.parse(e.source,{sourceType:"module",plugins:["jsx","typescript"]}),i=[],o=Se(s,t);F(s,{JSXElement(u){let c=u.node.openingElement,b=c.name;if(!ve(b)||B(c,n.key))return;let S=Ce(u)??"default",w=we(u),E=ge(t,S,w),x=P(b)??"unknown",f=Ee(b)?"jsx":"component-call";if(f==="component-call"&&!Pe(u.node.children)&&ie.has(x))return;let a=f==="component-call"&&he(x,o);Ie(c,n.key,E),a&&z(c,se),fe(u)&&z(c,re),i.push({key:E,tag:x,component_name:S,source_file:t,source_kind:f})}});let p=e.cmsPreviewModulePath?le(s,e.cmsPreviewModulePath,t,e.file,e.projectRoot):!1;return i.length===0&&!p?{code:e.source,elements:i}:{code:te(s,{jsescOption:{minimal:!0}}).code,elements:i}}function W(e,t,n){if(!d.isAbsolute(e))return e;let s=d.isAbsolute(t)?t:d.resolve(n||process.cwd(),t),i=d.relative(d.dirname(s),e).split(d.sep).join("/");return i.startsWith(".")?i:`./${i}`}function le(e,t,n,s,i){let o;if(F(e,{Program(a){o=a,a.stop()}}),!o)return!1;let p=o.scope.generateUidIdentifier("vcpAttachCmsPreviewRecord"),u=o.scope.generateUidIdentifier("vcpStripCmsPreviewMetadata"),c=o.scope.generateUidIdentifier("vcpReadCmsPreviewCandidate"),b=o.scope.generateUidIdentifier("VcpCmsPreviewProbe"),S=!1,w=!1,E=!1,x=/(?:^|\/)(?:[^/]+-)?(?:list|detail)-view\.[jt]sx$/.test(n)||e.program.body.some(a=>r.isImportDeclaration(a)&&(a.source.value==="@/lib/cms"||/^@\/lib\/cms\/generated\//.test(a.source.value)));F(e,{CallExpression(a){let l=a.node;if(r.isMemberExpression(l.callee)&&!l.callee.computed&&r.isIdentifier(l.callee.property,{name:"map"})&&l.arguments.length===1&&r.isIdentifier(l.arguments[0])){let I=D(a,l.arguments[0].name),C=O(a,l.callee.object);if(I&&C?.operation==="list"&&C.slug===I.slug){let R=a.scope.generateUidIdentifier("vcpCmsRecord");l.arguments[0]=r.arrowFunctionExpression([R],r.callExpression(p,[r.callExpression(r.cloneNode(l.arguments[0]),[r.cloneNode(R)]),r.cloneNode(R),r.stringLiteral(I.slug),r.stringLiteral("list")])),S=!0,a.skip()}return}if(!r.isIdentifier(l.callee)||l.arguments.length!==1)return;let m=D(a,l.callee.name),y=r.isIdentifier(l.arguments[0]),v=y?l.arguments[0].name:N(l.arguments[0],["data"]),h=v&&Z(a,v);if(!m||h?.operation!=="detail"||(y?h.slug!==m.slug:h.slug!==void 0))return;let T=r.cloneNode(l,!0),A=r.cloneNode(l.arguments[0],!0);a.replaceWith(r.callExpression(p,[T,A,r.stringLiteral(m.slug),r.stringLiteral("detail")])),S=!0,a.skip()},JSXSpreadAttribute(a){a.node.argument=r.callExpression(u,[a.node.argument]),w=!0},JSXElement:{exit(a){if(!x)return;let l=a.node.openingElement;if(!B(l,K.key))return;let m=ue(a);if(!m)return;let y=l.attributes.findIndex(C=>r.isJSXAttribute(C)&&r.isJSXIdentifier(C.name,{name:"key"})),v=y<0?null:l.attributes.splice(y,1)[0],h=[r.identifier(m.base),r.stringLiteral(m.field)];m.index&&h.push(r.cloneNode(m.index,!0));let T=[...v?[v]:[],r.jsxAttribute(r.jsxIdentifier("candidate"),r.jsxExpressionContainer(r.callExpression(c,h)))],A=r.jsxIdentifier(b.name),I=r.jsxElement(r.jsxOpeningElement(r.cloneNode(A),T,!1),r.jsxClosingElement(r.cloneNode(A)),[a.node]);a.replaceWith(I),E=!0,a.skip()}}});let f=[];return S&&f.push(r.importSpecifier(p,r.identifier("attachCmsPreviewRecord"))),w&&f.push(r.importSpecifier(u,r.identifier("stripCmsPreviewMetadata"))),E&&f.push(r.importSpecifier(c,r.identifier("readCmsPreviewCandidate"))),f.length&&e.program.body.unshift(r.importDeclaration(f,r.stringLiteral(W(t,s,i)))),E&&e.program.body.unshift(r.importDeclaration([r.importSpecifier(b,r.identifier("CmsPreviewProbe"))],r.stringLiteral(W(d.join(d.dirname(t),"cms-preview-probe.js"),s,i)))),f.length>0||E}function ue(e){let t=e.node,n=t.openingElement.name,s=P(n);if(!s)return null;let i=t.openingElement.attributes.find(c=>r.isJSXAttribute(c)&&r.isJSXIdentifier(c.name,{name:"src"})),o=t.openingElement.attributes.find(c=>r.isJSXAttribute(c)&&r.isJSXIdentifier(c.name,{name:"dangerouslySetInnerHTML"})),p=s==="img"||/^[A-Z]/.test(s)&&i,u=p?r.isJSXAttribute(i)&&r.isJSXExpressionContainer(i.value)&&r.isExpression(i.value.expression)?[i.value.expression]:[]:r.isJSXAttribute(o)&&r.isJSXExpressionContainer(o.value)&&r.isExpression(o.value.expression)?[o.value.expression]:t.children.flatMap(c=>r.isJSXExpressionContainer(c)&&r.isExpression(c.expression)?[c.expression]:[]);return u.length!==1?null:p&&r.isIdentifier(u[0])&&e.scope.hasBinding(u[0].name)?{base:u[0].name,field:"url"}:pe(u[0],e)}function pe(e,t){let n=e,s=[];for(;r.isMemberExpression(n)||r.isOptionalMemberExpression(n);){if(!n.computed&&r.isIdentifier(n.property))s.unshift({property:n.property.name});else if(n.computed&&r.isNumericLiteral(n.property)&&Number.isSafeInteger(n.property.value)&&n.property.value>=0)s.unshift({index:n.property});else if(n.computed&&r.isIdentifier(n.property)&&t.scope.hasBinding(n.property.name))s.unshift({index:n.property});else return null;n=n.object}if(!r.isIdentifier(n)||!t.scope.hasBinding(n.name)||(s[0]?.property==="fields"&&s.shift(),!s[0]?.property||s.slice(2).some(o=>o.index)))return null;let i=s[1];return{base:n.name,field:s[0].property,...i?.index?{index:i.index}:{}}}function D(e,t){let n=e.scope.getBinding(t);if(!n?.constant||!n.path.isImportSpecifier())return null;let s=n.path.node;if(!r.isIdentifier(s.imported)||!/^to[A-Za-z0-9_$]+View$/.test(s.imported.name))return null;let i=n.path.parentPath.node.source.value,o=typeof i=="string"&&i.match(/^@\/lib\/cms\/generated\/([a-z0-9][a-z0-9-]*)$/);return o?{slug:o[1]}:null}function N(e,t){let n=e;for(let s of[...t].reverse()){if(!r.isMemberExpression(n)||n.computed||!r.isIdentifier(n.property,{name:s}))return null;n=n.object}return r.isIdentifier(n)?n.name:null}function de(e){if(r.isConditionalExpression(e)){let t=N(e.consequent,["data","items"]);return!t||!r.isArrayExpression(e.alternate)||e.alternate.elements.length!==0||!r.isBinaryExpression(e.test,{operator:"==="})||N(e.test.left,["kind"])!==t||!r.isStringLiteral(e.test.right,{value:"ok"})?null:t}return N(e,["data","items"])}function O(e,t,n=new Set){let s=de(t);if(s)return Z(e,s);if(n.size>=20)return null;if(r.isIdentifier(t)){let i=e.scope.getBinding(t.name);return!i?.constant||!i.path.isVariableDeclarator()||!i.path.node.init||n.has(i)?null:(n.add(i),O(e,i.path.node.init,n))}return r.isCallExpression(t)&&r.isMemberExpression(t.callee)&&!t.callee.computed&&r.isIdentifier(t.callee.property)&&["filter","slice","sort","toSorted","reverse","toReversed"].includes(t.callee.property.name)?(n.add(t),O(e,t.callee.object,n)):null}function Z(e,t){let n=e.scope.getBinding(t);if(!n?.constant||!n.path.isVariableDeclarator())return null;let s=n.path.node.init,i=r.isAwaitExpression(s)?s.argument:null;if(!r.isCallExpression(i)||!r.isMemberExpression(i.callee)||i.callee.computed||!r.isIdentifier(i.callee.property)||!r.isMemberExpression(i.callee.object)||i.callee.object.computed||!r.isIdentifier(i.callee.object.property,{name:"collections"})||!r.isIdentifier(i.callee.object.object))return null;let o=e.scope.getBinding(i.callee.object.object.name);return!o?.constant||!o.path.isImportSpecifier()||!r.isIdentifier(o.path.node.imported,{name:"cms"})||o.path.parentPath.node.source.value!=="@/lib/cms"?null:i.callee.property.name==="listStrict"&&r.isStringLiteral(i.arguments[0])?{operation:"list",slug:i.arguments[0].value}:i.callee.property.name==="getByUrlStrict"?{operation:"detail"}:i.callee.property.name==="get"&&r.isStringLiteral(i.arguments[0])?{operation:"detail",slug:i.arguments[0].value}:null}function fe(e){return!e.findParent(t=>t.isJSXElement())}function me(e){if(typeof e!="string")throw new TypeError("site edit instrumentation source must be a string");return/(?:^|[=(:,?;\[{>&|\n\r]|return\s+)\s*<(?:[A-Za-z][\w.:-]*|>)(?:\s|>|\/)/m.test(e)}function ge(e,t,n){return`${e}::${t}::${n}`}function be(e,t){let n=e.replace(/\\/g,"/").replace(/^\.\//,""),s=t?.replace(/\\/g,"/").replace(/\/+$/,"");if(s&&n.startsWith(`${s}/`))return n.slice(s.length+1);for(let[o,p]of[["/src/app/",1],["/src/components/",1],["/app/",1],["/components/",1]]){let u=n.indexOf(o);if(u>=0)return n.slice(u+p)}return n.match(/(?:^|\/)((?:src\/app|src\/components|app|components)\/.+)$/)?.[1]??n}function Ee(e){return r.isJSXIdentifier(e)&&/^[a-z]/.test(e.name)}function P(e){return r.isJSXIdentifier(e)?e.name:r.isJSXMemberExpression(e)?G(e):null}function Se(e,t){let n=new Set;for(let s of e.program.body)if(!(!r.isImportDeclaration(s)||!xe(s.source.value,t)))for(let i of s.specifiers)n.add(i.local.name);return n}function he(e,t){let n=e.split(".")[0]??e;return t.has(n)}function X(e){if(!e)return!1;let t=$(e);return t===J||t.startsWith(`${J}/`)||t.includes(`/${J}/`)||t.endsWith(`/${J}`)}function xe(e,t){return e?e.startsWith("@/")||e.startsWith("~/")?X(e.slice(2)):e.startsWith("./")||e.startsWith("../")?X($(`${ye(t)}/${e}`)):X(e):!1}function $(e){let n=(e.split("?")[0]??e).replace(/\\/g,"/").replace(/^\.\//,""),s=[];for(let i of n.split("/"))if(!(!i||i===".")){if(i===".."){s.pop();continue}s.push(i)}return s.join("/")}function ye(e){let t=$(e),n=t.lastIndexOf("/");return n>=0?t.slice(0,n):""}function ve(e){let t=P(e);return!t||t==="Fragment"||t.endsWith(".Fragment")?!1:!ne.has(t)}function B(e,t){return e.attributes.some(n=>r.isJSXAttribute(n)&&r.isJSXIdentifier(n.name,{name:t}))}function Ie(e,t,n){H(e,r.jsxAttribute(r.jsxIdentifier(t),r.stringLiteral(n)))}function z(e,t){B(e,t)||H(e,r.jsxAttribute(r.jsxIdentifier(t),null))}function H(e,t){let n=e.attributes.findIndex(s=>r.isJSXSpreadAttribute(s));if(n>=0){e.attributes.splice(n,0,t);return}e.attributes.push(t)}function G(e){return`${r.isJSXIdentifier(e.object)?e.object.name:G(e.object)}.${e.property.name}`}function Ce(e){let t=e;for(;t;){if(t.isFunctionDeclaration()||t.isClassDeclaration())return t.node.id?.name??null;if(t.isVariableDeclarator()&&r.isIdentifier(t.node.id)){let n=t.get("init");if(!Array.isArray(n)&&(n.isArrowFunctionExpression()||n.isFunctionExpression()))return t.node.id.name}t=t.parentPath}return null}function Pe(e){return e.some(t=>r.isJSXElement(t)||r.isJSXFragment(t))}var q=new WeakMap,j=new WeakMap,_=new WeakMap;function we(e){let t=q.get(e.node);if(t!==void 0)return t;let n=[],s=e;for(;s;){if(s.isJSXElement()){let o=Ae(s);o&&n.unshift(o)}s=s.parentPath}let i=n.join("/");return q.set(e.node,i),i}function Ae(e){let t=P(e.node.openingElement.name);return!t||t==="Fragment"||t.endsWith(".Fragment")?null:/^[A-Z]/.test(t)?`component:${t}#${V(e,t)}`:`${t}#${V(e,t)}`}function V(e,t){let n=Y(e)??e.getFunctionParent();if(!n)return 0;let s=_.get(n.node)?.get(t)?.get(e.node);if(s!==void 0)return s;let i=_.get(n.node);i||(i=new Map,_.set(n.node,i));let o=i.get(t);o||(o=new WeakMap,i.set(t,o));let p=0,u=n.isJSXElement()||n.isJSXFragment()?M(n):Je(n);for(let c of u)if(P(c.node.openingElement.name)===t){if(o.set(c.node,p),c.node===e.node)return p;p+=1}return o.get(e.node)??0}function Je(e){let t=j.get(e.node);if(t)return t;let n=[];return e.traverse({JSXElement(s){s.getFunctionParent()?.node===e.node&&!Y(s)&&n.push(s)}}),j.set(e.node,n),n}function Y(e){let t=e.parentPath,n=null;for(;t;){if(t.isJSXFragment()){n=t,t=t.parentPath;continue}if(t.isJSXElement())return t;t=t.parentPath}return n}function M(e){let t=j.get(e.node);if(t)return t;let n=[],s=e.get("children");for(let i of s)if(!Array.isArray(i)){if(i.isJSXElement()){n.push(i);continue}if(i.isJSXFragment()){n.push(...M(i));continue}i.isJSXExpressionContainer()&&n.push(...g(i.get("expression")))}return j.set(e.node,n),n}function g(e){return e.isJSXElement()?[e]:e.isJSXFragment()?M(e):e.isParenthesizedExpression()?g(e.get("expression")):e.isLogicalExpression()&&e.node.operator==="&&"?g(e.get("right")):e.isConditionalExpression()?[...g(e.get("consequent")),...g(e.get("alternate"))]:e.isCallExpression()&&Xe(e.node)?Ne(e.get("arguments")[0]):[]}function Ne(e){if(!e?.isArrowFunctionExpression()&&!e?.isFunctionExpression())return[];let t=e.get("body");if(t.isJSXElement())return[t];if(t.isJSXFragment())return M(t);if(t.isParenthesizedExpression())return g(t.get("expression"));if(!t.isBlockStatement())return[];let n=[];for(let s of t.get("body")){if(s.isVariableDeclaration())for(let o of s.get("declarations")){let p=o.get("init");(p.isJSXElement()||p.isJSXFragment())&&n.push(...g(p))}if(!s.isReturnStatement())continue;let i=s.get("argument");return i.node&&n.push(...g(i)),n}return n}function Xe(e){return r.isMemberExpression(e.callee)&&r.isIdentifier(e.callee.property)&&e.callee.property.name==="map"}module.exports=oe;module.exports.instrumentSiteEditRuntimeAttributes=Q;
|
package/dist/webpack-loader.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var ne=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var te=ne((ke,B)=>{"use strict";var k=require("@babel/generator"),re=require("@babel/parser"),L=require("@babel/traverse"),r=require("@babel/types"),W=require("node:fs"),d=require("node:path"),ie=typeof k=="function"?k:k.default,F=typeof L=="function"?L:L.default,H={key:"data-vcp-edit-key",parentKey:"data-vcp-edit-parent-key",movable:"data-vcp-movable",removable:"data-vcp-removable",flags:"data-vcp-flags"},se=new Set(["base","body","head","html","link","meta","noscript","script","style","template","title"]),oe="suppressHydrationWarning",ae=new Set(["SiteEditBootstrap"]),ce="data-vcp-ui-component",J="components/ui";function ue(e,t){this.cacheable?.();let n=this.async();try{if(!pe(this.resourcePath)){n(null,e,t);return}let i=this.getOptions?.()??{},s=Q({source:e,file:this.resourcePath,projectRoot:i.projectRoot,cmsPreviewModulePath:le(i.projectRoot)});n(null,s.code,s.code===e?t:null)}catch(i){n(i)}}function le(e){try{let t=require.resolve("@vcp-dev/site-edit-bridge/auto-bootstrap",{paths:[e||process.cwd()]});for(let n of[d.dirname(t),d.resolve(d.dirname(t),"../dist")]){let i=d.join(n,"cms-preview-record.js");if(W.existsSync(i)&&W.existsSync(d.join(n,"cms-preview-probe.js")))return i}return}catch{return}}function pe(e){return typeof e=="string"&&/\.(jsx|tsx)$/.test(e)&&!e.includes("/node_modules/")&&!e.endsWith(".d.ts")}function Q(e){let t=he(e.file,e.projectRoot);if(!Se(e.source)||X(t))return{code:e.source,elements:[]};let n=e.attributes??H,i=re.parse(e.source,{sourceType:"module",plugins:["jsx","typescript"]}),s=[],o=ye(i,t);F(i,{JSXElement(l){let c=l.node.openingElement,E=c.name;if(!Pe(E)||$(c,n.key))return;let b=Ae(l)??"default",w=Ne(l),S=be(t,b,w),x=P(E)??"unknown",f=xe(E)?"jsx":"component-call";if(f==="component-call"&&!Je(l.node.children)&&ae.has(x))return;let a=f==="component-call"&&ve(x,o);we(c,n.key,S),a&&K(c,ce),Ee(l)&&K(c,oe),s.push({key:S,tag:x,component_name:b,source_file:t,source_kind:f})}});let p=e.cmsPreviewModulePath?de(i,e.cmsPreviewModulePath,t,e.file,e.projectRoot):!1;return s.length===0&&!p?{code:e.source,elements:s}:{code:ie(i,{jsescOption:{minimal:!0}}).code,elements:s}}function D(e,t,n){if(!d.isAbsolute(e))return e;let i=d.isAbsolute(t)?t:d.resolve(n||process.cwd(),t),s=d.relative(d.dirname(i),e).split(d.sep).join("/");return s.startsWith(".")?s:`./${s}`}function de(e,t,n,i,s){let o;if(F(e,{Program(a){o=a,a.stop()}}),!o)return!1;let p=o.scope.generateUidIdentifier("vcpAttachCmsPreviewRecord"),l=o.scope.generateUidIdentifier("vcpStripCmsPreviewMetadata"),c=o.scope.generateUidIdentifier("vcpReadCmsPreviewCandidate"),E=o.scope.generateUidIdentifier("VcpCmsPreviewProbe"),b=!1,w=!1,S=!1,x=/(?:^|\/)(?:[^/]+-)?(?:list|detail)-view\.[jt]sx$/.test(n)||e.program.body.some(a=>r.isImportDeclaration(a)&&(a.source.value==="@/lib/cms"||/^@\/lib\/cms\/generated\//.test(a.source.value)));F(e,{CallExpression(a){let u=a.node;if(r.isMemberExpression(u.callee)&&!u.callee.computed&&r.isIdentifier(u.callee.property,{name:"map"})&&u.arguments.length===1&&r.isIdentifier(u.arguments[0])){let I=q(a,u.arguments[0].name),C=ge(u.callee.object),U=C&&z(a,C);if(I&&U?.operation==="list"&&U.slug===I.slug){let R=a.scope.generateUidIdentifier("vcpCmsRecord");u.arguments[0]=r.arrowFunctionExpression([R],r.callExpression(p,[r.callExpression(r.cloneNode(u.arguments[0]),[r.cloneNode(R)]),r.cloneNode(R),r.stringLiteral(I.slug),r.stringLiteral("list")])),b=!0,a.skip()}return}if(!r.isIdentifier(u.callee)||u.arguments.length!==1)return;let m=q(a,u.callee.name),y=r.isIdentifier(u.arguments[0]),v=y?u.arguments[0].name:N(u.arguments[0],["data"]),h=v&&z(a,v);if(!m||h?.operation!=="detail"||(y?h.slug!==m.slug:h.slug!==void 0))return;let M=r.cloneNode(u,!0),A=r.cloneNode(u.arguments[0],!0);a.replaceWith(r.callExpression(p,[M,A,r.stringLiteral(m.slug),r.stringLiteral("detail")])),b=!0,a.skip()},JSXSpreadAttribute(a){a.node.argument=r.callExpression(l,[a.node.argument]),w=!0},JSXElement:{exit(a){if(!x)return;let u=a.node.openingElement;if(!$(u,H.key))return;let m=fe(a);if(!m)return;let y=u.attributes.findIndex(C=>r.isJSXAttribute(C)&&r.isJSXIdentifier(C.name,{name:"key"})),v=y<0?null:u.attributes.splice(y,1)[0],h=[r.identifier(m.base),r.stringLiteral(m.field)];m.index&&h.push(r.cloneNode(m.index,!0));let M=[...v?[v]:[],r.jsxAttribute(r.jsxIdentifier("candidate"),r.jsxExpressionContainer(r.callExpression(c,h)))],A=r.jsxIdentifier(E.name),I=r.jsxElement(r.jsxOpeningElement(r.cloneNode(A),M,!1),r.jsxClosingElement(r.cloneNode(A)),[a.node]);a.replaceWith(I),S=!0,a.skip()}}});let f=[];return b&&f.push(r.importSpecifier(p,r.identifier("attachCmsPreviewRecord"))),w&&f.push(r.importSpecifier(l,r.identifier("stripCmsPreviewMetadata"))),S&&f.push(r.importSpecifier(c,r.identifier("readCmsPreviewCandidate"))),f.length&&e.program.body.unshift(r.importDeclaration(f,r.stringLiteral(D(t,i,s)))),S&&e.program.body.unshift(r.importDeclaration([r.importSpecifier(E,r.identifier("CmsPreviewProbe"))],r.stringLiteral(D(d.join(d.dirname(t),"cms-preview-probe.js"),i,s)))),f.length>0||S}function fe(e){let t=e.node,n=t.openingElement.name,i=P(n);if(!i)return null;let s=t.openingElement.attributes.find(c=>r.isJSXAttribute(c)&&r.isJSXIdentifier(c.name,{name:"src"})),o=t.openingElement.attributes.find(c=>r.isJSXAttribute(c)&&r.isJSXIdentifier(c.name,{name:"dangerouslySetInnerHTML"})),p=i==="img"||/^[A-Z]/.test(i)&&s,l=p?r.isJSXAttribute(s)&&r.isJSXExpressionContainer(s.value)&&r.isExpression(s.value.expression)?[s.value.expression]:[]:r.isJSXAttribute(o)&&r.isJSXExpressionContainer(o.value)&&r.isExpression(o.value.expression)?[o.value.expression]:t.children.flatMap(c=>r.isJSXExpressionContainer(c)&&r.isExpression(c.expression)?[c.expression]:[]);return l.length!==1?null:p&&r.isIdentifier(l[0])&&e.scope.hasBinding(l[0].name)?{base:l[0].name,field:"url"}:me(l[0],e)}function me(e,t){let n=e,i=[];for(;r.isMemberExpression(n)||r.isOptionalMemberExpression(n);){if(!n.computed&&r.isIdentifier(n.property))i.unshift({property:n.property.name});else if(n.computed&&r.isNumericLiteral(n.property)&&Number.isSafeInteger(n.property.value)&&n.property.value>=0)i.unshift({index:n.property});else if(n.computed&&r.isIdentifier(n.property)&&t.scope.hasBinding(n.property.name))i.unshift({index:n.property});else return null;n=n.object}if(!r.isIdentifier(n)||!t.scope.hasBinding(n.name)||(i[0]?.property==="fields"&&i.shift(),!i[0]?.property||i.slice(2).some(o=>o.index)))return null;let s=i[1];return{base:n.name,field:i[0].property,...s?.index?{index:s.index}:{}}}function q(e,t){let n=e.scope.getBinding(t);if(!n?.constant||!n.path.isImportSpecifier())return null;let i=n.path.node;if(!r.isIdentifier(i.imported)||!/^to[A-Za-z0-9_$]+View$/.test(i.imported.name))return null;let s=n.path.parentPath.node.source.value,o=typeof s=="string"&&s.match(/^@\/lib\/cms\/generated\/([a-z0-9][a-z0-9-]*)$/);return o?{slug:o[1]}:null}function N(e,t){let n=e;for(let i of[...t].reverse()){if(!r.isMemberExpression(n)||n.computed||!r.isIdentifier(n.property,{name:i}))return null;n=n.object}return r.isIdentifier(n)?n.name:null}function ge(e){if(r.isConditionalExpression(e)){let t=N(e.consequent,["data","items"]);return!t||!r.isArrayExpression(e.alternate)||e.alternate.elements.length!==0||!r.isBinaryExpression(e.test,{operator:"==="})||N(e.test.left,["kind"])!==t||!r.isStringLiteral(e.test.right,{value:"ok"})?null:t}return N(e,["data","items"])}function z(e,t){let n=e.scope.getBinding(t);if(!n?.constant||!n.path.isVariableDeclarator())return null;let i=n.path.node.init,s=r.isAwaitExpression(i)?i.argument:null;if(!r.isCallExpression(s)||!r.isMemberExpression(s.callee)||s.callee.computed||!r.isIdentifier(s.callee.property)||!r.isMemberExpression(s.callee.object)||s.callee.object.computed||!r.isIdentifier(s.callee.object.property,{name:"collections"})||!r.isIdentifier(s.callee.object.object))return null;let o=e.scope.getBinding(s.callee.object.object.name);return!o?.constant||!o.path.isImportSpecifier()||!r.isIdentifier(o.path.node.imported,{name:"cms"})||o.path.parentPath.node.source.value!=="@/lib/cms"?null:s.callee.property.name==="listStrict"&&r.isStringLiteral(s.arguments[0])?{operation:"list",slug:s.arguments[0].value}:s.callee.property.name==="getByUrlStrict"?{operation:"detail"}:s.callee.property.name==="get"&&r.isStringLiteral(s.arguments[0])?{operation:"detail",slug:s.arguments[0].value}:null}function Ee(e){return!e.findParent(t=>t.isJSXElement())}function Se(e){if(typeof e!="string")throw new TypeError("site edit instrumentation source must be a string");return/(?:^|[=(:,?;\[{>&|\n\r]|return\s+)\s*<(?:[A-Za-z][\w.:-]*|>)(?:\s|>|\/)/m.test(e)}function be(e,t,n){return`${e}::${t}::${n}`}function he(e,t){let n=e.replace(/\\/g,"/").replace(/^\.\//,""),i=t?.replace(/\\/g,"/").replace(/\/+$/,"");if(i&&n.startsWith(`${i}/`))return n.slice(i.length+1);for(let[o,p]of[["/src/app/",1],["/src/components/",1],["/app/",1],["/components/",1]]){let l=n.indexOf(o);if(l>=0)return n.slice(l+p)}return n.match(/(?:^|\/)((?:src\/app|src\/components|app|components)\/.+)$/)?.[1]??n}function xe(e){return r.isJSXIdentifier(e)&&/^[a-z]/.test(e.name)}function P(e){return r.isJSXIdentifier(e)?e.name:r.isJSXMemberExpression(e)?Y(e):null}function ye(e,t){let n=new Set;for(let i of e.program.body)if(!(!r.isImportDeclaration(i)||!Ie(i.source.value,t)))for(let s of i.specifiers)n.add(s.local.name);return n}function ve(e,t){let n=e.split(".")[0]??e;return t.has(n)}function X(e){if(!e)return!1;let t=O(e);return t===J||t.startsWith(`${J}/`)||t.includes(`/${J}/`)||t.endsWith(`/${J}`)}function Ie(e,t){return e?e.startsWith("@/")||e.startsWith("~/")?X(e.slice(2)):e.startsWith("./")||e.startsWith("../")?X(O(`${Ce(t)}/${e}`)):X(e):!1}function O(e){let n=(e.split("?")[0]??e).replace(/\\/g,"/").replace(/^\.\//,""),i=[];for(let s of n.split("/"))if(!(!s||s===".")){if(s===".."){i.pop();continue}i.push(s)}return i.join("/")}function Ce(e){let t=O(e),n=t.lastIndexOf("/");return n>=0?t.slice(0,n):""}function Pe(e){let t=P(e);return!t||t==="Fragment"||t.endsWith(".Fragment")?!1:!se.has(t)}function $(e,t){return e.attributes.some(n=>r.isJSXAttribute(n)&&r.isJSXIdentifier(n.name,{name:t}))}function we(e,t,n){G(e,r.jsxAttribute(r.jsxIdentifier(t),r.stringLiteral(n)))}function K(e,t){$(e,t)||G(e,r.jsxAttribute(r.jsxIdentifier(t),null))}function G(e,t){let n=e.attributes.findIndex(i=>r.isJSXSpreadAttribute(i));if(n>=0){e.attributes.splice(n,0,t);return}e.attributes.push(t)}function Y(e){return`${r.isJSXIdentifier(e.object)?e.object.name:Y(e.object)}.${e.property.name}`}function Ae(e){let t=e;for(;t;){if(t.isFunctionDeclaration()||t.isClassDeclaration())return t.node.id?.name??null;if(t.isVariableDeclarator()&&r.isIdentifier(t.node.id)){let n=t.get("init");if(!Array.isArray(n)&&(n.isArrowFunctionExpression()||n.isFunctionExpression()))return t.node.id.name}t=t.parentPath}return null}function Je(e){return e.some(t=>r.isJSXElement(t)||r.isJSXFragment(t))}var V=new WeakMap,j=new WeakMap,_=new WeakMap;function Ne(e){let t=V.get(e.node);if(t!==void 0)return t;let n=[],i=e;for(;i;){if(i.isJSXElement()){let o=Xe(i);o&&n.unshift(o)}i=i.parentPath}let s=n.join("/");return V.set(e.node,s),s}function Xe(e){let t=P(e.node.openingElement.name);return!t||t==="Fragment"||t.endsWith(".Fragment")?null:/^[A-Z]/.test(t)?`component:${t}#${Z(e,t)}`:`${t}#${Z(e,t)}`}function Z(e,t){let n=ee(e)??e.getFunctionParent();if(!n)return 0;let i=_.get(n.node)?.get(t)?.get(e.node);if(i!==void 0)return i;let s=_.get(n.node);s||(s=new Map,_.set(n.node,s));let o=s.get(t);o||(o=new WeakMap,s.set(t,o));let p=0,l=n.isJSXElement()||n.isJSXFragment()?T(n):je(n);for(let c of l)if(P(c.node.openingElement.name)===t){if(o.set(c.node,p),c.node===e.node)return p;p+=1}return o.get(e.node)??0}function je(e){let t=j.get(e.node);if(t)return t;let n=[];return e.traverse({JSXElement(i){i.getFunctionParent()?.node===e.node&&!ee(i)&&n.push(i)}}),j.set(e.node,n),n}function ee(e){let t=e.parentPath,n=null;for(;t;){if(t.isJSXFragment()){n=t,t=t.parentPath;continue}if(t.isJSXElement())return t;t=t.parentPath}return n}function T(e){let t=j.get(e.node);if(t)return t;let n=[],i=e.get("children");for(let s of i)if(!Array.isArray(s)){if(s.isJSXElement()){n.push(s);continue}if(s.isJSXFragment()){n.push(...T(s));continue}s.isJSXExpressionContainer()&&n.push(...g(s.get("expression")))}return j.set(e.node,n),n}function g(e){return e.isJSXElement()?[e]:e.isJSXFragment()?T(e):e.isParenthesizedExpression()?g(e.get("expression")):e.isLogicalExpression()&&e.node.operator==="&&"?g(e.get("right")):e.isConditionalExpression()?[...g(e.get("consequent")),...g(e.get("alternate"))]:e.isCallExpression()&&Me(e.node)?Te(e.get("arguments")[0]):[]}function Te(e){if(!e?.isArrowFunctionExpression()&&!e?.isFunctionExpression())return[];let t=e.get("body");if(t.isJSXElement())return[t];if(t.isJSXFragment())return T(t);if(t.isParenthesizedExpression())return g(t.get("expression"));if(!t.isBlockStatement())return[];let n=[];for(let i of t.get("body")){if(i.isVariableDeclaration())for(let o of i.get("declarations")){let p=o.get("init");(p.isJSXElement()||p.isJSXFragment())&&n.push(...g(p))}if(!i.isReturnStatement())continue;let s=i.get("argument");return s.node&&n.push(...g(s)),n}return n}function Me(e){return r.isMemberExpression(e.callee)&&r.isIdentifier(e.callee.property)&&e.callee.property.name==="map"}B.exports=ue;B.exports.instrumentSiteEditRuntimeAttributes=Q});module.exports=te();
|
|
1
|
+
"use strict";var ne=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var te=ne((ke,U)=>{"use strict";var k=require("@babel/generator"),re=require("@babel/parser"),L=require("@babel/traverse"),r=require("@babel/types"),W=require("node:fs"),d=require("node:path"),ie=typeof k=="function"?k:k.default,F=typeof L=="function"?L:L.default,Q={key:"data-vcp-edit-key",parentKey:"data-vcp-edit-parent-key",movable:"data-vcp-movable",removable:"data-vcp-removable",flags:"data-vcp-flags"},se=new Set(["base","body","head","html","link","meta","noscript","script","style","template","title"]),oe="suppressHydrationWarning",ae=new Set(["SiteEditBootstrap"]),ce="data-vcp-ui-component",J="components/ui";function le(e,t){this.cacheable?.();let n=this.async();try{if(!pe(this.resourcePath)){n(null,e,t);return}let s=this.getOptions?.()??{},i=Z({source:e,file:this.resourcePath,projectRoot:s.projectRoot,cmsPreviewModulePath:ue(s.projectRoot)});n(null,i.code,i.code===e?t:null)}catch(s){n(s)}}function ue(e){try{let t=require.resolve("@vcp-dev/site-edit-bridge/auto-bootstrap",{paths:[e||process.cwd()]});for(let n of[d.dirname(t),d.resolve(d.dirname(t),"../dist")]){let s=d.join(n,"cms-preview-record.js");if(W.existsSync(s)&&W.existsSync(d.join(n,"cms-preview-probe.js")))return s}return}catch{return}}function pe(e){return typeof e=="string"&&/\.(jsx|tsx)$/.test(e)&&!e.includes("/node_modules/")&&!e.endsWith(".d.ts")}function Z(e){let t=he(e.file,e.projectRoot);if(!Ee(e.source)||X(t))return{code:e.source,elements:[]};let n=e.attributes??Q,s=re.parse(e.source,{sourceType:"module",plugins:["jsx","typescript"]}),i=[],o=ye(s,t);F(s,{JSXElement(u){let c=u.node.openingElement,b=c.name;if(!Pe(b)||B(c,n.key))return;let S=Ae(u)??"default",w=Ne(u),E=Se(t,S,w),x=P(b)??"unknown",f=xe(b)?"jsx":"component-call";if(f==="component-call"&&!Je(u.node.children)&&ae.has(x))return;let a=f==="component-call"&&ve(x,o);we(c,n.key,E),a&&q(c,ce),be(u)&&q(c,oe),i.push({key:E,tag:x,component_name:S,source_file:t,source_kind:f})}});let p=e.cmsPreviewModulePath?de(s,e.cmsPreviewModulePath,t,e.file,e.projectRoot):!1;return i.length===0&&!p?{code:e.source,elements:i}:{code:ie(s,{jsescOption:{minimal:!0}}).code,elements:i}}function D(e,t,n){if(!d.isAbsolute(e))return e;let s=d.isAbsolute(t)?t:d.resolve(n||process.cwd(),t),i=d.relative(d.dirname(s),e).split(d.sep).join("/");return i.startsWith(".")?i:`./${i}`}function de(e,t,n,s,i){let o;if(F(e,{Program(a){o=a,a.stop()}}),!o)return!1;let p=o.scope.generateUidIdentifier("vcpAttachCmsPreviewRecord"),u=o.scope.generateUidIdentifier("vcpStripCmsPreviewMetadata"),c=o.scope.generateUidIdentifier("vcpReadCmsPreviewCandidate"),b=o.scope.generateUidIdentifier("VcpCmsPreviewProbe"),S=!1,w=!1,E=!1,x=/(?:^|\/)(?:[^/]+-)?(?:list|detail)-view\.[jt]sx$/.test(n)||e.program.body.some(a=>r.isImportDeclaration(a)&&(a.source.value==="@/lib/cms"||/^@\/lib\/cms\/generated\//.test(a.source.value)));F(e,{CallExpression(a){let l=a.node;if(r.isMemberExpression(l.callee)&&!l.callee.computed&&r.isIdentifier(l.callee.property,{name:"map"})&&l.arguments.length===1&&r.isIdentifier(l.arguments[0])){let I=z(a,l.arguments[0].name),C=O(a,l.callee.object);if(I&&C?.operation==="list"&&C.slug===I.slug){let R=a.scope.generateUidIdentifier("vcpCmsRecord");l.arguments[0]=r.arrowFunctionExpression([R],r.callExpression(p,[r.callExpression(r.cloneNode(l.arguments[0]),[r.cloneNode(R)]),r.cloneNode(R),r.stringLiteral(I.slug),r.stringLiteral("list")])),S=!0,a.skip()}return}if(!r.isIdentifier(l.callee)||l.arguments.length!==1)return;let m=z(a,l.callee.name),y=r.isIdentifier(l.arguments[0]),v=y?l.arguments[0].name:N(l.arguments[0],["data"]),h=v&&H(a,v);if(!m||h?.operation!=="detail"||(y?h.slug!==m.slug:h.slug!==void 0))return;let T=r.cloneNode(l,!0),A=r.cloneNode(l.arguments[0],!0);a.replaceWith(r.callExpression(p,[T,A,r.stringLiteral(m.slug),r.stringLiteral("detail")])),S=!0,a.skip()},JSXSpreadAttribute(a){a.node.argument=r.callExpression(u,[a.node.argument]),w=!0},JSXElement:{exit(a){if(!x)return;let l=a.node.openingElement;if(!B(l,Q.key))return;let m=fe(a);if(!m)return;let y=l.attributes.findIndex(C=>r.isJSXAttribute(C)&&r.isJSXIdentifier(C.name,{name:"key"})),v=y<0?null:l.attributes.splice(y,1)[0],h=[r.identifier(m.base),r.stringLiteral(m.field)];m.index&&h.push(r.cloneNode(m.index,!0));let T=[...v?[v]:[],r.jsxAttribute(r.jsxIdentifier("candidate"),r.jsxExpressionContainer(r.callExpression(c,h)))],A=r.jsxIdentifier(b.name),I=r.jsxElement(r.jsxOpeningElement(r.cloneNode(A),T,!1),r.jsxClosingElement(r.cloneNode(A)),[a.node]);a.replaceWith(I),E=!0,a.skip()}}});let f=[];return S&&f.push(r.importSpecifier(p,r.identifier("attachCmsPreviewRecord"))),w&&f.push(r.importSpecifier(u,r.identifier("stripCmsPreviewMetadata"))),E&&f.push(r.importSpecifier(c,r.identifier("readCmsPreviewCandidate"))),f.length&&e.program.body.unshift(r.importDeclaration(f,r.stringLiteral(D(t,s,i)))),E&&e.program.body.unshift(r.importDeclaration([r.importSpecifier(b,r.identifier("CmsPreviewProbe"))],r.stringLiteral(D(d.join(d.dirname(t),"cms-preview-probe.js"),s,i)))),f.length>0||E}function fe(e){let t=e.node,n=t.openingElement.name,s=P(n);if(!s)return null;let i=t.openingElement.attributes.find(c=>r.isJSXAttribute(c)&&r.isJSXIdentifier(c.name,{name:"src"})),o=t.openingElement.attributes.find(c=>r.isJSXAttribute(c)&&r.isJSXIdentifier(c.name,{name:"dangerouslySetInnerHTML"})),p=s==="img"||/^[A-Z]/.test(s)&&i,u=p?r.isJSXAttribute(i)&&r.isJSXExpressionContainer(i.value)&&r.isExpression(i.value.expression)?[i.value.expression]:[]:r.isJSXAttribute(o)&&r.isJSXExpressionContainer(o.value)&&r.isExpression(o.value.expression)?[o.value.expression]:t.children.flatMap(c=>r.isJSXExpressionContainer(c)&&r.isExpression(c.expression)?[c.expression]:[]);return u.length!==1?null:p&&r.isIdentifier(u[0])&&e.scope.hasBinding(u[0].name)?{base:u[0].name,field:"url"}:me(u[0],e)}function me(e,t){let n=e,s=[];for(;r.isMemberExpression(n)||r.isOptionalMemberExpression(n);){if(!n.computed&&r.isIdentifier(n.property))s.unshift({property:n.property.name});else if(n.computed&&r.isNumericLiteral(n.property)&&Number.isSafeInteger(n.property.value)&&n.property.value>=0)s.unshift({index:n.property});else if(n.computed&&r.isIdentifier(n.property)&&t.scope.hasBinding(n.property.name))s.unshift({index:n.property});else return null;n=n.object}if(!r.isIdentifier(n)||!t.scope.hasBinding(n.name)||(s[0]?.property==="fields"&&s.shift(),!s[0]?.property||s.slice(2).some(o=>o.index)))return null;let i=s[1];return{base:n.name,field:s[0].property,...i?.index?{index:i.index}:{}}}function z(e,t){let n=e.scope.getBinding(t);if(!n?.constant||!n.path.isImportSpecifier())return null;let s=n.path.node;if(!r.isIdentifier(s.imported)||!/^to[A-Za-z0-9_$]+View$/.test(s.imported.name))return null;let i=n.path.parentPath.node.source.value,o=typeof i=="string"&&i.match(/^@\/lib\/cms\/generated\/([a-z0-9][a-z0-9-]*)$/);return o?{slug:o[1]}:null}function N(e,t){let n=e;for(let s of[...t].reverse()){if(!r.isMemberExpression(n)||n.computed||!r.isIdentifier(n.property,{name:s}))return null;n=n.object}return r.isIdentifier(n)?n.name:null}function ge(e){if(r.isConditionalExpression(e)){let t=N(e.consequent,["data","items"]);return!t||!r.isArrayExpression(e.alternate)||e.alternate.elements.length!==0||!r.isBinaryExpression(e.test,{operator:"==="})||N(e.test.left,["kind"])!==t||!r.isStringLiteral(e.test.right,{value:"ok"})?null:t}return N(e,["data","items"])}function O(e,t,n=new Set){let s=ge(t);if(s)return H(e,s);if(n.size>=20)return null;if(r.isIdentifier(t)){let i=e.scope.getBinding(t.name);return!i?.constant||!i.path.isVariableDeclarator()||!i.path.node.init||n.has(i)?null:(n.add(i),O(e,i.path.node.init,n))}return r.isCallExpression(t)&&r.isMemberExpression(t.callee)&&!t.callee.computed&&r.isIdentifier(t.callee.property)&&["filter","slice","sort","toSorted","reverse","toReversed"].includes(t.callee.property.name)?(n.add(t),O(e,t.callee.object,n)):null}function H(e,t){let n=e.scope.getBinding(t);if(!n?.constant||!n.path.isVariableDeclarator())return null;let s=n.path.node.init,i=r.isAwaitExpression(s)?s.argument:null;if(!r.isCallExpression(i)||!r.isMemberExpression(i.callee)||i.callee.computed||!r.isIdentifier(i.callee.property)||!r.isMemberExpression(i.callee.object)||i.callee.object.computed||!r.isIdentifier(i.callee.object.property,{name:"collections"})||!r.isIdentifier(i.callee.object.object))return null;let o=e.scope.getBinding(i.callee.object.object.name);return!o?.constant||!o.path.isImportSpecifier()||!r.isIdentifier(o.path.node.imported,{name:"cms"})||o.path.parentPath.node.source.value!=="@/lib/cms"?null:i.callee.property.name==="listStrict"&&r.isStringLiteral(i.arguments[0])?{operation:"list",slug:i.arguments[0].value}:i.callee.property.name==="getByUrlStrict"?{operation:"detail"}:i.callee.property.name==="get"&&r.isStringLiteral(i.arguments[0])?{operation:"detail",slug:i.arguments[0].value}:null}function be(e){return!e.findParent(t=>t.isJSXElement())}function Ee(e){if(typeof e!="string")throw new TypeError("site edit instrumentation source must be a string");return/(?:^|[=(:,?;\[{>&|\n\r]|return\s+)\s*<(?:[A-Za-z][\w.:-]*|>)(?:\s|>|\/)/m.test(e)}function Se(e,t,n){return`${e}::${t}::${n}`}function he(e,t){let n=e.replace(/\\/g,"/").replace(/^\.\//,""),s=t?.replace(/\\/g,"/").replace(/\/+$/,"");if(s&&n.startsWith(`${s}/`))return n.slice(s.length+1);for(let[o,p]of[["/src/app/",1],["/src/components/",1],["/app/",1],["/components/",1]]){let u=n.indexOf(o);if(u>=0)return n.slice(u+p)}return n.match(/(?:^|\/)((?:src\/app|src\/components|app|components)\/.+)$/)?.[1]??n}function xe(e){return r.isJSXIdentifier(e)&&/^[a-z]/.test(e.name)}function P(e){return r.isJSXIdentifier(e)?e.name:r.isJSXMemberExpression(e)?Y(e):null}function ye(e,t){let n=new Set;for(let s of e.program.body)if(!(!r.isImportDeclaration(s)||!Ie(s.source.value,t)))for(let i of s.specifiers)n.add(i.local.name);return n}function ve(e,t){let n=e.split(".")[0]??e;return t.has(n)}function X(e){if(!e)return!1;let t=$(e);return t===J||t.startsWith(`${J}/`)||t.includes(`/${J}/`)||t.endsWith(`/${J}`)}function Ie(e,t){return e?e.startsWith("@/")||e.startsWith("~/")?X(e.slice(2)):e.startsWith("./")||e.startsWith("../")?X($(`${Ce(t)}/${e}`)):X(e):!1}function $(e){let n=(e.split("?")[0]??e).replace(/\\/g,"/").replace(/^\.\//,""),s=[];for(let i of n.split("/"))if(!(!i||i===".")){if(i===".."){s.pop();continue}s.push(i)}return s.join("/")}function Ce(e){let t=$(e),n=t.lastIndexOf("/");return n>=0?t.slice(0,n):""}function Pe(e){let t=P(e);return!t||t==="Fragment"||t.endsWith(".Fragment")?!1:!se.has(t)}function B(e,t){return e.attributes.some(n=>r.isJSXAttribute(n)&&r.isJSXIdentifier(n.name,{name:t}))}function we(e,t,n){G(e,r.jsxAttribute(r.jsxIdentifier(t),r.stringLiteral(n)))}function q(e,t){B(e,t)||G(e,r.jsxAttribute(r.jsxIdentifier(t),null))}function G(e,t){let n=e.attributes.findIndex(s=>r.isJSXSpreadAttribute(s));if(n>=0){e.attributes.splice(n,0,t);return}e.attributes.push(t)}function Y(e){return`${r.isJSXIdentifier(e.object)?e.object.name:Y(e.object)}.${e.property.name}`}function Ae(e){let t=e;for(;t;){if(t.isFunctionDeclaration()||t.isClassDeclaration())return t.node.id?.name??null;if(t.isVariableDeclarator()&&r.isIdentifier(t.node.id)){let n=t.get("init");if(!Array.isArray(n)&&(n.isArrowFunctionExpression()||n.isFunctionExpression()))return t.node.id.name}t=t.parentPath}return null}function Je(e){return e.some(t=>r.isJSXElement(t)||r.isJSXFragment(t))}var V=new WeakMap,j=new WeakMap,_=new WeakMap;function Ne(e){let t=V.get(e.node);if(t!==void 0)return t;let n=[],s=e;for(;s;){if(s.isJSXElement()){let o=Xe(s);o&&n.unshift(o)}s=s.parentPath}let i=n.join("/");return V.set(e.node,i),i}function Xe(e){let t=P(e.node.openingElement.name);return!t||t==="Fragment"||t.endsWith(".Fragment")?null:/^[A-Z]/.test(t)?`component:${t}#${K(e,t)}`:`${t}#${K(e,t)}`}function K(e,t){let n=ee(e)??e.getFunctionParent();if(!n)return 0;let s=_.get(n.node)?.get(t)?.get(e.node);if(s!==void 0)return s;let i=_.get(n.node);i||(i=new Map,_.set(n.node,i));let o=i.get(t);o||(o=new WeakMap,i.set(t,o));let p=0,u=n.isJSXElement()||n.isJSXFragment()?M(n):je(n);for(let c of u)if(P(c.node.openingElement.name)===t){if(o.set(c.node,p),c.node===e.node)return p;p+=1}return o.get(e.node)??0}function je(e){let t=j.get(e.node);if(t)return t;let n=[];return e.traverse({JSXElement(s){s.getFunctionParent()?.node===e.node&&!ee(s)&&n.push(s)}}),j.set(e.node,n),n}function ee(e){let t=e.parentPath,n=null;for(;t;){if(t.isJSXFragment()){n=t,t=t.parentPath;continue}if(t.isJSXElement())return t;t=t.parentPath}return n}function M(e){let t=j.get(e.node);if(t)return t;let n=[],s=e.get("children");for(let i of s)if(!Array.isArray(i)){if(i.isJSXElement()){n.push(i);continue}if(i.isJSXFragment()){n.push(...M(i));continue}i.isJSXExpressionContainer()&&n.push(...g(i.get("expression")))}return j.set(e.node,n),n}function g(e){return e.isJSXElement()?[e]:e.isJSXFragment()?M(e):e.isParenthesizedExpression()?g(e.get("expression")):e.isLogicalExpression()&&e.node.operator==="&&"?g(e.get("right")):e.isConditionalExpression()?[...g(e.get("consequent")),...g(e.get("alternate"))]:e.isCallExpression()&&Te(e.node)?Me(e.get("arguments")[0]):[]}function Me(e){if(!e?.isArrowFunctionExpression()&&!e?.isFunctionExpression())return[];let t=e.get("body");if(t.isJSXElement())return[t];if(t.isJSXFragment())return M(t);if(t.isParenthesizedExpression())return g(t.get("expression"));if(!t.isBlockStatement())return[];let n=[];for(let s of t.get("body")){if(s.isVariableDeclaration())for(let o of s.get("declarations")){let p=o.get("init");(p.isJSXElement()||p.isJSXFragment())&&n.push(...g(p))}if(!s.isReturnStatement())continue;let i=s.get("argument");return i.node&&n.push(...g(i)),n}return n}function Te(e){return r.isMemberExpression(e.callee)&&r.isIdentifier(e.callee.property)&&e.callee.property.name==="map"}U.exports=le;U.exports.instrumentSiteEditRuntimeAttributes=Z});module.exports=te();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vcp-dev/site-edit-engine",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.13",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"@babel/parser": "^7.29.3",
|
|
61
61
|
"@babel/traverse": "^7.29.0",
|
|
62
62
|
"@babel/types": "^7.29.0",
|
|
63
|
-
"@vcp-dev/contracts": "0.11.
|
|
63
|
+
"@vcp-dev/contracts": "0.11.13",
|
|
64
64
|
"yaml": "^2.9.0"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|