@vtj/coder 0.7.13 → 0.7.15

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/dist/index.cjs CHANGED
@@ -1,31 +1,44 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@vtj/base"),k=require("prettier/standalone"),L=require("prettier/plugins/html"),V=require("prettier/plugins/babel"),D=require("prettier/plugins/postcss"),M=require("prettier/plugins/estree");function b(t){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(t){for(const r in t)if(r!=="default"){const n=Object.getOwnPropertyDescriptor(t,r);Object.defineProperty(e,r,n.get?n:{enumerable:!0,get:()=>t[r]})}}return e.default=t,Object.freeze(e)}const Q=b(L),z=b(V),G=b(D),H=b(M);/**!
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("@vtj/base"),k=require("prettier/standalone"),Q=require("prettier/plugins/html"),z=require("prettier/plugins/babel"),G=require("prettier/plugins/postcss"),H=require("prettier/plugins/estree");function w(t){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(t){for(const r in t)if(r!=="default"){const s=Object.getOwnPropertyDescriptor(t,r);Object.defineProperty(e,r,s.get?s:{enumerable:!0,get:()=>t[r]})}}return e.default=t,Object.freeze(e)}const U=w(Q),T=w(z),q=w(G),B=w(H);/**!
2
2
  * Copyright (c) 2024, VTJ.PRO All rights reserved.
3
3
  * @name @vtj/coder
4
4
  * @author CHC chenhuachun1549@dingtalk.com
5
- * @version 0.7.13
5
+ * @version 0.7.15
6
6
  * @license <a href="https://vtj.pro/license.html">MIT License</a>
7
- */const U="0.7.13";u.mitt();const x={arrowParens:"always",bracketSpacing:!0,bracketSameLine:!0,endOfLine:"lf",htmlWhitespaceSensitivity:"css",insertPragma:!1,jsxBracketSameLine:!0,jsxSingleQuote:!0,printWidth:80,proseWrap:"preserve",quoteProps:"as-needed",requirePragma:!1,semi:!0,singleQuote:!0,tabWidth:2,trailingComma:"none",useTabs:!1,vueIndentScriptAndStyle:!0};async function N(t,e){return e?t:await k.format(t,{parser:"html",...x,plugins:[Q]})}async function X(t,e){return e?t:await k.format(t,{parser:"babel-ts",...x,plugins:[z,H]})}async function Y(t,e){return e?t:k.format(t,{parser:"scss",...x,plugins:[G]})}function C(t){return t&&t.type==="JSExpression"}function O(t){return typeof t=="object"&&t&&t.type==="JSFunction"}function P(t){return C(t)||O(t)}function _(t){return t.replace(new RegExp("this.","g"),"")}function F(t){return t.replace(/this\.context\??\./g,"")}function p(t,e=!0,r=!0){const n=P(t)?t.value:e?JSON.stringify(t):t;return r?_(F(n)):F(n)}function A(t,e=[]){let r=t;for(const n of e)r=r.replace(new RegExp(`this.${n}.value`,"g"),`this.${n}`);return r}function R(t){let e=t.trim();if(e=/^\((\(|async|function)/.test(e)?e.substring(1,e.length-1):e,e.startsWith("{"))return e;if(e.startsWith("async function"))e=e.replace(/^async function/,"async");else if(e.startsWith("function"))e=e.replace(/^function/,"");else{const i=/^(async\s)?\([\w]*\)\s+\=\>\s([\w\W]+)/,s=e.match(i);s&&s[2]&&(s[2].startsWith("{")||(e=e.replace(s[2],`{ return ${s[2]} }`))),e=e.replace("=>","")}return e}function Z(t={}){return Object.entries(t).map(([e,r])=>`"${e}": ${p(r)}`)}function W(t={},e=!1){const r=Object.keys(t);return e?r.map(n=>"."+n):r}class K{constructor(e,r){this.dsl=e,this.dependencies=r,this.libraryRegex=this.collectLibrary(),this.walk(e),this.walkNodes(e),this.members=this.getLibraryMember()}imports={};context={};members=[];libraryRegex=[];collectLibrary(){return this.dependencies.filter(e=>!!e.library).map(e=>new RegExp(`(this.\\$libs.${e.library}.([\\w]+))`,"g"))}collectImport(e){const r=e.split(".");if(r.length===4){const n=r.pop(),i=r.join(".")+".",s=r.pop();if(n&&s){const o=this.dependencies.find(a=>a.library===s)?.package;o&&(this.imports[o]||(this.imports[o]=new Set)).add(n)}return{name:n,path:i,library:s}}return null}replaceLibraryPath(e){const{libraryRegex:r}=this;let n=e.value;for(const i of r){const s=e.value.match(i)||[];for(const o of s){const a=this.collectImport(o);if(a){const c=a.path.replace(/\$/g,"\\$");n=n.replace(new RegExp(c,"g"),"")}}}return n}walk(e){const r=n=>{if(!n||typeof n!="object")return;if(Array.isArray(n)){for(let s of n)r(s);return}const i=Object.values(n);for(const s of i)P(s)?s.value=this.replaceLibraryPath(s):r(s)};r(e)}getLibraryMember(e=[]){let r=[...e];for(const n of Object.values(this.imports))r=r.concat(Array.from(n));return u.dedupArray(r)}collectContext(e,r){const n=new Set(r?.id?this.context[r.id]:[]),i=(e.directives||[]).find(a=>a.name==="vFor");let s=new Set(Array.from(n));if(i){const{item:a="item",index:c="index"}=i.iterator||{};s=new Set([a,c,...Array.from(s)])}const o=e.slot;if(o){const a=typeof o=="string"?[]:o.params||[],c=a.length?a:[`scope_${r?.id}`];s=new Set([...c,...Array.from(s)])}this.context[e.id]=s}walkNodes(e){const r=(n,i)=>{this.collectContext(n,i),Array.isArray(n.children)&&n.children.forEach(s=>r(s,n))};Array.isArray(e.nodes)&&e.nodes.forEach(n=>r(n))}}function ee(t={}){return Object.entries(t).map(([e,r])=>{const n=p(r,!1);return`${e}:${n}`})}function te(t=[]){return t.map(e=>`${e.name}: {
7
+ */const X="0.7.15";/**!
8
+ * Copyright (c) 2024, VTJ.PRO All rights reserved.
9
+ * @name @vtj/core
10
+ * @author CHC chenhuachun1549@dingtalk.com
11
+ * @version 0.7.15
12
+ * @license <a href="https://vtj.pro/license.html">MIT License</a>
13
+ */const Y=["slot","template","component","img","div","p","h1","h2","h3","span","a"];l.mitt();const x={arrowParens:"always",bracketSpacing:!0,bracketSameLine:!0,endOfLine:"lf",htmlWhitespaceSensitivity:"css",insertPragma:!1,jsxBracketSameLine:!0,jsxSingleQuote:!0,printWidth:80,proseWrap:"preserve",quoteProps:"as-needed",requirePragma:!1,semi:!0,singleQuote:!0,tabWidth:2,trailingComma:"none",useTabs:!1,vueIndentScriptAndStyle:!0};async function J(t,e){return e?t:await k.format(t,{parser:"vue",...x,plugins:[U,T,B,q]})}async function Z(t,e){return e?t:await k.format(t,{parser:"babel-ts",...x,plugins:[T,B]})}async function W(t,e){return e?t:k.format(t,{parser:"scss",...x,plugins:[q]})}function C(t){return t&&t.type==="JSExpression"}function P(t){return typeof t=="object"&&t&&t.type==="JSFunction"}function b(t){return C(t)||P(t)}function A(t){return t.replace(new RegExp("this.","g"),"")}function I(t){return t.replace(/this\.context\??\./g,"")}function u(t,e=!0,r=!0){const s=b(t)?t.value:e?JSON.stringify(t):t;return r?A(I(s)):I(s)}function j(t,e=[]){let r=t;for(const s of e)r=r.replace(new RegExp(`this.${s}.value`,"g"),`this.${s}`);return r}function L(t){let e=t.trim();if(e=/^\((\(|async|function)/.test(e)?e.substring(1,e.length-1):e,e.startsWith("{"))return e;if(e.startsWith("async function"))e=e.replace(/^async function/,"async");else if(e.startsWith("function"))e=e.replace(/^function/,"");else{const i=/^(async\s)?\([\w]*\)\s+\=\>\s([\w\W]+)/,n=e.match(i);n&&n[2]&&(n[2].startsWith("{")||(e=e.replace(n[2],`{ return ${n[2]} }`))),e=e.replace("=>","")}return e}function K(t={}){return Object.entries(t).map(([e,r])=>`"${e}": ${u(r)}`)}function M(t={},e=!1){const r=Object.keys(t);return e?r.map(s=>"."+s):r}function ee(t){let e="";for(var r in t)if(t.hasOwnProperty(r)){var s=t[r];e+=r+": "+s+";"}return e}class te{constructor(e,r){this.dsl=e,this.dependencies=r,this.libraryRegex=this.collectLibrary(),this.walk(e),this.walkNodes(e),this.members=this.getLibraryMember()}imports={};context={};style={};members=[];libraryRegex=[];collectLibrary(){return this.dependencies.filter(e=>!!e.library).map(e=>new RegExp(`(this.\\$libs.${e.library}.([\\w]+))`,"g"))}collectImport(e){const r=e.split(".");if(r.length===4){const s=r.pop(),i=r.join(".")+".",n=r.pop();if(s&&n){const o=this.dependencies.find(a=>a.library===n)?.package;o&&(this.imports[o]||(this.imports[o]=new Set)).add(s)}return{name:s,path:i,library:n}}return null}replaceLibraryPath(e){const{libraryRegex:r}=this;let s=e.value;for(const i of r){const n=e.value.match(i)||[];for(const o of n){const a=this.collectImport(o);if(a){const c=a.path.replace(/\$/g,"\\$");s=s.replace(new RegExp(c,"g"),"")}}}return s}walk(e){const r=s=>{if(!s||typeof s!="object")return;if(Array.isArray(s)){for(let n of s)r(n);return}const i=Object.values(s);for(const n of i)b(n)?n.value=this.replaceLibraryPath(n):r(n)};r(e)}getLibraryMember(e=[]){let r=[...e];for(const s of Object.values(this.imports))r=r.concat(Array.from(s));return l.dedupArray(r)}collectContext(e,r){const s=new Set(r?.id?this.context[r.id]:[]),i=(e.directives||[]).find(a=>a.name==="vFor");let n=new Set(Array.from(s));if(i){const{item:a="item",index:c="index"}=i.iterator||{};n=new Set([a,c,...Array.from(n)])}const o=e.slot;if(o){const a=typeof o=="string"?[]:o.params||[],c=a.length?a:[`scope_${r?.id}`];n=new Set([...c,...Array.from(n)])}this.context[e.id]=n}collectStyle(e){e.id&&e.props?.style&&(this.style[`.${e.name}_${e.id}`]=e.props.style)}walkNodes(e){const r=(s,i)=>{this.collectContext(s,i),this.collectStyle(s),Array.isArray(s.children)&&s.children.forEach(n=>r(n,s))};Array.isArray(e.nodes)&&e.nodes.forEach(s=>r(s))}}function re(t={}){return Object.entries(t).map(([e,r])=>{const s=u(r,!1);return`${e}:${s}`})}function se(t=[]){return t.map(e=>`${e.name}: {
8
14
  from: '${e.from||e.name}',
9
- default: ${p(e.default,!0,!1)}
10
- }`)}function re(t=[]){const e=r=>r?`[${u.toArray(r).map(s=>s.replace(/\'|\"/gi,"")).join(",")}]`:void 0;return t.map(r=>typeof r=="string"?`${r}: {}`:`${r.name}: {
15
+ default: ${u(e.default,!0,!1)}
16
+ }`)}function ne(t=[]){const e=r=>r?`[${l.toArray(r).map(n=>n.replace(/\'|\"/gi,"")).join(",")}]`:void 0;return t.map(r=>typeof r=="string"?`${r}: {}`:`${r.name}: {
11
17
  type:${e(r.type)},
12
18
  required: ${r.required},
13
- default: ${p(r.default,!0,!1)}
14
- }`)}function ne(t=[]){return t.map(e=>`'${e}'`)}function $(t={},e=[]){return Object.entries(t).map(([r,n])=>{let i=R(p(n,!1,!1));return i=A(i,e),i.startsWith("async")?`async ${r}${i.replace(/^async/,"")}`:`${r}${i}`})}function se(t=[],e=[]){const r=t.reduce((s,o)=>(o.id&&O(o.source)&&(s[`watcher_${o.id}`]=o.source),s),{}),n=$(r,e),i=t.map(s=>`watcher_${s.id}: {
15
- deep: ${s.deep},
16
- immediate:${s.immediate},
17
- handler${R(s.handler.value)}
18
- }`);return{computed:n,watches:i}}function oe(t={}){return Object.values(t).map(e=>{const r=O(e.transform)&&e.transform.value||"(res) => res";return`async ${e.name}(...args:any[]) {
19
+ default: ${u(r.default,!0,!1)}
20
+ }`)}function ie(t=[]){return t.map(e=>`'${e}'`)}function v(t={},e=[]){return Object.entries(t).map(([r,s])=>{let i=L(u(s,!1,!1));return i=j(i,e),i.startsWith("async")?`async ${r}${i.replace(/^async/,"")}`:`${r}${i}`})}function oe(t=[],e=[]){const r=t.reduce((n,o)=>(o.id&&P(o.source)&&(n[`watcher_${o.id}`]=o.source),n),{}),s=v(r,e),i=t.map(n=>`watcher_${n.id}: {
21
+ deep: ${n.deep},
22
+ immediate:${n.immediate},
23
+ handler${L(n.handler.value)}
24
+ }`);return{computed:s,watches:i}}function ae(t={}){return Object.values(t).map(e=>{const r=P(e.transform)&&e.transform.value||"(res) => res";return`async ${e.name}(...args:any[]) {
19
25
  return await this.provider.apis.${e.ref}.apply(this, args).then(${r});
20
- }`})}function I(t,e,r=[],n={},i){const s=[];let o={},a=[],c=[];return ie(t).forEach(f=>{const y=[];for(const l of f.children){let{id:j,name:m,invisible:g,from:w}=l;if(g)continue;const E=ae(m,e,w);E&&a.push(E),ce(w)&&c.push({id:w.id,name:m});const{props:q,events:B,handlers:T}=me(j,l.props,l.events,n,r),J=de(l.directives).join(" "),d=l.children?ye(l.children,r,e,n,l):"";Object.assign(o,T);let S="";typeof d=="string"?S=d:(S=(d?.nodes||[]).join(`
21
- `),Object.assign(o,d?.methods||{}),a=a.concat(d?.components||[]),c=c.concat(d?.importBlocks||[])),y.push(`<${m} ${J} ${q} ${B}>${S.trim()}</${m}>`)}const v=ge(f.slot,y.join(`
22
- `),i?.id);s.push(v)}),{nodes:s,methods:o,components:u.dedupArray(a),importBlocks:u.dedupArray(c,"id")}}function ie(t=[]){const e=new Map;for(const r of t){const n=typeof r.slot=="string"?r.slot:r.slot?.name,i=e.get(n);i?i.children.push(r):e.set(n,{slot:r.slot,children:[r]})}return e}function ae(t,e,r){if(["slot","component","template"].includes(t))return null;const n=e.get(t);if(n&&n.alias){const i=n.parent?`${n.parent}.${n.alias}`:n.alias;return`${t}: ${i}`}return r||n?t:null}function ce(t){return!!t&&typeof t=="object"&&t.type==="Schema"}function ue(t,e,r=[]){return typeof e=="string"?`${t}="${e}"`:P(e)?`:${t}="${p({...e,value:A(e.value,r)})}"`:u.isPlainObject(e)?`:${t}='{${Z(e).join(", ")}}'`:`:${t}='${JSON.stringify(e)}'`}function pe(t={},e=[]){return Object.entries(t).map(([r,n])=>ue(r,n,e))}function le(t,e,r,n){const i=W(e.modifiers,!0);return n&&n.length>0?`@${t}${i.join("")}="(...args:any[]) => ${r}"`:`@${t}${i.join("")}="${r}"`}function fe(t,e={},r={}){const n={},i=Array.from(r[t]||new Set([])),s=i.length?`({${i.join(", ")}}, args)`:"";return{binders:Object.entries(e).map(([a,c])=>{const h=c.handler.value.startsWith("this."),f=h?_(c.handler.value):`${u.camelCase(a)}_handler_${t}${s}`;return h||(n[f]=i.length?{type:"JSFunction",value:`{
26
+ }`})}function V(t,e,r=[],s={},i){const n=[];let o={},a=[],c=[];return ce(t).forEach(f=>{const y=[];for(const p of f.children){let{id:O,name:m,invisible:g,from:_}=p;if(g)continue;const E=le(m,e,_);E&&a.push(E),ue(_)&&c.push({id:_.id,name:m});const{props:F,events:N,handlers:D}=he(p,O,p.props,p.events,s,r),R=ye(p.directives).join(" "),d=p.children?$e(p.children,r,e,s,p):"";Object.assign(o,D);let $="";typeof d=="string"?$=d:($=(d?.nodes||[]).join(`
27
+ `),Object.assign(o,d?.methods||{}),a=a.concat(d?.components||[]),c=c.concat(d?.importBlocks||[])),y.push(m==="img"?`<${m} ${R} ${F} ${N} />
28
+ `:`<${m} ${R} ${F} ${N}>${$?`
29
+ `+$.trim():""}</${m}>`)}const S=ve(f.slot,y.join(`
30
+ `),i?.id);n.push(S)}),{nodes:n,methods:o,components:l.dedupArray(a),importBlocks:l.dedupArray(c,"id")}}function ce(t=[]){const e=new Map;for(const r of t){const s=typeof r.slot=="string"?r.slot:r.slot?.name,i=e.get(s);i?i.children.push(r):e.set(s,{slot:r.slot,children:[r]})}return e}function le(t,e,r){if(Y.includes(t))return null;const s=e.get(t);if(s&&s.alias){const i=s.parent?`${s.parent}.${s.alias}`:s.alias;return`${t}: ${i}`}return r||s?t:null}function ue(t){return!!t&&typeof t=="object"&&t.type==="Schema"}function pe(t,e,r=[]){return t==="style"?"":t==="__class"&&b(e)?`:class="${u({...e,value:j(e.value,r)})}"`:typeof e=="string"?`${t}="${e}"`:b(e)?`:${t}="${u({...e,value:j(e.value,r)})}"`:l.isPlainObject(e)?`:${t}='{${K(e).join(", ")}}'`:`:${t}='${JSON.stringify(e)}'`}function fe(t,e={},r=[]){if(!!Object.keys(e.style||{}).length){const i=`${t.name}_${t.id}`;e.class?typeof e.class=="string"?e.class=[e.class,i].join(" "):(e.__class=e.class,e.class=i):(e.class=i,delete e.style)}return Object.entries(e).map(([i,n])=>pe(i,n,r))}function me(t,e,r,s){const i=M(e.modifiers,!0);return s&&s.length>0?`@${t}${i.join("")}="(...args:any[]) => ${r}"`:`@${t}${i.join("")}="${r}"`}function de(t,e={},r={}){const s={},i=Array.from(r[t]||new Set([])),n=i.length?`({${i.join(", ")}}, args)`:"";return{binders:Object.entries(e).map(([a,c])=>{const h=c.handler.value.startsWith("this."),f=h?A(c.handler.value):`${l.camelCase(a)}_handler_${t}${n}`;return h||(s[f]=i.length?{type:"JSFunction",value:`{
23
31
  return (${c.handler.value}).apply(this, args);
24
- }`}:c.handler),le(a,c,f,i)}),handlers:n}}function me(t,e={},r={},n={},i){const{binders:s,handlers:o}=fe(t,r,n);return{props:pe(e,i).join(" "),handlers:o,binders:s,events:s.join(" ")}}function de(t=[]){const e=[],{vIf:r,vShow:n,vModels:i,vFor:s}=he(t);if(r&&e.push(`v-if="${p(r.value)}"`),n&&e.push(`v-show="${p(n.value)}"`),i.forEach(o=>{const a=W(o.modifiers,!0),c=o.arg?C(o.arg)?`:[${p(o.arg)}]`:`:${o.arg}`:"";e.push(`v-model${c}${a}="${p(o.value)}"`)}),s){const{item:o,index:a}={item:"item",index:"index",...s.iterator};e.push(`v-for="(${o}, ${a}) in ${p(s.value)}"`)}return e}function he(t=[]){const e=t.find(o=>u.camelCase(o.name)==="vIf"),r=t.find(o=>u.camelCase(o.name)==="vFor"),n=t.find(o=>u.camelCase(o.name)==="vShow"),i=t.find(o=>u.camelCase(o.name)==="vBind"),s=t.filter(o=>u.camelCase(o.name)==="vModel");return{vIf:e,vFor:r,vShow:n,vModels:s,vBind:i}}function ye(t,e,r,n,i){if(typeof t=="string")return t;if(C(t)){let s=p(t,!1);return s=A(s,e),s=_(s),`{{ ${s} }}`}return Array.isArray(t)?I(t,r,e,n,i):""}function ge(t,e,r){if(!t)return e;const n=typeof t=="string"?{name:t,params:[]}:{params:[],...t};return`<template ${`#${n.name}="${n.params?.length>0?`{${n.params?.join(",")}}`:`scope_${r}`}"`}>
32
+ }`}:c.handler),me(a,c,f,i)}),handlers:s}}function he(t,e,r={},s={},i={},n){const{binders:o,handlers:a}=de(e,s,i);return{props:fe(t,r,n).join(" "),handlers:a,binders:o,events:o.join(" ")}}function ye(t=[]){const e=[],{vIf:r,vShow:s,vModels:i,vFor:n}=ge(t);if(r&&e.push(`v-if="${u(r.value)}"`),s&&e.push(`v-show="${u(s.value)}"`),i.forEach(o=>{const a=M(o.modifiers,!0),c=o.arg?C(o.arg)?`:[${u(o.arg)}]`:`:${o.arg}`:"";e.push(`v-model${c}${a}="${u(o.value)}"`)}),n){const{item:o,index:a}={item:"item",index:"index",...n.iterator};e.push(`v-for="(${o}, ${a}) in ${u(n.value)}"`)}return e}function ge(t=[]){const e=t.find(o=>l.camelCase(o.name)==="vIf"),r=t.find(o=>l.camelCase(o.name)==="vFor"),s=t.find(o=>l.camelCase(o.name)==="vShow"),i=t.find(o=>l.camelCase(o.name)==="vBind"),n=t.filter(o=>l.camelCase(o.name)==="vModel");return{vIf:e,vFor:r,vShow:s,vModels:n,vBind:i}}function $e(t,e,r,s,i){if(typeof t=="string")return t;if(C(t)){let n=u(t,!1);return n=j(n,e),n=A(n),`{{ ${n} }}`}return Array.isArray(t)?V(t,r,e,s,i):""}function ve(t,e,r){if(!t)return e;const s=typeof t=="string"?{name:t,params:[]}:{params:[],...t};return`<template ${`#${s.name}="${s.params?.length>0?`{${s.params?.join(",")}}`:`scope_${r}`}"`}>
25
33
  ${e}
26
- </template>`}function $e(t,e=[],r=[],n={}){const i={vue:["defineComponent","reactive"]};for(const s of e){const o=t.get(s.split(":")[0]);o&&o.package&&(i[o.package]??(i[o.package]=[])).push(o.parent||(o.alias||"").split(".")[0]||o.name)}for(const[s,o]of Object.entries(n))(i[s]??(i[s]=[])).push(...Array.from(o));return Object.entries(i).filter(([s,o])=>!!o.length).map(([s,o])=>`import { ${u.dedupArray(o).join(",")}} from '${s}';`).concat(r)}function be(t,e){const{dsl:r}=t,n=Object.keys(r.computed||{}),i=$(r.lifeCycles,n),s=$(r.computed,n),o=se(r.watch,n),a=oe(r.dataSources),{methods:c,nodes:h,components:f,importBlocks:y}=I(r.nodes||[],e,n,t.context),v=[...s,...o.computed],l=$({...c,...r.methods||{}},n),j=y.map(g=>`import ${g.name} from './${g.id}.vue';`),m=$e(e,f,j,t.imports);return{id:r.id,version:r.__VERSION__,name:r.name,state:ee(r.state).join(","),inject:te(r.inject).join(","),props:re(r.props).join(","),emits:ne(r.emits).join(","),watch:o.watches.join(","),lifeCycles:i.join(","),computed:v.join(","),methods:[...a,...l].join(","),imports:m.join(`
34
+ </template>`}function be(t,e=[],r=[],s={}){const i={vue:["defineComponent","reactive"]};for(const n of e){const o=t.get(n.split(":")[0]);o&&o.package&&(i[o.package]??(i[o.package]=[])).push(o.parent||(o.alias||"").split(".")[0]||o.name)}for(const[n,o]of Object.entries(s))(i[n]??(i[n]=[])).push(...Array.from(o));return Object.entries(i).filter(([n,o])=>!!o.length).map(([n,o])=>`import { ${l.dedupArray(o).join(",")}} from '${n}';`).concat(r)}function je(t={}){const e=[];for(const[r,s]of Object.entries(t))e.push(`
35
+ ${r} {
36
+ ${ee(s)}
37
+ }
38
+ `);return e.join(`
39
+ `)}function we(t,e){const{dsl:r}=t,s=Object.keys(r.computed||{}),i=v(r.lifeCycles,s),n=v(r.computed,s),o=oe(r.watch,s),a=ae(r.dataSources),{methods:c,nodes:h,components:f,importBlocks:y}=V(r.nodes||[],e,s,t.context),S=[...n,...o.computed],p=v({...c,...r.methods||{}},s),O=y.map(g=>`import ${g.name} from './${g.id}.vue';`),m=be(e,f,O,t.imports);return{id:r.id,version:r.__VERSION__,name:r.name,state:re(r.state).join(","),inject:se(r.inject).join(","),props:ne(r.props).join(","),emits:ie(r.emits).join(","),watch:o.watches.join(","),lifeCycles:i.join(","),computed:S.join(","),methods:[...a,...p].join(","),imports:m.join(`
27
40
  `),components:f.join(","),returns:t.members.join(","),template:h.join(`
28
- `),css:r.css||""}}const ve=`
41
+ `),css:r.css||"",style:je(t.style)}}const Se=`
29
42
  <%= imports %>
30
43
  import { useProvider } from '@vtj/renderer';
31
44
  export default defineComponent({
@@ -51,7 +64,7 @@ export default defineComponent({
51
64
  <% if(methods) { %> methods: { <%= methods %> }, <% } %>
52
65
  <% if(watch) { %> watch: { <%= watch %> }, <% } %> <%= lifeCycles %>
53
66
  });
54
- `.replace(/(\n|\r|\t)/g,""),je=`
67
+ `.replace(/(\n|\r|\t)/g,""),Oe=`
55
68
  <template>
56
69
  <%= template %>
57
70
  </template>
@@ -60,8 +73,9 @@ export default defineComponent({
60
73
  <\/script>
61
74
  <style lang="scss" scoped>
62
75
  <%= css %>
76
+ <%= style %>
63
77
  </style>
64
- `,we=u.template(ve),Se=u.template(je);async function ke(t,e=new Map,r=[],n){const i=new K(u.cloneDeep(t),r),s=be(i,e),o=we(s),a=Se({template:s.template,css:await Y(s.css,n),script:await X(o,n)});return await N(a,n)}async function xe(t){const e=`
78
+ `,_e=l.template(Se),ke=l.template(Oe);async function xe(t,e=new Map,r=[],s){const i=new te(l.cloneDeep(t),r),n=we(i,e),o=_e(n),a=ke({template:n.template,css:await W(n.css,s),script:await Z(o,s),style:await W(n.style,s)});return await J(a,s)}async function Ce(t){const e=`
65
79
  <template>
66
80
  <div>
67
81
  <h3>源码模式页面</h3>
@@ -72,4 +86,4 @@ export default defineComponent({
72
86
  <\/script>
73
87
  <style scoped lang="scss">
74
88
  </style>
75
- `;return await N(e)}exports.VTJ_CODER_VERSION=U;exports.createEmptyPage=xe;exports.generator=ke;
89
+ `;return await J(e)}exports.VTJ_CODER_VERSION=X;exports.createEmptyPage=Ce;exports.generator=xe;
package/dist/index.mjs CHANGED
@@ -1,19 +1,39 @@
1
- import { mitt as q, dedupArray as v, toArray as D, camelCase as d, isPlainObject as M, template as N, cloneDeep as Q } from "@vtj/base";
2
- import { format as x } from "prettier/standalone";
3
- import * as z from "prettier/plugins/html";
4
- import * as G from "prettier/plugins/babel";
5
- import * as H from "prettier/plugins/postcss";
6
- import * as U from "prettier/plugins/estree";
1
+ import { mitt as z, dedupArray as j, toArray as G, camelCase as d, isPlainObject as H, template as B, cloneDeep as U } from "@vtj/base";
2
+ import { format as O } from "prettier/standalone";
3
+ import * as X from "prettier/plugins/html";
4
+ import * as J from "prettier/plugins/babel";
5
+ import * as L from "prettier/plugins/postcss";
6
+ import * as T from "prettier/plugins/estree";
7
7
  /**!
8
8
  * Copyright (c) 2024, VTJ.PRO All rights reserved.
9
9
  * @name @vtj/coder
10
10
  * @author CHC chenhuachun1549@dingtalk.com
11
- * @version 0.7.13
11
+ * @version 0.7.15
12
12
  * @license <a href="https://vtj.pro/license.html">MIT License</a>
13
13
  */
14
- const Ce = "0.7.13";
15
- q();
16
- const k = {
14
+ const Pt = "0.7.15";
15
+ /**!
16
+ * Copyright (c) 2024, VTJ.PRO All rights reserved.
17
+ * @name @vtj/core
18
+ * @author CHC chenhuachun1549@dingtalk.com
19
+ * @version 0.7.15
20
+ * @license <a href="https://vtj.pro/license.html">MIT License</a>
21
+ */
22
+ const Y = [
23
+ "slot",
24
+ "template",
25
+ "component",
26
+ "img",
27
+ "div",
28
+ "p",
29
+ "h1",
30
+ "h2",
31
+ "h3",
32
+ "span",
33
+ "a"
34
+ ];
35
+ z();
36
+ const C = {
17
37
  arrowParens: "always",
18
38
  bracketSpacing: !0,
19
39
  bracketSameLine: !0,
@@ -33,415 +53,450 @@ const k = {
33
53
  useTabs: !1,
34
54
  vueIndentScriptAndStyle: !0
35
55
  };
36
- async function R(t, e) {
37
- return e ? t : await x(t, {
38
- parser: "html",
39
- ...k,
40
- plugins: [z]
56
+ async function V(e, t) {
57
+ return t ? e : await O(e, {
58
+ parser: "vue",
59
+ ...C,
60
+ plugins: [X, J, T, L]
41
61
  });
42
62
  }
43
- async function X(t, e) {
44
- return e ? t : await x(t, {
63
+ async function Z(e, t) {
64
+ return t ? e : await O(e, {
45
65
  parser: "babel-ts",
46
- ...k,
47
- plugins: [G, U]
66
+ ...C,
67
+ plugins: [J, T]
48
68
  });
49
69
  }
50
- async function Y(t, e) {
51
- return e ? t : x(t, {
70
+ async function W(e, t) {
71
+ return t ? e : O(e, {
52
72
  parser: "scss",
53
- ...k,
54
- plugins: [H]
73
+ ...C,
74
+ plugins: [L]
55
75
  });
56
76
  }
57
- function C(t) {
58
- return t && t.type === "JSExpression";
77
+ function _(e) {
78
+ return e && e.type === "JSExpression";
59
79
  }
60
- function O(t) {
61
- return typeof t == "object" && t && t.type === "JSFunction";
80
+ function P(e) {
81
+ return typeof e == "object" && e && e.type === "JSFunction";
62
82
  }
63
- function E(t) {
64
- return C(t) || O(t);
83
+ function b(e) {
84
+ return _(e) || P(e);
65
85
  }
66
- function P(t) {
67
- return t.replace(new RegExp("this.", "g"), "");
86
+ function E(e) {
87
+ return e.replace(new RegExp("this.", "g"), "");
68
88
  }
69
- function F(t) {
70
- return t.replace(/this\.context\??\./g, "");
89
+ function I(e) {
90
+ return e.replace(/this\.context\??\./g, "");
71
91
  }
72
- function p(t, e = !0, r = !0) {
73
- const n = E(t) ? t.value : e ? JSON.stringify(t) : t;
74
- return r ? P(F(n)) : F(n);
92
+ function l(e, t = !0, s = !0) {
93
+ const r = b(e) ? e.value : t ? JSON.stringify(e) : e;
94
+ return s ? E(I(r)) : I(r);
75
95
  }
76
- function A(t, e = []) {
77
- let r = t;
78
- for (const n of e)
79
- r = r.replace(
80
- new RegExp(`this.${n}.value`, "g"),
81
- `this.${n}`
96
+ function w(e, t = []) {
97
+ let s = e;
98
+ for (const r of t)
99
+ s = s.replace(
100
+ new RegExp(`this.${r}.value`, "g"),
101
+ `this.${r}`
82
102
  );
83
- return r;
103
+ return s;
84
104
  }
85
- function W(t) {
86
- let e = t.trim();
87
- if (e = /^\((\(|async|function)/.test(e) ? e.substring(1, e.length - 1) : e, e.startsWith("{"))
88
- return e;
89
- if (e.startsWith("async function"))
90
- e = e.replace(/^async function/, "async");
91
- else if (e.startsWith("function"))
92
- e = e.replace(/^function/, "");
105
+ function q(e) {
106
+ let t = e.trim();
107
+ if (t = /^\((\(|async|function)/.test(t) ? t.substring(1, t.length - 1) : t, t.startsWith("{"))
108
+ return t;
109
+ if (t.startsWith("async function"))
110
+ t = t.replace(/^async function/, "async");
111
+ else if (t.startsWith("function"))
112
+ t = t.replace(/^function/, "");
93
113
  else {
94
- const i = /^(async\s)?\([\w]*\)\s+\=\>\s([\w\W]+)/, s = e.match(i);
95
- s && s[2] && (s[2].startsWith("{") || (e = e.replace(s[2], `{ return ${s[2]} }`))), e = e.replace("=>", "");
114
+ const i = /^(async\s)?\([\w]*\)\s+\=\>\s([\w\W]+)/, n = t.match(i);
115
+ n && n[2] && (n[2].startsWith("{") || (t = t.replace(n[2], `{ return ${n[2]} }`))), t = t.replace("=>", "");
96
116
  }
97
- return e;
117
+ return t;
118
+ }
119
+ function K(e = {}) {
120
+ return Object.entries(e).map(([t, s]) => `"${t}": ${l(s)}`);
98
121
  }
99
- function Z(t = {}) {
100
- return Object.entries(t).map(([e, r]) => `"${e}": ${p(r)}`);
122
+ function M(e = {}, t = !1) {
123
+ const s = Object.keys(e);
124
+ return t ? s.map((r) => "." + r) : s;
101
125
  }
102
- function I(t = {}, e = !1) {
103
- const r = Object.keys(t);
104
- return e ? r.map((n) => "." + n) : r;
126
+ function tt(e) {
127
+ let t = "";
128
+ for (var s in e)
129
+ if (e.hasOwnProperty(s)) {
130
+ var r = e[s];
131
+ t += s + ": " + r + ";";
132
+ }
133
+ return t;
105
134
  }
106
- class K {
107
- constructor(e, r) {
108
- this.dsl = e, this.dependencies = r, this.libraryRegex = this.collectLibrary(), this.walk(e), this.walkNodes(e), this.members = this.getLibraryMember();
135
+ class et {
136
+ constructor(t, s) {
137
+ this.dsl = t, this.dependencies = s, this.libraryRegex = this.collectLibrary(), this.walk(t), this.walkNodes(t), this.members = this.getLibraryMember();
109
138
  }
110
139
  /**
111
140
  * { 'element-plus': ['ElButton', 'ElInput' ...] }
112
141
  */
113
142
  imports = {};
114
143
  context = {};
144
+ style = {};
115
145
  members = [];
116
146
  libraryRegex = [];
117
147
  collectLibrary() {
118
- return this.dependencies.filter((e) => !!e.library).map((e) => new RegExp(`(this.\\$libs.${e.library}.([\\w]+))`, "g"));
148
+ return this.dependencies.filter((t) => !!t.library).map((t) => new RegExp(`(this.\\$libs.${t.library}.([\\w]+))`, "g"));
119
149
  }
120
150
  /**
121
151
  * 收集 import 信息
122
152
  * @param regexMatchItem ex: this.$libs.ElementPlus.ElButton
123
153
  * @returns ex: { name: 'ElButton', path: 'this.$libs.ElementPlus.', library: 'ElementPlus' }
124
154
  */
125
- collectImport(e) {
126
- const r = e.split(".");
127
- if (r.length === 4) {
128
- const n = r.pop(), i = r.join(".") + ".", s = r.pop();
129
- if (n && s) {
155
+ collectImport(t) {
156
+ const s = t.split(".");
157
+ if (s.length === 4) {
158
+ const r = s.pop(), i = s.join(".") + ".", n = s.pop();
159
+ if (r && n) {
130
160
  const o = this.dependencies.find(
131
- (a) => a.library === s
161
+ (a) => a.library === n
132
162
  )?.package;
133
- o && (this.imports[o] || (this.imports[o] = /* @__PURE__ */ new Set())).add(n);
163
+ o && (this.imports[o] || (this.imports[o] = /* @__PURE__ */ new Set())).add(r);
134
164
  }
135
165
  return {
136
- name: n,
166
+ name: r,
137
167
  path: i,
138
- library: s
168
+ library: n
139
169
  };
140
170
  }
141
171
  return null;
142
172
  }
143
173
  // 代码中包含依赖库的引用,需要从代码中移除
144
- replaceLibraryPath(e) {
145
- const { libraryRegex: r } = this;
146
- let n = e.value;
147
- for (const i of r) {
148
- const s = e.value.match(i) || [];
149
- for (const o of s) {
174
+ replaceLibraryPath(t) {
175
+ const { libraryRegex: s } = this;
176
+ let r = t.value;
177
+ for (const i of s) {
178
+ const n = t.value.match(i) || [];
179
+ for (const o of n) {
150
180
  const a = this.collectImport(o);
151
181
  if (a) {
152
182
  const c = a.path.replace(/\$/g, "\\$");
153
- n = n.replace(new RegExp(c, "g"), "");
183
+ r = r.replace(new RegExp(c, "g"), "");
154
184
  }
155
185
  }
156
186
  }
157
- return n;
187
+ return r;
158
188
  }
159
- walk(e) {
160
- const r = (n) => {
161
- if (!n || typeof n != "object")
189
+ walk(t) {
190
+ const s = (r) => {
191
+ if (!r || typeof r != "object")
162
192
  return;
163
- if (Array.isArray(n)) {
164
- for (let s of n)
165
- r(s);
193
+ if (Array.isArray(r)) {
194
+ for (let n of r)
195
+ s(n);
166
196
  return;
167
197
  }
168
- const i = Object.values(n);
169
- for (const s of i)
170
- E(s) ? s.value = this.replaceLibraryPath(s) : r(s);
198
+ const i = Object.values(r);
199
+ for (const n of i)
200
+ b(n) ? n.value = this.replaceLibraryPath(n) : s(n);
171
201
  };
172
- r(e);
202
+ s(t);
173
203
  }
174
- getLibraryMember(e = []) {
175
- let r = [...e];
176
- for (const n of Object.values(this.imports))
177
- r = r.concat(Array.from(n));
178
- return v(r);
204
+ getLibraryMember(t = []) {
205
+ let s = [...t];
206
+ for (const r of Object.values(this.imports))
207
+ s = s.concat(Array.from(r));
208
+ return j(s);
179
209
  }
180
- collectContext(e, r) {
181
- const n = new Set(r?.id ? this.context[r.id] : []), i = (e.directives || []).find((a) => a.name === "vFor");
182
- let s = new Set(Array.from(n));
210
+ collectContext(t, s) {
211
+ const r = new Set(s?.id ? this.context[s.id] : []), i = (t.directives || []).find((a) => a.name === "vFor");
212
+ let n = new Set(Array.from(r));
183
213
  if (i) {
184
214
  const { item: a = "item", index: c = "index" } = i.iterator || {};
185
- s = /* @__PURE__ */ new Set([a, c, ...Array.from(s)]);
215
+ n = /* @__PURE__ */ new Set([a, c, ...Array.from(n)]);
186
216
  }
187
- const o = e.slot;
217
+ const o = t.slot;
188
218
  if (o) {
189
- const a = typeof o == "string" ? [] : o.params || [], c = a.length ? a : [`scope_${r?.id}`];
190
- s = /* @__PURE__ */ new Set([...c, ...Array.from(s)]);
219
+ const a = typeof o == "string" ? [] : o.params || [], c = a.length ? a : [`scope_${s?.id}`];
220
+ n = /* @__PURE__ */ new Set([...c, ...Array.from(n)]);
191
221
  }
192
- this.context[e.id] = s;
222
+ this.context[t.id] = n;
193
223
  }
194
- walkNodes(e) {
195
- const r = (n, i) => {
196
- this.collectContext(n, i), Array.isArray(n.children) && n.children.forEach((s) => r(s, n));
224
+ collectStyle(t) {
225
+ t.id && t.props?.style && (this.style[`.${t.name}_${t.id}`] = t.props.style);
226
+ }
227
+ walkNodes(t) {
228
+ const s = (r, i) => {
229
+ this.collectContext(r, i), this.collectStyle(r), Array.isArray(r.children) && r.children.forEach((n) => s(n, r));
197
230
  };
198
- Array.isArray(e.nodes) && e.nodes.forEach((n) => r(n));
231
+ Array.isArray(t.nodes) && t.nodes.forEach((r) => s(r));
199
232
  }
200
233
  }
201
- function ee(t = {}) {
202
- return Object.entries(t).map(([e, r]) => {
203
- const n = p(r, !1);
204
- return `${e}:${n}`;
234
+ function st(e = {}) {
235
+ return Object.entries(e).map(([t, s]) => {
236
+ const r = l(s, !1);
237
+ return `${t}:${r}`;
205
238
  });
206
239
  }
207
- function te(t = []) {
208
- return t.map((e) => `${e.name}: {
209
- from: '${e.from || e.name}',
210
- default: ${p(e.default, !0, !1)}
240
+ function rt(e = []) {
241
+ return e.map((t) => `${t.name}: {
242
+ from: '${t.from || t.name}',
243
+ default: ${l(t.default, !0, !1)}
211
244
  }`);
212
245
  }
213
- function re(t = []) {
214
- const e = (r) => r ? `[${D(r).map((s) => s.replace(/\'|\"/gi, "")).join(",")}]` : void 0;
215
- return t.map((r) => typeof r == "string" ? `${r}: {}` : `${r.name}: {
216
- type:${e(r.type)},
217
- required: ${r.required},
218
- default: ${p(r.default, !0, !1)}
246
+ function nt(e = []) {
247
+ const t = (s) => s ? `[${G(s).map((n) => n.replace(/\'|\"/gi, "")).join(",")}]` : void 0;
248
+ return e.map((s) => typeof s == "string" ? `${s}: {}` : `${s.name}: {
249
+ type:${t(s.type)},
250
+ required: ${s.required},
251
+ default: ${l(s.default, !0, !1)}
219
252
  }`);
220
253
  }
221
- function ne(t = []) {
222
- return t.map((e) => `'${e}'`);
254
+ function it(e = []) {
255
+ return e.map((t) => `'${t}'`);
223
256
  }
224
- function $(t = {}, e = []) {
225
- return Object.entries(t).map(([r, n]) => {
226
- let i = W(p(n, !1, !1));
227
- return i = A(i, e), i.startsWith("async") ? `async ${r}${i.replace(/^async/, "")}` : `${r}${i}`;
257
+ function v(e = {}, t = []) {
258
+ return Object.entries(e).map(([s, r]) => {
259
+ let i = q(l(r, !1, !1));
260
+ return i = w(i, t), i.startsWith("async") ? `async ${s}${i.replace(/^async/, "")}` : `${s}${i}`;
228
261
  });
229
262
  }
230
- function se(t = [], e = []) {
231
- const r = t.reduce(
232
- (s, o) => (o.id && O(o.source) && (s[`watcher_${o.id}`] = o.source), s),
263
+ function ot(e = [], t = []) {
264
+ const s = e.reduce(
265
+ (n, o) => (o.id && P(o.source) && (n[`watcher_${o.id}`] = o.source), n),
233
266
  {}
234
- ), n = $(r, e), i = t.map((s) => `watcher_${s.id}: {
235
- deep: ${s.deep},
236
- immediate:${s.immediate},
237
- handler${W(s.handler.value)}
267
+ ), r = v(s, t), i = e.map((n) => `watcher_${n.id}: {
268
+ deep: ${n.deep},
269
+ immediate:${n.immediate},
270
+ handler${q(n.handler.value)}
238
271
  }`);
239
272
  return {
240
- computed: n,
273
+ computed: r,
241
274
  watches: i
242
275
  };
243
276
  }
244
- function oe(t = {}) {
245
- return Object.values(t).map((e) => {
246
- const r = O(e.transform) && e.transform.value || "(res) => res";
247
- return `async ${e.name}(...args:any[]) {
248
- return await this.provider.apis.${e.ref}.apply(this, args).then(${r});
277
+ function at(e = {}) {
278
+ return Object.values(e).map((t) => {
279
+ const s = P(t.transform) && t.transform.value || "(res) => res";
280
+ return `async ${t.name}(...args:any[]) {
281
+ return await this.provider.apis.${t.ref}.apply(this, args).then(${s});
249
282
  }`;
250
283
  });
251
284
  }
252
- function B(t, e, r = [], n = {}, i) {
253
- const s = [];
285
+ function D(e, t, s = [], r = {}, i) {
286
+ const n = [];
254
287
  let o = {}, a = [], c = [];
255
- return ie(t).forEach((l) => {
288
+ return ct(e).forEach((p) => {
256
289
  const y = [];
257
- for (const u of l.children) {
258
- let { id: j, name: f, invisible: g, from: w } = u;
259
- if (g)
290
+ for (const u of p.children) {
291
+ let { id: x, name: f, invisible: $, from: k } = u;
292
+ if ($)
260
293
  continue;
261
- const _ = ae(f, e, w);
262
- _ && a.push(_), ce(w) && c.push({ id: w.id, name: f });
263
- const { props: J, events: L, handlers: T } = me(
264
- j,
294
+ const A = lt(f, t, k);
295
+ A && a.push(A), ut(k) && c.push({ id: k.id, name: f });
296
+ const { props: F, events: N, handlers: Q } = ht(
297
+ u,
298
+ x,
265
299
  u.props,
266
300
  u.events,
267
- n,
268
- r
269
- ), V = de(u.directives).join(" "), m = u.children ? ye(
301
+ r,
302
+ s
303
+ ), R = yt(u.directives).join(" "), m = u.children ? gt(
270
304
  u.children,
305
+ s,
306
+ t,
271
307
  r,
272
- e,
273
- n,
274
308
  u
275
309
  ) : "";
276
- Object.assign(o, T);
277
- let S = "";
278
- typeof m == "string" ? S = m : (S = (m?.nodes || []).join(`
310
+ Object.assign(o, Q);
311
+ let g = "";
312
+ typeof m == "string" ? g = m : (g = (m?.nodes || []).join(`
279
313
  `), Object.assign(o, m?.methods || {}), a = a.concat(m?.components || []), c = c.concat(m?.importBlocks || [])), y.push(
280
- `<${f} ${V} ${J} ${L}>${S.trim()}</${f}>`
314
+ f === "img" ? `<${f} ${R} ${F} ${N} />
315
+ ` : `<${f} ${R} ${F} ${N}>${g ? `
316
+ ` + g.trim() : ""}</${f}>`
281
317
  );
282
318
  }
283
- const b = ge(l.slot, y.join(`
319
+ const S = vt(p.slot, y.join(`
284
320
  `), i?.id);
285
- s.push(b);
321
+ n.push(S);
286
322
  }), {
287
- nodes: s,
323
+ nodes: n,
288
324
  methods: o,
289
- components: v(a),
290
- importBlocks: v(c, "id")
325
+ components: j(a),
326
+ importBlocks: j(c, "id")
291
327
  };
292
328
  }
293
- function ie(t = []) {
294
- const e = /* @__PURE__ */ new Map();
295
- for (const r of t) {
296
- const n = typeof r.slot == "string" ? r.slot : r.slot?.name, i = e.get(n);
297
- i ? i.children.push(r) : e.set(n, { slot: r.slot, children: [r] });
329
+ function ct(e = []) {
330
+ const t = /* @__PURE__ */ new Map();
331
+ for (const s of e) {
332
+ const r = typeof s.slot == "string" ? s.slot : s.slot?.name, i = t.get(r);
333
+ i ? i.children.push(s) : t.set(r, { slot: s.slot, children: [s] });
298
334
  }
299
- return e;
335
+ return t;
300
336
  }
301
- function ae(t, e, r) {
302
- if (["slot", "component", "template"].includes(t))
337
+ function lt(e, t, s) {
338
+ if (Y.includes(e))
303
339
  return null;
304
- const n = e.get(t);
305
- if (n && n.alias) {
306
- const i = n.parent ? `${n.parent}.${n.alias}` : n.alias;
307
- return `${t}: ${i}`;
340
+ const r = t.get(e);
341
+ if (r && r.alias) {
342
+ const i = r.parent ? `${r.parent}.${r.alias}` : r.alias;
343
+ return `${e}: ${i}`;
308
344
  }
309
- return r || n ? t : null;
310
- }
311
- function ce(t) {
312
- return !!t && typeof t == "object" && t.type === "Schema";
313
- }
314
- function pe(t, e, r = []) {
315
- return typeof e == "string" ? `${t}="${e}"` : E(e) ? `:${t}="${p({
316
- ...e,
317
- value: A(e.value, r)
318
- })}"` : M(e) ? `:${t}='{${Z(
319
- e
320
- ).join(", ")}}'` : `:${t}='${JSON.stringify(e)}'`;
321
- }
322
- function ue(t = {}, e = []) {
323
- return Object.entries(t).map(([r, n]) => pe(r, n, e));
345
+ return s || r ? e : null;
346
+ }
347
+ function ut(e) {
348
+ return !!e && typeof e == "object" && e.type === "Schema";
349
+ }
350
+ function pt(e, t, s = []) {
351
+ return e === "style" ? "" : e === "__class" && b(t) ? `:class="${l({
352
+ ...t,
353
+ value: w(t.value, s)
354
+ })}"` : typeof t == "string" ? `${e}="${t}"` : b(t) ? `:${e}="${l({
355
+ ...t,
356
+ value: w(t.value, s)
357
+ })}"` : H(t) ? `:${e}='{${K(
358
+ t
359
+ ).join(", ")}}'` : `:${e}='${JSON.stringify(t)}'`;
360
+ }
361
+ function ft(e, t = {}, s = []) {
362
+ if (!!Object.keys(t.style || {}).length) {
363
+ const i = `${e.name}_${e.id}`;
364
+ t.class ? typeof t.class == "string" ? t.class = [t.class, i].join(" ") : (t.__class = t.class, t.class = i) : (t.class = i, delete t.style);
365
+ }
366
+ return Object.entries(t).map(([i, n]) => pt(i, n, s));
324
367
  }
325
- function le(t, e, r, n) {
326
- const i = I(e.modifiers, !0);
327
- return n && n.length > 0 ? `@${t}${i.join("")}="(...args:any[]) => ${r}"` : `@${t}${i.join("")}="${r}"`;
368
+ function mt(e, t, s, r) {
369
+ const i = M(t.modifiers, !0);
370
+ return r && r.length > 0 ? `@${e}${i.join("")}="(...args:any[]) => ${s}"` : `@${e}${i.join("")}="${s}"`;
328
371
  }
329
- function fe(t, e = {}, r = {}) {
330
- const n = {}, i = Array.from(r[t] || /* @__PURE__ */ new Set([])), s = i.length ? `({${i.join(", ")}}, args)` : "";
372
+ function dt(e, t = {}, s = {}) {
373
+ const r = {}, i = Array.from(s[e] || /* @__PURE__ */ new Set([])), n = i.length ? `({${i.join(", ")}}, args)` : "";
331
374
  return {
332
- binders: Object.entries(e).map(([a, c]) => {
333
- const h = c.handler.value.startsWith("this."), l = h ? P(c.handler.value) : `${d(a)}_handler_${t}${s}`;
334
- return h || (n[l] = i.length ? {
375
+ binders: Object.entries(t).map(([a, c]) => {
376
+ const h = c.handler.value.startsWith("this."), p = h ? E(c.handler.value) : `${d(a)}_handler_${e}${n}`;
377
+ return h || (r[p] = i.length ? {
335
378
  type: "JSFunction",
336
379
  value: `{
337
380
  return (${c.handler.value}).apply(this, args);
338
381
  }`
339
- } : c.handler), le(a, c, l, i);
382
+ } : c.handler), mt(a, c, p, i);
340
383
  }),
341
- handlers: n
384
+ handlers: r
342
385
  };
343
386
  }
344
- function me(t, e = {}, r = {}, n = {}, i) {
345
- const { binders: s, handlers: o } = fe(t, r, n);
387
+ function ht(e, t, s = {}, r = {}, i = {}, n) {
388
+ const { binders: o, handlers: a } = dt(t, r, i);
346
389
  return {
347
- props: ue(e, i).join(" "),
348
- handlers: o,
349
- binders: s,
350
- events: s.join(" ")
390
+ props: ft(e, s, n).join(" "),
391
+ handlers: a,
392
+ binders: o,
393
+ events: o.join(" ")
351
394
  };
352
395
  }
353
- function de(t = []) {
354
- const e = [], { vIf: r, vShow: n, vModels: i, vFor: s } = he(t);
355
- if (r && e.push(`v-if="${p(r.value)}"`), n && e.push(`v-show="${p(n.value)}"`), i.forEach((o) => {
356
- const a = I(o.modifiers, !0), c = o.arg ? C(o.arg) ? `:[${p(o.arg)}]` : `:${o.arg}` : "";
357
- e.push(`v-model${c}${a}="${p(o.value)}"`);
358
- }), s) {
359
- const { item: o, index: a } = { item: "item", index: "index", ...s.iterator };
360
- e.push(`v-for="(${o}, ${a}) in ${p(s.value)}"`);
396
+ function yt(e = []) {
397
+ const t = [], { vIf: s, vShow: r, vModels: i, vFor: n } = $t(e);
398
+ if (s && t.push(`v-if="${l(s.value)}"`), r && t.push(`v-show="${l(r.value)}"`), i.forEach((o) => {
399
+ const a = M(o.modifiers, !0), c = o.arg ? _(o.arg) ? `:[${l(o.arg)}]` : `:${o.arg}` : "";
400
+ t.push(`v-model${c}${a}="${l(o.value)}"`);
401
+ }), n) {
402
+ const { item: o, index: a } = { item: "item", index: "index", ...n.iterator };
403
+ t.push(`v-for="(${o}, ${a}) in ${l(n.value)}"`);
361
404
  }
362
- return e;
405
+ return t;
363
406
  }
364
- function he(t = []) {
365
- const e = t.find((o) => d(o.name) === "vIf"), r = t.find((o) => d(o.name) === "vFor"), n = t.find((o) => d(o.name) === "vShow"), i = t.find((o) => d(o.name) === "vBind"), s = t.filter((o) => d(o.name) === "vModel");
407
+ function $t(e = []) {
408
+ const t = e.find((o) => d(o.name) === "vIf"), s = e.find((o) => d(o.name) === "vFor"), r = e.find((o) => d(o.name) === "vShow"), i = e.find((o) => d(o.name) === "vBind"), n = e.filter((o) => d(o.name) === "vModel");
366
409
  return {
367
- vIf: e,
368
- vFor: r,
369
- vShow: n,
370
- vModels: s,
410
+ vIf: t,
411
+ vFor: s,
412
+ vShow: r,
413
+ vModels: n,
371
414
  vBind: i
372
415
  };
373
416
  }
374
- function ye(t, e, r, n, i) {
375
- if (typeof t == "string")
376
- return t;
377
- if (C(t)) {
378
- let s = p(t, !1);
379
- return s = A(s, e), s = P(s), `{{ ${s} }}`;
417
+ function gt(e, t, s, r, i) {
418
+ if (typeof e == "string")
419
+ return e;
420
+ if (_(e)) {
421
+ let n = l(e, !1);
422
+ return n = w(n, t), n = E(n), `{{ ${n} }}`;
380
423
  }
381
- return Array.isArray(t) ? B(t, r, e, n, i) : "";
424
+ return Array.isArray(e) ? D(e, s, t, r, i) : "";
382
425
  }
383
- function ge(t, e, r) {
384
- if (!t)
385
- return e;
386
- const n = typeof t == "string" ? { name: t, params: [] } : { params: [], ...t };
387
- return `<template ${`#${n.name}="${n.params?.length > 0 ? `{${n.params?.join(",")}}` : `scope_${r}`}"`}>
388
- ${e}
426
+ function vt(e, t, s) {
427
+ if (!e)
428
+ return t;
429
+ const r = typeof e == "string" ? { name: e, params: [] } : { params: [], ...e };
430
+ return `<template ${`#${r.name}="${r.params?.length > 0 ? `{${r.params?.join(",")}}` : `scope_${s}`}"`}>
431
+ ${t}
389
432
  </template>`;
390
433
  }
391
- function $e(t, e = [], r = [], n = {}) {
434
+ function jt(e, t = [], s = [], r = {}) {
392
435
  const i = {
393
436
  vue: ["defineComponent", "reactive"]
394
437
  };
395
- for (const s of e) {
396
- const o = t.get(s.split(":")[0]);
438
+ for (const n of t) {
439
+ const o = e.get(n.split(":")[0]);
397
440
  o && o.package && (i[o.package] ?? (i[o.package] = [])).push(o.parent || (o.alias || "").split(".")[0] || o.name);
398
441
  }
399
- for (const [s, o] of Object.entries(n))
400
- (i[s] ?? (i[s] = [])).push(...Array.from(o));
401
- return Object.entries(i).filter(([s, o]) => !!o.length).map(([s, o]) => `import { ${v(o).join(
442
+ for (const [n, o] of Object.entries(r))
443
+ (i[n] ?? (i[n] = [])).push(...Array.from(o));
444
+ return Object.entries(i).filter(([n, o]) => !!o.length).map(([n, o]) => `import { ${j(o).join(
402
445
  ","
403
- )}} from '${s}';`).concat(r);
404
- }
405
- function ve(t, e) {
406
- const { dsl: r } = t, n = Object.keys(r.computed || {}), i = $(r.lifeCycles, n), s = $(r.computed, n), o = se(r.watch, n), a = oe(r.dataSources), { methods: c, nodes: h, components: l, importBlocks: y } = B(
407
- r.nodes || [],
408
- e,
409
- n,
410
- t.context
411
- ), b = [...s, ...o.computed], u = $(
446
+ )}} from '${n}';`).concat(s);
447
+ }
448
+ function bt(e = {}) {
449
+ const t = [];
450
+ for (const [s, r] of Object.entries(e))
451
+ t.push(`
452
+ ${s} {
453
+ ${tt(r)}
454
+ }
455
+ `);
456
+ return t.join(`
457
+ `);
458
+ }
459
+ function wt(e, t) {
460
+ const { dsl: s } = e, r = Object.keys(s.computed || {}), i = v(s.lifeCycles, r), n = v(s.computed, r), o = ot(s.watch, r), a = at(s.dataSources), { methods: c, nodes: h, components: p, importBlocks: y } = D(
461
+ s.nodes || [],
462
+ t,
463
+ r,
464
+ e.context
465
+ ), S = [...n, ...o.computed], u = v(
412
466
  {
413
467
  ...c,
414
- ...r.methods || {}
468
+ ...s.methods || {}
415
469
  },
416
- n
417
- ), j = y.map((g) => `import ${g.name} from './${g.id}.vue';`), f = $e(
418
- e,
419
- l,
420
- j,
421
- t.imports
470
+ r
471
+ ), x = y.map(($) => `import ${$.name} from './${$.id}.vue';`), f = jt(
472
+ t,
473
+ p,
474
+ x,
475
+ e.imports
422
476
  );
423
477
  return {
424
- id: r.id,
425
- version: r.__VERSION__,
426
- name: r.name,
427
- state: ee(r.state).join(","),
428
- inject: te(r.inject).join(","),
429
- props: re(r.props).join(","),
430
- emits: ne(r.emits).join(","),
478
+ id: s.id,
479
+ version: s.__VERSION__,
480
+ name: s.name,
481
+ state: st(s.state).join(","),
482
+ inject: rt(s.inject).join(","),
483
+ props: nt(s.props).join(","),
484
+ emits: it(s.emits).join(","),
431
485
  watch: o.watches.join(","),
432
486
  lifeCycles: i.join(","),
433
- computed: b.join(","),
487
+ computed: S.join(","),
434
488
  methods: [...a, ...u].join(","),
435
489
  imports: f.join(`
436
490
  `),
437
- components: l.join(","),
438
- returns: t.members.join(","),
491
+ components: p.join(","),
492
+ returns: e.members.join(","),
439
493
  template: h.join(`
440
494
  `),
441
- css: r.css || ""
495
+ css: s.css || "",
496
+ style: bt(e.style)
442
497
  };
443
498
  }
444
- const be = `
499
+ const St = `
445
500
  <%= imports %>
446
501
  import { useProvider } from '@vtj/renderer';
447
502
  export default defineComponent({
@@ -467,7 +522,7 @@ export default defineComponent({
467
522
  <% if(methods) { %> methods: { <%= methods %> }, <% } %>
468
523
  <% if(watch) { %> watch: { <%= watch %> }, <% } %> <%= lifeCycles %>
469
524
  });
470
- `.replace(/(\n|\r|\t)/g, ""), je = `
525
+ `.replace(/(\n|\r|\t)/g, ""), xt = `
471
526
  <template>
472
527
  <%= template %>
473
528
  </template>
@@ -476,22 +531,24 @@ export default defineComponent({
476
531
  <\/script>
477
532
  <style lang="scss" scoped>
478
533
  <%= css %>
534
+ <%= style %>
479
535
  </style>
480
- `, we = N(be), Se = N(je);
481
- async function Oe(t, e = /* @__PURE__ */ new Map(), r = [], n) {
482
- const i = new K(Q(t), r), s = ve(i, e), o = we(s), a = Se({
483
- template: s.template,
484
- css: await Y(s.css, n),
485
- script: await X(o, n)
536
+ `, kt = B(St), Ot = B(xt);
537
+ async function Et(e, t = /* @__PURE__ */ new Map(), s = [], r) {
538
+ const i = new et(U(e), s), n = wt(i, t), o = kt(n), a = Ot({
539
+ template: n.template,
540
+ css: await W(n.css, r),
541
+ script: await Z(o, r),
542
+ style: await W(n.style, r)
486
543
  });
487
- return await R(a, n);
544
+ return await V(a, r);
488
545
  }
489
- async function Ee(t) {
490
- const e = `
546
+ async function At(e) {
547
+ const t = `
491
548
  <template>
492
549
  <div>
493
550
  <h3>源码模式页面</h3>
494
- <div>文件路径:/.vtj/vue/${t.id}.vue</div>
551
+ <div>文件路径:/.vtj/vue/${e.id}.vue</div>
495
552
  </div>
496
553
  </template>
497
554
  <script lang="ts" setup>
@@ -499,10 +556,10 @@ async function Ee(t) {
499
556
  <style scoped lang="scss">
500
557
  </style>
501
558
  `;
502
- return await R(e);
559
+ return await V(t);
503
560
  }
504
561
  export {
505
- Ce as VTJ_CODER_VERSION,
506
- Ee as createEmptyPage,
507
- Oe as generator
562
+ Pt as VTJ_CODER_VERSION,
563
+ At as createEmptyPage,
564
+ Et as generator
508
565
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vtj/coder",
3
3
  "private": false,
4
- "version": "0.7.13",
4
+ "version": "0.7.15",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "build": "vue-tsc && vite build",
@@ -10,12 +10,12 @@
10
10
  "coverage": "vitest run --coverage"
11
11
  },
12
12
  "dependencies": {
13
- "@vtj/base": "^0.7.5",
14
- "@vtj/core": "^0.7.13",
13
+ "@vtj/base": "^0.7.6",
14
+ "@vtj/core": "^0.7.15",
15
15
  "prettier": "~3.2.1"
16
16
  },
17
17
  "devDependencies": {
18
- "@vtj/cli": "^0.7.10"
18
+ "@vtj/cli": "^0.7.11"
19
19
  },
20
20
  "exports": {
21
21
  ".": {
@@ -31,7 +31,7 @@
31
31
  "dist",
32
32
  "types"
33
33
  ],
34
- "gitHead": "010201b133340a84d345745a0ea351c98098fa14",
34
+ "gitHead": "cbb81a983f9271342f987a2507923ef7c2e55369",
35
35
  "publishConfig": {
36
36
  "access": "public"
37
37
  }
@@ -7,6 +7,7 @@ export declare class Collecter {
7
7
  */
8
8
  imports: Record<string, Set<string>>;
9
9
  context: Record<string, Set<string>>;
10
+ style: Record<string, Record<string, any>>;
10
11
  members: string[];
11
12
  private libraryRegex;
12
13
  constructor(dsl: BlockSchema, dependencies: Dependencie[]);
@@ -21,5 +22,6 @@ export declare class Collecter {
21
22
  private walk;
22
23
  private getLibraryMember;
23
24
  private collectContext;
25
+ private collectStyle;
24
26
  private walkNodes;
25
27
  }
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * 代码格式化工具
3
3
  */
4
+ export declare function vueFormatter(content: string, disabled?: boolean): Promise<string>;
4
5
  /**
5
6
  * 格式化HTMl代码
6
7
  */
@@ -17,5 +17,6 @@ export interface Token {
17
17
  returns: string;
18
18
  template: string;
19
19
  css: string;
20
+ style: string;
20
21
  }
21
22
  export declare function parser(collecter: Collecter, componentMap: Map<string, MaterialDescription>): Token;
@@ -0,0 +1 @@
1
+ export declare function parseStyle(style?: Record<string, Record<string, any>>): string;
package/types/utils.d.ts CHANGED
@@ -10,3 +10,4 @@ export declare function replaceComputedValue(content: string, keys?: string[]):
10
10
  export declare function replaceFunctionTag(content: string): string;
11
11
  export declare function parsePlainObjectValue(obj?: Record<string, any>): string[];
12
12
  export declare function getModifiers(modifiers?: Record<string, boolean>, isToString?: boolean): string[];
13
+ export declare function jsonToStyle(json: Record<string, any>): string;
@@ -2,7 +2,7 @@
2
2
  * Copyright (c) 2024, VTJ.PRO All rights reserved.
3
3
  * @name @vtj/coder
4
4
  * @author CHC chenhuachun1549@dingtalk.com
5
- * @version 0.7.13
5
+ * @version 0.7.14
6
6
  * @license <a href="https://vtj.pro/license.html">MIT License</a>
7
7
  */
8
- export declare const version = "0.7.13";
8
+ export declare const version = "0.7.14";