babel-preset-dlight
Advanced tools
Comparing version 0.10.6 to 1.0.0-alpha.0
@@ -0,1 +1,4 @@ | ||
import { ConfigAPI, TransformOptions } from '@babel/core'; | ||
type HTMLTags = string[] | ((defaultHtmlTags: string[]) => string[]); | ||
interface DLightOption { | ||
@@ -12,8 +15,20 @@ /** | ||
excludeFiles?: string | string[]; | ||
/** | ||
* Enable devtools | ||
* @default false | ||
*/ | ||
enableDevTools?: boolean; | ||
/** | ||
* Custom HTML tags. | ||
* Accepts 2 types: | ||
* 1. string[], e.g. ["div", "span"] | ||
* if contains "*", then all default tags will be included | ||
* 2. (defaultHtmlTags: string[]) => string[] | ||
* @default defaultHtmlTags => defaultHtmlTags | ||
*/ | ||
htmlTags?: HTMLTags; | ||
} | ||
declare function export_default(api: any, options: DLightOption): { | ||
plugins: any[]; | ||
}; | ||
declare function export_default(_: ConfigAPI, options: DLightOption): TransformOptions; | ||
export { DLightOption, export_default as default }; |
1353
dist/index.js
@@ -1,2 +0,1353 @@ | ||
import{declare as be}from"@babel/helper-plugin-utils";function z(a,t){let s=[];a.forEach((i,r)=>{(Array.isArray(i)?i[0]:i)===t&&s.unshift(r)});for(let i of s)a.splice(i,1)}var G=be((a,t)=>{a.assertVersion(7);let{disallowAmbiguousJSXLike:s,dts:i}=t??{},{isTSX:r}=t??{};return{name:"syntax-typescript",manipulateOptions(n,o){let{plugins:p}=o;z(p,"flow"),z(p,"jsx"),p.push("objectRestSpread","classProperties"),r&&p.push("jsx"),o.plugins.push(["typescript",{disallowAmbiguousJSXLike:s,dts:i}])}}});import Ee from"@babel/plugin-syntax-do-expressions";import he from"@babel/plugin-syntax-decorators";import*as y from"@babel/types";import*as d from"@babel/types";function Q(a,t,s,i){i.body.includes(s)||i.body.unshift(s),s.value.properties.unshift(d.objectProperty(d.identifier(a),d.arrayExpression(t.map(r=>d.stringLiteral(r)))))}var ge=["escape","$"];function ve(a,t){let s=!1,i=a.parentPath;for(;i&&i.node!==t;){let r=i.node;if(d.isCallExpression(r)&&d.isIdentifier(r.callee)&&ge.includes(r.callee.name)){s=!0;break}i=i.parentPath}return s}function De(a,t){let s=!1,i=a.parentPath;for(;i&&i.node!==t;){let r=i.node,n=i.parentPath?.node;if((d.isArrowFunctionExpression(r)||d.isFunctionExpression(r))&&d.isCallExpression(n)&&d.isIdentifier(n.callee)&&n.callee.name==="manual"){s=!0;break}i=i.parentPath}return s}function Se(a){let t=a.parentPath.node;return d.isAssignmentExpression(t)&&t.left===a.node}function K(a,t){return!De(a,t)&&!ve(a,t)}function Y(a){a.value=d.arrowFunctionExpression([],a.value)}function T(a,t){return d.isMemberExpression(a)&&!a.computed&&a.property===t}function B(a,t){return d.isObjectProperty(a)&&a.key===t}function Z(a,t){t.length!==0&&a.body.unshift(d.classMethod("constructor",d.identifier("constructor"),[],d.blockStatement([d.expressionStatement(d.callExpression(d.super(),[])),...t.map(s=>d.expressionStatement(d.assignmentExpression("=",d.memberExpression(d.thisExpression(),d.identifier(s)),d.callExpression(d.memberExpression(d.memberExpression(d.thisExpression(),d.identifier(s)),d.identifier("bind")),[d.thisExpression()]))))])))}function M(a){let t=a.node;return(t.decorators??[]).find(r=>d.isIdentifier(r.expression,{name:"View"}))&&(t.superClass=d.identifier("View"),t.decorators=t.decorators?.filter(r=>!d.isIdentifier(r.expression,{name:"View"}))),d.isIdentifier(t.superClass,{name:"View"})}function ee(a,t){let s=a;return t.scope.traverse(t.node,{ClassProperty(i){if(i.node!==a)return;let r=a.value.body,n=d.isExpression(r)?d.blockStatement([d.returnStatement(r)]):r,o=d.classMethod("method",a.key,a.value.params,n);s=o,i.replaceWith(o)}}),s}function U(a,t,s,i){let r=[],n=[];return a.scope.traverse(t,{MemberExpression(o){Se(o)?n.push(o.node.property.name):i.includes(o.node.property.name)&&d.isThisExpression(o.node.object)&&K(o,s)&&r.push(o.node.property.name)}}),{deps:r,assignDeps:n}}function te(a,t,s,i){let r=t.filter(f=>!i.includes(f)),o=a.value.properties.reduce((f,b)=>{let P=b.key.name,w=b.value.elements.map(C=>C.value);return f[P]=w,f},{}),p=new Set,l=new Set;function u(f){if(!l.has(f)){l.add(f),p.add(f);for(let b of o[f]??[])u(b)}}for(let f of s)u(f);return r.filter(f=>!p.has(f))}import*as m from"@babel/types";import{parseBody as J}from"@dlightjs/view-parser";import*as x from"@babel/types";import{isAHtmlTag as $e}from"@dlightjs/view-parser";function ie(a){let t=a.tag;return typeof t=="string"?!1:x.isIdentifier(t)&&$e(t.name)?(a.tag=x.stringLiteral(t.name),!0):x.isCallExpression(t)&&t.callee.name==="htmlTag"?(a.tag=t.arguments[0],!0):!1}function re(a,t){let s=t.tag;return!x.isMemberExpression(s)||!x.isThisExpression(s.object)?!1:!!a.includes(s.property.name)}function se(a){let t=a.tag;if(typeof t=="string")return!1;if(x.isCallExpression(t)&&t.callee.name==="tag")return a.tag=t.arguments[0],!0}function H(a,t){return x.isIdentifier(a.tag)&&a.tag.name===t}function R(){return Math.random().toString(20).slice(2,8)}function V(a){return x.variableDeclaration("const",[x.variableDeclarator(x.identifier("_"),a)])}function ne(a,t,s,i=[]){let r=[];return a.scope.traverse(V(t),{MemberExpression(n){s.includes(n.node.property.name)&&x.isThisExpression(n.node.object)&&K(n)&&r.push(x.stringLiteral(n.node.property.name))}}),r=[...new Set([...r,...i])],r}function oe(a,t,s,i=[]){let r=[];return a.scope.traverse(V(t),{Identifier(n){for(let{ids:o,propNames:p}of s)o.includes(n.node.name)&&r.push(...p)}}),r=[...new Set([...r,...i])],r}function X(a,t){if(x.isIdentifier(t))return[t.name];let s=[];return a.scope.traverse(t,{Identifier(i){x.isObjectProperty(i.parentPath.node)||s.push(i.node.name)},ObjectProperty(i){s.push(i.node.value.name)}}),[...new Set(s)]}function ae(a,t,s,i){let r=X(a,s);a.scope.traverse(x.functionDeclaration(null,[],t),{Identifier(n){if(r.includes(n.node.name)&&!T(n.parentPath.node,n.node)&&!B(n.parentPath.node,n.node)){let o=x.memberExpression(x.identifier(i),x.identifier(n.node.name));n.replaceWith(o),n.skip()}}})}function q(a){if(!x.isMemberExpression(a))return!1;for(;a.property;){if(!x.isMemberExpression(a.property)&&!x.isIdentifier(a.property))return!1;a=a.property}return!0}import*as e from"@babel/types";function F(a,t,s){return e.variableDeclaration("const",[e.variableDeclarator(e.identifier(a),e.newExpression(e.memberExpression(e.identifier("DLight"),t),s))])}var j=class{depChain;subViews;idDepsArr=[];usedProperties=[];path;constructor(t,s,i,r=[]){this.path=t,this.depChain=s,this.subViews=i,this.idDepsArr=r}generate(t){let s=[];for(let[i,r]of t.entries())s.push(...this.resolveParserNode(r,i));return s.push(e.returnStatement(e.arrayExpression(t.map((i,r)=>i.attr.isSubView?e.spreadElement(e.identifier(`_$node${r}`)):e.identifier(`_$node${r}`))))),this.usedProperties=[...new Set(this.usedProperties)],e.blockStatement(s)}geneDeps(t){let s=[...new Set([...ne(this.path,t,this.depChain),...oe(this.path,t,this.idDepsArr)])];return this.usedProperties.push(...s.filter(i=>e.isStringLiteral(i)).map(i=>i.value)),s}resolveParserNode(t,s){return re(this.subViews,t)?this.resolveSubView(t,s):H(t,"_")?this.resolveExpression(t,s):H(t,"env")?this.resolveEnv(t,s):t.tag==="if"?this.resolveIf(t,s):t.tag==="for"?this.resolveFor(t,s):t.tag==="_$text"?this.resolveText(t,s):ie(t)?this.resolveHTML(t,s):(se(t),this.resolveCustom(t,s))}resolveIf(t,s){let i=[],r=`_$node${s}`;i.push(F(r,e.identifier("IfNode"),[]));for(let n of t.attr.conditions){let o=this.geneDeps(n.condition);if(o.length>0){i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier("_$addCond")),[e.arrowFunctionExpression([],n.condition),e.arrowFunctionExpression([],this.generate(n.parserNodes)),e.thisExpression(),e.arrayExpression(o)])));continue}i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier("_$addCond")),[e.arrowFunctionExpression([],n.condition),e.arrowFunctionExpression([],this.generate(n.parserNodes))])))}return i}resolveFor(t,s){let i=[],r=t.attr.key,n=t.attr.item,o=t.attr.array,p=`_$node${s}`;i.push(F(p,e.identifier("ForNode"),[]));let l=this.geneDeps(o);if(l.length>0){let u=X(this.path,n),b=`_$valuedItem${R()}`,P=new j(this.path,this.depChain,this.subViews,[...this.idDepsArr,{ids:u,propNames:l}]),w=P.generate(t.children);this.usedProperties.push(...P.usedProperties);let C,A;e.isIdentifier(n)?(C=[e.variableDeclaration("let",[e.variableDeclarator(n,e.callExpression(e.memberExpression(e.identifier("node_for"),e.identifier("_$getItem")),[e.identifier("_$key"),e.identifier("_$idx")]))])],A=e.variableDeclaration("const",[e.variableDeclarator(e.identifier("updateFunc"),e.arrowFunctionExpression([],e.assignmentExpression("=",n,e.callExpression(e.memberExpression(e.identifier("node_for"),e.identifier("_$getItem")),[e.identifier("_$key"),e.identifier("_$idx")]))))])):(ae(this.path,w,n,b),C=[e.variableDeclaration("const",[e.variableDeclarator(n,e.callExpression(e.memberExpression(e.identifier("node_for"),e.identifier("_$getItem")),[e.identifier("_$key"),e.identifier("_$idx")]))]),e.variableDeclaration("const",[e.variableDeclarator(e.identifier(b),e.objectExpression(u.map(L=>e.objectProperty(e.identifier(L),e.identifier(L),void 0,!0))))])],A=e.variableDeclaration("const",[e.variableDeclarator(e.identifier("updateFunc"),e.arrowFunctionExpression([],e.blockStatement([e.variableDeclaration("const",[e.variableDeclarator(n,e.callExpression(e.memberExpression(e.identifier("node_for"),e.identifier("_$getItem")),[e.identifier("_$key"),e.identifier("_$idx")]))]),...u.map(L=>e.expressionStatement(e.assignmentExpression("=",e.memberExpression(e.identifier(b),e.identifier(L)),e.identifier(L))))])))])),i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(p),e.identifier("_$addNodeFunc")),[e.arrowFunctionExpression([e.identifier("_$key"),e.identifier("_$idx"),e.identifier("node_for")],e.blockStatement([...C,A,e.expressionStatement(e.callExpression(e.memberExpression(e.thisExpression(),e.identifier("_$addDeps")),[e.arrayExpression(l),e.identifier("updateFunc")])),...w.body.slice(0,-1),e.expressionStatement(e.callExpression(e.memberExpression(e.thisExpression(),e.identifier("_$deleteDeps")),[e.arrayExpression(l),e.identifier("updateFunc"),e.conditionalExpression(e.callExpression(e.memberExpression(e.identifier("Array"),e.identifier("isArray")),[e.identifier("_$node0")]),e.memberExpression(e.identifier("_$node0"),e.numericLiteral(0),!0),e.identifier("_$node0"))])),w.body[w.body.length-1]]))]))),r&&i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(p),e.identifier("_$addKeyFunc")),[e.arrowFunctionExpression([],e.blockStatement([e.variableDeclaration("const",[e.variableDeclarator(e.identifier("keys"),e.arrayExpression())]),e.forOfStatement(e.variableDeclaration("const",[e.variableDeclarator(n)]),o,e.blockStatement([e.expressionStatement(e.callExpression(e.memberExpression(e.identifier("keys"),e.identifier("push")),[r]))])),e.returnStatement(e.identifier("keys"))]))]))),i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(p),e.identifier("_$addArrayFunc")),[e.thisExpression(),e.arrowFunctionExpression([],o),e.arrayExpression(l)])))}else i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(p),e.identifier("_$addNodess")),[e.arrowFunctionExpression([],e.callExpression(e.memberExpression(e.callExpression(e.memberExpression(e.identifier("Array"),e.identifier("from")),[o]),e.identifier("map")),[e.arrowFunctionExpression([n],this.generate(t.children))]))])));return i}resolveText(t,s){let i=t.attr._$content,r=this.geneDeps(i),n=`_$node${s}`,o=[];return r.length>0?o.push(F(n,e.identifier("TextNode"),[e.arrowFunctionExpression([],i),e.thisExpression(),e.arrayExpression(r)])):o.push(F(n,e.identifier("TextNode"),[i])),o}resolveHTML(t,s){let i=[],r=`_$node${s}`;i.push(F(r,e.identifier("HtmlNode"),[t.tag]));for(let{key:n,value:o,nodes:p}of t.attr.props){if(this.parsePropNodes(o,p),n==="do"){i.push(e.expressionStatement(e.callExpression(o,[e.identifier(r)])));continue}if(n==="forwardProps"){i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.thisExpression(),e.identifier("forwardProps")),[e.identifier(r)])));continue}if(["willAppear","didAppear","willDisappear","didDisappear"].includes(n)){i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier("_$addLifeCycle")),[o,e.stringLiteral(n)])));continue}let l=this.geneDeps(o);if(n==="element"){e.isMemberExpression(o)&&e.isThisExpression(o.object)&&e.isIdentifier(o.property)&&(l=l.filter(b=>!e.isStringLiteral(b)||e.isStringLiteral(b)&&b.value!==o.property.name)),q(o)?i.push(e.variableDeclaration("const",[e.variableDeclarator(e.identifier(`${r}Element`),e.arrowFunctionExpression([],e.conditionalExpression(e.binaryExpression("===",e.unaryExpression("typeof",o),e.stringLiteral("function")),e.callExpression(o,[e.memberExpression(e.identifier(r),e.identifier("_$el"))]),e.assignmentExpression("=",o,e.memberExpression(e.identifier(r),e.identifier("_$el"))))))])):i.push(e.variableDeclaration("const",[e.variableDeclarator(e.identifier(`${r}Element`),e.arrowFunctionExpression([],e.callExpression(o,[e.memberExpression(e.identifier(r),e.identifier("_$el"))])))])),i.push(e.expressionStatement(e.callExpression(e.identifier(`${r}Element`),[]))),l.length>0&&i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.thisExpression(),e.identifier("_$addDeps")),[e.arrayExpression(l),e.identifier(`${r}Element`),e.identifier(r)])));continue}n==="_$content"&&(n="innerText");let u={addEvents:"_$addEvents",setAttributes:"_$addAttributes",style:"_$addStyle",className:"_$addClassName"},f="_$addProp";if(Object.keys(u).includes(n)){if(f=u[n],l.length>0){i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier(f)),[e.arrowFunctionExpression([],o),e.thisExpression(),e.arrayExpression(l)])));continue}i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier(f)),[o])));continue}if(n.startsWith("on")&&(n=n.slice(2),f="_$addEvent"),l.length>0){i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier(f)),[e.stringLiteral(n),e.arrowFunctionExpression([],o),e.thisExpression(),e.arrayExpression(l)])));continue}i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier(f)),[e.stringLiteral(n),o])))}return t.children.length>0&&i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier("_$addNodes")),[e.callExpression(e.arrowFunctionExpression([],this.generate(t.children)),[])]))),i}resolveCustom(t,s){let i=[],r=`_$node${s}`;i.push(e.variableDeclaration("const",[e.variableDeclarator(e.identifier(r),e.newExpression(t.tag,[]))]));for(let{key:n,value:o,nodes:p}of t.attr.props){if(o=this.parsePropNodes(o,p),n==="do"){i.push(e.expressionStatement(e.callExpression(o,[e.identifier(r)])));continue}if(n==="forwardProps"){i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.thisExpression(),e.identifier("forwardProps")),[e.identifier(r)])));continue}if(["willMount","didMount","willUnmount","didUnmount"].includes(n)){i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier("_$addLifeCycle")),[o,e.stringLiteral(n)])));continue}let l=this.geneDeps(o);if(n==="element"){e.isMemberExpression(o)&&e.isThisExpression(o.object)&&e.isIdentifier(o.property)&&(l=l.filter(f=>!e.isStringLiteral(f)||e.isStringLiteral(f)&&f.value!==o.property.name)),q(o)?i.push(e.variableDeclaration("const",[e.variableDeclarator(e.identifier(`${r}Element`),e.arrowFunctionExpression([],e.conditionalExpression(e.binaryExpression("===",e.unaryExpression("typeof",o),e.stringLiteral("function")),e.callExpression(o,[e.memberExpression(e.identifier(r),e.identifier("_$el"))]),e.assignmentExpression("=",o,e.memberExpression(e.identifier(r),e.identifier("_$el"))))))])):i.push(e.variableDeclaration("const",[e.variableDeclarator(e.identifier(`${r}Element`),e.arrowFunctionExpression([],e.callExpression(o,[e.memberExpression(e.identifier(r),e.identifier("_$el"))])))])),i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier("_$addLifeCycle")),[e.identifier(`${r}Element`),e.stringLiteral("didMount")]))),l.length>0&&i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.thisExpression(),e.identifier("_$addDeps")),[e.arrayExpression(l),e.identifier(`${r}Element`),e.identifier(r)])));continue}let u=n==="_$content"?"_$addDefaultProp":"_$addProp";if(l.length>0){i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier(u)),[e.stringLiteral(n),e.arrowFunctionExpression([],o),e.thisExpression(),e.arrayExpression(l)])));continue}i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier(u)),[e.stringLiteral(n),o])))}return t.children.length>0&&i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier("_$addChildren")),[e.arrowFunctionExpression([],this.generate(t.children))]))),i}resolveSubView(t,s){t.attr.isSubView=!0;let i=`_$node${s}`,r=[],n=t.attr.props.map(({key:l,value:u,nodes:f})=>(u=this.parsePropNodes(u,f),{key:l,value:u})),o=R(),p=[];for(let{key:l,value:u}of n){l==="_$content"&&(l="content");let f=`${l}_${o}`,b=this.geneDeps(u);r.push(e.variableDeclaration("const",[e.variableDeclarator(e.identifier(f),e.objectExpression([e.objectProperty(e.identifier("value"),u),e.objectProperty(e.identifier("deps"),e.arrayExpression(b))]))])),b.length>0&&(r.push(e.variableDeclaration("const",[e.variableDeclarator(e.identifier(`${l}UpdateFunc${s}`),e.arrowFunctionExpression([],e.assignmentExpression("=",e.memberExpression(e.identifier(f),e.identifier("value")),u)))])),r.push(e.expressionStatement(e.callExpression(e.memberExpression(e.thisExpression(),e.identifier("_$addDeps")),[e.arrayExpression(b),e.identifier(`${l}UpdateFunc${s}`)])))),p.push({key:l,keyWithId:f,listenDeps:b})}r.push(e.variableDeclaration("const",[e.variableDeclarator(e.identifier(i),e.callExpression(t.tag,[e.objectExpression(p.map(({key:l,keyWithId:u})=>e.objectProperty(e.identifier(l),e.identifier(u))))]))]));for(let{key:l,listenDeps:u}of p)u.length>0&&r.push(e.expressionStatement(e.callExpression(e.memberExpression(e.thisExpression(),e.identifier("_$deleteDeps")),[e.arrayExpression(u),e.identifier(`${l}UpdateFunc${s}`),e.memberExpression(e.identifier(i),e.numericLiteral(0),!0)])));return r}resolveEnv(t,s){let i=`_$node${s}`,r=[];r.push(F(i,e.identifier("EnvNode"),[])),t.children.length>0&&r.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(i),e.identifier("_$addNodes")),[e.callExpression(e.arrowFunctionExpression([],this.generate(t.children)),[])])));for(let{key:n,value:o,nodes:p}of t.attr.props){o=this.parsePropNodes(o,p);let l=this.geneDeps(o);if(l.length>0){r.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(i),e.identifier("_$addProp")),[e.stringLiteral(n),e.arrowFunctionExpression([],o),e.thisExpression(),e.arrayExpression(l)])));continue}r.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(i),e.identifier("_$addProp")),[e.stringLiteral(n),o])))}return r}resolveExpression(t,s){let i=[],r=`_$node${s}`;for(let{key:n,value:o,nodes:p}of t.attr.props){if(o=this.parsePropNodes(o,p),n==="_$content"){let u=this.geneDeps(o);u.length>0?i.push(F(r,e.identifier("ExpressionNode"),[e.arrowFunctionExpression([],o),e.thisExpression(),e.arrayExpression(u)])):i.push(F(r,e.identifier("ExpressionNode"),[o]));continue}if(n==="onUpdateNodes"){i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier("_$onUpdateNodes")),[o])));continue}let l=this.geneDeps(o);if(l.length>0&&!["willAppear","didAppear","willDisappear","didDisappear"].includes(n)){i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier("_$addProp")),[e.stringLiteral(n),e.arrowFunctionExpression([],o),e.thisExpression(),e.arrayExpression(l)])));continue}i.push(e.expressionStatement(e.callExpression(e.memberExpression(e.identifier(r),e.identifier("_$addProp")),[e.stringLiteral(n),o])))}return i}parsePropNodes(t,s){let i=t,r=this.generate.bind(this);return this.path.scope.traverse(V(t),{StringLiteral(n){let o=n.node.value,p=s[o];if(!p)return;let l=e.callExpression(e.arrowFunctionExpression([],r(p)),[]);t===n.node&&(i=l),n.replaceWith(l),n.skip()}}),i}};function W(a,t,s,i,r=[]){let n=new j(a,s,i,r);return{code:n.generate(t),useProperties:n.usedProperties}}function pe(a,t,s,i,r=!1){let n,o,p=[...a.body.directives,...a.body.body];if(r){let l=a.params[0];if(!l||!m.isObjectPattern(l)){let u=W(i,J(p,i),t,s);n=u.code,o=u.useProperties}else{let f=l.properties.map(P=>P.key.name).map(P=>({ids:[P],propNames:[m.arrayExpression([m.spreadElement(m.logicalExpression("??",m.optionalMemberExpression(m.identifier(P),m.identifier("deps"),!1,!0),m.arrayExpression()))]).elements[0]]})),b=W(i,J(p,i),t,s,f);n=b.code,o=b.useProperties}}else{let l=W(i,J(p,i),t,s);n=l.code,o=l.useProperties}return a.body=n,o}function Pe(a,t){let s=a.params[0];if(!s||!m.isObjectPattern(s))return;let i=[];for(let r of s.properties){let n=r.key.name;i.push(n),m.isAssignmentPattern(r.value)&&(r.value.right=m.objectExpression([m.objectProperty(m.identifier("value"),r.value.right),m.objectProperty(m.identifier("deps"),m.arrayExpression())]))}t.scope.traverse(m.functionDeclaration(null,[],a.body),{Identifier(r){i.includes(r.node.name)&&!T(r.parentPath.node,r.node)&&!B(r.parentPath.node,r.node)&&(r.replaceWith(m.optionalMemberExpression(m.identifier(r.node.name),m.identifier("value"),!1,!0)),r.skip())}})}function de(a,t,s){let i=[],r,n=[];for(let p of a.body){if(m.isClassProperty(p)){let f=p.value;for(;m.isTSAsExpression(f);)f=f.expression;if(p.value=f,!m.isArrowFunctionExpression(p.value))continue}let l=p.decorators?.find(f=>m.isIdentifier(f.expression)&&f.expression.name==="View"),u=p.key.name==="Body";!l&&!u||(m.isClassProperty(p)&&(p=ee(p,s)),l&&(p.decorators=null,n.push(p)),r=p)}let o=n.map(p=>p.key.name);for(let p of n)Pe(p,s),i.push(...pe(p,t,o,s,!0));return i.push(...pe(r,t,o,s)),i}import*as c from"@babel/types";function le(a,t){let s=a.key.name;a.key.name=`_$$${s}`;let i=t.body.indexOf(a),r=c.classProperty(c.identifier(`_$$${s}Deps`),c.newExpression(c.identifier("Set"),[])),n=c.classMethod("get",c.identifier(s),[],c.blockStatement([c.returnStatement(c.memberExpression(c.thisExpression(),c.identifier(`_$$${s}`)))])),o=c.classMethod("set",c.identifier(s),[c.identifier("value")],c.blockStatement([c.expressionStatement(c.callExpression(c.memberExpression(c.thisExpression(),c.identifier("_$updateProperty")),[c.stringLiteral(s),c.identifier("value")]))]));t.body.splice(i+1,0,r,n,o)}function ce(a,t,s,i){let r=t.body.indexOf(a),n=s.toLowerCase(),o=c.classProperty(c.identifier(`_$$$${i}`),c.stringLiteral(n));t.body.splice(r,0,o)}function fe(a,t,s){if(t.body.find(n=>c.isClassProperty(n)&&n.key.name==="_$defaultProp"))return;let i=t.body.indexOf(a),r=c.classProperty(c.identifier("_$defaultProp"),c.stringLiteral(s));t.body.splice(i,0,r)}function ue(a,t,s){let i=t.body.indexOf(a),r=c.classMethod("get",c.identifier(s),[],c.blockStatement([c.returnStatement(c.callExpression(c.memberExpression(c.thisExpression(),c.identifier("_$childrenFuncs")),[]))]));t.body.splice(i,1,r)}function me(a,t,s){let i=t.body.indexOf(a),r=c.classProperty(c.identifier(`_$$${s}`),c.stringLiteral("Watcher"));t.body.splice(i,0,r)}import{minimatch as xe}from"minimatch";function ye(a,t){let{files:s="**/*.{js,jsx,ts,tsx}",excludeFiles:i="**/{dist,node_modules,lib}/*.{js,ts}"}=t,r=Array.isArray(s)?s:[s],n=Array.isArray(i)?i:[i],o=null,p=null,l=null,u=[],f={},b=[],P=[],w;function C(){let g=de(p,u.filter(D=>!b.includes(D)),w),v=Object.entries(f).find(([,{propOrEnv:D,isContent:E}])=>E&&D==="Prop")?.[0]??"",S=Object.entries(f).reverse();for(let[D,{node:E,derivedFrom:$,assignDeps:_,isStatic:I,isChildren:h,propOrEnv:O,isWatcher:k}]of S){if(y.isClassProperty(E)&&!E.value&&(E.value=y.identifier("undefined")),h){ue(E,p,D);continue}if($.length!==0){let N=te(l,$,_,b);g.push(...N),Q(D,N,l,p),k?me(E,p,D):Y(E)}O&&(ce(E,p,O,D),v===D&&fe(E,p,D)),!I&&g.includes(D)&&le(E,p)}Z(p,P),L()}function A(g){let v=g.node;o=v,p=v.body,l=y.classProperty(y.identifier("_$derivedPairs"),y.objectExpression([])),u=p.body.filter(S=>y.isClassProperty(S)).map(S=>S.key.name),b=p.body.filter(S=>{if(!y.isClassProperty(S))return!1;let D=S.decorators?.map(E=>E.expression.name)??[];return D.includes("Static")||D.includes("Children")}).map(S=>S.key.name),f={},w=g,this.addDLightImport()}function L(){o=null,p=null,l=null,u=[],b=[],P=[],f={}}return{visitor:{Program(g,v){if(v.filename){for(let E of r)if(xe(v.filename,E)){this.enter=!0;break}for(let E of n)if(xe(v.filename,E)){this.enter=!1;break}}else this.enter=!0;this.didAddDLightImport=!1;let D=g.node.body.filter(y.isImportDeclaration).filter(E=>E.source.value==="@dlightjs/dlight");D.length===0&&(this.enter=!1),this.addDLightImport=()=>{if(this.didAddDLightImport||D.length===0)return;let E=!1;for(let $ of D)if($.specifiers.find(_=>y.isImportDefaultSpecifier(_))){E=!0;break}E||D[0].specifiers.unshift(y.importDefaultSpecifier(y.identifier("DLight"))),this.didAddDLightImport=!0}},ClassDeclaration:{enter(g){this.enter&&M(g)&&A.call(this,g)},exit(g){this.enter&&M(g)&&C()}},ClassExpression:{enter(g){this.enter&&M(g)&&A.call(this,g)},exit(g){this.enter&&M(g)&&C()}},ClassMethod(g){if(!this.enter||!o||!y.isIdentifier(g.node.key))return;let v=g.node,S=g.node.key.name;if(S==="Body"||(P.push(S),v.decorators?.find(h=>y.isIdentifier(h.expression)&&h.expression.name==="View")))return;let E=h=>y.isIdentifier(h.expression)&&h.expression.name==="Watch",$=v.decorators?.find(E),_=h=>y.isCallExpression(h.expression)&&y.isIdentifier(h.expression.callee)&&h.expression.callee.name==="Watch",I=v.decorators?.find(_);if($??I){let{deps:h,assignDeps:O}=U(g,v,o,u);if(I){let k=I.expression.arguments[0];y.isArrayExpression(k)&&(h=k.elements.filter(N=>y.isStringLiteral(N)).map(N=>N.value))}f[S]={node:v,isWatcher:!0,derivedFrom:[...new Set(h)],assignDeps:[...new Set(O)]},v.decorators=v.decorators?.filter(k=>!(E(k)||_(k)))}},ClassProperty(g){if(!this.enter||!o)return;let v=g.node,S=v.key.name;if(S==="Body"||v.decorators?.find(h=>y.isIdentifier(h.expression)&&h.expression.name==="View"))return;let E=["Static","Prop","Env","Content","Children"],$=v.decorators?.filter(h=>y.isIdentifier(h.expression)&&E.includes(h.expression.name)).map(h=>h.expression.name)??[],_=[],I=[];if(!($.includes("Prop")||$.includes("Env"))){let h=U(g,v,o,u);_=h.deps,I=h.assignDeps}f[S]={node:v,isStatic:$.includes("Static"),isContent:$.includes("Content"),isChildren:$.includes("Children"),propOrEnv:$.includes("Prop")?"Prop":$.includes("Env")?"Env":void 0,derivedFrom:[...new Set(_)],assignDeps:[...new Set(I)]},v.decorators=v.decorators?.filter(h=>!(y.isIdentifier(h.expression)&&E.includes(h.expression.name)))}}}}function Je(a,t){return{plugins:[G,Ee.default??Ee,[he.default??he,{legacy:!0}],[ye,t]]}}export{Je as default}; | ||
var __defProp = Object.defineProperty; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __publicField = (obj, key, value) => { | ||
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); | ||
return value; | ||
}; | ||
// src/index.ts | ||
import syntaxTypescript from "babel-plugin-syntax-typescript-new"; | ||
import syntaxDecorators from "@babel/plugin-syntax-decorators"; | ||
// src/pluginProvider.ts | ||
import { minimatch } from "minimatch"; | ||
import { parseView } from "@dlightjs/view-parser"; | ||
import { parseReactivity } from "@dlightjs/reactivity-parser"; | ||
import { generateSubView, generateView } from "@dlightjs/view-generator"; | ||
var devMode = process.env.NODE_ENV === "development"; | ||
var _PluginProvider = class { | ||
dlightPackageName = _PluginProvider.dlightDefaultPackageName; | ||
// ---- Plugin Level | ||
babelApi; | ||
t; | ||
traverse; | ||
enableDevTools; | ||
includes; | ||
excludes; | ||
htmlTags; | ||
constructor(babelApi, types, includes, excludes, enableDevTools, htmlTags) { | ||
this.babelApi = babelApi; | ||
this.t = types; | ||
this.traverse = babelApi.traverse; | ||
this.includes = includes; | ||
this.excludes = excludes; | ||
this.enableDevTools = devMode && enableDevTools; | ||
this.htmlTags = typeof htmlTags === "function" ? htmlTags(_PluginProvider.defaultHTMLTags) : htmlTags.includes("*") ? [ | ||
.../* @__PURE__ */ new Set([..._PluginProvider.defaultHTMLTags, ...htmlTags]) | ||
].filter((tag) => tag !== "*") : htmlTags; | ||
} | ||
// ---- DLight class Level | ||
classRootPath; | ||
classDeclarationNode; | ||
classBodyNode; | ||
constructorNode; | ||
propertiesContainer = {}; | ||
dependencyMap = {}; | ||
enter = true; | ||
enterClassNode = false; | ||
className; | ||
// ---- File Level | ||
programNode; | ||
allImports = []; | ||
didAlterImports = false; | ||
/* ---- DLight Class Level Hooks ---- */ | ||
/** | ||
* @brief Clear all DLight Node Level variables after a class is transformed | ||
*/ | ||
clearNode() { | ||
this.classRootPath = void 0; | ||
this.classDeclarationNode = void 0; | ||
this.classBodyNode = void 0; | ||
this.constructorNode = void 0; | ||
this.propertiesContainer = {}; | ||
this.dependencyMap = {}; | ||
this.enter = true; | ||
this.enterClassNode = false; | ||
this.allImports = []; | ||
this.didAlterImports = false; | ||
this.className = void 0; | ||
} | ||
get availableProperties() { | ||
return Object.entries(this.propertiesContainer).filter( | ||
([key, { isWatcher, isStatic, isChildren }]) => key !== "_$compName" && !isWatcher && !isStatic && !isChildren | ||
).map(([key]) => key); | ||
} | ||
/** | ||
* @brief Initialize DLight Node Level variables when entering a class | ||
* @param path | ||
*/ | ||
initNode(path) { | ||
this.classRootPath = path; | ||
const node = path.node; | ||
this.classDeclarationNode = node; | ||
this.classBodyNode = node.body; | ||
this.propertiesContainer = {}; | ||
if (!node.id?.name) { | ||
node.id = this.t.identifier(`Anonymous_${_PluginProvider.uid()}`); | ||
} | ||
this.className = node.id?.name; | ||
this.handleClassCustomDecorators(); | ||
if (this.enableDevTools) { | ||
this.classBodyNode.body.unshift( | ||
this.t.classProperty( | ||
this.t.identifier("_$compName"), | ||
this.t.stringLiteral(this.className) | ||
) | ||
); | ||
} | ||
this.addConstructor(); | ||
if (!this.didAlterImports) { | ||
const dlightImports = this.allImports.filter( | ||
(n) => n.source.value === _PluginProvider.dlightDefaultPackageName | ||
); | ||
if (this.dlightPackageName !== _PluginProvider.dlightDefaultPackageName) { | ||
dlightImports.forEach((i) => { | ||
i.source.value = this.dlightPackageName; | ||
}); | ||
} | ||
this.programNode.body.unshift( | ||
this.t.importDeclaration( | ||
Object.entries(_PluginProvider.importMap).map( | ||
([key, value]) => this.t.importSpecifier( | ||
this.t.identifier(value), | ||
this.t.identifier(key) | ||
) | ||
), | ||
this.t.stringLiteral(this.dlightPackageName) | ||
) | ||
); | ||
this.didAlterImports = true; | ||
} | ||
} | ||
handleClassCustomDecorators() { | ||
if (!this.classBodyNode) | ||
return; | ||
const decorators = this.classDeclarationNode?.decorators; | ||
if (!decorators) | ||
return; | ||
const forwardPropDeco = this.findDecoratorByName(decorators, "ForwardProps"); | ||
if (forwardPropDeco) { | ||
this.classBodyNode.body.unshift( | ||
this.t.classProperty(this.t.identifier("_$forwardProps")), | ||
this.t.classProperty( | ||
this.t.identifier("_$forwardPropsSet"), | ||
this.t.newExpression(this.t.identifier("Set"), []) | ||
), | ||
this.t.classProperty( | ||
this.t.identifier("_$forwardPropsId"), | ||
this.t.arrayExpression([]) | ||
) | ||
); | ||
this.classDeclarationNode.decorators = this.removeDecorators( | ||
decorators, | ||
["ForwardProps"] | ||
); | ||
} | ||
} | ||
/** | ||
* @brief Transform the whole DLight class when exiting the class | ||
* 1. Alter all the state properties | ||
* 2. Transform MainView and SubViews with DLight syntax | ||
*/ | ||
transformDLightClass() { | ||
const usedProperties = this.handleView(); | ||
const propertyArr = Object.entries(this.propertiesContainer).reverse(); | ||
const depReversedMap = this.dependencyMapReversed(); | ||
for (const [ | ||
key, | ||
{ node, deps, isStatic, isChildren, isPropOrEnv, isWatcher, isContent } | ||
] of propertyArr) { | ||
if (isChildren) { | ||
this.resolveChildrenDecorator(node); | ||
continue; | ||
} | ||
if (deps.length > 0) { | ||
usedProperties.push(...deps); | ||
if (isWatcher) | ||
this.resolveWatcherDecorator(node); | ||
else | ||
this.handleDerivedProperty(node); | ||
} | ||
if (isPropOrEnv) { | ||
this.resolvePropDecorator(node, isPropOrEnv); | ||
} | ||
if (isContent) { | ||
this.resolvePropDecorator(node, "Prop"); | ||
this.resolveContentDecorator(node); | ||
} | ||
if (isStatic) | ||
continue; | ||
if (usedProperties.includes(key)) { | ||
this.resolveStateDecorator( | ||
node, | ||
this.availableProperties.indexOf(key), | ||
depReversedMap[key] | ||
); | ||
} | ||
} | ||
} | ||
/* ---- DLight Class View Handlers ---- */ | ||
/** | ||
* @brief Transform Body and SubViews with DLight syntax | ||
* @returns used properties | ||
*/ | ||
handleView() { | ||
if (!this.classBodyNode) | ||
return []; | ||
const usedPropertySet = /* @__PURE__ */ new Set(); | ||
let mainView; | ||
const subViewNodes = []; | ||
for (let viewNode of this.classBodyNode.body) { | ||
if (!this.t.isClassProperty(viewNode) && !this.t.isClassMethod(viewNode)) | ||
continue; | ||
if (!this.t.isIdentifier(viewNode.key)) | ||
continue; | ||
const isSubView = this.findDecoratorByName(viewNode.decorators, "View"); | ||
const isMainView = viewNode.key.name === "View"; | ||
if (!isSubView && !isMainView) | ||
continue; | ||
if (this.t.isClassProperty(viewNode)) { | ||
let exp = viewNode.value; | ||
while (this.t.isTSAsExpression(exp)) | ||
exp = exp.expression; | ||
if (!this.t.isArrowFunctionExpression(viewNode.value)) | ||
continue; | ||
viewNode.value = exp; | ||
const newViewNode = this.arrowFunctionPropertyToMethod(viewNode); | ||
if (!newViewNode) | ||
continue; | ||
viewNode = newViewNode; | ||
} | ||
if (isSubView) { | ||
viewNode.decorators = null; | ||
subViewNodes.push(viewNode); | ||
} else { | ||
mainView = viewNode; | ||
} | ||
} | ||
const subViewNames = subViewNodes.map((v) => v.key.name); | ||
const subViewPropSubDepMap = Object.fromEntries( | ||
subViewNodes.map((v) => { | ||
const prop = v.params[0]; | ||
if (!prop || !this.t.isObjectPattern(prop)) | ||
return ["-", null]; | ||
const props = Object.fromEntries( | ||
prop.properties.map((p) => { | ||
if (!this.t.isObjectProperty(p)) | ||
return ["-", null]; | ||
const key = p.key.name; | ||
const subDeps = this.getIdentifiers( | ||
this.t.assignmentExpression( | ||
"=", | ||
this.t.objectPattern([ | ||
this.t.objectProperty(this.t.numericLiteral(0), p.value) | ||
]), | ||
this.t.numericLiteral(0) | ||
) | ||
).filter((v2) => v2 !== key); | ||
return [key, subDeps]; | ||
}).filter(([_, props2]) => props2) | ||
); | ||
return [v.key.name, props]; | ||
}).filter(([_, props]) => props) | ||
); | ||
let templateIdx = -1; | ||
if (mainView) { | ||
let usedProperties2; | ||
[usedProperties2, templateIdx] = this.alterMainView( | ||
mainView, | ||
subViewNames, | ||
subViewPropSubDepMap | ||
); | ||
usedProperties2.forEach(usedPropertySet.add.bind(usedPropertySet)); | ||
} | ||
subViewNodes.forEach((viewNode) => { | ||
let usedProperties2; | ||
[usedProperties2, templateIdx] = this.alterSubView( | ||
viewNode, | ||
subViewNames, | ||
subViewPropSubDepMap, | ||
templateIdx | ||
); | ||
usedProperties2.forEach(usedPropertySet.add.bind(usedPropertySet)); | ||
}); | ||
const usedProperties = []; | ||
this.availableProperties.forEach((p) => { | ||
if (usedPropertySet.has(p)) | ||
usedProperties.push(p); | ||
}); | ||
return usedProperties; | ||
} | ||
/** | ||
* @brief Transform Views with DLight syntax | ||
* @param viewNode | ||
* @param subViewNames | ||
* @param isSubView | ||
* @returns Used properties | ||
*/ | ||
alterMainView(viewNode, subViewNames, subViewPropSubDepMap) { | ||
const viewUnits = parseView(viewNode.body, { | ||
babelApi: this.babelApi, | ||
subviewNames: subViewNames, | ||
htmlTags: this.htmlTags | ||
}); | ||
const [viewParticles, usedPropertySet] = parseReactivity(viewUnits, { | ||
babelApi: this.babelApi, | ||
availableProperties: this.availableProperties, | ||
dependencyMap: this.dependencyMap | ||
}); | ||
const [body, classProperties, templateIdx] = generateView(viewParticles, { | ||
babelApi: this.babelApi, | ||
className: this.className, | ||
importMap: _PluginProvider.importMap, | ||
subViewPropMap: Object.fromEntries( | ||
Object.entries(subViewPropSubDepMap).map(([key, props]) => [ | ||
key, | ||
Object.keys(props) | ||
]) | ||
), | ||
templateIdx: -1 | ||
}); | ||
viewNode.body = body; | ||
this.classBodyNode?.body.push(...classProperties); | ||
return [usedPropertySet, templateIdx]; | ||
} | ||
alterSubView(viewNode, subViewNames, subViewPropSubDepMap, templateIdx) { | ||
const viewUnits = parseView(viewNode.body, { | ||
babelApi: this.babelApi, | ||
subviewNames: subViewNames, | ||
htmlTags: this.htmlTags | ||
}); | ||
const [viewParticlesProperty, usedPropertySet] = parseReactivity( | ||
viewUnits, | ||
{ | ||
babelApi: this.babelApi, | ||
availableProperties: this.availableProperties, | ||
dependencyMap: this.dependencyMap | ||
} | ||
); | ||
const subViewProp = subViewPropSubDepMap[viewNode.key.name] ?? []; | ||
const identifierDepMap = {}; | ||
Object.entries(subViewProp).forEach(([key, subDeps]) => { | ||
subDeps.forEach((dep) => { | ||
identifierDepMap[dep] = [key]; | ||
}); | ||
}); | ||
const [viewParticlesIdentifier] = parseReactivity(viewUnits, { | ||
babelApi: this.babelApi, | ||
availableProperties: Object.keys(subViewProp), | ||
dependencyMap: this.dependencyMap, | ||
dependencyParseType: "identifier", | ||
identifierDepMap | ||
}); | ||
const subViewPropMap = Object.fromEntries( | ||
Object.entries(subViewPropSubDepMap).map(([key, props]) => [ | ||
key, | ||
Object.keys(props) | ||
]) | ||
); | ||
const [body, classProperties, newTemplateIdx] = generateSubView( | ||
viewParticlesProperty, | ||
viewParticlesIdentifier, | ||
viewNode.params[0], | ||
{ | ||
babelApi: this.babelApi, | ||
className: this.className, | ||
importMap: _PluginProvider.importMap, | ||
subViewPropMap, | ||
templateIdx | ||
} | ||
); | ||
viewNode.body = body; | ||
this.classBodyNode?.body.push(...classProperties); | ||
return [usedPropertySet, newTemplateIdx]; | ||
} | ||
/* ---- Babel Visitors ---- */ | ||
visitProgram(_path) { | ||
} | ||
programVisitor(path, filename) { | ||
this.enter = this.fileAllowed(filename); | ||
if (!this.enter) | ||
return; | ||
this.allImports = path.node.body.filter( | ||
(n) => this.t.isImportDeclaration(n) | ||
); | ||
const dlightImports = this.allImports.filter( | ||
(n) => n.source.value === _PluginProvider.dlightDefaultPackageName | ||
); | ||
if (dlightImports.length === 0) { | ||
this.enter = false; | ||
return; | ||
} | ||
this.programNode = path.node; | ||
this.visitProgram(path); | ||
} | ||
enterClass(_path) { | ||
} | ||
classEnter(path) { | ||
if (!this.enter) | ||
return; | ||
this.enterClassNode = this.isDLightView(path); | ||
if (!this.enterClass) | ||
return; | ||
this.initNode(path); | ||
this.enterClass(path); | ||
} | ||
exitClass(_path) { | ||
} | ||
classExit(path) { | ||
if (!this.enter) | ||
return; | ||
if (!this.enterClassNode) | ||
return; | ||
this.transformDLightClass(); | ||
this.addInit(); | ||
this.exitClass(path); | ||
this.clearNode(); | ||
this.enterClassNode = false; | ||
} | ||
visitClassMethod(_path) { | ||
} | ||
classMethodVisitor(path) { | ||
if (!this.enterClassNode) | ||
return; | ||
if (!this.t.isIdentifier(path.node.key)) | ||
return; | ||
const key = path.node.key.name; | ||
if (key === "View") | ||
return; | ||
const isSubView = this.findDecoratorByName(path.node.decorators, "View"); | ||
if (isSubView) | ||
return; | ||
const node = path.node; | ||
const watchDeco = this.findDecoratorByName(node.decorators, "Watch"); | ||
if (!watchDeco) { | ||
if (this.t.isIdentifier(node.key, { name: "constructor" })) | ||
return; | ||
this.autoBindMethods(node); | ||
return; | ||
} | ||
let deps = []; | ||
if (this.t.isIdentifier(watchDeco)) { | ||
deps = this.getDependencies(path); | ||
} else { | ||
const listenDeps = watchDeco.arguments[0]; | ||
if (this.t.isArrayExpression(listenDeps)) { | ||
deps = listenDeps.elements.filter((arg) => this.t.isStringLiteral(arg)).map((arg) => arg.value); | ||
deps = [...new Set(deps)]; | ||
} | ||
} | ||
this.propertiesContainer[key] = { | ||
node, | ||
deps, | ||
isWatcher: true | ||
}; | ||
node.decorators = this.removeDecorators(node.decorators, ["Watch"]); | ||
this.visitClassMethod(path); | ||
} | ||
visitClassProperty(_path) { | ||
} | ||
classPropertyVisitor(path) { | ||
if (!this.enterClassNode) | ||
return; | ||
const node = path.node; | ||
if (!this.t.isIdentifier(node.key)) | ||
return; | ||
const key = node.key.name; | ||
if (key === "View") | ||
return; | ||
const decorators = node.decorators; | ||
const isSubView = this.findDecoratorByName(decorators, "View"); | ||
if (isSubView) | ||
return; | ||
const isProp = !!this.findDecoratorByName(decorators, "Prop"); | ||
const isEnv = !!this.findDecoratorByName(decorators, "Env"); | ||
const isChildren = !!this.findDecoratorByName(node.decorators, "Children"); | ||
const deps = !isChildren ? this.getDependencies(path) : []; | ||
this.propertiesContainer[key] = { | ||
node, | ||
deps, | ||
isStatic: !!this.findDecoratorByName(decorators, "Static"), | ||
isContent: !!this.findDecoratorByName(decorators, "Content"), | ||
isChildren, | ||
isPropOrEnv: isProp ? "Prop" : isEnv ? "Env" : void 0 | ||
}; | ||
node.decorators = this.removeDecorators( | ||
decorators, | ||
_PluginProvider.availableDecoNames | ||
); | ||
this.visitClassProperty(path); | ||
} | ||
/* ---- Decorator Resolvers ---- */ | ||
/** | ||
* @brief Decorator resolver: Watcher | ||
* Add: | ||
* $wW${key} | ||
* @param node | ||
*/ | ||
resolveWatcherDecorator(node) { | ||
if (!this.t.isIdentifier(node.key)) | ||
return; | ||
const key = node.key.name; | ||
const propertyIdx = this.classBodyNode.body.indexOf(node); | ||
const watcherNode = this.t.classProperty(this.t.identifier(`$w$${key}`)); | ||
this.classBodyNode.body.splice(propertyIdx, 0, watcherNode); | ||
} | ||
/** | ||
* @brief Decorator resolver: Children | ||
* Add: | ||
* get ${key}() { | ||
* return this._$children | ||
* } | ||
* @param node | ||
*/ | ||
resolveChildrenDecorator(node) { | ||
if (!this.classBodyNode) | ||
return; | ||
if (!this.t.isIdentifier(node.key)) | ||
return; | ||
const key = node.key.name; | ||
const propertyIdx = this.classBodyNode.body.indexOf(node); | ||
const childrenFuncCallNode = this.t.memberExpression( | ||
this.t.thisExpression(), | ||
this.t.identifier("_$children") | ||
); | ||
const getterNode = this.t.classMethod( | ||
"get", | ||
this.t.identifier(key), | ||
[], | ||
this.t.blockStatement([this.t.returnStatement(childrenFuncCallNode)]) | ||
); | ||
this.classBodyNode.body.splice(propertyIdx, 1, getterNode); | ||
} | ||
/** | ||
* @brief Decorator resolver: Content | ||
* Add: | ||
* _$contentKey = "key" | ||
* @param node | ||
*/ | ||
resolveContentDecorator(node) { | ||
if (!this.classBodyNode) | ||
return; | ||
if (!this.t.isIdentifier(node.key)) | ||
return; | ||
if (this.classBodyNode.body.some( | ||
(n) => this.t.isClassProperty(n) && n.key.name === "_$contentKey" | ||
)) | ||
return; | ||
const key = node.key.name; | ||
const propertyIdx = this.classBodyNode.body.indexOf(node); | ||
const derivedStatusKey = this.t.classProperty( | ||
this.t.identifier("_$contentKey"), | ||
this.t.stringLiteral(key) | ||
); | ||
this.classBodyNode.body.splice(propertyIdx, 0, derivedStatusKey); | ||
} | ||
/** | ||
* @brief Decorator resolver: Prop/Env | ||
* Add: | ||
* $p/e$${key} | ||
* @param node | ||
*/ | ||
resolvePropDecorator(node, decoratorName) { | ||
if (!this.classBodyNode) | ||
return; | ||
if (!this.t.isIdentifier(node.key)) | ||
return; | ||
const key = node.key.name; | ||
const propertyIdx = this.classBodyNode.body.indexOf(node); | ||
const tag = decoratorName.toLowerCase() === "prop" ? "p" : "e"; | ||
const derivedStatusKey = this.t.classProperty( | ||
this.t.identifier(`$${tag}$${key}`) | ||
); | ||
this.classBodyNode.body.splice(propertyIdx, 0, derivedStatusKey); | ||
} | ||
/** | ||
* @brief Decorator resolver: State | ||
* Add: | ||
* $${key} = ${value} | ||
* $$${key} = ${depIdx} | ||
* $sub$${key} = [${reversedDeps}] | ||
* get ${key}() { | ||
* return this.$${key} | ||
* } | ||
* set ${key}(value) { | ||
* this._$updateProp("${key}", value) | ||
* } | ||
* @param node | ||
*/ | ||
resolveStateDecorator(node, idx, reverseDeps) { | ||
if (!this.classBodyNode) | ||
return; | ||
if (!this.t.isIdentifier(node.key)) | ||
return; | ||
const key = node.key.name; | ||
node.key.name = `$${key}`; | ||
const propertyIdx = this.classBodyNode.body.indexOf(node); | ||
const idxNode = this.t.classProperty( | ||
this.t.identifier(`$$${key}`), | ||
this.t.numericLiteral(1 << idx) | ||
); | ||
const depsNode = reverseDeps ? [ | ||
this.t.classProperty( | ||
this.t.identifier(`$s$${key}`), | ||
this.t.arrayExpression( | ||
[...reverseDeps].map((d) => this.t.stringLiteral(d)) | ||
) | ||
) | ||
] : []; | ||
const getterNode = this.t.classMethod( | ||
"get", | ||
this.t.identifier(key), | ||
[], | ||
this.t.blockStatement([ | ||
this.t.returnStatement( | ||
this.t.memberExpression( | ||
this.t.thisExpression(), | ||
this.t.identifier(`$${key}`) | ||
) | ||
) | ||
]) | ||
); | ||
const setterNode = this.t.classMethod( | ||
"set", | ||
this.t.identifier(key), | ||
[this.t.identifier("value")], | ||
this.t.blockStatement([ | ||
this.t.expressionStatement( | ||
this.t.callExpression( | ||
this.t.memberExpression( | ||
this.t.thisExpression(), | ||
this.t.identifier("_$updateProp") | ||
), | ||
[this.t.stringLiteral(key), this.t.identifier("value")] | ||
) | ||
) | ||
]) | ||
); | ||
this.classBodyNode.body.splice( | ||
propertyIdx + 1, | ||
0, | ||
idxNode, | ||
...depsNode, | ||
getterNode, | ||
setterNode | ||
); | ||
} | ||
/* ---- Helper Functions ---- */ | ||
/** | ||
* @brief Test if the file is allowed to be transformed | ||
* @param fileName | ||
* @returns is file allowed | ||
*/ | ||
fileAllowed(fileName) { | ||
if (this.includes.includes("*")) | ||
return true; | ||
if (!fileName) | ||
return false; | ||
if (this.excludes.some((pattern) => minimatch(fileName, pattern))) | ||
return false; | ||
if (!this.includes.some((pattern) => minimatch(fileName, pattern))) | ||
return false; | ||
return true; | ||
} | ||
/** | ||
* @brief Test if the class is a dlight view | ||
* @param path | ||
* @returns | ||
*/ | ||
isDLightView(path) { | ||
const node = path.node; | ||
const decorators = node.decorators ?? []; | ||
const isDecorator = decorators.find( | ||
(deco) => this.t.isIdentifier(deco.expression, { name: "View" }) | ||
); | ||
if (isDecorator) { | ||
node.superClass = this.t.identifier("View"); | ||
node.decorators = node.decorators?.filter( | ||
(deco) => !this.t.isIdentifier(deco.expression, { name: "View" }) | ||
); | ||
} | ||
return this.t.isIdentifier(node.superClass, { name: "View" }); | ||
} | ||
/** | ||
* @brief Remove decorators by name | ||
* Only search for Identifier and CallExpression, e.g, @Ok, @Ok() | ||
* @param decorators | ||
* @param names | ||
* @returns new decorators | ||
*/ | ||
removeDecorators(decorators, names) { | ||
if (!decorators) | ||
return []; | ||
return decorators.filter( | ||
(d) => !(this.t.isIdentifier(d.expression) && names.includes(d.expression.name) || this.t.isCallExpression(d.expression) && this.t.isIdentifier(d.expression.callee) && names.includes(d.expression.callee.name)) | ||
); | ||
} | ||
/** | ||
* @brief Find decorator by name, | ||
* Only search for Identifier and CallExpression, e.g, @Ok, @Ok() | ||
* @param decorators | ||
* @param name | ||
* @returns Identifier or CallExpression or nothing | ||
*/ | ||
findDecoratorByName(decorators, name) { | ||
if (!decorators) | ||
return; | ||
return decorators.find( | ||
(deco) => this.t.isIdentifier(deco.expression, { name }) || this.t.isCallExpression(deco.expression) && this.t.isIdentifier(deco.expression.callee, { name }) | ||
)?.expression; | ||
} | ||
/** | ||
* constructor(props, content, children, forwardPropsScope) { | ||
* super() | ||
* } | ||
*/ | ||
addConstructor() { | ||
if (!this.classBodyNode) | ||
return; | ||
let constructor = this.classBodyNode.body.find( | ||
(n) => this.t.isClassMethod(n, { kind: "constructor" }) | ||
); | ||
if (constructor) | ||
throw new Error("DLight class should not have constructor"); | ||
constructor = this.t.classMethod( | ||
"constructor", | ||
this.t.identifier("constructor"), | ||
[ | ||
this.t.identifier("props"), | ||
this.t.identifier("content"), | ||
this.t.identifier("children"), | ||
this.t.identifier("forwardPropsScope") | ||
], | ||
this.t.blockStatement([ | ||
this.t.expressionStatement(this.t.callExpression(this.t.super(), [])) | ||
]) | ||
); | ||
this.constructorNode = constructor; | ||
this.classBodyNode.body.unshift(constructor); | ||
} | ||
addInit() { | ||
this.constructorNode.body.body.push( | ||
this.t.expressionStatement( | ||
this.t.callExpression( | ||
this.t.memberExpression( | ||
this.t.thisExpression(), | ||
this.t.identifier("_$init") | ||
), | ||
[ | ||
this.t.identifier("props"), | ||
this.t.identifier("content"), | ||
this.t.identifier("children"), | ||
this.t.identifier("forwardPropsScope") | ||
] | ||
) | ||
) | ||
); | ||
} | ||
autoBindMethods(node) { | ||
this.constructorNode.body.body.push( | ||
this.t.expressionStatement( | ||
this.t.assignmentExpression( | ||
"=", | ||
this.t.memberExpression(this.t.thisExpression(), node.key), | ||
this.t.callExpression( | ||
this.t.memberExpression( | ||
this.t.memberExpression(this.t.thisExpression(), node.key), | ||
this.t.identifier("bind") | ||
), | ||
[this.t.thisExpression()] | ||
) | ||
) | ||
) | ||
); | ||
} | ||
/** | ||
* ${key} = ${value} | ||
* get $f$${key}() { | ||
* return ${value} | ||
* } | ||
*/ | ||
handleDerivedProperty(node) { | ||
if (!this.t.isIdentifier(node.key)) | ||
return; | ||
const key = node.key.name; | ||
const value = node.value; | ||
const propertyIdx = this.classBodyNode.body.indexOf(node); | ||
const getterNode = this.t.classMethod( | ||
"get", | ||
this.t.identifier(`$f$${key}`), | ||
[], | ||
this.t.blockStatement([this.t.returnStatement(value)]) | ||
); | ||
this.classBodyNode.body.splice(propertyIdx + 1, 0, getterNode); | ||
} | ||
/** | ||
* @brief Get all valid dependencies of a babel path | ||
* @param path | ||
* @returns dependencies | ||
*/ | ||
getDependencies(path) { | ||
const node = path.node; | ||
if (!this.t.isIdentifier(node.key)) | ||
return []; | ||
const deps = /* @__PURE__ */ new Set(); | ||
const assignDeps = /* @__PURE__ */ new Set(); | ||
path.scope.traverse(node, { | ||
MemberExpression: (innerPath) => { | ||
if (!this.t.isIdentifier(innerPath.node.property)) | ||
return; | ||
const propertyKey2 = innerPath.node.property.name; | ||
if (this.isAssignmentExpressionLeft(innerPath)) { | ||
assignDeps.add(propertyKey2); | ||
} else if (this.availableProperties.includes(propertyKey2) && this.t.isThisExpression(innerPath.node.object) && !this.isMemberInEscapeFunction( | ||
innerPath, | ||
this.classDeclarationNode | ||
) && !this.isMemberInManualFunction( | ||
innerPath, | ||
this.classDeclarationNode | ||
) && !this.isAssignmentExpressionRight( | ||
innerPath, | ||
this.classDeclarationNode | ||
)) { | ||
deps.add(propertyKey2); | ||
this.dependencyMap[propertyKey2]?.forEach(deps.add.bind(deps)); | ||
} | ||
} | ||
}); | ||
assignDeps.forEach(deps.delete.bind(deps)); | ||
const propertyKey = node.key.name; | ||
const depArr = [...deps]; | ||
if (deps.size > 0) { | ||
this.dependencyMap[propertyKey] = depArr; | ||
} | ||
return depArr; | ||
} | ||
dependencyMapReversed() { | ||
const reversedMap = {}; | ||
Object.entries(this.dependencyMap).forEach(([key, deps]) => { | ||
deps.forEach((dep) => { | ||
if (!reversedMap[dep]) | ||
reversedMap[dep] = /* @__PURE__ */ new Set(); | ||
reversedMap[dep].add(key); | ||
}); | ||
}); | ||
return reversedMap; | ||
} | ||
/** | ||
* @brief Transform arrow function property to method | ||
* @param propertyNode | ||
* @returns new method node | ||
*/ | ||
arrowFunctionPropertyToMethod(propertyNode) { | ||
if (this.t.isArrowFunctionExpression(propertyNode.value)) | ||
return; | ||
let newNode; | ||
this.classRootPath.scope.traverse(this.classBodyNode, { | ||
ClassProperty: (innerPath) => { | ||
if (innerPath.node !== propertyNode) | ||
return; | ||
const propertyBody = propertyNode.value.body; | ||
const body = this.t.isExpression(propertyBody) ? this.t.blockStatement([this.t.returnStatement(propertyBody)]) : propertyBody; | ||
const methodNode = this.t.classMethod( | ||
"method", | ||
propertyNode.key, | ||
propertyNode.value.params, | ||
body | ||
); | ||
newNode = methodNode; | ||
innerPath.replaceWith(methodNode); | ||
} | ||
}); | ||
return newNode; | ||
} | ||
/** | ||
* @brief Check if a member expression is a property of a member expression | ||
* @param parentNode | ||
* @param currentNode | ||
* @returns is a property of a member expression | ||
*/ | ||
isMemberExpressionProperty(parentNode, currentNode) { | ||
return this.t.isMemberExpression(parentNode) && !parentNode.computed && parentNode.property === currentNode; | ||
} | ||
/** | ||
* @brief Check if a member expression is a key of an object | ||
* @param parentNode | ||
* @param currentNode | ||
* @returns is a key of an object | ||
*/ | ||
isObjectKey(parentNode, currentNode) { | ||
return this.t.isObjectProperty(parentNode) && parentNode.key === currentNode; | ||
} | ||
/** | ||
* @brief Add arrow function to property value | ||
* @param node | ||
*/ | ||
valueWithArrowFunc(node) { | ||
if (!node.value) { | ||
node.value = this.t.identifier("undefined"); | ||
} | ||
node.value = this.t.arrowFunctionExpression([], node.value); | ||
} | ||
/** | ||
* @brief Wrap the value in a file | ||
* @param node | ||
* @returns wrapped value | ||
*/ | ||
valueWrapper(node) { | ||
return this.t.file( | ||
this.t.program([ | ||
this.t.isStatement(node) ? node : this.t.expressionStatement(node) | ||
]) | ||
); | ||
} | ||
/** | ||
* @brief check if the identifier is from a function param till the stopNode | ||
* e.g: | ||
* function myFunc1(ok) { // stopNode = functionBody | ||
* const myFunc2 = ok => ok // from function param | ||
* console.log(ok) // not from function param | ||
* } | ||
* @param path | ||
* @param idName | ||
*/ | ||
isAttrFromFunction(path, idName) { | ||
let reversePath = path.parentPath; | ||
const checkParam = (param) => { | ||
if (this.t.isIdentifier(param)) | ||
return param.name === idName; | ||
if (this.t.isAssignmentPattern(param)) | ||
return checkParam(param.left); | ||
if (this.t.isArrayPattern(param)) { | ||
return param.elements.filter(Boolean).map((el) => checkParam(el)).includes(true); | ||
} | ||
if (this.t.isObjectPattern(param)) { | ||
return param.properties.filter( | ||
(prop) => this.t.isObjectProperty(prop) && this.t.isIdentifier(prop.key) | ||
).map((prop) => prop.key.name).includes(idName); | ||
} | ||
if (this.t.isRestElement(param)) | ||
return checkParam(param.argument); | ||
return false; | ||
}; | ||
while (reversePath) { | ||
const node = reversePath.node; | ||
if (this.t.isArrowFunctionExpression(node) || this.t.isFunctionDeclaration(node)) { | ||
for (const param of node.params) { | ||
if (checkParam(param)) | ||
return true; | ||
} | ||
} | ||
reversePath = reversePath.parentPath; | ||
} | ||
return false; | ||
} | ||
/** | ||
* @brief Check if an identifier is a simple identifier, i.e., not a member expression, or a function param | ||
* @param path | ||
* 1. not a member expression | ||
* 2. not a function param | ||
* 3. not in a declaration | ||
* 4. not as object property's not computed key | ||
*/ | ||
isStandAloneIdentifier(path) { | ||
const node = path.node; | ||
const parentNode = path.parentPath?.node; | ||
const isMemberExpression = this.t.isMemberExpression(parentNode) && parentNode.property === node; | ||
if (isMemberExpression) | ||
return false; | ||
const isFunctionParam = this.isAttrFromFunction(path, node.name); | ||
if (isFunctionParam) | ||
return false; | ||
while (path.parentPath) { | ||
if (this.t.isVariableDeclarator(path.parentPath.node)) | ||
return false; | ||
if (this.t.isObjectProperty(path.parentPath.node) && path.parentPath.node.key === path.node && !path.parentPath.node.computed) | ||
return false; | ||
path = path.parentPath; | ||
} | ||
return true; | ||
} | ||
/** | ||
* @brief Get all identifiers as strings in a node | ||
* @param node | ||
* @returns identifiers | ||
*/ | ||
getIdentifiers(node) { | ||
if (this.t.isIdentifier(node)) | ||
return [node.name]; | ||
const identifierKeys = /* @__PURE__ */ new Set(); | ||
this.traverse(this.valueWrapper(node), { | ||
Identifier: (innerPath) => { | ||
if (!this.isStandAloneIdentifier(innerPath)) | ||
return; | ||
identifierKeys.add(innerPath.node.name); | ||
} | ||
}); | ||
return [...identifierKeys]; | ||
} | ||
/** | ||
* @brief Check if it's the left side of an assignment expression, e.g. this.count = 1 | ||
* @param innerPath | ||
* @returns is left side of an assignment expression | ||
*/ | ||
isAssignmentExpressionLeft(innerPath) { | ||
const parentNode = innerPath.parentPath?.node; | ||
return this.t.isAssignmentExpression(parentNode) && parentNode.left === innerPath.node || this.t.isUpdateExpression(parentNode); | ||
} | ||
/** | ||
* @brief Check if a member expression is the right side of an assignment expression | ||
* e.g. this.count = this.count + 1 | ||
* @param innerPath | ||
* @returns is the right side of an assignment expression | ||
*/ | ||
isAssignmentExpressionRight(innerPath, stopNode) { | ||
const currNode = innerPath.node; | ||
let isRightExp = false; | ||
let reversePath = innerPath.parentPath; | ||
while (reversePath && reversePath.node !== stopNode) { | ||
if (this.t.isAssignmentExpression(reversePath.node)) { | ||
const leftNode = reversePath.node.left; | ||
const typeEqual = currNode.type === leftNode.type; | ||
const identifierEqual = currNode.property.name === leftNode.property.name; | ||
isRightExp = typeEqual && identifierEqual; | ||
} | ||
reversePath = reversePath.parentPath; | ||
} | ||
return isRightExp; | ||
} | ||
/** | ||
* @brief Check if it's in an "escape" function, | ||
* e.g. escape(() => { console.log(this.count) }) | ||
* deps will be empty instead of ["count"] | ||
* @param innerPath | ||
* @param classDeclarationNode | ||
* @returns is in escape function | ||
*/ | ||
isMemberInEscapeFunction(innerPath, stopNode) { | ||
let isInFunction = false; | ||
let reversePath = innerPath.parentPath; | ||
while (reversePath && reversePath.node !== stopNode) { | ||
const node = reversePath.node; | ||
if (this.t.isCallExpression(node) && this.t.isIdentifier(node.callee) && _PluginProvider.escapeNamings.includes(node.callee.name)) { | ||
isInFunction = true; | ||
break; | ||
} | ||
reversePath = reversePath.parentPath; | ||
} | ||
return isInFunction; | ||
} | ||
/** | ||
* @brief Check if it's in a "manual" function, | ||
* e.g. manual(() => { console.log(this.count) }, ["flag"]) | ||
* deps will be ["flag"] instead of ["count"] | ||
* @param innerPath | ||
* @param classDeclarationNode | ||
* @returns is in manual function | ||
*/ | ||
isMemberInManualFunction(innerPath, stopNode) { | ||
let isInFunction = false; | ||
let reversePath = innerPath.parentPath; | ||
while (reversePath && reversePath.node !== stopNode) { | ||
const node = reversePath.node; | ||
const parentNode = reversePath.parentPath?.node; | ||
const isFunction = this.t.isFunctionExpression(node) || this.t.isArrowFunctionExpression(node); | ||
const isManual = this.t.isCallExpression(parentNode) && this.t.isIdentifier(parentNode.callee) && parentNode.callee.name === "manual"; | ||
if (isFunction && isManual) { | ||
isInFunction = true; | ||
break; | ||
} | ||
reversePath = reversePath.parentPath; | ||
} | ||
return isInFunction; | ||
} | ||
/** | ||
* @brief Generate a random string | ||
* @param length | ||
* @returns random string | ||
*/ | ||
static uid(length = 4) { | ||
return Math.random().toString(32).slice(2, length + 2); | ||
} | ||
}; | ||
var PluginProvider = _PluginProvider; | ||
// ---- Const Level | ||
__publicField(PluginProvider, "defaultHTMLTags", [ | ||
"a", | ||
"abbr", | ||
"address", | ||
"area", | ||
"article", | ||
"aside", | ||
"audio", | ||
"b", | ||
"base", | ||
"bdi", | ||
"bdo", | ||
"blockquote", | ||
"body", | ||
"br", | ||
"button", | ||
"canvas", | ||
"caption", | ||
"cite", | ||
"code", | ||
"col", | ||
"colgroup", | ||
"data", | ||
"datalist", | ||
"dd", | ||
"del", | ||
"details", | ||
"dfn", | ||
"dialog", | ||
"div", | ||
"dl", | ||
"dt", | ||
"em", | ||
"embed", | ||
"fieldset", | ||
"figcaption", | ||
"figure", | ||
"footer", | ||
"form", | ||
"h1", | ||
"h2", | ||
"h3", | ||
"h4", | ||
"h5", | ||
"h6", | ||
"head", | ||
"header", | ||
"hgroup", | ||
"hr", | ||
"html", | ||
"i", | ||
"iframe", | ||
"img", | ||
"input", | ||
"ins", | ||
"kbd", | ||
"label", | ||
"legend", | ||
"li", | ||
"link", | ||
"main", | ||
"map", | ||
"mark", | ||
"menu", | ||
"meta", | ||
"meter", | ||
"nav", | ||
"noscript", | ||
"object", | ||
"ol", | ||
"optgroup", | ||
"option", | ||
"output", | ||
"p", | ||
"picture", | ||
"pre", | ||
"progress", | ||
"q", | ||
"rp", | ||
"rt", | ||
"ruby", | ||
"s", | ||
"samp", | ||
"script", | ||
"section", | ||
"select", | ||
"slot", | ||
"small", | ||
"source", | ||
"span", | ||
"strong", | ||
"style", | ||
"sub", | ||
"summary", | ||
"sup", | ||
"table", | ||
"tbody", | ||
"td", | ||
"template", | ||
"textarea", | ||
"tfoot", | ||
"th", | ||
"thead", | ||
"time", | ||
"title", | ||
"tr", | ||
"track", | ||
"u", | ||
"ul", | ||
"var", | ||
"video", | ||
"wbr", | ||
"acronym", | ||
"applet", | ||
"basefont", | ||
"bgsound", | ||
"big", | ||
"blink", | ||
"center", | ||
"dir", | ||
"font", | ||
"frame", | ||
"frameset", | ||
"isindex", | ||
"keygen", | ||
"listing", | ||
"marquee", | ||
"menuitem", | ||
"multicol", | ||
"nextid", | ||
"nobr", | ||
"noembed", | ||
"noframes", | ||
"param", | ||
"plaintext", | ||
"rb", | ||
"rtc", | ||
"spacer", | ||
"strike", | ||
"tt", | ||
"xmp", | ||
"animate", | ||
"animateMotion", | ||
"animateTransform", | ||
"circle", | ||
"clipPath", | ||
"defs", | ||
"desc", | ||
"ellipse", | ||
"feBlend", | ||
"feColorMatrix", | ||
"feComponentTransfer", | ||
"feComposite", | ||
"feConvolveMatrix", | ||
"feDiffuseLighting", | ||
"feDisplacementMap", | ||
"feDistantLight", | ||
"feDropShadow", | ||
"feFlood", | ||
"feFuncA", | ||
"feFuncB", | ||
"feFuncG", | ||
"feFuncR", | ||
"feGaussianBlur", | ||
"feImage", | ||
"feMerge", | ||
"feMergeNode", | ||
"feMorphology", | ||
"feOffset", | ||
"fePointLight", | ||
"feSpecularLighting", | ||
"feSpotLight", | ||
"feTile", | ||
"feTurbulence", | ||
"filter", | ||
"foreignObject", | ||
"g", | ||
"image", | ||
"line", | ||
"linearGradient", | ||
"marker", | ||
"mask", | ||
"metadata", | ||
"mpath", | ||
"path", | ||
"pattern", | ||
"polygon", | ||
"polyline", | ||
"radialGradient", | ||
"rect", | ||
"set", | ||
"stop", | ||
"svg", | ||
"switch", | ||
"symbol", | ||
"text", | ||
"textPath", | ||
"tspan", | ||
"use", | ||
"view" | ||
]); | ||
__publicField(PluginProvider, "availableDecoNames", ["Static", "Prop", "Env", "Content", "Children"]); | ||
__publicField(PluginProvider, "dlightDefaultPackageName", "@dlightjs/dlight"); | ||
__publicField(PluginProvider, "importMap", Object.fromEntries( | ||
[ | ||
"createTemplate", | ||
"setStyle", | ||
"setDataset", | ||
"setMemorizedEvent", | ||
"setHTMLProp", | ||
"setHTMLAttr", | ||
"setHTMLProps", | ||
"setHTMLAttrs", | ||
"insertNode", | ||
"createElement", | ||
"ForNode", | ||
"IfNode", | ||
"EnvNode", | ||
"createTextNode", | ||
"updateText", | ||
"ExpNode", | ||
"PropView" | ||
].map( | ||
(funcName, idx) => devMode ? [funcName, funcName] : [funcName, `$${idx}$`] | ||
) | ||
)); | ||
__publicField(PluginProvider, "escapeNamings", ["escape", "$"]); | ||
var PluginProviderClass = PluginProvider; | ||
// src/plugin.ts | ||
function plugin_default(api, options) { | ||
const { types } = api; | ||
const { | ||
files = "**/*.{js,jsx,ts,tsx}", | ||
excludeFiles = "**/{dist,node_modules,lib}/*.{js,ts}", | ||
enableDevTools = false, | ||
htmlTags = (defaultHtmlTags) => defaultHtmlTags | ||
} = options; | ||
const pluginProvider = new PluginProviderClass( | ||
api, | ||
types, | ||
Array.isArray(files) ? files : [files], | ||
Array.isArray(excludeFiles) ? excludeFiles : [excludeFiles], | ||
enableDevTools, | ||
htmlTags | ||
); | ||
return { | ||
visitor: { | ||
Program(path, { filename }) { | ||
return pluginProvider.programVisitor(path, filename); | ||
}, | ||
ClassDeclaration: { | ||
enter: pluginProvider.classEnter.bind(pluginProvider), | ||
exit: pluginProvider.classExit.bind(pluginProvider) | ||
}, | ||
ClassExpression: { | ||
enter: pluginProvider.classEnter.bind(pluginProvider), | ||
exit: pluginProvider.classExit.bind(pluginProvider) | ||
}, | ||
ClassMethod: pluginProvider.classMethodVisitor.bind(pluginProvider), | ||
ClassProperty: pluginProvider.classPropertyVisitor.bind(pluginProvider) | ||
} | ||
}; | ||
} | ||
// src/index.ts | ||
function src_default(_, options) { | ||
return { | ||
plugins: [ | ||
syntaxTypescript, | ||
[syntaxDecorators.default ?? syntaxDecorators, { legacy: true }], | ||
[plugin_default, options] | ||
] | ||
}; | ||
} | ||
export { | ||
src_default as default | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "babel-preset-dlight", | ||
"version": "0.10.6", | ||
"description": "DLight transpiler for babel", | ||
"version": "1.0.0-alpha.0", | ||
"description": "DLight transpiler babel preset", | ||
"author": { | ||
@@ -22,13 +22,27 @@ "name": "IanDx", | ||
"devDependencies": { | ||
"tsup": "^6.5.0", | ||
"typescript": "^5.1.6" | ||
"@types/babel__core": "^7.20.5", | ||
"@types/node": "^20.10.5", | ||
"tsup": "^6.7.0", | ||
"typescript": "^5.3.2" | ||
}, | ||
"dependencies": { | ||
"@babel/types": "^7.20.7", | ||
"@babel/helper-plugin-utils": "^7.22.5", | ||
"@babel/plugin-syntax-decorators": "^7.21.0", | ||
"@babel/plugin-syntax-do-expressions": "^7.18.6", | ||
"@babel/plugin-syntax-decorators": "^7.23.3", | ||
"minimatch": "^9.0.3", | ||
"@dlightjs/view-parser": "0.10.1" | ||
"babel-plugin-syntax-typescript-new": "1.0.0", | ||
"@dlightjs/reactivity-parser": "1.0.0-alpha.0", | ||
"@dlightjs/view-generator": "1.0.0-alpha.0", | ||
"@dlightjs/view-parser": "1.0.0-alpha.0" | ||
}, | ||
"tsup": { | ||
"entry": [ | ||
"src/index.ts" | ||
], | ||
"format": [ | ||
"cjs", | ||
"esm" | ||
], | ||
"clean": true, | ||
"dts": true, | ||
"minify": true | ||
}, | ||
"scripts": { | ||
@@ -35,0 +49,0 @@ "build": "tsup --sourcemap" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
2755
2
223864
4
3
1
+ Added@dlightjs/error-handler@1.0.0-alpha.0(transitive)
+ Added@dlightjs/reactivity-parser@1.0.0-alpha.0(transitive)
+ Added@dlightjs/view-generator@1.0.0-alpha.0(transitive)
+ Added@dlightjs/view-parser@1.0.0-alpha.0(transitive)
+ Addedbabel-plugin-syntax-typescript-new@1.0.0(transitive)
- Removed@babel/helper-plugin-utils@^7.22.5
- Removed@babel/types@^7.20.7
- Removed@babel/plugin-syntax-do-expressions@7.25.9(transitive)
- Removed@dlightjs/view-parser@0.10.1(transitive)