babel-plugin-essor 0.0.14-beta.9 → 0.0.15-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -17
- package/dist/index.cjs +2169 -576
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -18
- package/dist/index.d.ts +2 -18
- package/dist/index.js +2166 -573
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,695 +1,2288 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
2
4
|
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
6
|
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
7
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
-
var __spreadValues = (
|
|
8
|
+
var __spreadValues = (a2, b) => {
|
|
7
9
|
for (var prop in b || (b = {}))
|
|
8
10
|
if (__hasOwnProp.call(b, prop))
|
|
9
|
-
__defNormalProp(
|
|
11
|
+
__defNormalProp(a2, prop, b[prop]);
|
|
10
12
|
if (__getOwnPropSymbols)
|
|
11
13
|
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
14
|
if (__propIsEnum.call(b, prop))
|
|
13
|
-
__defNormalProp(
|
|
15
|
+
__defNormalProp(a2, prop, b[prop]);
|
|
14
16
|
}
|
|
15
|
-
return
|
|
17
|
+
return a2;
|
|
16
18
|
};
|
|
19
|
+
var __spreadProps = (a2, b) => __defProps(a2, __getOwnPropDescs(b));
|
|
17
20
|
|
|
18
|
-
// src/
|
|
19
|
-
import { types as
|
|
21
|
+
// src/program.ts
|
|
22
|
+
import { types as t2 } from "@babel/core";
|
|
20
23
|
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
var
|
|
27
|
-
function
|
|
28
|
-
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
return str.indexOf(searchString) === 0;
|
|
24
|
+
// src/import.ts
|
|
25
|
+
import { types as t } from "@babel/core";
|
|
26
|
+
|
|
27
|
+
// ../shared/dist/shared.esm.js
|
|
28
|
+
var w = (e) => e !== null && typeof e == "object";
|
|
29
|
+
var d = Array.isArray;
|
|
30
|
+
function m(e) {
|
|
31
|
+
return typeof e == "string";
|
|
32
32
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
function T(e) {
|
|
34
|
+
return typeof e == "number";
|
|
35
|
+
}
|
|
36
|
+
function y(e) {
|
|
37
|
+
return e === null;
|
|
38
|
+
}
|
|
39
|
+
function A(e) {
|
|
40
|
+
return typeof e == "symbol";
|
|
41
|
+
}
|
|
42
|
+
function E(e) {
|
|
43
|
+
return a.call(e) === "[object Map]";
|
|
44
|
+
}
|
|
45
|
+
function N(e) {
|
|
46
|
+
return e == null;
|
|
47
|
+
}
|
|
48
|
+
var j = (e) => ["string", "number", "boolean", "symbol", "undefined"].includes(typeof e) || y(e);
|
|
49
|
+
var P = (e) => a.call(e) === "[object Object]";
|
|
50
|
+
function _(e) {
|
|
51
|
+
return typeof e == "undefined";
|
|
52
|
+
}
|
|
53
|
+
function z(e) {
|
|
54
|
+
return typeof e == "boolean";
|
|
55
|
+
}
|
|
56
|
+
var a = Object.prototype.toString;
|
|
57
|
+
function G(e) {
|
|
58
|
+
return d(e) ? e : [e];
|
|
59
|
+
}
|
|
60
|
+
var V = Function.prototype;
|
|
61
|
+
function q(e, t10) {
|
|
62
|
+
return m(e) ? e.indexOf(t10) === 0 : false;
|
|
63
|
+
}
|
|
64
|
+
var u = (e) => {
|
|
65
|
+
let t10 = /* @__PURE__ */ Object.create(null);
|
|
66
|
+
return ((n) => t10[n] || (t10[n] = e(n)));
|
|
67
|
+
};
|
|
68
|
+
var W = Object.freeze({});
|
|
69
|
+
var Y = Object.freeze([]);
|
|
70
|
+
var J = /\B([A-Z])/g;
|
|
71
|
+
var Z = u((e) => e.replaceAll(J, "-$1").toLowerCase());
|
|
72
|
+
var Q = /[_-](\w)/g;
|
|
73
|
+
var ee = u((e) => (e = e.replaceAll(/^[_-]+|[_-]+$/g, ""), e = e.replaceAll(/[_-]+/g, "-"), e.replaceAll(Q, (t10, n) => n.toUpperCase())));
|
|
74
|
+
var te = u((e) => e.charAt(0).toUpperCase() + e.slice(1));
|
|
75
|
+
function ne(e, ...t10) {
|
|
76
|
+
console.warn(`[Essor warn]: ${e}`, ...t10);
|
|
77
|
+
}
|
|
78
|
+
function h(e, ...t10) {
|
|
79
|
+
console.error(`[Essor error]: ${e}`, ...t10);
|
|
80
|
+
}
|
|
81
|
+
function o(e) {
|
|
82
|
+
let t10 = /* @__PURE__ */ Object.create(null);
|
|
83
|
+
for (let n of e.split(",")) t10[n] = 1;
|
|
84
|
+
return (n) => n in t10;
|
|
85
|
+
}
|
|
86
|
+
var x = "itemscope,allowfullscreen,formnovalidate,ismap,nomodule,novalidate,readonly";
|
|
87
|
+
var pe = o(x);
|
|
88
|
+
var ue = o(`${x},async,autofocus,autoplay,controls,default,defer,disabled,hidden,inert,loop,open,required,reversed,scoped,seamless,checked,muted,multiple,selected`);
|
|
89
|
+
var he = o("accept,accept-charset,accesskey,action,align,allow,alt,async,autocapitalize,autocomplete,autofocus,autoplay,background,bgcolor,border,buffered,capture,challenge,charset,checked,cite,class,code,codebase,color,cols,colspan,content,contenteditable,contextmenu,controls,coords,crossorigin,csp,data,datetime,decoding,default,defer,dir,dirname,disabled,download,draggable,dropzone,enctype,enterkeyhint,for,form,formaction,formenctype,formmethod,formnovalidate,formtarget,headers,height,hidden,high,href,hreflang,http-equiv,icon,id,importance,inert,integrity,ismap,itemprop,keytype,kind,label,lang,language,loading,list,loop,low,manifest,max,maxlength,minlength,media,min,multiple,muted,name,novalidate,open,optimum,pattern,ping,placeholder,poster,preload,radiogroup,readonly,referrerpolicy,rel,required,reversed,rows,rowspan,sandbox,scope,scoped,selected,shape,size,sizes,slot,span,spellcheck,src,srcdoc,srclang,srcset,start,step,style,summary,tabindex,target,title,translate,type,usemap,value,width,wrap");
|
|
90
|
+
var be = o("xmlns,accent-height,accumulate,additive,alignment-baseline,alphabetic,amplitude,arabic-form,ascent,attributeName,attributeType,azimuth,baseFrequency,baseline-shift,baseProfile,bbox,begin,bias,by,calcMode,cap-height,class,clip,clipPathUnits,clip-path,clip-rule,color,color-interpolation,color-interpolation-filters,color-profile,color-rendering,contentScriptType,contentStyleType,crossorigin,cursor,cx,cy,d,decelerate,descent,diffuseConstant,direction,display,divisor,dominant-baseline,dur,dx,dy,edgeMode,elevation,enable-background,end,exponent,fill,fill-opacity,fill-rule,filter,filterRes,filterUnits,flood-color,flood-opacity,font-family,font-size,font-size-adjust,font-stretch,font-style,font-variant,font-weight,format,from,fr,fx,fy,g1,g2,glyph-name,glyph-orientation-horizontal,glyph-orientation-vertical,glyphRef,gradientTransform,gradientUnits,hanging,height,href,hreflang,horiz-adv-x,horiz-origin-x,id,ideographic,image-rendering,in,in2,intercept,k,k1,k2,k3,k4,kernelMatrix,kernelUnitLength,kerning,keyPoints,keySplines,keyTimes,lang,lengthAdjust,letter-spacing,lighting-color,limitingConeAngle,local,marker-end,marker-mid,marker-start,markerHeight,markerUnits,markerWidth,mask,maskContentUnits,maskUnits,mathematical,max,media,method,min,mode,name,numOctaves,offset,opacity,operator,order,orient,orientation,origin,overflow,overline-position,overline-thickness,panose-1,paint-order,path,pathLength,patternContentUnits,patternTransform,patternUnits,ping,pointer-events,points,pointsAtX,pointsAtY,pointsAtZ,preserveAlpha,preserveAspectRatio,primitiveUnits,r,radius,referrerPolicy,refX,refY,rel,rendering-intent,repeatCount,repeatDur,requiredExtensions,requiredFeatures,restart,result,rotate,rx,ry,scale,seed,shape-rendering,slope,spacing,specularConstant,specularExponent,speed,spreadMethod,startOffset,stdDeviation,stemh,stemv,stitchTiles,stop-color,stop-opacity,strikethrough-position,strikethrough-thickness,string,stroke,stroke-dasharray,stroke-dashoffset,stroke-linecap,stroke-linejoin,stroke-miterlimit,stroke-opacity,stroke-width,style,surfaceScale,systemLanguage,tabindex,tableValues,target,targetX,targetY,text-anchor,text-decoration,text-rendering,textLength,to,transform,transform-origin,type,u1,u2,underline-position,underline-thickness,unicode,unicode-bidi,unicode-range,units-per-em,v-alphabetic,v-hanging,v-ideographic,v-mathematical,values,vector-effect,version,vert-adv-y,vert-origin-x,vert-origin-y,viewBox,viewTarget,visibility,width,widths,word-spacing,writing-mode,x,x-height,x1,x2,xChannelSelector,xlink:actuate,xlink:arcrole,xlink:href,xlink:role,xlink:show,xlink:title,xlink:type,xmlns:xlink,xml:base,xml:lang,xml:space,y,y1,y2,yChannelSelector,z,zoomAndPan");
|
|
91
|
+
var ke = "html,body,base,head,link,meta,style,title,address,article,aside,footer,header,hgroup,h1,h2,h3,h4,h5,h6,nav,section,div,dd,dl,dt,figcaption,figure,picture,hr,img,li,main,ol,p,pre,ul,a,b,abbr,bdi,bdo,br,cite,code,data,dfn,em,i,kbd,mark,q,rp,rt,ruby,s,samp,small,span,strong,sub,sup,time,u,var,wbr,area,audio,map,track,video,embed,object,param,source,canvas,script,noscript,del,ins,caption,col,colgroup,table,thead,tbody,td,th,tr,button,datalist,fieldset,form,input,label,legend,meter,optgroup,option,output,progress,select,textarea,details,dialog,menu,summary,template,blockquote,iframe,tfoot";
|
|
92
|
+
var xe = "svg,animate,animateMotion,animateTransform,circle,clipPath,color-profile,defs,desc,discard,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,hatch,hatchpath,image,line,linearGradient,marker,mask,mesh,meshgradient,meshpatch,meshrow,metadata,mpath,path,pattern,polygon,polyline,radialGradient,rect,set,solidcolor,stop,switch,symbol,text,textPath,title,tspan,unknown,use,view";
|
|
93
|
+
var we = "annotation,annotation-xml,maction,maligngroup,malignmark,math,menclose,merror,mfenced,mfrac,mfraction,mglyph,mi,mlabeledtr,mlongdiv,mmultiscripts,mn,mo,mover,mpadded,mphantom,mprescripts,mroot,mrow,ms,mscarries,mscarry,msgroup,msline,mspace,msqrt,msrow,mstack,mstyle,msub,msubsup,msup,mtable,mtd,mtext,mtr,munder,munderover,none,semantics";
|
|
94
|
+
var ve = "beforeinput,click,dblclick,contextmenu,focusin,focusout,input,keydown,keyup,mousedown,mousemove,mouseout,mouseover,mouseup,pointerdown,pointermove,pointerout,pointerover,pointerup,touchend,touchmove,touchstart";
|
|
95
|
+
var Te = "area,base,br,col,embed,hr,img,input,link,meta,param,source,track,wbr";
|
|
96
|
+
var Ae = "area,base,br,col,embed,hr,img,input,link,meta,param,source,track,wbr";
|
|
97
|
+
var Se = o(ke);
|
|
98
|
+
var Me = o(xe);
|
|
99
|
+
var Ce = o(we);
|
|
100
|
+
var Ee = o(Te);
|
|
101
|
+
var Ne = o(Ae);
|
|
102
|
+
var Oe = o(ve);
|
|
103
|
+
|
|
104
|
+
// src/constants.ts
|
|
105
|
+
var DEFAULT_OPTIONS = {
|
|
106
|
+
mode: "client",
|
|
107
|
+
symbol: "$",
|
|
108
|
+
props: true,
|
|
109
|
+
hmr: false,
|
|
110
|
+
styled: false
|
|
111
|
+
};
|
|
112
|
+
var IMPORTS_MAPS = [
|
|
113
|
+
// Reactive API
|
|
114
|
+
"signal",
|
|
115
|
+
"computed",
|
|
116
|
+
"reactive",
|
|
117
|
+
"memoEffect",
|
|
118
|
+
"omitProps",
|
|
119
|
+
// component
|
|
120
|
+
"createComponent",
|
|
121
|
+
"Fragment",
|
|
122
|
+
"For",
|
|
123
|
+
"Suspense",
|
|
124
|
+
"Portal",
|
|
125
|
+
// Template related
|
|
126
|
+
"mapNodes",
|
|
127
|
+
"template",
|
|
128
|
+
"delegateEvents",
|
|
129
|
+
// styled
|
|
130
|
+
"styled",
|
|
131
|
+
// binding related
|
|
132
|
+
"insert",
|
|
133
|
+
"patchStyle",
|
|
134
|
+
"patchClass",
|
|
135
|
+
"patchAttr",
|
|
136
|
+
"bindElement",
|
|
137
|
+
"setSpread",
|
|
138
|
+
"addEventListener",
|
|
139
|
+
// rendering related
|
|
140
|
+
"render",
|
|
141
|
+
"escapeHTML",
|
|
142
|
+
"getHydrationKey"
|
|
143
|
+
];
|
|
144
|
+
var SSG_IMPORTS_MAPS = {
|
|
145
|
+
createComponent: "createSSGComponent",
|
|
146
|
+
patchAttr: "setSSGAttr"
|
|
147
|
+
};
|
|
148
|
+
var SSR_IMPORTS_MAPS = {
|
|
149
|
+
mapNodes: "mapSSRNodes",
|
|
150
|
+
template: "getElement"
|
|
39
151
|
};
|
|
40
|
-
var
|
|
41
|
-
|
|
42
|
-
|
|
152
|
+
var TRANSFORM_PROPERTY_NAME = "__props";
|
|
153
|
+
|
|
154
|
+
// src/import.ts
|
|
155
|
+
function createImportIdentifiers(path) {
|
|
156
|
+
const identifiers = IMPORTS_MAPS.reduce((acc, name) => {
|
|
157
|
+
var _a, _b;
|
|
158
|
+
const identifier = (_b = (_a = path.scope) == null ? void 0 : _a.generateUidIdentifier(`${name}$`)) != null ? _b : t.identifier(`${name}$`);
|
|
159
|
+
if (!identifier) {
|
|
160
|
+
throw new Error(`Failed to generate identifier for ${name}`);
|
|
161
|
+
}
|
|
162
|
+
acc[name] = identifier;
|
|
163
|
+
return acc;
|
|
164
|
+
}, {});
|
|
165
|
+
return identifiers;
|
|
166
|
+
}
|
|
167
|
+
var importMap = IMPORTS_MAPS.reduce(
|
|
168
|
+
(acc, name) => __spreadProps(__spreadValues({}, acc), { [name]: name }),
|
|
169
|
+
{}
|
|
43
170
|
);
|
|
44
|
-
var
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
(
|
|
50
|
-
|
|
171
|
+
var importedSets = /* @__PURE__ */ new Set();
|
|
172
|
+
function addImport(name) {
|
|
173
|
+
importedSets.add(name);
|
|
174
|
+
}
|
|
175
|
+
function clearImport() {
|
|
176
|
+
importedSets.clear();
|
|
177
|
+
}
|
|
178
|
+
function createImport(path, imports, from) {
|
|
179
|
+
const state = path.state;
|
|
180
|
+
const mode = state.opts.mode;
|
|
181
|
+
if (!importedSets.size) {
|
|
182
|
+
return;
|
|
51
183
|
}
|
|
52
|
-
|
|
184
|
+
try {
|
|
185
|
+
const importSpecifiers = Array.from(importedSets).map((name) => {
|
|
186
|
+
const importIdentifier = imports[name];
|
|
187
|
+
if (!importIdentifier) {
|
|
188
|
+
throw new Error(`Import identifier not found for: ${name}`);
|
|
189
|
+
}
|
|
190
|
+
const local = t.identifier(importIdentifier.name);
|
|
191
|
+
if (mode === "ssg" /* SSG */) {
|
|
192
|
+
name = SSG_IMPORTS_MAPS[name] || name;
|
|
193
|
+
}
|
|
194
|
+
if (mode === "ssr" /* SSR */) {
|
|
195
|
+
name = SSR_IMPORTS_MAPS[name] || name;
|
|
196
|
+
}
|
|
197
|
+
const imported = t.identifier(name);
|
|
198
|
+
return t.importSpecifier(local, imported);
|
|
199
|
+
});
|
|
200
|
+
const importDeclaration = t.importDeclaration(importSpecifiers, t.stringLiteral(from));
|
|
201
|
+
path.node.body.unshift(importDeclaration);
|
|
202
|
+
} catch (error_) {
|
|
203
|
+
h("Failed to create import declaration:", h);
|
|
204
|
+
throw error_;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
53
207
|
|
|
54
208
|
// src/program.ts
|
|
55
|
-
import { types as t } from "@babel/core";
|
|
56
|
-
var imports = /* @__PURE__ */ new Set();
|
|
57
|
-
var defaultOption = {
|
|
58
|
-
ssg: false,
|
|
59
|
-
symbol: "$",
|
|
60
|
-
props: true
|
|
61
|
-
};
|
|
62
209
|
var transformProgram = {
|
|
63
|
-
enter(path, state) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
};
|
|
210
|
+
enter: (path, state) => {
|
|
211
|
+
const opts = __spreadValues(__spreadValues({}, DEFAULT_OPTIONS), state.opts);
|
|
212
|
+
const imports = createImportIdentifiers(path);
|
|
213
|
+
clearImport();
|
|
214
|
+
path.state = __spreadProps(__spreadValues({}, state), {
|
|
215
|
+
opts,
|
|
216
|
+
imports,
|
|
217
|
+
declarations: [],
|
|
218
|
+
// Collect template declarations during transformation
|
|
219
|
+
filename: state.filename,
|
|
220
|
+
events: /* @__PURE__ */ new Set()
|
|
221
|
+
// Track delegated events for optimization
|
|
222
|
+
});
|
|
76
223
|
},
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
224
|
+
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
225
|
+
exit: (path, state) => {
|
|
226
|
+
const pluginState = path.state;
|
|
227
|
+
const { imports, declarations, events } = pluginState;
|
|
228
|
+
const insertIndex = path.node.body.findIndex(
|
|
229
|
+
(node) => !t2.isImportDeclaration(node) && !t2.isExportDeclaration(node)
|
|
230
|
+
);
|
|
231
|
+
if (declarations == null ? void 0 : declarations.length) {
|
|
232
|
+
const templateDeclaration = t2.variableDeclaration("const", declarations);
|
|
233
|
+
if (insertIndex !== -1) {
|
|
234
|
+
path.node.body.splice(insertIndex, 0, templateDeclaration);
|
|
235
|
+
} else {
|
|
236
|
+
path.node.body.push(templateDeclaration);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (events && events.size > 0) {
|
|
240
|
+
const eventsDeclaration = t2.expressionStatement(
|
|
241
|
+
t2.callExpression(imports.delegateEvents, [
|
|
242
|
+
t2.arrayExpression(Array.from(events).map((event) => t2.stringLiteral(event)))
|
|
243
|
+
])
|
|
244
|
+
);
|
|
245
|
+
addImport(importMap.delegateEvents);
|
|
246
|
+
path.node.body.push(eventsDeclaration);
|
|
247
|
+
}
|
|
248
|
+
createImport(path, imports, "essor");
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// src/signals/props.ts
|
|
253
|
+
import { types as t4 } from "@babel/core";
|
|
254
|
+
|
|
255
|
+
// src/signals/utils.ts
|
|
256
|
+
import { types as t3 } from "@babel/core";
|
|
257
|
+
function isValidPath(path) {
|
|
258
|
+
return !!(path && path.node);
|
|
259
|
+
}
|
|
260
|
+
function isJSXNode(node) {
|
|
261
|
+
return !!(node && (t3.isJSXElement(node) || t3.isJSXFragment(node)));
|
|
262
|
+
}
|
|
263
|
+
function mightReturnJSX(expr) {
|
|
264
|
+
if (isJSXNode(expr)) {
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
if (t3.isConditionalExpression(expr)) {
|
|
268
|
+
return mightReturnJSX(expr.consequent) || mightReturnJSX(expr.alternate);
|
|
269
|
+
}
|
|
270
|
+
if (t3.isLogicalExpression(expr)) {
|
|
271
|
+
return mightReturnJSX(expr.left) || mightReturnJSX(expr.right);
|
|
272
|
+
}
|
|
273
|
+
if (t3.isSequenceExpression(expr)) {
|
|
274
|
+
return expr.expressions.some(mightReturnJSX);
|
|
275
|
+
}
|
|
276
|
+
if (t3.isParenthesizedExpression(expr)) {
|
|
277
|
+
return mightReturnJSX(expr.expression);
|
|
278
|
+
}
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
function checkHasJSXReturn(path) {
|
|
282
|
+
if (!isValidPath(path)) return false;
|
|
283
|
+
try {
|
|
284
|
+
const body = path.get("body");
|
|
285
|
+
if (!isValidPath(body)) return false;
|
|
286
|
+
if (!t3.isBlockStatement(body.node)) {
|
|
287
|
+
return mightReturnJSX(body.node);
|
|
288
|
+
}
|
|
289
|
+
let hasJSX = false;
|
|
290
|
+
body.traverse({
|
|
291
|
+
"ReturnStatement": function(returnPath) {
|
|
292
|
+
if (!hasJSX && returnPath.node.argument && mightReturnJSX(returnPath.node.argument)) {
|
|
293
|
+
hasJSX = true;
|
|
294
|
+
returnPath.stop();
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
"FunctionDeclaration|FunctionExpression|ArrowFunctionExpression": (nestedPath) => {
|
|
298
|
+
nestedPath.skip();
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
return hasJSX;
|
|
302
|
+
} catch (e) {
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
function isMemberAccessingProperty(node, propertyName) {
|
|
307
|
+
if (!node.computed && t3.isIdentifier(node.property) && node.property.name === propertyName) {
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
if (node.computed && t3.isStringLiteral(node.property) && node.property.value === propertyName) {
|
|
311
|
+
return true;
|
|
312
|
+
}
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// src/signals/props.ts
|
|
317
|
+
function transformProperty(path, properties, parentPath, defaultValues = {}) {
|
|
318
|
+
properties.forEach((property, index) => {
|
|
319
|
+
try {
|
|
320
|
+
if (!t4.isObjectProperty(property)) {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if (!t4.isIdentifier(property.key)) {
|
|
324
|
+
if (true) {
|
|
325
|
+
ne("transformObjectProperties: Skipping computed property", { index });
|
|
326
|
+
}
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
const keyName = property.key.name;
|
|
330
|
+
if (t4.isIdentifier(property.value)) {
|
|
331
|
+
path.scope.rename(property.value.name, `${parentPath}.${keyName}`);
|
|
332
|
+
} else if (t4.isAssignmentPattern(property.value)) {
|
|
333
|
+
if (t4.isIdentifier(property.value.left)) {
|
|
334
|
+
defaultValues[keyName] = property.value.right;
|
|
335
|
+
path.scope.rename(property.value.left.name, `${parentPath}.${keyName}`);
|
|
336
|
+
} else if (t4.isObjectPattern(property.value.left)) {
|
|
337
|
+
transformProperty(
|
|
338
|
+
path,
|
|
339
|
+
property.value.left.properties,
|
|
340
|
+
`${parentPath}.${keyName}`,
|
|
341
|
+
defaultValues
|
|
342
|
+
);
|
|
343
|
+
defaultValues[keyName] = property.value.right;
|
|
344
|
+
}
|
|
345
|
+
} else if (t4.isObjectPattern(property.value)) {
|
|
346
|
+
transformProperty(
|
|
347
|
+
path,
|
|
348
|
+
property.value.properties,
|
|
349
|
+
`${parentPath}.${keyName}`,
|
|
350
|
+
defaultValues
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
} catch (error) {
|
|
354
|
+
ne("transformProperty", `Failed to process property at index ${index}`, {
|
|
355
|
+
error: error instanceof Error ? error.message : String(error)
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
return defaultValues;
|
|
360
|
+
}
|
|
361
|
+
function buildRestVariableDeclaration(state, restName, parentPath, excludeProps) {
|
|
362
|
+
const validExcludeProps = excludeProps.filter(Boolean);
|
|
363
|
+
let init;
|
|
364
|
+
const pathParts = parentPath.split(".").filter((part) => part.length > 0);
|
|
365
|
+
let sourceObject = t4.identifier(pathParts[0] || "__props");
|
|
366
|
+
for (let i = 1; i < pathParts.length; i++) {
|
|
367
|
+
sourceObject = t4.memberExpression(sourceObject, t4.identifier(pathParts[i]));
|
|
368
|
+
}
|
|
369
|
+
if (validExcludeProps.length === 0) {
|
|
370
|
+
init = sourceObject;
|
|
371
|
+
} else {
|
|
372
|
+
init = t4.callExpression(state.imports.omitProps, [
|
|
373
|
+
sourceObject,
|
|
374
|
+
t4.arrayExpression(validExcludeProps.map((name) => t4.stringLiteral(name)))
|
|
375
|
+
]);
|
|
376
|
+
}
|
|
377
|
+
return t4.variableDeclaration("const", [t4.variableDeclarator(t4.identifier(restName), init)]);
|
|
378
|
+
}
|
|
379
|
+
function transformRestProperties(path, restProperties, notRestNames = [], nestedRestParams = []) {
|
|
380
|
+
if (!t4.isIdentifier(restProperties.argument)) {
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
const restName = restProperties.argument.name;
|
|
384
|
+
if (notRestNames.length === 0 && nestedRestParams.length === 0) {
|
|
385
|
+
path.node.params[0] = t4.identifier(restName);
|
|
386
|
+
} else {
|
|
387
|
+
const restDeclarations = [];
|
|
388
|
+
if (nestedRestParams.length > 0) {
|
|
389
|
+
for (const nestedRest of nestedRestParams) {
|
|
390
|
+
const nestedRestDeclaration = buildRestVariableDeclaration(
|
|
391
|
+
path.state,
|
|
392
|
+
nestedRest.name,
|
|
393
|
+
nestedRest.parentPath,
|
|
394
|
+
nestedRest.excludeProps
|
|
395
|
+
);
|
|
396
|
+
restDeclarations.push(nestedRestDeclaration);
|
|
397
|
+
if (nestedRest.excludeProps.length > 0) {
|
|
398
|
+
addImport(importMap.omitProps);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
if (restProperties) {
|
|
403
|
+
const restDeclaration = buildRestVariableDeclaration(
|
|
404
|
+
path.state,
|
|
405
|
+
restName,
|
|
406
|
+
TRANSFORM_PROPERTY_NAME,
|
|
407
|
+
notRestNames
|
|
82
408
|
);
|
|
83
|
-
|
|
409
|
+
restDeclarations.push(restDeclaration);
|
|
410
|
+
if (notRestNames.length) {
|
|
411
|
+
addImport(importMap.omitProps);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
for (const declaration of restDeclarations) {
|
|
415
|
+
const body = path.node.body;
|
|
416
|
+
body.body.unshift(declaration);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
function buildDefaultValueObject(defaultValues) {
|
|
421
|
+
if (!P(defaultValues)) {
|
|
422
|
+
return t4.objectExpression([]);
|
|
423
|
+
}
|
|
424
|
+
const properties = [];
|
|
425
|
+
for (const [key, value] of Object.entries(defaultValues)) {
|
|
426
|
+
if (!key) {
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
let propertyValue;
|
|
430
|
+
if (P(value) && !t4.isNode(value)) {
|
|
431
|
+
propertyValue = buildDefaultValueObject(value);
|
|
432
|
+
} else if (t4.isExpression(value)) {
|
|
433
|
+
propertyValue = value;
|
|
434
|
+
} else {
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
properties.push(t4.objectProperty(t4.identifier(key), propertyValue));
|
|
438
|
+
}
|
|
439
|
+
return t4.objectExpression(properties);
|
|
440
|
+
}
|
|
441
|
+
function buildDefaultValue(path, defaultValues) {
|
|
442
|
+
const propsParam = Object.keys(defaultValues).length > 0 ? t4.assignmentPattern(
|
|
443
|
+
t4.identifier(TRANSFORM_PROPERTY_NAME),
|
|
444
|
+
buildDefaultValueObject(defaultValues)
|
|
445
|
+
) : t4.identifier(TRANSFORM_PROPERTY_NAME);
|
|
446
|
+
path.node.params[0] = propsParam;
|
|
447
|
+
}
|
|
448
|
+
function transformProps(path) {
|
|
449
|
+
const firstParam = path.node.params[0];
|
|
450
|
+
if (!firstParam || !t4.isObjectPattern(firstParam) || !checkHasJSXReturn(path)) {
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
const state = path.state;
|
|
454
|
+
const properties = firstParam.properties;
|
|
455
|
+
const signalPrefix = state.opts.symbol || "$";
|
|
456
|
+
const notRestProperties = properties.filter((prop) => !t4.isRestElement(prop));
|
|
457
|
+
const restProperties = properties.find((prop) => t4.isRestElement(prop));
|
|
458
|
+
const notRestNames = notRestProperties.map((prop) => t4.isIdentifier(prop.key) ? prop.key.name : null).filter((name) => name !== null);
|
|
459
|
+
if (true) {
|
|
460
|
+
if (notRestNames.some((name) => q(name, signalPrefix))) {
|
|
461
|
+
ne(
|
|
462
|
+
"transformProps",
|
|
463
|
+
"Property names cannot start with signal prefix",
|
|
464
|
+
notRestNames.filter((name) => q(name, signalPrefix))
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (notRestProperties.length) {
|
|
469
|
+
const defaultValues = transformProperty(path, notRestProperties, TRANSFORM_PROPERTY_NAME);
|
|
470
|
+
buildDefaultValue(path, defaultValues);
|
|
471
|
+
}
|
|
472
|
+
if (restProperties) {
|
|
473
|
+
transformRestProperties(path, restProperties, notRestNames);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// src/jsx/context.ts
|
|
478
|
+
var contextStack = [];
|
|
479
|
+
function getContext() {
|
|
480
|
+
if (!contextStack.length) {
|
|
481
|
+
throw new Error("No active context found. Ensure setContext has been called.");
|
|
482
|
+
}
|
|
483
|
+
return contextStack[contextStack.length - 1];
|
|
484
|
+
}
|
|
485
|
+
function setContext(context) {
|
|
486
|
+
contextStack.push(context);
|
|
487
|
+
}
|
|
488
|
+
function resetContext() {
|
|
489
|
+
contextStack.pop();
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// src/jsx/client.ts
|
|
493
|
+
import { types as t8 } from "@babel/core";
|
|
494
|
+
|
|
495
|
+
// src/signals/symbol.ts
|
|
496
|
+
import { types as t5 } from "@babel/core";
|
|
497
|
+
function isSignal(name) {
|
|
498
|
+
return !!name && name.startsWith("$");
|
|
499
|
+
}
|
|
500
|
+
function replaceSymbol(path) {
|
|
501
|
+
const { init, id } = path.node;
|
|
502
|
+
if (!t5.isIdentifier(id)) {
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
const variableName = id.name;
|
|
506
|
+
if (!isSignal(variableName)) {
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
if (isAlreadySignalCall(init)) {
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
const isComputed = init && (t5.isFunctionExpression(init) || t5.isArrowFunctionExpression(init)) && path.parent.kind === "const";
|
|
513
|
+
const importName = isComputed ? "computed" : "signal";
|
|
514
|
+
const state = path.state;
|
|
515
|
+
const args = init ? [init] : [];
|
|
516
|
+
const newInit = t5.callExpression(t5.identifier(state.imports[importName].name), args);
|
|
517
|
+
addImport(importName);
|
|
518
|
+
path.node.init = newInit;
|
|
519
|
+
}
|
|
520
|
+
function isAlreadySignalCall(init) {
|
|
521
|
+
if (!init || !t5.isCallExpression(init) || !t5.isIdentifier(init.callee)) {
|
|
522
|
+
return false;
|
|
523
|
+
}
|
|
524
|
+
const calleeName = init.callee.name;
|
|
525
|
+
return calleeName === "signal" || calleeName === "computed" || calleeName.includes("signal") || calleeName.includes("computed");
|
|
526
|
+
}
|
|
527
|
+
function symbolIdentifier(path) {
|
|
528
|
+
if (!isValidPath(path)) {
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
const name = path.node.name;
|
|
532
|
+
if (!isSignal(name)) {
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
if (!shouldProcessIdentifier(path, path.parentPath)) {
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
if (isAlreadyValueAccess(path)) {
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
path.replaceWith(t5.memberExpression(t5.identifier(name), t5.identifier("value")));
|
|
542
|
+
}
|
|
543
|
+
function shouldProcessIdentifier(path, parentPath) {
|
|
544
|
+
if (!parentPath) {
|
|
545
|
+
return false;
|
|
546
|
+
}
|
|
547
|
+
const parent = parentPath.node;
|
|
548
|
+
const currentNode = path.node;
|
|
549
|
+
if (t5.isVariableDeclarator(parent) || t5.isArrayPattern(parent) || t5.isObjectPattern(parent)) {
|
|
550
|
+
return false;
|
|
551
|
+
}
|
|
552
|
+
if (t5.isImportSpecifier(parent) || t5.isImportDefaultSpecifier(parent) || t5.isImportNamespaceSpecifier(parent)) {
|
|
553
|
+
return false;
|
|
554
|
+
}
|
|
555
|
+
if (t5.isFunctionDeclaration(parent) || t5.isFunctionExpression(parent) || t5.isArrowFunctionExpression(parent)) {
|
|
556
|
+
return false;
|
|
557
|
+
}
|
|
558
|
+
if (t5.isClassDeclaration(parent) && parent.id === currentNode) {
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
if (t5.isObjectMethod(parent) || t5.isClassMethod(parent)) {
|
|
562
|
+
return false;
|
|
563
|
+
}
|
|
564
|
+
if (t5.isObjectProperty(parent) && parent.key === currentNode) {
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
567
|
+
if (t5.isLabeledStatement(parent) && parent.label === currentNode) {
|
|
568
|
+
return false;
|
|
569
|
+
}
|
|
570
|
+
if ((t5.isBreakStatement(parent) || t5.isContinueStatement(parent)) && parent.label === currentNode) {
|
|
571
|
+
return false;
|
|
572
|
+
}
|
|
573
|
+
return true;
|
|
574
|
+
}
|
|
575
|
+
function isAlreadyValueAccess(path) {
|
|
576
|
+
const parent = path.parent;
|
|
577
|
+
if (t5.isMemberExpression(parent) && parent.object === path.node) {
|
|
578
|
+
return isMemberAccessingProperty(parent, "value");
|
|
579
|
+
}
|
|
580
|
+
if (!t5.isParenthesizedExpression(parent) && !t5.isTSAsExpression(parent) && !t5.isTSNonNullExpression(parent)) {
|
|
581
|
+
return false;
|
|
582
|
+
}
|
|
583
|
+
const ancestorCheck = path.findParent((p) => {
|
|
584
|
+
if (!p.isMemberExpression()) {
|
|
585
|
+
return false;
|
|
586
|
+
}
|
|
587
|
+
const memberExpr = p.node;
|
|
588
|
+
return memberExpr.object === path.node && isMemberAccessingProperty(memberExpr, "value");
|
|
589
|
+
});
|
|
590
|
+
return !!ancestorCheck;
|
|
591
|
+
}
|
|
592
|
+
function symbolAssignment(path) {
|
|
593
|
+
if (!isValidPath(path)) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
const { left } = path.node;
|
|
597
|
+
if (!t5.isIdentifier(left)) {
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
const name = left.name;
|
|
601
|
+
if (!isSignal(name)) {
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
if (isAlreadyValueAssignment(left)) {
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
path.node.left = t5.memberExpression(t5.identifier(name), t5.identifier("value"));
|
|
608
|
+
}
|
|
609
|
+
function isAlreadyValueAssignment(left) {
|
|
610
|
+
return t5.isMemberExpression(left) && isMemberAccessingProperty(left, "value");
|
|
611
|
+
}
|
|
612
|
+
function symbolUpdate(path) {
|
|
613
|
+
if (!isValidPath(path)) {
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
const { argument } = path.node;
|
|
617
|
+
if (!t5.isIdentifier(argument)) {
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
const name = argument.name;
|
|
621
|
+
if (!isSignal(name)) {
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
624
|
+
if (isAlreadyValueUpdate(argument)) {
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
path.node.argument = t5.memberExpression(t5.identifier(name), t5.identifier("value"));
|
|
628
|
+
}
|
|
629
|
+
function isAlreadyValueUpdate(argument) {
|
|
630
|
+
return t5.isMemberExpression(argument) && isMemberAccessingProperty(argument, "value");
|
|
631
|
+
}
|
|
632
|
+
function symbolObjectPattern(path) {
|
|
633
|
+
if (!isValidPath(path)) {
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
const properties = path.node.properties;
|
|
637
|
+
if (!Array.isArray(properties) || properties.length === 0) {
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
for (const property of properties) {
|
|
641
|
+
if (!property) continue;
|
|
642
|
+
if (t5.isObjectProperty(property)) {
|
|
643
|
+
handleObjectProperty(property, path);
|
|
644
|
+
} else if (t5.isRestElement(property)) {
|
|
645
|
+
handleRestElement(property, path);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
function handleObjectProperty(property, parentPath) {
|
|
650
|
+
if (!property || !property.value) {
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
const value = property.value;
|
|
654
|
+
if (t5.isObjectPattern(value)) {
|
|
655
|
+
const mockPath = {
|
|
656
|
+
node: value,
|
|
657
|
+
state: parentPath.state,
|
|
658
|
+
parentPath
|
|
659
|
+
};
|
|
660
|
+
symbolObjectPattern(mockPath);
|
|
661
|
+
} else if (t5.isArrayPattern(value)) {
|
|
662
|
+
const mockPath = {
|
|
663
|
+
node: value,
|
|
664
|
+
state: parentPath.state,
|
|
665
|
+
parentPath
|
|
666
|
+
};
|
|
667
|
+
symbolArrayPattern(mockPath);
|
|
668
|
+
} else if (t5.isAssignmentPattern(value)) {
|
|
669
|
+
handleAssignmentPattern(value, parentPath);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
function handleAssignmentPattern(pattern, parentPath) {
|
|
673
|
+
const left = pattern.left;
|
|
674
|
+
if (t5.isObjectPattern(left)) {
|
|
675
|
+
const mockPath = {
|
|
676
|
+
node: left,
|
|
677
|
+
state: parentPath.state,
|
|
678
|
+
parentPath
|
|
679
|
+
};
|
|
680
|
+
symbolObjectPattern(mockPath);
|
|
681
|
+
} else if (t5.isArrayPattern(left)) {
|
|
682
|
+
const mockPath = {
|
|
683
|
+
node: left,
|
|
684
|
+
state: parentPath.state,
|
|
685
|
+
parentPath
|
|
686
|
+
};
|
|
687
|
+
symbolArrayPattern(mockPath);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
function handleRestElement(restElement, parentPath) {
|
|
691
|
+
if (!restElement || !restElement.argument) {
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
const argument = restElement.argument;
|
|
695
|
+
if (t5.isObjectPattern(argument)) {
|
|
696
|
+
const mockPath = {
|
|
697
|
+
node: argument,
|
|
698
|
+
state: parentPath.state,
|
|
699
|
+
parentPath
|
|
700
|
+
};
|
|
701
|
+
symbolObjectPattern(mockPath);
|
|
702
|
+
} else if (t5.isArrayPattern(argument)) {
|
|
703
|
+
const mockPath = {
|
|
704
|
+
node: argument,
|
|
705
|
+
state: parentPath.state,
|
|
706
|
+
parentPath
|
|
707
|
+
};
|
|
708
|
+
symbolArrayPattern(mockPath);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
function symbolArrayPattern(path) {
|
|
712
|
+
if (!isValidPath(path)) {
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
const elements = path.node.elements;
|
|
716
|
+
if (!Array.isArray(elements) || elements.length === 0) {
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
for (const element of elements) {
|
|
720
|
+
if (!element) continue;
|
|
721
|
+
if (t5.isAssignmentPattern(element)) {
|
|
722
|
+
handleAssignmentPattern(element, path);
|
|
723
|
+
} else if (t5.isRestElement(element)) {
|
|
724
|
+
handleArrayRestElement(element, path);
|
|
725
|
+
} else if (t5.isObjectPattern(element)) {
|
|
726
|
+
const mockPath = {
|
|
727
|
+
node: element,
|
|
728
|
+
state: path.state,
|
|
729
|
+
parentPath: path
|
|
730
|
+
};
|
|
731
|
+
symbolObjectPattern(mockPath);
|
|
732
|
+
} else if (t5.isArrayPattern(element)) {
|
|
733
|
+
const mockPath = {
|
|
734
|
+
node: element,
|
|
735
|
+
state: path.state,
|
|
736
|
+
parentPath: path
|
|
737
|
+
};
|
|
738
|
+
symbolArrayPattern(mockPath);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
function handleArrayRestElement(restElement, parentPath) {
|
|
743
|
+
if (!restElement || !restElement.argument) {
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
const argument = restElement.argument;
|
|
747
|
+
if (t5.isArrayPattern(argument)) {
|
|
748
|
+
const mockPath = {
|
|
749
|
+
node: argument,
|
|
750
|
+
state: parentPath.state,
|
|
751
|
+
parentPath
|
|
752
|
+
};
|
|
753
|
+
symbolArrayPattern(mockPath);
|
|
754
|
+
} else if (t5.isObjectPattern(argument)) {
|
|
755
|
+
const mockPath = {
|
|
756
|
+
node: argument,
|
|
757
|
+
state: parentPath.state,
|
|
758
|
+
parentPath
|
|
759
|
+
};
|
|
760
|
+
symbolObjectPattern(mockPath);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// src/jsx/constants.ts
|
|
765
|
+
var CLASS_NAME = "class";
|
|
766
|
+
var STYLE_NAME = "style";
|
|
767
|
+
var EVENT_ATTR_NAME = "on";
|
|
768
|
+
var UPDATE_PREFIX = "update";
|
|
769
|
+
var FRAGMENT_NAME = "Fragment";
|
|
770
|
+
var CHILDREN_NAME = "children";
|
|
771
|
+
var SPREAD_NAME = "_$spread$";
|
|
772
|
+
var CREATE_COMPONENT_NAME = "createComponent";
|
|
773
|
+
var BIND_REG = /^bind:.+/;
|
|
774
|
+
var BUILT_IN_COMPONENTS = ["Fragment", "Portal", "Suspense", "For"];
|
|
775
|
+
|
|
776
|
+
// src/jsx/shared.ts
|
|
777
|
+
import { types as t7 } from "@babel/core";
|
|
778
|
+
|
|
779
|
+
// src/jsx/tree.ts
|
|
780
|
+
import { types as t6 } from "@babel/core";
|
|
781
|
+
function createDefaultTree() {
|
|
782
|
+
return {
|
|
783
|
+
root: true,
|
|
784
|
+
type: 0 /* NORMAL */,
|
|
785
|
+
tag: "",
|
|
786
|
+
props: {},
|
|
787
|
+
children: [],
|
|
788
|
+
index: 1,
|
|
789
|
+
parentIndex: null,
|
|
790
|
+
isLastChild: false,
|
|
791
|
+
selfClosing: false,
|
|
792
|
+
_isTreeNode: true
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
function isTreeNode(node) {
|
|
796
|
+
return !!(node && node._isTreeNode);
|
|
797
|
+
}
|
|
798
|
+
var treeIndex = 1;
|
|
799
|
+
function createTree(path, parentNode) {
|
|
800
|
+
const treeNode = createDefaultTree();
|
|
801
|
+
if (parentNode) {
|
|
802
|
+
const nodeInfo = path.node;
|
|
803
|
+
const isBuiltInComponent = BUILT_IN_COMPONENTS.includes(parentNode.tag);
|
|
804
|
+
const isComponentOrFragment = isComponentName(getTagName(nodeInfo));
|
|
805
|
+
if (!isComponentOrFragment && !isBuiltInComponent) {
|
|
806
|
+
treeNode.index = ++treeIndex;
|
|
807
|
+
}
|
|
808
|
+
if (isBuiltInComponent) {
|
|
809
|
+
treeIndex = 1;
|
|
810
|
+
}
|
|
811
|
+
treeNode.parentIndex = treeIndex;
|
|
812
|
+
} else {
|
|
813
|
+
treeNode.root = true;
|
|
814
|
+
treeIndex = 1;
|
|
815
|
+
}
|
|
816
|
+
if (t6.isJSXText(path.node)) {
|
|
817
|
+
return processJSXText(path, treeNode);
|
|
818
|
+
}
|
|
819
|
+
if (t6.isJSXElement(path.node) || t6.isJSXFragment(path.node)) {
|
|
820
|
+
return processJSXElement(path, treeNode);
|
|
821
|
+
}
|
|
822
|
+
return treeNode;
|
|
823
|
+
}
|
|
824
|
+
function processJSXElement(path, treeNode) {
|
|
825
|
+
const tagName = getTagName(path.node);
|
|
826
|
+
treeNode.tag = tagName;
|
|
827
|
+
treeNode.type = determineNodeType(tagName);
|
|
828
|
+
treeNode.selfClosing = Ne(tagName);
|
|
829
|
+
if (!path.isJSXFragment()) {
|
|
830
|
+
treeNode.props = processProps(path);
|
|
831
|
+
}
|
|
832
|
+
if (!treeNode.selfClosing) {
|
|
833
|
+
processChildren(path, treeNode);
|
|
834
|
+
}
|
|
835
|
+
return treeNode;
|
|
836
|
+
}
|
|
837
|
+
function processProps(path) {
|
|
838
|
+
const props = {};
|
|
839
|
+
const attributes = path.get("openingElement.attributes");
|
|
840
|
+
if (!attributes.length) {
|
|
841
|
+
return props;
|
|
842
|
+
}
|
|
843
|
+
attributes.forEach((attribute) => {
|
|
844
|
+
if (t6.isJSXAttribute(attribute.node)) {
|
|
845
|
+
const name = getAttrName(attribute.node);
|
|
846
|
+
const value = attribute.get("value");
|
|
847
|
+
if (!value.node) {
|
|
848
|
+
props[name] = true;
|
|
849
|
+
} else if (value) {
|
|
850
|
+
if (value.isJSXExpressionContainer()) {
|
|
851
|
+
const expression = value.get("expression");
|
|
852
|
+
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
853
|
+
props[name] = expression.node.value;
|
|
854
|
+
} else if (expression.isJSXElement() || expression.isJSXFragment()) {
|
|
855
|
+
props[name] = expression.node;
|
|
856
|
+
} else if (expression.isExpression()) {
|
|
857
|
+
processPropsExpression(expression, name, props, path);
|
|
858
|
+
}
|
|
859
|
+
} else if (value.isStringLiteral() || value.isNumericLiteral()) {
|
|
860
|
+
props[name] = value.node.value;
|
|
861
|
+
} else if (value.isJSXElement() || value.isJSXFragment()) {
|
|
862
|
+
props[name] = value.node;
|
|
863
|
+
} else if (value.isExpression()) {
|
|
864
|
+
processPropsExpression(value, name, props, path);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
} else if (t6.isJSXSpreadAttribute(attribute.node)) {
|
|
868
|
+
props[SPREAD_NAME] = attribute.get("argument").node;
|
|
869
|
+
}
|
|
870
|
+
});
|
|
871
|
+
return props;
|
|
872
|
+
}
|
|
873
|
+
function processPropsExpression(expression, name, props, path) {
|
|
874
|
+
if (BIND_REG.test(name)) {
|
|
875
|
+
processPropsBind(name, expression, props, path);
|
|
876
|
+
} else {
|
|
877
|
+
props[name] = expression.node;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
function processPropsBind(name, expression, props, path) {
|
|
881
|
+
const value = path.scope.generateUidIdentifier("value");
|
|
882
|
+
const bindName = name.slice(5).toLowerCase();
|
|
883
|
+
props[`${UPDATE_PREFIX}:${bindName}`] = [
|
|
884
|
+
expression.node,
|
|
885
|
+
// Getter: the bound variable
|
|
886
|
+
t6.arrowFunctionExpression(
|
|
887
|
+
[value],
|
|
888
|
+
// Type guard: expression.node must be LVal for bind directive
|
|
889
|
+
t6.isLVal(expression.node) ? t6.assignmentExpression("=", expression.node, value) : t6.assignmentExpression("=", t6.identifier("_"), value)
|
|
890
|
+
// Fallback for invalid LVal
|
|
891
|
+
)
|
|
892
|
+
];
|
|
893
|
+
}
|
|
894
|
+
function processChildren(path, treeNode) {
|
|
895
|
+
const children = path.node.children;
|
|
896
|
+
if (!children || !children.length) {
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
const optimizedChildren = optimizeChildNodes(path.get("children"));
|
|
900
|
+
optimizedChildren.forEach((child) => {
|
|
901
|
+
processChild(child, treeNode, optimizedChildren.length === 1);
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
function processChild(child, treeNode, isLastChild) {
|
|
905
|
+
if (t6.isJSXElement(child.node) || t6.isJSXFragment(child.node)) {
|
|
906
|
+
const childNode = createTree(child, treeNode);
|
|
907
|
+
childNode.isLastChild = isLastChild;
|
|
908
|
+
treeNode.children.push(childNode);
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
if (t6.isJSXExpressionContainer(child.node)) {
|
|
912
|
+
processChildExpressionContainer(
|
|
913
|
+
child,
|
|
914
|
+
treeNode,
|
|
915
|
+
isLastChild
|
|
916
|
+
);
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
if (t6.isJSXText(child.node)) {
|
|
920
|
+
processJSXChildText(child, treeNode, isLastChild);
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
923
|
+
if (t6.isJSXSpreadChild(child.node)) {
|
|
924
|
+
processChildSpread(child, treeNode, isLastChild);
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
function processChildExpressionContainer(child, treeNode, isLastChild) {
|
|
929
|
+
const expression = child.get("expression");
|
|
930
|
+
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
931
|
+
treeNode.children.push({
|
|
932
|
+
type: 2 /* TEXT */,
|
|
933
|
+
children: [String(expression.node.value)],
|
|
934
|
+
index: ++treeIndex,
|
|
935
|
+
// Static text increments index
|
|
936
|
+
parentIndex: treeNode.index,
|
|
937
|
+
isLastChild,
|
|
938
|
+
_isTreeNode: true
|
|
939
|
+
});
|
|
940
|
+
} else if (expression.isJSXElement() || expression.isJSXFragment()) {
|
|
941
|
+
const childNode = createTree(child, treeNode);
|
|
942
|
+
childNode.isLastChild = isLastChild;
|
|
943
|
+
treeNode.children.push(childNode);
|
|
944
|
+
} else if (expression.isExpression()) {
|
|
945
|
+
treeNode.children.push({
|
|
946
|
+
type: 4 /* EXPRESSION */,
|
|
947
|
+
index: treeIndex,
|
|
948
|
+
// Expression doesn't update index because it's dynamic content
|
|
949
|
+
isLastChild,
|
|
950
|
+
// Type guard: expression.node is Expression in JSXExpressionContainer
|
|
951
|
+
children: t6.isExpression(expression.node) ? [expression.node] : [],
|
|
952
|
+
parentIndex: treeNode.index,
|
|
953
|
+
_isTreeNode: true
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
function shouldInsertComment(childrenLength, previousNode) {
|
|
958
|
+
return !!(childrenLength >= 2 && // At least two child nodes
|
|
959
|
+
previousNode && // Previous node exists
|
|
960
|
+
previousNode.type !== 6 /* COMMENT */ && // Previous node is not a comment
|
|
961
|
+
// Previous node is dynamic content
|
|
962
|
+
(previousNode.type === 4 /* EXPRESSION */ || previousNode.type === 1 /* COMPONENT */));
|
|
963
|
+
}
|
|
964
|
+
function processJSXChildText(child, treeNode, isLastChild) {
|
|
965
|
+
const text = textTrim(child.node);
|
|
966
|
+
if (!text) {
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
const childrenLength = treeNode.children.length;
|
|
970
|
+
const previousNode = treeNode.children[childrenLength - 1];
|
|
971
|
+
if (shouldInsertComment(childrenLength, previousNode)) {
|
|
972
|
+
const commentNode = {
|
|
973
|
+
type: 6 /* COMMENT */,
|
|
974
|
+
children: [],
|
|
975
|
+
index: ++treeIndex,
|
|
976
|
+
_isTreeNode: true
|
|
977
|
+
};
|
|
978
|
+
treeNode.children.push(commentNode);
|
|
979
|
+
}
|
|
980
|
+
const textNode = {
|
|
981
|
+
type: 2 /* TEXT */,
|
|
982
|
+
children: [text],
|
|
983
|
+
index: ++treeIndex,
|
|
984
|
+
isLastChild,
|
|
985
|
+
_isTreeNode: true
|
|
986
|
+
};
|
|
987
|
+
treeNode.children.push(textNode);
|
|
988
|
+
}
|
|
989
|
+
function processChildSpread(child, treeNode, isLastChild) {
|
|
990
|
+
const spreadExpression = child.get("expression");
|
|
991
|
+
treeNode.children.push({
|
|
992
|
+
type: 7 /* SPREAD */,
|
|
993
|
+
// Type guard: spreadExpression.node is Expression in JSXSpreadChild
|
|
994
|
+
children: t6.isExpression(spreadExpression.node) ? [spreadExpression.node] : [],
|
|
995
|
+
index: treeIndex,
|
|
996
|
+
// Note: doesn't increment here because this is dynamic content
|
|
997
|
+
parentIndex: treeNode.index,
|
|
998
|
+
isLastChild,
|
|
999
|
+
_isTreeNode: true
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
function determineNodeType(tagName) {
|
|
1003
|
+
const isComponent = isComponentName(tagName);
|
|
1004
|
+
if (isComponent) {
|
|
1005
|
+
return 1 /* COMPONENT */;
|
|
1006
|
+
}
|
|
1007
|
+
if (Me(tagName)) {
|
|
1008
|
+
return 5 /* SVG */;
|
|
1009
|
+
}
|
|
1010
|
+
return 0 /* NORMAL */;
|
|
1011
|
+
}
|
|
1012
|
+
function processJSXText(path, treeNode) {
|
|
1013
|
+
treeNode.type = 2 /* TEXT */;
|
|
1014
|
+
try {
|
|
1015
|
+
const textValue = textTrim(path.node);
|
|
1016
|
+
treeNode.children = textValue ? [textValue] : [];
|
|
1017
|
+
} catch (error) {
|
|
1018
|
+
ne(`Text node processing failed: ${error}`);
|
|
1019
|
+
treeNode.children = [];
|
|
1020
|
+
}
|
|
1021
|
+
return treeNode;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// src/jsx/shared.ts
|
|
1025
|
+
function isComponentName(tagName) {
|
|
1026
|
+
if (!tagName) {
|
|
1027
|
+
return false;
|
|
1028
|
+
}
|
|
1029
|
+
const firstCharCode = tagName.charCodeAt(0);
|
|
1030
|
+
if (firstCharCode >= 65 && firstCharCode <= 90) {
|
|
1031
|
+
return true;
|
|
1032
|
+
}
|
|
1033
|
+
const dotIndex = tagName.indexOf(".");
|
|
1034
|
+
const colonIndex = tagName.indexOf(":");
|
|
1035
|
+
if (dotIndex !== -1 || colonIndex !== -1) {
|
|
1036
|
+
const segments = tagName.split(/[:.]/);
|
|
1037
|
+
const len = segments.length;
|
|
1038
|
+
for (let i = 0; i < len; i++) {
|
|
1039
|
+
const segment = segments[i];
|
|
1040
|
+
if (segment.length > 0) {
|
|
1041
|
+
const segmentCharCode = segment.charCodeAt(0);
|
|
1042
|
+
if (segmentCharCode >= 65 && segmentCharCode <= 90) {
|
|
1043
|
+
return true;
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
return !(firstCharCode >= 97 && firstCharCode <= 122);
|
|
1049
|
+
}
|
|
1050
|
+
var getTagName = (node) => {
|
|
1051
|
+
if (t7.isJSXFragment(node)) {
|
|
1052
|
+
return FRAGMENT_NAME;
|
|
1053
|
+
}
|
|
1054
|
+
const tag = node.openingElement.name;
|
|
1055
|
+
return jsxElementNameToString(tag);
|
|
1056
|
+
};
|
|
1057
|
+
function jsxElementNameToString(node) {
|
|
1058
|
+
var _a, _b, _c, _d;
|
|
1059
|
+
if (t7.isJSXMemberExpression(node)) {
|
|
1060
|
+
return `${jsxElementNameToString(node.object)}.${jsxElementNameToString(node.property)}`;
|
|
1061
|
+
}
|
|
1062
|
+
if (t7.isMemberExpression(node)) {
|
|
1063
|
+
const objectName = jsxElementNameToString(
|
|
1064
|
+
node.object
|
|
1065
|
+
);
|
|
1066
|
+
const propertyName = jsxElementNameToString(
|
|
1067
|
+
node.property
|
|
1068
|
+
);
|
|
1069
|
+
return `${objectName}.${propertyName}`;
|
|
1070
|
+
}
|
|
1071
|
+
if (t7.isJSXIdentifier(node) || t7.isIdentifier(node)) {
|
|
1072
|
+
return node.name;
|
|
1073
|
+
}
|
|
1074
|
+
const namespace = (_b = (_a = node.namespace) == null ? void 0 : _a.name) != null ? _b : "";
|
|
1075
|
+
const name = (_d = (_c = node.name) == null ? void 0 : _c.name) != null ? _d : "";
|
|
1076
|
+
if (!namespace) {
|
|
1077
|
+
return name;
|
|
1078
|
+
}
|
|
1079
|
+
if (!name) {
|
|
1080
|
+
return namespace;
|
|
1081
|
+
}
|
|
1082
|
+
return `${namespace}:${name}`;
|
|
1083
|
+
}
|
|
1084
|
+
function textTrim(node) {
|
|
1085
|
+
if (!node || !node.value) return "";
|
|
1086
|
+
return node.value.trim();
|
|
1087
|
+
}
|
|
1088
|
+
function isValidChild(path) {
|
|
1089
|
+
const regex = /^\s*$/;
|
|
1090
|
+
if (t7.isStringLiteral(path.node)) {
|
|
1091
|
+
return !regex.test(path.node.value);
|
|
1092
|
+
}
|
|
1093
|
+
if (t7.isJSXText(path.node)) {
|
|
1094
|
+
return !regex.test(path.node.value);
|
|
1095
|
+
}
|
|
1096
|
+
return Object.keys(path.node).length > 0;
|
|
1097
|
+
}
|
|
1098
|
+
function getRawNodeText(path) {
|
|
1099
|
+
if (path.isJSXText()) {
|
|
1100
|
+
return path.node.value || "";
|
|
1101
|
+
}
|
|
1102
|
+
if (path.isJSXExpressionContainer()) {
|
|
1103
|
+
const expression = path.get("expression");
|
|
1104
|
+
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
1105
|
+
return String(expression.node.value);
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
return "";
|
|
1109
|
+
}
|
|
1110
|
+
function setNodeText(path, text) {
|
|
1111
|
+
if (path.isJSXText()) {
|
|
1112
|
+
path.node.value = text;
|
|
1113
|
+
}
|
|
1114
|
+
if (path.isJSXExpressionContainer()) {
|
|
1115
|
+
const expression = path.get("expression");
|
|
1116
|
+
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
1117
|
+
expression.replaceWith(t7.stringLiteral(text));
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
function optimizeChildNodes(children) {
|
|
1122
|
+
const mergedWithExpression = /* @__PURE__ */ new WeakSet();
|
|
1123
|
+
return children.reduce((acc, cur) => {
|
|
1124
|
+
var _a, _b, _c, _d;
|
|
1125
|
+
if (isValidChild(cur)) {
|
|
1126
|
+
const lastChild = acc.at(-1);
|
|
1127
|
+
if (!lastChild) {
|
|
1128
|
+
acc.push(cur);
|
|
1129
|
+
return acc;
|
|
1130
|
+
}
|
|
1131
|
+
const lastIsPlainText = (_b = (_a = lastChild.isJSXText) == null ? void 0 : _a.call(lastChild)) != null ? _b : false;
|
|
1132
|
+
const currentIsPlainText = (_d = (_c = cur.isJSXText) == null ? void 0 : _c.call(cur)) != null ? _d : false;
|
|
1133
|
+
if (lastIsPlainText && t7.isJSXExpressionContainer(cur.node) && (cur.get("expression").isStringLiteral() || cur.get("expression").isNumericLiteral()) && !mergedWithExpression.has(lastChild)) {
|
|
1134
|
+
const mergedText = getRawNodeText(lastChild) + getRawNodeText(cur);
|
|
1135
|
+
setNodeText(lastChild, mergedText);
|
|
1136
|
+
mergedWithExpression.add(lastChild);
|
|
1137
|
+
return acc;
|
|
1138
|
+
}
|
|
1139
|
+
if (lastIsPlainText && currentIsPlainText && !mergedWithExpression.has(lastChild)) {
|
|
1140
|
+
const mergedText = getRawNodeText(lastChild) + getRawNodeText(cur);
|
|
1141
|
+
setNodeText(lastChild, mergedText);
|
|
1142
|
+
return acc;
|
|
1143
|
+
}
|
|
1144
|
+
acc.push(cur);
|
|
1145
|
+
}
|
|
1146
|
+
return acc;
|
|
1147
|
+
}, []);
|
|
1148
|
+
}
|
|
1149
|
+
function deepCheckObjectDynamic(node) {
|
|
1150
|
+
if (!t7.isObjectExpression(node)) return false;
|
|
1151
|
+
let dynamic = false;
|
|
1152
|
+
function walk(current) {
|
|
1153
|
+
if (dynamic) return;
|
|
1154
|
+
if (t7.isIdentifier(current) || t7.isCallExpression(current) || t7.isMemberExpression(current) || t7.isConditionalExpression(current) || t7.isTemplateLiteral(current) || t7.isBinaryExpression(current) || t7.isUpdateExpression(current) || t7.isUnaryExpression(current) || t7.isAwaitExpression(current) || t7.isYieldExpression(current) || t7.isTaggedTemplateExpression(current)) {
|
|
1155
|
+
dynamic = true;
|
|
1156
|
+
return;
|
|
1157
|
+
}
|
|
1158
|
+
if (t7.isObjectExpression(current)) {
|
|
1159
|
+
current.properties.forEach((prop) => {
|
|
1160
|
+
if (t7.isObjectProperty(prop)) walk(prop.value);
|
|
1161
|
+
if (t7.isSpreadElement(prop)) walk(prop.argument);
|
|
1162
|
+
});
|
|
1163
|
+
return;
|
|
1164
|
+
}
|
|
1165
|
+
if (t7.isArrayExpression(current)) {
|
|
1166
|
+
current.elements.forEach((el) => el && walk(el));
|
|
1167
|
+
return;
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
walk(node);
|
|
1171
|
+
return dynamic;
|
|
1172
|
+
}
|
|
1173
|
+
function processObjectExpression(propName, objectExpr, propsCollection, isClassOrStyleAttr = false) {
|
|
1174
|
+
let classStyleString = "";
|
|
1175
|
+
if (deepCheckObjectDynamic(objectExpr)) {
|
|
1176
|
+
propsCollection[propName] = objectExpr;
|
|
1177
|
+
} else if (isClassOrStyleAttr) {
|
|
1178
|
+
classStyleString = objectExpr.properties.filter((property) => t7.isObjectProperty(property)).reduce((acc, property) => {
|
|
1179
|
+
const key = t7.isIdentifier(property.key) ? property.key.name : t7.isStringLiteral(property.key) ? property.key.value : String(property.key);
|
|
1180
|
+
if (t7.isStringLiteral(property.value)) {
|
|
1181
|
+
return `${acc}${key}:${property.value.value};`;
|
|
1182
|
+
}
|
|
1183
|
+
return acc;
|
|
1184
|
+
}, "");
|
|
1185
|
+
delete propsCollection[propName];
|
|
1186
|
+
}
|
|
1187
|
+
return classStyleString;
|
|
1188
|
+
}
|
|
1189
|
+
function getAttrName(attribute) {
|
|
1190
|
+
if (t7.isJSXIdentifier(attribute.name)) {
|
|
1191
|
+
return attribute.name.name;
|
|
1192
|
+
}
|
|
1193
|
+
if (t7.isJSXNamespacedName(attribute.name)) {
|
|
1194
|
+
return `${attribute.name.namespace.name}:${attribute.name.name.name}`;
|
|
1195
|
+
}
|
|
1196
|
+
return "";
|
|
1197
|
+
}
|
|
1198
|
+
function serializeAttributes(attributes) {
|
|
1199
|
+
if (!attributes || !w(attributes)) {
|
|
1200
|
+
return "";
|
|
1201
|
+
}
|
|
1202
|
+
let attributesString = "";
|
|
1203
|
+
let classNames = "";
|
|
1204
|
+
let styleString = "";
|
|
1205
|
+
for (const [attrName, attrValue] of Object.entries(attributes)) {
|
|
1206
|
+
if (attrName === CLASS_NAME && m(attrValue)) {
|
|
1207
|
+
classNames += ` ${attrValue}`;
|
|
1208
|
+
delete attributes[attrName];
|
|
1209
|
+
} else if (attrName === STYLE_NAME && m(attrValue)) {
|
|
1210
|
+
styleString += `${attrValue}${attrValue.at(-1) === ";" ? "" : ";"}`;
|
|
1211
|
+
delete attributes[attrName];
|
|
1212
|
+
} else if (attrValue === true) {
|
|
1213
|
+
attributesString += ` ${attrName}`;
|
|
1214
|
+
delete attributes[attrName];
|
|
1215
|
+
} else if (attrValue === false) {
|
|
1216
|
+
delete attributes[attrName];
|
|
1217
|
+
} else if (m(attrValue) || T(attrValue)) {
|
|
1218
|
+
attributesString += ` ${attrName}="${attrValue}"`;
|
|
1219
|
+
delete attributes[attrName];
|
|
1220
|
+
} else if (t7.isConditionalExpression(attrValue)) {
|
|
1221
|
+
attributes[attrName] = attrValue;
|
|
1222
|
+
} else if (t7.isObjectExpression(attrValue)) {
|
|
1223
|
+
const result = processObjectExpression(
|
|
1224
|
+
attrName,
|
|
1225
|
+
attrValue,
|
|
1226
|
+
attributes,
|
|
1227
|
+
attrName === CLASS_NAME || attrName === STYLE_NAME
|
|
1228
|
+
);
|
|
1229
|
+
if (result) {
|
|
1230
|
+
if (attrName === CLASS_NAME) {
|
|
1231
|
+
classNames += ` ${result}`;
|
|
1232
|
+
}
|
|
1233
|
+
if (attrName === STYLE_NAME) {
|
|
1234
|
+
styleString += `${result}${result.at(-1) === ";" ? "" : ";"}`;
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
if (classNames.trim()) {
|
|
1240
|
+
attributesString += ` ${CLASS_NAME}="${classNames.trim()}"`;
|
|
1241
|
+
}
|
|
1242
|
+
if (styleString.trim()) {
|
|
1243
|
+
attributesString += ` ${STYLE_NAME}="${styleString.trim()}"`;
|
|
1244
|
+
}
|
|
1245
|
+
return attributesString;
|
|
1246
|
+
}
|
|
1247
|
+
function findBeforeIndex(currentNode, parentNode) {
|
|
1248
|
+
var _a;
|
|
1249
|
+
if (!((_a = parentNode == null ? void 0 : parentNode.children) == null ? void 0 : _a.length) || currentNode.isLastChild) {
|
|
1250
|
+
return null;
|
|
1251
|
+
}
|
|
1252
|
+
const children = parentNode.children;
|
|
1253
|
+
const childrenLength = children.length;
|
|
1254
|
+
let nodeIndex = -1;
|
|
1255
|
+
for (let i = 0; i < childrenLength; i++) {
|
|
1256
|
+
if (children[i] === currentNode) {
|
|
1257
|
+
nodeIndex = i;
|
|
1258
|
+
break;
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
if (nodeIndex === -1 || nodeIndex === childrenLength - 1) {
|
|
1262
|
+
return null;
|
|
1263
|
+
}
|
|
1264
|
+
for (let searchIndex = nodeIndex + 1; searchIndex < childrenLength; searchIndex++) {
|
|
1265
|
+
const siblingNode = children[searchIndex];
|
|
1266
|
+
const siblingType = siblingNode.type;
|
|
1267
|
+
if (siblingType !== 4 /* EXPRESSION */ && siblingType !== 3 /* FRAGMENT */ && siblingType !== 1 /* COMPONENT */) {
|
|
1268
|
+
return siblingNode.index;
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
return null;
|
|
1272
|
+
}
|
|
1273
|
+
function findIndexPosition(targetIndex, indexMap) {
|
|
1274
|
+
if (d(indexMap)) {
|
|
1275
|
+
return indexMap.indexOf(targetIndex);
|
|
1276
|
+
}
|
|
1277
|
+
if (E(indexMap)) {
|
|
1278
|
+
const position = indexMap.get(targetIndex);
|
|
1279
|
+
return T(position) ? position : -1;
|
|
1280
|
+
}
|
|
1281
|
+
return -1;
|
|
1282
|
+
}
|
|
1283
|
+
function normalizeProps(raw) {
|
|
1284
|
+
const normalized = {};
|
|
1285
|
+
const classBuffer = [];
|
|
1286
|
+
const styleBuffer = [];
|
|
1287
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
1288
|
+
if (key === CLASS_NAME || key === "className") {
|
|
1289
|
+
if (m(value)) classBuffer.push(value);
|
|
1290
|
+
else {
|
|
1291
|
+
normalized[CLASS_NAME] = value;
|
|
1292
|
+
}
|
|
1293
|
+
continue;
|
|
1294
|
+
}
|
|
1295
|
+
if (key === STYLE_NAME) {
|
|
1296
|
+
if (m(value)) styleBuffer.push(value);
|
|
1297
|
+
else normalized[STYLE_NAME] = value;
|
|
1298
|
+
continue;
|
|
1299
|
+
}
|
|
1300
|
+
if (key === SPREAD_NAME) {
|
|
1301
|
+
if (!normalized[SPREAD_NAME]) normalized[SPREAD_NAME] = [];
|
|
1302
|
+
normalized[SPREAD_NAME].push(value);
|
|
1303
|
+
continue;
|
|
1304
|
+
}
|
|
1305
|
+
normalized[key] = value;
|
|
1306
|
+
}
|
|
1307
|
+
if (classBuffer.length) normalized[CLASS_NAME] = classBuffer.join(" ").trim();
|
|
1308
|
+
if (styleBuffer.length) {
|
|
1309
|
+
const s = styleBuffer.map((str) => str.endsWith(";") ? str : `${str};`).join("");
|
|
1310
|
+
normalized[STYLE_NAME] = s;
|
|
1311
|
+
}
|
|
1312
|
+
return normalized;
|
|
1313
|
+
}
|
|
1314
|
+
function createPropsObjectExpression(propsData, transformJSX2) {
|
|
1315
|
+
const objectProperties = [];
|
|
1316
|
+
const propsToProcess = normalizeProps(propsData);
|
|
1317
|
+
for (const propName in propsToProcess) {
|
|
1318
|
+
const propValue = propsToProcess[propName];
|
|
1319
|
+
if (propName === CHILDREN_NAME && (!propValue || d(propValue) && !propValue.length)) {
|
|
1320
|
+
continue;
|
|
1321
|
+
}
|
|
1322
|
+
const astValue = convertValueToASTNode(propValue, transformJSX2);
|
|
1323
|
+
if (propName === SPREAD_NAME) {
|
|
1324
|
+
objectProperties.push(t7.spreadElement(astValue));
|
|
1325
|
+
} else {
|
|
1326
|
+
objectProperties.push(t7.objectProperty(t7.stringLiteral(propName), astValue));
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
return t7.objectExpression(objectProperties);
|
|
1330
|
+
}
|
|
1331
|
+
function convertValueToASTNode(value, transformJSX2) {
|
|
1332
|
+
if (t7.isExpression(value)) {
|
|
1333
|
+
return value;
|
|
1334
|
+
}
|
|
1335
|
+
if (d(value)) {
|
|
1336
|
+
return t7.arrayExpression(value.map((item) => convertValueToASTNode(item, transformJSX2)));
|
|
1337
|
+
}
|
|
1338
|
+
if (w(value)) {
|
|
1339
|
+
if (isTreeNode(value) && hasPureStringChildren(value)) {
|
|
1340
|
+
const stringContent = extractStringChildren(value);
|
|
1341
|
+
return t7.stringLiteral(stringContent);
|
|
1342
|
+
}
|
|
1343
|
+
if (value.type === 1 /* COMPONENT */ || value.type === 0 /* NORMAL */ || value.type === 2 /* TEXT */ || value.type === 5 /* SVG */) {
|
|
1344
|
+
const { path } = getContext();
|
|
1345
|
+
return transformJSX2(path, value);
|
|
1346
|
+
}
|
|
1347
|
+
if (value.type === 4 /* EXPRESSION */ && value.children && d(value.children) && value.children.length > 0) {
|
|
1348
|
+
return convertValueToASTNode(value.children[0], transformJSX2);
|
|
84
1349
|
}
|
|
85
|
-
if (
|
|
86
|
-
path
|
|
1350
|
+
if (t7.isJSXElement(value) || t7.isJSXFragment(value)) {
|
|
1351
|
+
const { path } = getContext();
|
|
1352
|
+
const mockNodePath = {
|
|
1353
|
+
node: value,
|
|
1354
|
+
parentPath: path,
|
|
1355
|
+
// Keep parent path reference
|
|
1356
|
+
scope: path.scope
|
|
1357
|
+
// Keep scope reference
|
|
1358
|
+
};
|
|
1359
|
+
const tree = createTree(mockNodePath);
|
|
1360
|
+
return transformJSX2(path, tree);
|
|
87
1361
|
}
|
|
1362
|
+
return createPropsObjectExpression(value, transformJSX2);
|
|
88
1363
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const ImportSpecifier = [];
|
|
92
|
-
imports.forEach((name) => {
|
|
93
|
-
const local = t.identifier(state[name].name);
|
|
94
|
-
const imported = t.identifier(name);
|
|
95
|
-
ImportSpecifier.push(t.importSpecifier(local, imported));
|
|
96
|
-
});
|
|
97
|
-
const importSource = t.stringLiteral(from);
|
|
98
|
-
return t.importDeclaration(ImportSpecifier, importSource);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// src/shared.ts
|
|
102
|
-
import { types as t2 } from "@babel/core";
|
|
103
|
-
function hasSiblingElement(path) {
|
|
104
|
-
const siblings = path.getAllPrevSiblings().concat(path.getAllNextSiblings());
|
|
105
|
-
const hasSibling = siblings.some(
|
|
106
|
-
(siblingPath) => siblingPath.isJSXElement() || siblingPath.isJSXExpressionContainer()
|
|
107
|
-
);
|
|
108
|
-
return hasSibling;
|
|
109
|
-
}
|
|
110
|
-
function getAttrName(attribute) {
|
|
111
|
-
if (t2.isJSXIdentifier(attribute.name)) {
|
|
112
|
-
return attribute.name.name;
|
|
1364
|
+
if (m(value)) {
|
|
1365
|
+
return t7.stringLiteral(value);
|
|
113
1366
|
}
|
|
114
|
-
if (
|
|
115
|
-
return
|
|
1367
|
+
if (T(value)) {
|
|
1368
|
+
return t7.numericLiteral(value);
|
|
116
1369
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
function getTagName(node) {
|
|
120
|
-
const tag = node.openingElement.name;
|
|
121
|
-
return jsxElementNameToString(tag);
|
|
122
|
-
}
|
|
123
|
-
function jsxElementNameToString(node) {
|
|
124
|
-
if (t2.isJSXMemberExpression(node)) {
|
|
125
|
-
return `${jsxElementNameToString(node.object)}.${jsxElementNameToString(node.property)}`;
|
|
1370
|
+
if (z(value)) {
|
|
1371
|
+
return t7.booleanLiteral(value);
|
|
126
1372
|
}
|
|
127
|
-
if (
|
|
128
|
-
return
|
|
1373
|
+
if (_(value)) {
|
|
1374
|
+
return t7.identifier("undefined");
|
|
129
1375
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
return
|
|
1376
|
+
if (y(value)) {
|
|
1377
|
+
return t7.nullLiteral();
|
|
1378
|
+
}
|
|
1379
|
+
return value;
|
|
134
1380
|
}
|
|
135
|
-
function
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
return
|
|
140
|
-
|
|
1381
|
+
function getSetFunctionForAttribute(attrName) {
|
|
1382
|
+
const { state } = getContext();
|
|
1383
|
+
switch (attrName) {
|
|
1384
|
+
case CLASS_NAME:
|
|
1385
|
+
return {
|
|
1386
|
+
name: "patchClass",
|
|
1387
|
+
value: state.imports.patchClass
|
|
1388
|
+
};
|
|
1389
|
+
case STYLE_NAME:
|
|
1390
|
+
return { name: "patchStyle", value: state.imports.patchStyle };
|
|
1391
|
+
default:
|
|
1392
|
+
return { name: "patchAttr", value: state.imports.patchAttr };
|
|
141
1393
|
}
|
|
142
|
-
|
|
1394
|
+
}
|
|
1395
|
+
function isDynamicExpression(node) {
|
|
1396
|
+
if (!node) return false;
|
|
1397
|
+
if (t7.isIdentifier(node) || t7.isMemberExpression(node) || t7.isOptionalMemberExpression(node) || t7.isCallExpression(node) || t7.isOptionalCallExpression(node) || t7.isConditionalExpression(node) || t7.isLogicalExpression(node) || t7.isTemplateLiteral(node) || t7.isTaggedTemplateExpression(node) || t7.isBinaryExpression(node) || t7.isUnaryExpression(node) || t7.isUpdateExpression(node) || t7.isAwaitExpression(node) || t7.isYieldExpression(node) || t7.isAssignmentExpression(node) || t7.isSequenceExpression(node) || t7.isArrowFunctionExpression(node) || t7.isFunctionExpression(node)) {
|
|
143
1398
|
return true;
|
|
144
1399
|
}
|
|
145
|
-
|
|
1400
|
+
if (t7.isObjectExpression(node)) {
|
|
1401
|
+
return deepCheckObjectDynamic(node);
|
|
1402
|
+
}
|
|
1403
|
+
if (t7.isArrayExpression(node)) {
|
|
1404
|
+
return node.elements.some((el) => el != null && t7.isNode(el) && isDynamicExpression(el));
|
|
1405
|
+
}
|
|
1406
|
+
if (t7.isStringLiteral(node) || t7.isNumericLiteral(node) || t7.isBooleanLiteral(node) || t7.isNullLiteral(node) || t7.isRegExpLiteral(node)) {
|
|
1407
|
+
return false;
|
|
1408
|
+
}
|
|
1409
|
+
return t7.isExpression(node);
|
|
146
1410
|
}
|
|
147
|
-
function
|
|
148
|
-
if (
|
|
149
|
-
|
|
1411
|
+
function hasPureStringChildren(node) {
|
|
1412
|
+
if (!node.children || node.children.length === 0) {
|
|
1413
|
+
return false;
|
|
150
1414
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
expression.replaceWith(t2.stringLiteral(text));
|
|
1415
|
+
return node.children.every((child) => {
|
|
1416
|
+
if (m(child)) {
|
|
1417
|
+
return true;
|
|
155
1418
|
}
|
|
156
|
-
|
|
1419
|
+
if (isTreeNode(child) && child.type === 2 /* TEXT */) {
|
|
1420
|
+
return true;
|
|
1421
|
+
}
|
|
1422
|
+
return false;
|
|
1423
|
+
});
|
|
157
1424
|
}
|
|
158
|
-
function
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
1425
|
+
function extractStringChildren(node) {
|
|
1426
|
+
if (!node.children || node.children.length === 0) {
|
|
1427
|
+
return "";
|
|
1428
|
+
}
|
|
1429
|
+
return node.children.map((child) => {
|
|
1430
|
+
if (m(child)) {
|
|
1431
|
+
return child;
|
|
1432
|
+
}
|
|
1433
|
+
if (isTreeNode(child) && child.type === 2 /* TEXT */ && child.children) {
|
|
1434
|
+
return child.children.join("");
|
|
1435
|
+
}
|
|
1436
|
+
return "";
|
|
1437
|
+
}).join("");
|
|
162
1438
|
}
|
|
163
1439
|
|
|
164
|
-
// src/jsx/
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
"
|
|
175
|
-
"
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
]
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
1440
|
+
// src/jsx/client.ts
|
|
1441
|
+
function transformJSXToClient(path, node) {
|
|
1442
|
+
const state = path.state;
|
|
1443
|
+
if (node.type === 1 /* COMPONENT */) {
|
|
1444
|
+
const props = __spreadProps(__spreadValues({}, node.props), { children: node.children });
|
|
1445
|
+
return createComponentExpression(node, props);
|
|
1446
|
+
}
|
|
1447
|
+
const staticTemplate = generateStaticTemplate(node);
|
|
1448
|
+
const dynamicCollection = generateDynamic(node);
|
|
1449
|
+
const indexMap = generateIndexMap(dynamicCollection);
|
|
1450
|
+
const elementId = path.scope.generateUidIdentifier("_$el");
|
|
1451
|
+
const nodesId = path.scope.generateUidIdentifier("_$nodes");
|
|
1452
|
+
const statements = [];
|
|
1453
|
+
if (staticTemplate) {
|
|
1454
|
+
addImport(importMap.template);
|
|
1455
|
+
const tmplId = path.scope.generateUidIdentifier("_$tmpl");
|
|
1456
|
+
state.declarations.push(
|
|
1457
|
+
t8.variableDeclarator(
|
|
1458
|
+
tmplId,
|
|
1459
|
+
t8.callExpression(state.imports.template, G(staticTemplate).map(t8.stringLiteral))
|
|
1460
|
+
)
|
|
1461
|
+
);
|
|
1462
|
+
statements.push(
|
|
1463
|
+
t8.variableDeclaration("const", [
|
|
1464
|
+
t8.variableDeclarator(elementId, t8.callExpression(tmplId, []))
|
|
1465
|
+
])
|
|
1466
|
+
);
|
|
1467
|
+
}
|
|
1468
|
+
if (dynamicCollection.children.length || dynamicCollection.props.length) {
|
|
1469
|
+
addImport(importMap.mapNodes);
|
|
1470
|
+
statements.push(
|
|
1471
|
+
t8.variableDeclaration("const", [
|
|
1472
|
+
t8.variableDeclarator(
|
|
1473
|
+
nodesId,
|
|
1474
|
+
t8.callExpression(state.imports.mapNodes, [
|
|
1475
|
+
elementId,
|
|
1476
|
+
t8.arrayExpression(indexMap.map((idx) => t8.numericLiteral(idx)))
|
|
1477
|
+
])
|
|
1478
|
+
)
|
|
1479
|
+
])
|
|
1480
|
+
);
|
|
1481
|
+
if (dynamicCollection.children.length) {
|
|
1482
|
+
generateDynamicChildrenCode(dynamicCollection.children, statements, state, nodesId, indexMap);
|
|
1483
|
+
}
|
|
1484
|
+
if (dynamicCollection.props.length) {
|
|
1485
|
+
generateDynamicPropsCode(dynamicCollection.props, statements, state, nodesId, indexMap);
|
|
1486
|
+
}
|
|
1487
|
+
if (dynamicCollection.operations.length) {
|
|
1488
|
+
generateUnifiedMemoizedEffect(
|
|
1489
|
+
dynamicCollection.operations,
|
|
1490
|
+
statements,
|
|
1491
|
+
state,
|
|
1492
|
+
nodesId,
|
|
1493
|
+
indexMap
|
|
1494
|
+
);
|
|
211
1495
|
}
|
|
212
|
-
} else {
|
|
213
|
-
result.template += content;
|
|
214
1496
|
}
|
|
1497
|
+
statements.push(t8.returnStatement(elementId));
|
|
1498
|
+
return t8.callExpression(t8.arrowFunctionExpression([], t8.blockStatement(statements)), []);
|
|
215
1499
|
}
|
|
216
|
-
function
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
props: {},
|
|
224
|
-
template: isSSG ? [] : ""
|
|
1500
|
+
function generatePropKey(attrName = "attr") {
|
|
1501
|
+
const { operationIndex } = getContext();
|
|
1502
|
+
const keyMap = {
|
|
1503
|
+
[CLASS_NAME]: "c",
|
|
1504
|
+
[STYLE_NAME]: "s",
|
|
1505
|
+
name: "n",
|
|
1506
|
+
attr: "a"
|
|
225
1507
|
};
|
|
226
|
-
|
|
227
|
-
|
|
1508
|
+
const baseKey = keyMap[attrName] || attrName.charAt(0);
|
|
1509
|
+
setContext(__spreadProps(__spreadValues({}, getContext()), { operationIndex: operationIndex + 1 }));
|
|
1510
|
+
setContext(__spreadProps(__spreadValues({}, getContext()), { operationIndex: operationIndex + 1 }));
|
|
1511
|
+
return `${baseKey}${operationIndex}`;
|
|
228
1512
|
}
|
|
229
|
-
function
|
|
230
|
-
return
|
|
1513
|
+
function transformJSXClientChildren(path, treeNode) {
|
|
1514
|
+
return transformJSXToClient(path, treeNode);
|
|
231
1515
|
}
|
|
232
|
-
function
|
|
233
|
-
const state =
|
|
234
|
-
|
|
235
|
-
const
|
|
236
|
-
if (
|
|
237
|
-
|
|
238
|
-
state.tmplDeclaration.declarations.push(t3.variableDeclarator(tmpl, template));
|
|
239
|
-
if (!isSSG) {
|
|
240
|
-
imports.add("template");
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
const key = result.props.key;
|
|
244
|
-
delete result.props.key;
|
|
245
|
-
const args = [tmpl, createProps(result.props)];
|
|
246
|
-
if (key) {
|
|
247
|
-
args.push(key);
|
|
1516
|
+
function createComponentExpression(node, componentProps) {
|
|
1517
|
+
const { state } = getContext();
|
|
1518
|
+
addImport(importMap.createComponent);
|
|
1519
|
+
const isBuiltIn = BUILT_IN_COMPONENTS.includes(node.tag);
|
|
1520
|
+
if (isBuiltIn) {
|
|
1521
|
+
addImport(importMap[node.tag]);
|
|
248
1522
|
}
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
return t3.stringLiteral(value);
|
|
264
|
-
case "number":
|
|
265
|
-
return t3.numericLiteral(value);
|
|
266
|
-
case "boolean":
|
|
267
|
-
return t3.booleanLiteral(value);
|
|
268
|
-
case "undefined":
|
|
269
|
-
case void 0:
|
|
270
|
-
return t3.tsUndefinedKeyword();
|
|
271
|
-
case null:
|
|
272
|
-
return t3.nullLiteral();
|
|
273
|
-
default:
|
|
274
|
-
return value;
|
|
275
|
-
}
|
|
276
|
-
};
|
|
277
|
-
const result = Object.entries(props).map(
|
|
278
|
-
([prop, value]) => prop === "_$spread$" ? t3.spreadElement(toAstNode(value)) : t3.objectProperty(t3.stringLiteral(prop), toAstNode(value))
|
|
279
|
-
);
|
|
280
|
-
return t3.objectExpression(result);
|
|
281
|
-
}
|
|
282
|
-
function transformJSXElement(path, result, isRoot = false) {
|
|
283
|
-
if (path.isJSXElement()) {
|
|
284
|
-
const tagName = getTagName(path.node);
|
|
285
|
-
const tagIsComponent = isComponent(tagName);
|
|
286
|
-
const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
|
|
287
|
-
const isSvg = svgTags.includes(tagName) && result.index === 1;
|
|
288
|
-
const { props, hasExpression } = getAttrProps(path);
|
|
289
|
-
if (props.key) {
|
|
290
|
-
result.props.key = props.key;
|
|
291
|
-
delete props.key;
|
|
292
|
-
}
|
|
293
|
-
if (tagIsComponent) {
|
|
294
|
-
handleComponentElement(path, result, isRoot, props);
|
|
295
|
-
} else {
|
|
296
|
-
handleHTMLElement(path, result, tagName, isSelfClose, isSvg, props, hasExpression);
|
|
1523
|
+
const argTag = isBuiltIn ? state.imports[node.tag] : t8.identifier(node.tag);
|
|
1524
|
+
const propsObj = createPropsObjectExpression(componentProps, transformJSXClientChildren);
|
|
1525
|
+
const args = [argTag, propsObj];
|
|
1526
|
+
return t8.callExpression(state.imports[CREATE_COMPONENT_NAME], args);
|
|
1527
|
+
}
|
|
1528
|
+
function generateIndexMap({
|
|
1529
|
+
props,
|
|
1530
|
+
children,
|
|
1531
|
+
operations = []
|
|
1532
|
+
}) {
|
|
1533
|
+
const indexSet = /* @__PURE__ */ new Set();
|
|
1534
|
+
for (const item of children) {
|
|
1535
|
+
if (!N(item.parentIndex)) {
|
|
1536
|
+
indexSet.add(item.parentIndex);
|
|
297
1537
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
transformChildren(path, result);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
function handleComponentElement(path, result, isRoot, props) {
|
|
304
|
-
if (isRoot) {
|
|
305
|
-
result.props = props;
|
|
306
|
-
const children = getChildren(path);
|
|
307
|
-
if (children.length > 0) {
|
|
308
|
-
const childrenGenerator = children.length === 1 ? children[0] : t3.arrayExpression(children);
|
|
309
|
-
if (t3.isConditionalExpression(childrenGenerator)) {
|
|
310
|
-
result.props.children = t3.arrowFunctionExpression([], childrenGenerator);
|
|
311
|
-
} else {
|
|
312
|
-
result.props.children = childrenGenerator;
|
|
313
|
-
}
|
|
1538
|
+
if (!N(item.before)) {
|
|
1539
|
+
indexSet.add(item.before);
|
|
314
1540
|
}
|
|
315
|
-
} else {
|
|
316
|
-
transformJSX(path);
|
|
317
|
-
replaceChild(path.node, result);
|
|
318
1541
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
1542
|
+
for (const item of props) {
|
|
1543
|
+
if (!N(item.parentIndex)) {
|
|
1544
|
+
indexSet.add(item.parentIndex);
|
|
1545
|
+
}
|
|
323
1546
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
if (hasSiblingElement(path) || isSSG) {
|
|
330
|
-
addToTemplate(result, `</${tagName}>`);
|
|
1547
|
+
if (operations && operations.length > 0) {
|
|
1548
|
+
for (const item of operations) {
|
|
1549
|
+
if (!N(item.nodeIndex)) {
|
|
1550
|
+
indexSet.add(item.nodeIndex);
|
|
1551
|
+
}
|
|
331
1552
|
}
|
|
332
1553
|
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
const lastChild = pre.at(-1);
|
|
339
|
-
if (lastChild && isTextChild(cur) && isTextChild(lastChild)) {
|
|
340
|
-
setNodeText(lastChild, getNodeText(lastChild) + getNodeText(cur));
|
|
341
|
-
} else {
|
|
342
|
-
pre.push(cur);
|
|
1554
|
+
const indexMap = Array.from(indexSet).sort((a2, b) => a2 - b);
|
|
1555
|
+
if (true) {
|
|
1556
|
+
for (const index of indexMap) {
|
|
1557
|
+
if (!Number.isInteger(index) || index < 1) {
|
|
1558
|
+
ne(`Invalid index in index map: ${index}. All indices must be positive integers.`);
|
|
343
1559
|
}
|
|
344
1560
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
result.parentIndex = parentIndex;
|
|
348
|
-
result.isLastChild = i === arr.length - 1;
|
|
349
|
-
transformChild(child, result);
|
|
350
|
-
});
|
|
1561
|
+
}
|
|
1562
|
+
return indexMap;
|
|
351
1563
|
}
|
|
352
|
-
function
|
|
353
|
-
|
|
354
|
-
if (
|
|
355
|
-
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
if (
|
|
359
|
-
|
|
360
|
-
} else if (expression.isExpression()) {
|
|
361
|
-
replaceChild(expression.node, result);
|
|
362
|
-
} else if (t3.isJSXEmptyExpression(expression.node)) {
|
|
1564
|
+
function createAttributeStatement(functionIdentifier, nodesId, nodeIndex, value, key) {
|
|
1565
|
+
const args = [t8.memberExpression(nodesId, t8.numericLiteral(nodeIndex), true)];
|
|
1566
|
+
if (key) {
|
|
1567
|
+
args.push(key);
|
|
1568
|
+
}
|
|
1569
|
+
if (value) {
|
|
1570
|
+
if (d(value)) {
|
|
1571
|
+
args.push(...value);
|
|
363
1572
|
} else {
|
|
364
|
-
|
|
1573
|
+
args.push(value);
|
|
365
1574
|
}
|
|
366
|
-
} else if (child.isJSXText()) {
|
|
367
|
-
addToTemplate(result, replaceSpace(child.node), true);
|
|
368
|
-
} else {
|
|
369
|
-
throw new Error("Unsupported child type");
|
|
370
1575
|
}
|
|
1576
|
+
const functionCall = t8.callExpression(functionIdentifier, args);
|
|
1577
|
+
return t8.expressionStatement(functionCall);
|
|
371
1578
|
}
|
|
372
|
-
function
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
return
|
|
1579
|
+
function addEventListenerStatement(attrName, attrValue, nodesId, nodeIndex, statements, state) {
|
|
1580
|
+
var _a;
|
|
1581
|
+
const eventName = attrName.slice(2).toLowerCase();
|
|
1582
|
+
if (Oe(eventName)) {
|
|
1583
|
+
const activeContext = getContext();
|
|
1584
|
+
if (!activeContext) {
|
|
1585
|
+
ne("Missing active context, unable to handle delegated events");
|
|
1586
|
+
return;
|
|
380
1587
|
}
|
|
1588
|
+
const elementRef = t8.memberExpression(nodesId, t8.numericLiteral(nodeIndex), true);
|
|
1589
|
+
const eventProperty = t8.memberExpression(elementRef, t8.stringLiteral(`_$${eventName}`), true);
|
|
1590
|
+
const assignmentStmt = t8.expressionStatement(
|
|
1591
|
+
t8.assignmentExpression("=", eventProperty, attrValue)
|
|
1592
|
+
);
|
|
1593
|
+
statements.push(assignmentStmt);
|
|
1594
|
+
(_a = state.events) == null ? void 0 : _a.add(eventName);
|
|
1595
|
+
return;
|
|
381
1596
|
}
|
|
382
|
-
|
|
1597
|
+
addImport(importMap.addEventListener);
|
|
1598
|
+
const eventListenerStmt = createAttributeStatement(
|
|
1599
|
+
state.imports.addEventListener,
|
|
1600
|
+
nodesId,
|
|
1601
|
+
nodeIndex,
|
|
1602
|
+
attrValue,
|
|
1603
|
+
t8.stringLiteral(eventName)
|
|
1604
|
+
);
|
|
1605
|
+
statements.push(eventListenerStmt);
|
|
383
1606
|
}
|
|
384
|
-
function
|
|
385
|
-
|
|
1607
|
+
function generateSpecificAttributeCode(attributeName, attributeValue, nodesId, nodeIndex, statements, state) {
|
|
1608
|
+
switch (attributeName) {
|
|
1609
|
+
case CLASS_NAME:
|
|
1610
|
+
addImport(importMap.patchClass);
|
|
1611
|
+
statements.push(
|
|
1612
|
+
createAttributeStatement(state.imports.patchClass, nodesId, nodeIndex, attributeValue)
|
|
1613
|
+
);
|
|
1614
|
+
break;
|
|
1615
|
+
case SPREAD_NAME:
|
|
1616
|
+
addImport(importMap.setSpread);
|
|
1617
|
+
statements.push(
|
|
1618
|
+
createAttributeStatement(state.imports.setSpread, nodesId, nodeIndex, attributeValue)
|
|
1619
|
+
);
|
|
1620
|
+
break;
|
|
1621
|
+
case STYLE_NAME:
|
|
1622
|
+
addImport(importMap.patchStyle);
|
|
1623
|
+
statements.push(
|
|
1624
|
+
createAttributeStatement(state.imports.patchStyle, nodesId, nodeIndex, attributeValue)
|
|
1625
|
+
);
|
|
1626
|
+
break;
|
|
1627
|
+
default:
|
|
1628
|
+
if (q(attributeName, `${UPDATE_PREFIX}:`)) {
|
|
1629
|
+
addImport(importMap.bindElement);
|
|
1630
|
+
const attrName = attributeName.split(":")[1];
|
|
1631
|
+
statements.push(
|
|
1632
|
+
createAttributeStatement(
|
|
1633
|
+
state.imports.bindElement,
|
|
1634
|
+
nodesId,
|
|
1635
|
+
nodeIndex,
|
|
1636
|
+
attributeValue,
|
|
1637
|
+
t8.stringLiteral(attrName)
|
|
1638
|
+
)
|
|
1639
|
+
);
|
|
1640
|
+
return;
|
|
1641
|
+
}
|
|
1642
|
+
addImport(importMap.patchAttr);
|
|
1643
|
+
statements.push(
|
|
1644
|
+
createAttributeStatement(
|
|
1645
|
+
state.imports.patchAttr,
|
|
1646
|
+
nodesId,
|
|
1647
|
+
nodeIndex,
|
|
1648
|
+
attributeValue,
|
|
1649
|
+
t8.stringLiteral(attributeName)
|
|
1650
|
+
)
|
|
1651
|
+
);
|
|
1652
|
+
break;
|
|
1653
|
+
}
|
|
386
1654
|
}
|
|
387
|
-
function
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
if (
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
delete props[prop];
|
|
397
|
-
} else if (value === true) {
|
|
398
|
-
addToTemplate(result, ` ${prop}`);
|
|
399
|
-
delete props[prop];
|
|
400
|
-
} else if (value === false) {
|
|
401
|
-
delete props[prop];
|
|
402
|
-
} else if (typeof value === "string" || typeof value === "number") {
|
|
403
|
-
addToTemplate(result, ` ${prop}="${value}"`);
|
|
404
|
-
delete props[prop];
|
|
405
|
-
} else if (t3.isConditionalExpression(value)) {
|
|
406
|
-
props[prop] = t3.arrowFunctionExpression([], value);
|
|
407
|
-
} else if (t3.isObjectExpression(value)) {
|
|
408
|
-
const val = handleObjectExpression(prop, value, props, isStyleClassName(prop));
|
|
409
|
-
if (val) {
|
|
410
|
-
if (prop === "class") {
|
|
411
|
-
klass += ` ${val}`;
|
|
412
|
-
}
|
|
413
|
-
if (prop === "style") {
|
|
414
|
-
style += `${val}${val.at(-1) === ";" ? "" : ";"}`;
|
|
1655
|
+
function processIIFEExpression(node) {
|
|
1656
|
+
if (t8.isCallExpression(node) && (t8.isArrowFunctionExpression(node.callee) || t8.isFunctionExpression(node.callee)) && node.arguments.length === 0) {
|
|
1657
|
+
const callee = node.callee;
|
|
1658
|
+
const body = callee.body;
|
|
1659
|
+
if (t8.isBlockStatement(body)) {
|
|
1660
|
+
if (body.body.length === 1) {
|
|
1661
|
+
const statement = body.body[0];
|
|
1662
|
+
if (t8.isReturnStatement(statement) && statement.argument) {
|
|
1663
|
+
return statement.argument;
|
|
415
1664
|
}
|
|
416
1665
|
}
|
|
1666
|
+
} else if (t8.isExpression(body)) {
|
|
1667
|
+
return body;
|
|
417
1668
|
}
|
|
418
1669
|
}
|
|
419
|
-
|
|
420
|
-
|
|
1670
|
+
return node;
|
|
1671
|
+
}
|
|
1672
|
+
function createInsertArguments(dynamicContent, nodesIdentifier, indexMap) {
|
|
1673
|
+
if (N(dynamicContent.parentIndex)) {
|
|
1674
|
+
throw new Error("Dynamic content missing valid parent node index");
|
|
421
1675
|
}
|
|
422
|
-
|
|
423
|
-
|
|
1676
|
+
const parentPosition = findIndexPosition(dynamicContent.parentIndex, indexMap);
|
|
1677
|
+
if (parentPosition === -1) {
|
|
1678
|
+
throw new Error(
|
|
1679
|
+
`Cannot find parent node index in index mapping: ${dynamicContent.parentIndex}`
|
|
1680
|
+
);
|
|
424
1681
|
}
|
|
425
|
-
|
|
426
|
-
|
|
1682
|
+
const argExpressions = [
|
|
1683
|
+
// Target parent node reference: nodes[parentPosition]
|
|
1684
|
+
t8.memberExpression(nodesIdentifier, t8.numericLiteral(parentPosition), true),
|
|
1685
|
+
// CallExpression not be use call function
|
|
1686
|
+
// Content lazy function: () => dynamicContent (implements on-demand calculation)
|
|
1687
|
+
t8.isCallExpression(dynamicContent.node) || t8.isArrowFunctionExpression(dynamicContent.node) || t8.isFunctionExpression(dynamicContent.node) ? dynamicContent.node : t8.arrowFunctionExpression([], dynamicContent.node)
|
|
1688
|
+
];
|
|
1689
|
+
if (dynamicContent.before !== null) {
|
|
1690
|
+
const beforePosition = findIndexPosition(dynamicContent.before, indexMap);
|
|
1691
|
+
if (beforePosition === -1) {
|
|
1692
|
+
throw new Error(`Cannot find before node index in index mapping: ${dynamicContent.before}`);
|
|
1693
|
+
}
|
|
1694
|
+
argExpressions.push(
|
|
1695
|
+
t8.memberExpression(nodesIdentifier, t8.numericLiteral(beforePosition), true)
|
|
1696
|
+
);
|
|
427
1697
|
}
|
|
1698
|
+
return argExpressions;
|
|
428
1699
|
}
|
|
429
|
-
function
|
|
430
|
-
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
}
|
|
441
|
-
});
|
|
442
|
-
delete props[prop];
|
|
1700
|
+
function generateDynamicChildrenCode(dynamicChildren, statements, state, nodesId, indexMap) {
|
|
1701
|
+
addImport(importMap.insert);
|
|
1702
|
+
for (const dynamicContent of dynamicChildren) {
|
|
1703
|
+
const processedNode = processIIFEExpression(dynamicContent.node);
|
|
1704
|
+
const insertArgs = createInsertArguments(
|
|
1705
|
+
__spreadProps(__spreadValues({}, dynamicContent), { node: processedNode }),
|
|
1706
|
+
nodesId,
|
|
1707
|
+
indexMap
|
|
1708
|
+
);
|
|
1709
|
+
const insertCall = t8.callExpression(state.imports.insert, insertArgs);
|
|
1710
|
+
statements.push(t8.expressionStatement(insertCall));
|
|
443
1711
|
}
|
|
444
|
-
return ct;
|
|
445
1712
|
}
|
|
446
|
-
function
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
addToTemplate(result, "<!>");
|
|
452
|
-
}
|
|
453
|
-
(_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
|
|
454
|
-
(_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
|
|
455
|
-
result.props[result.parentIndex].children.push(
|
|
456
|
-
t3.arrayExpression([
|
|
457
|
-
t3.arrowFunctionExpression([], node),
|
|
458
|
-
result.isLastChild ? t3.nullLiteral() : t3.identifier(String(result.index))
|
|
459
|
-
])
|
|
460
|
-
);
|
|
461
|
-
}
|
|
462
|
-
function getChildren(path) {
|
|
463
|
-
return path.get("children").filter((child) => isValidChild(child)).map((child) => {
|
|
464
|
-
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
465
|
-
transformJSX(child);
|
|
466
|
-
} else if (child.isJSXExpressionContainer()) {
|
|
467
|
-
child.replaceWith(child.get("expression"));
|
|
468
|
-
} else if (child.isJSXText()) {
|
|
469
|
-
child.replaceWith(t3.stringLiteral(replaceSpace(child.node)));
|
|
470
|
-
} else {
|
|
471
|
-
throw new Error("Unsupported child type");
|
|
1713
|
+
function generateDynamicPropsCode(dynamicProps, statements, state, nodesId, indexMap) {
|
|
1714
|
+
for (const propItem of dynamicProps) {
|
|
1715
|
+
const { parentIndex, props } = propItem;
|
|
1716
|
+
if (parentIndex === null) {
|
|
1717
|
+
continue;
|
|
472
1718
|
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
props[name] = true;
|
|
492
|
-
} else if (value.isStringLiteral()) {
|
|
493
|
-
props[name] = value.node.value;
|
|
494
|
-
} else {
|
|
495
|
-
if (value.isJSXExpressionContainer()) {
|
|
496
|
-
const expression = value.get("expression");
|
|
497
|
-
if (expression.isStringLiteral()) {
|
|
498
|
-
props[name] = expression.node.value;
|
|
499
|
-
} else if (expression.isNumericLiteral()) {
|
|
500
|
-
props[name] = expression.node.value;
|
|
501
|
-
} else if (expression.isJSXElement() || expression.isJSXFragment()) {
|
|
502
|
-
transformJSX(expression);
|
|
503
|
-
props[name] = expression.node;
|
|
504
|
-
} else if (expression.isExpression()) {
|
|
505
|
-
hasExpression = true;
|
|
506
|
-
if (/^key|ref|on.+$/.test(name)) {
|
|
507
|
-
props[name] = expression.node;
|
|
508
|
-
} else if (/^bind:.+/.test(name)) {
|
|
509
|
-
const value2 = path.scope.generateUidIdentifier("value");
|
|
510
|
-
const bindName = name.slice(5).toLocaleLowerCase();
|
|
511
|
-
props[bindName] = expression.node;
|
|
512
|
-
props[`update${capitalize(bindName)}`] = t3.arrowFunctionExpression(
|
|
513
|
-
[value2],
|
|
514
|
-
t3.assignmentExpression("=", expression.node, value2)
|
|
515
|
-
);
|
|
516
|
-
} else {
|
|
517
|
-
if (expression.isConditionalExpression()) {
|
|
518
|
-
props[name] = t3.arrowFunctionExpression([], expression.node);
|
|
519
|
-
} else {
|
|
520
|
-
props[name] = expression.node;
|
|
521
|
-
}
|
|
522
|
-
}
|
|
1719
|
+
const parentIndexPosition = indexMap.indexOf(parentIndex);
|
|
1720
|
+
if (parentIndexPosition === -1) {
|
|
1721
|
+
ne(`Cannot find parent node index: ${parentIndex}`);
|
|
1722
|
+
continue;
|
|
1723
|
+
}
|
|
1724
|
+
for (const [attrName, attrValue] of Object.entries(props)) {
|
|
1725
|
+
try {
|
|
1726
|
+
if (attrName.startsWith(`${UPDATE_PREFIX}:`) && Array.isArray(attrValue)) {
|
|
1727
|
+
if (attrValue.length === 2 && t8.isExpression(attrValue[0]) && t8.isExpression(attrValue[1])) {
|
|
1728
|
+
generateSpecificAttributeCode(
|
|
1729
|
+
attrName,
|
|
1730
|
+
attrValue,
|
|
1731
|
+
// Pass array as-is, createAttributeStatement will spread it
|
|
1732
|
+
nodesId,
|
|
1733
|
+
parentIndexPosition,
|
|
1734
|
+
statements,
|
|
1735
|
+
state
|
|
1736
|
+
);
|
|
523
1737
|
}
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
1738
|
+
continue;
|
|
1739
|
+
}
|
|
1740
|
+
if (!t8.isExpression(attrValue)) {
|
|
1741
|
+
continue;
|
|
1742
|
+
}
|
|
1743
|
+
if (attrName.startsWith("on")) {
|
|
1744
|
+
addEventListenerStatement(
|
|
1745
|
+
attrName,
|
|
1746
|
+
attrValue,
|
|
1747
|
+
nodesId,
|
|
1748
|
+
parentIndexPosition,
|
|
1749
|
+
statements,
|
|
1750
|
+
state
|
|
1751
|
+
);
|
|
1752
|
+
} else {
|
|
1753
|
+
generateSpecificAttributeCode(
|
|
1754
|
+
attrName,
|
|
1755
|
+
attrValue,
|
|
1756
|
+
nodesId,
|
|
1757
|
+
parentIndexPosition,
|
|
1758
|
+
statements,
|
|
1759
|
+
state
|
|
1760
|
+
);
|
|
527
1761
|
}
|
|
1762
|
+
} catch (error) {
|
|
1763
|
+
ne(`Attribute processing failed (${attrName}): ${error}`);
|
|
528
1764
|
}
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
function generateUnifiedMemoizedEffect(reactiveOperations, statements, state, nodesId, indexMap) {
|
|
1769
|
+
addImport(importMap.memoEffect);
|
|
1770
|
+
const variableDeclarations = reactiveOperations.map((op, index) => {
|
|
1771
|
+
let attrValue = op.attrValue;
|
|
1772
|
+
if (t8.isIdentifier(op.attrValue) && isSignal(op.attrValue.name)) {
|
|
1773
|
+
attrValue = t8.memberExpression(attrValue, t8.identifier("value"));
|
|
1774
|
+
}
|
|
1775
|
+
return t8.variableDeclarator(t8.identifier(`_v$${index}`), attrValue);
|
|
1776
|
+
});
|
|
1777
|
+
const updateStatements = reactiveOperations.map((op, index) => {
|
|
1778
|
+
const parentPosition = findIndexPosition(op.nodeIndex, indexMap);
|
|
1779
|
+
if (parentPosition === -1) {
|
|
1780
|
+
throw new Error(`Cannot find parent node index in index mapping: ${op.nodeIndex}`);
|
|
1781
|
+
}
|
|
1782
|
+
const varName = t8.identifier(`_v$${index}`);
|
|
1783
|
+
const elementRef = t8.memberExpression(nodesId, t8.numericLiteral(parentPosition), true);
|
|
1784
|
+
let domOperationCall;
|
|
1785
|
+
if (op.setFunction.name === "patchAttr") {
|
|
1786
|
+
domOperationCall = t8.callExpression(op.setFunction.value, [
|
|
1787
|
+
elementRef,
|
|
1788
|
+
t8.stringLiteral(op.attrName),
|
|
1789
|
+
t8.memberExpression(t8.identifier("_p$"), t8.identifier(op.propKey)),
|
|
1790
|
+
t8.assignmentExpression(
|
|
1791
|
+
"=",
|
|
1792
|
+
t8.memberExpression(t8.identifier("_p$"), t8.identifier(op.propKey)),
|
|
1793
|
+
varName
|
|
1794
|
+
)
|
|
1795
|
+
]);
|
|
532
1796
|
} else {
|
|
533
|
-
|
|
1797
|
+
domOperationCall = t8.callExpression(op.setFunction.value, [
|
|
1798
|
+
elementRef,
|
|
1799
|
+
t8.memberExpression(t8.identifier("_p$"), t8.identifier(op.propKey)),
|
|
1800
|
+
t8.assignmentExpression(
|
|
1801
|
+
"=",
|
|
1802
|
+
t8.memberExpression(t8.identifier("_p$"), t8.identifier(op.propKey)),
|
|
1803
|
+
varName
|
|
1804
|
+
)
|
|
1805
|
+
]);
|
|
534
1806
|
}
|
|
1807
|
+
return t8.expressionStatement(
|
|
1808
|
+
t8.logicalExpression(
|
|
1809
|
+
"&&",
|
|
1810
|
+
t8.binaryExpression(
|
|
1811
|
+
"!==",
|
|
1812
|
+
varName,
|
|
1813
|
+
t8.memberExpression(t8.identifier("_p$"), t8.identifier(op.propKey))
|
|
1814
|
+
),
|
|
1815
|
+
domOperationCall
|
|
1816
|
+
)
|
|
1817
|
+
);
|
|
535
1818
|
});
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
1819
|
+
const effectBody = t8.blockStatement([
|
|
1820
|
+
// var _v$0 = expr1, _v$1 = expr2, ...;
|
|
1821
|
+
t8.variableDeclaration("var", variableDeclarations),
|
|
1822
|
+
// All update statements
|
|
1823
|
+
...updateStatements,
|
|
1824
|
+
// return _p$;
|
|
1825
|
+
t8.returnStatement(t8.identifier("_p$"))
|
|
1826
|
+
]);
|
|
1827
|
+
const effectFunction = t8.arrowFunctionExpression([t8.identifier("_p$")], effectBody);
|
|
1828
|
+
const initialStateProperties = reactiveOperations.map(
|
|
1829
|
+
(op) => t8.objectProperty(t8.identifier(op.propKey), t8.identifier("undefined"))
|
|
1830
|
+
);
|
|
1831
|
+
const initialState = t8.objectExpression(initialStateProperties);
|
|
1832
|
+
const memoizedEffectCall = t8.callExpression(state.imports.memoEffect, [
|
|
1833
|
+
effectFunction,
|
|
1834
|
+
initialState
|
|
1835
|
+
]);
|
|
1836
|
+
statements.push(t8.expressionStatement(memoizedEffectCall));
|
|
540
1837
|
}
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
1838
|
+
function generateStaticTemplate(node) {
|
|
1839
|
+
if (!node || node.type === 1 /* COMPONENT */) {
|
|
1840
|
+
return "";
|
|
1841
|
+
}
|
|
1842
|
+
switch (node.type) {
|
|
1843
|
+
case 2 /* TEXT */:
|
|
1844
|
+
return Array.isArray(node.children) ? node.children.join("") : "";
|
|
1845
|
+
case 6 /* COMMENT */:
|
|
1846
|
+
return "<!>";
|
|
1847
|
+
case 0 /* NORMAL */:
|
|
1848
|
+
case 5 /* SVG */:
|
|
1849
|
+
return gstForNormal(node);
|
|
1850
|
+
default:
|
|
1851
|
+
return gstForChildren(node);
|
|
1852
|
+
}
|
|
553
1853
|
}
|
|
554
|
-
function
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
const { node } = path;
|
|
558
|
-
if (!isSymbolStart(path, node.name)) return;
|
|
559
|
-
if (!path.findParent((p) => p.isMemberExpression() && p.node.property.name === "value")) {
|
|
560
|
-
path.replaceWith(t4.memberExpression(t4.identifier(node.name), t4.identifier("value")));
|
|
1854
|
+
function gstForNormal(node) {
|
|
1855
|
+
if (!node.tag) {
|
|
1856
|
+
return gstForChildren(node);
|
|
561
1857
|
}
|
|
1858
|
+
const attributes = serializeAttributes(node.props);
|
|
1859
|
+
const startTag = `<${node.tag}${attributes ? ` ${attributes}` : ""}`;
|
|
1860
|
+
if (node.selfClosing) {
|
|
1861
|
+
return `${startTag}/>`;
|
|
1862
|
+
}
|
|
1863
|
+
return `${startTag}>${gstForChildren(node)}</${node.tag}>`;
|
|
562
1864
|
}
|
|
563
|
-
function
|
|
564
|
-
|
|
1865
|
+
function gstForChildren(node) {
|
|
1866
|
+
if (!node.children || !node.children.length) {
|
|
1867
|
+
return "";
|
|
1868
|
+
}
|
|
1869
|
+
const childTemplates = [];
|
|
1870
|
+
for (const child of node.children) {
|
|
1871
|
+
if (w(child)) {
|
|
1872
|
+
const childTemplate = generateStaticTemplate(child);
|
|
1873
|
+
childTemplates.push(childTemplate);
|
|
1874
|
+
} else if (m(child)) {
|
|
1875
|
+
childTemplates.push(child);
|
|
1876
|
+
}
|
|
1877
|
+
}
|
|
1878
|
+
return childTemplates.join("");
|
|
565
1879
|
}
|
|
566
|
-
function
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
1880
|
+
function generateDynamic(node) {
|
|
1881
|
+
const dynamicCollection = {
|
|
1882
|
+
props: [],
|
|
1883
|
+
children: [],
|
|
1884
|
+
operations: []
|
|
1885
|
+
};
|
|
1886
|
+
function walk(node2, parentNode) {
|
|
1887
|
+
processNodeDynamic(dynamicCollection, node2, parentNode);
|
|
1888
|
+
if (node2.type === 1 /* COMPONENT */ || node2.type === 3 /* FRAGMENT */) {
|
|
1889
|
+
return;
|
|
570
1890
|
}
|
|
571
|
-
|
|
1891
|
+
if (node2.children && node2.children.length) {
|
|
1892
|
+
node2.children.forEach((child) => {
|
|
1893
|
+
if (isTreeNode(child)) {
|
|
1894
|
+
walk(child, node2);
|
|
1895
|
+
}
|
|
1896
|
+
});
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
walk(node);
|
|
1900
|
+
return dynamicCollection;
|
|
572
1901
|
}
|
|
573
|
-
function
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
1902
|
+
function processNodeDynamic(dynamicCollection, node, parentNode) {
|
|
1903
|
+
var _a, _b, _c, _d;
|
|
1904
|
+
const { children, props, operations } = dynamicCollection;
|
|
1905
|
+
switch (node.type) {
|
|
1906
|
+
case 1 /* COMPONENT */: {
|
|
1907
|
+
const componentProps = __spreadProps(__spreadValues({}, node.props), { children: node.children });
|
|
1908
|
+
const componentExpr = createComponentExpression(node, componentProps);
|
|
1909
|
+
children.push({
|
|
1910
|
+
index: node.index,
|
|
1911
|
+
node: componentExpr,
|
|
1912
|
+
before: findBeforeIndex(node, parentNode),
|
|
1913
|
+
parentIndex: (_a = parentNode == null ? void 0 : parentNode.index) != null ? _a : null
|
|
1914
|
+
});
|
|
1915
|
+
break;
|
|
579
1916
|
}
|
|
580
|
-
|
|
1917
|
+
case 4 /* EXPRESSION */:
|
|
1918
|
+
if (node.children && node.children.length > 0) {
|
|
1919
|
+
const firstChild = node.children[0];
|
|
1920
|
+
if (w(firstChild) && t8.isNode(firstChild) && t8.isExpression(firstChild)) {
|
|
1921
|
+
children.push({
|
|
1922
|
+
index: node.index,
|
|
1923
|
+
node: t8.arrowFunctionExpression([], firstChild),
|
|
1924
|
+
before: findBeforeIndex(node, parentNode),
|
|
1925
|
+
parentIndex: (_b = parentNode == null ? void 0 : parentNode.index) != null ? _b : null
|
|
1926
|
+
});
|
|
1927
|
+
} else if (w(firstChild) && t8.isNode(firstChild) && t8.isJSXElement(firstChild)) {
|
|
1928
|
+
children.push({
|
|
1929
|
+
index: node.index,
|
|
1930
|
+
node: createComponentExpression(node, { children: [firstChild] }),
|
|
1931
|
+
before: findBeforeIndex(node, parentNode),
|
|
1932
|
+
parentIndex: (_c = parentNode == null ? void 0 : parentNode.index) != null ? _c : null
|
|
1933
|
+
});
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1936
|
+
break;
|
|
1937
|
+
// Text nodes don't need dynamic processing (already handled in template stage)
|
|
1938
|
+
case 2 /* TEXT */:
|
|
1939
|
+
break;
|
|
1940
|
+
default:
|
|
1941
|
+
if (node.props && Object.keys(node.props).length > 0) {
|
|
1942
|
+
const currentProps = {};
|
|
1943
|
+
for (const [attrName, attrValue] of Object.entries(node.props)) {
|
|
1944
|
+
const isReactive = t8.isNode(attrValue) && isDynamicExpression(attrValue) && !q(attrName, `${UPDATE_PREFIX}:`) && !q(attrName, EVENT_ATTR_NAME);
|
|
1945
|
+
if (q(attrName, `${UPDATE_PREFIX}:`)) {
|
|
1946
|
+
const name = attrName.split(":")[1];
|
|
1947
|
+
const setFunction = getSetFunctionForAttribute();
|
|
1948
|
+
addImport(importMap[setFunction.name]);
|
|
1949
|
+
operations.push({
|
|
1950
|
+
nodeIndex: node == null ? void 0 : node.index,
|
|
1951
|
+
attrName: name,
|
|
1952
|
+
attrValue: attrValue[0],
|
|
1953
|
+
setFunction,
|
|
1954
|
+
propKey: generatePropKey()
|
|
1955
|
+
});
|
|
1956
|
+
}
|
|
1957
|
+
if (isReactive && t8.isExpression(attrValue)) {
|
|
1958
|
+
const setFunction = getSetFunctionForAttribute(attrName);
|
|
1959
|
+
addImport(importMap[setFunction.name]);
|
|
1960
|
+
operations.push({
|
|
1961
|
+
nodeIndex: node == null ? void 0 : node.index,
|
|
1962
|
+
attrName,
|
|
1963
|
+
attrValue,
|
|
1964
|
+
setFunction,
|
|
1965
|
+
propKey: generatePropKey(attrName)
|
|
1966
|
+
});
|
|
1967
|
+
} else {
|
|
1968
|
+
currentProps[attrName] = attrValue;
|
|
1969
|
+
}
|
|
1970
|
+
}
|
|
1971
|
+
if (Object.keys(currentProps).length > 0) {
|
|
1972
|
+
props.push({
|
|
1973
|
+
props: currentProps,
|
|
1974
|
+
parentIndex: (_d = node == null ? void 0 : node.index) != null ? _d : null
|
|
1975
|
+
});
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
break;
|
|
1979
|
+
}
|
|
581
1980
|
}
|
|
582
1981
|
|
|
583
|
-
// src/
|
|
584
|
-
import { types as
|
|
585
|
-
function
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
1982
|
+
// src/jsx/ssg.ts
|
|
1983
|
+
import { types as t9 } from "@babel/core";
|
|
1984
|
+
function transformJSXToSSG(path, treeNode) {
|
|
1985
|
+
const { state } = getContext();
|
|
1986
|
+
const result = transformTemplate(treeNode);
|
|
1987
|
+
const { templates, dynamics } = result;
|
|
1988
|
+
if (treeNode.type === 1 /* COMPONENT */ || treeNode.type === 3 /* FRAGMENT */) {
|
|
1989
|
+
const componentProps = __spreadProps(__spreadValues({}, treeNode.props), { children: treeNode.children });
|
|
1990
|
+
addImport(importMap.createComponent);
|
|
1991
|
+
return t9.callExpression(state.imports.createComponent, [
|
|
1992
|
+
t9.identifier(treeNode.tag),
|
|
1993
|
+
createPropsObjectExpression(componentProps, transformJSXToSSG)
|
|
1994
|
+
]);
|
|
1995
|
+
}
|
|
1996
|
+
const args = [];
|
|
1997
|
+
if (templates && templates.length > 0) {
|
|
1998
|
+
addImport(importMap.template);
|
|
1999
|
+
const tmplId = path.scope.generateUidIdentifier("_tmpl$");
|
|
2000
|
+
const templateNode = t9.arrayExpression(templates.map((str) => t9.stringLiteral(str)));
|
|
2001
|
+
state.declarations.push(t9.variableDeclarator(tmplId, templateNode));
|
|
2002
|
+
args.push(tmplId);
|
|
2003
|
+
}
|
|
2004
|
+
args.push(t9.callExpression(state.imports.getHydrationKey, []));
|
|
2005
|
+
addImport(importMap.render);
|
|
2006
|
+
addImport(importMap.getHydrationKey);
|
|
2007
|
+
const textDynamics = dynamics.filter((d2) => d2.type === "text");
|
|
2008
|
+
const attrDynamics = dynamics.filter((d2) => d2.type === "attr");
|
|
2009
|
+
attrDynamics.forEach((dynamic) => {
|
|
2010
|
+
args.push(dynamic.node);
|
|
592
2011
|
});
|
|
2012
|
+
textDynamics.forEach((dynamic) => {
|
|
2013
|
+
args.push(dynamic.node);
|
|
2014
|
+
});
|
|
2015
|
+
return t9.callExpression(state.imports.render, args);
|
|
593
2016
|
}
|
|
594
|
-
function
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
2017
|
+
function transformTemplate(treeNode) {
|
|
2018
|
+
const result = {
|
|
2019
|
+
templates: [],
|
|
2020
|
+
dynamics: []
|
|
2021
|
+
};
|
|
2022
|
+
walkTreeNode(treeNode, result);
|
|
2023
|
+
return result;
|
|
2024
|
+
}
|
|
2025
|
+
function walkTreeNode(treeNode, result) {
|
|
2026
|
+
if (!treeNode) {
|
|
2027
|
+
return;
|
|
2028
|
+
}
|
|
2029
|
+
switch (treeNode.type) {
|
|
2030
|
+
case 1 /* COMPONENT */:
|
|
2031
|
+
case 3 /* FRAGMENT */:
|
|
2032
|
+
handleComponent(treeNode, result);
|
|
2033
|
+
break;
|
|
2034
|
+
case 4 /* EXPRESSION */:
|
|
2035
|
+
handleExpression(treeNode, result);
|
|
2036
|
+
break;
|
|
2037
|
+
case 2 /* TEXT */:
|
|
2038
|
+
handleText(treeNode, result);
|
|
2039
|
+
break;
|
|
2040
|
+
case 0 /* NORMAL */:
|
|
2041
|
+
case 5 /* SVG */:
|
|
2042
|
+
handleElement(treeNode, result);
|
|
2043
|
+
break;
|
|
2044
|
+
case 6 /* COMMENT */:
|
|
2045
|
+
addTemplate(result, "<!>", true);
|
|
2046
|
+
break;
|
|
2047
|
+
default:
|
|
2048
|
+
if (treeNode.children && treeNode.children.length > 0) {
|
|
2049
|
+
processChildren2(treeNode.children, result);
|
|
605
2050
|
}
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
})) || false;
|
|
2051
|
+
break;
|
|
2052
|
+
}
|
|
609
2053
|
}
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
2054
|
+
var addTemplate = (result, content, join = false) => {
|
|
2055
|
+
if (result.templates.length === 0) {
|
|
2056
|
+
result.templates.push(content);
|
|
2057
|
+
} else {
|
|
2058
|
+
if (join) {
|
|
2059
|
+
result.templates[result.templates.length - 1] += content;
|
|
2060
|
+
} else {
|
|
2061
|
+
result.templates.push(content);
|
|
2062
|
+
}
|
|
2063
|
+
}
|
|
2064
|
+
};
|
|
2065
|
+
var handleComponent = (node, result) => {
|
|
2066
|
+
result.templates.push("");
|
|
2067
|
+
const { state } = getContext();
|
|
2068
|
+
const componentProps = __spreadValues({}, node.props);
|
|
2069
|
+
if (node.children.length) {
|
|
2070
|
+
componentProps.children = node.children;
|
|
2071
|
+
}
|
|
2072
|
+
addImport(importMap.createComponent);
|
|
2073
|
+
const componentExpr = t9.callExpression(state.imports.createComponent, [
|
|
2074
|
+
t9.identifier(node.tag),
|
|
2075
|
+
createPropsObjectExpression(componentProps, transformJSXToSSG)
|
|
2076
|
+
]);
|
|
2077
|
+
result.dynamics.push({
|
|
2078
|
+
type: "text",
|
|
2079
|
+
node: componentExpr
|
|
2080
|
+
});
|
|
2081
|
+
};
|
|
2082
|
+
var handleExpression = (node, result) => {
|
|
2083
|
+
if (!node.children || node.children.length === 0) {
|
|
2084
|
+
return;
|
|
2085
|
+
}
|
|
2086
|
+
const firstChild = node.children[0];
|
|
2087
|
+
result.templates.push("");
|
|
2088
|
+
if (j(firstChild)) {
|
|
2089
|
+
const { state } = getContext();
|
|
2090
|
+
addImport(importMap.escapeHTML);
|
|
2091
|
+
result.dynamics.push({
|
|
2092
|
+
type: "text",
|
|
2093
|
+
node: t9.callExpression(state.imports.escapeHTML, [t9.valueToNode(firstChild)])
|
|
2094
|
+
});
|
|
2095
|
+
} else if (w(firstChild)) {
|
|
2096
|
+
const exprNode = firstChild;
|
|
2097
|
+
if (t9.isCallExpression(exprNode) && t9.isMemberExpression(exprNode.callee) && exprNode.arguments.length > 0) {
|
|
2098
|
+
const mapCallback = exprNode.arguments[0];
|
|
2099
|
+
if ((t9.isArrowFunctionExpression(mapCallback) || t9.isFunctionExpression(mapCallback)) && mapCallback.body) {
|
|
2100
|
+
let jsxElement = void 0;
|
|
2101
|
+
if (t9.isJSXElement(mapCallback.body)) {
|
|
2102
|
+
jsxElement = mapCallback.body;
|
|
2103
|
+
} else if (t9.isBlockStatement(mapCallback.body)) {
|
|
2104
|
+
const returnStmt = mapCallback.body.body.find((stmt) => t9.isReturnStatement(stmt));
|
|
2105
|
+
if (returnStmt && t9.isReturnStatement(returnStmt) && returnStmt.argument && t9.isJSXElement(returnStmt.argument)) {
|
|
2106
|
+
jsxElement = returnStmt.argument;
|
|
2107
|
+
}
|
|
2108
|
+
} else if (t9.isParenthesizedExpression(mapCallback.body) && t9.isJSXElement(mapCallback.body.expression)) {
|
|
2109
|
+
jsxElement = mapCallback.body.expression;
|
|
2110
|
+
}
|
|
2111
|
+
if (jsxElement) {
|
|
2112
|
+
const { state } = getContext();
|
|
2113
|
+
const tagName = jsxElement.openingElement.name.type === "JSXIdentifier" ? jsxElement.openingElement.name.name : "";
|
|
2114
|
+
const props = {};
|
|
2115
|
+
jsxElement.openingElement.attributes.forEach((attr) => {
|
|
2116
|
+
if (t9.isJSXAttribute(attr)) {
|
|
2117
|
+
const name = attr.name.name;
|
|
2118
|
+
if (t9.isJSXExpressionContainer(attr.value)) {
|
|
2119
|
+
props[name] = attr.value.expression;
|
|
2120
|
+
} else if (t9.isStringLiteral(attr.value)) {
|
|
2121
|
+
props[name] = attr.value.value;
|
|
2122
|
+
} else if (attr.value === null) {
|
|
2123
|
+
props[name] = true;
|
|
2124
|
+
}
|
|
2125
|
+
} else if (t9.isJSXSpreadAttribute(attr)) {
|
|
2126
|
+
props._$spread$ = attr.argument;
|
|
2127
|
+
}
|
|
2128
|
+
});
|
|
2129
|
+
if (tagName === FRAGMENT_NAME) {
|
|
2130
|
+
addImport(importMap.Fragment);
|
|
2131
|
+
const newCallback = t9.arrowFunctionExpression(
|
|
2132
|
+
mapCallback.params,
|
|
2133
|
+
t9.callExpression(state.imports.Fragment, [
|
|
2134
|
+
createPropsObjectExpression(props, transformJSXToSSG)
|
|
2135
|
+
])
|
|
2136
|
+
);
|
|
2137
|
+
exprNode.arguments[0] = newCallback;
|
|
2138
|
+
} else if (tagName && tagName[0] === tagName[0].toUpperCase()) {
|
|
2139
|
+
addImport(importMap.createComponent);
|
|
2140
|
+
const newCallback = t9.arrowFunctionExpression(
|
|
2141
|
+
mapCallback.params,
|
|
2142
|
+
t9.callExpression(state.imports.createComponent, [
|
|
2143
|
+
t9.identifier(tagName),
|
|
2144
|
+
createPropsObjectExpression(props, transformJSXToSSG)
|
|
2145
|
+
])
|
|
2146
|
+
);
|
|
2147
|
+
exprNode.arguments[0] = newCallback;
|
|
2148
|
+
}
|
|
628
2149
|
}
|
|
629
2150
|
}
|
|
2151
|
+
}
|
|
2152
|
+
result.dynamics.push({
|
|
2153
|
+
type: "text",
|
|
2154
|
+
node: exprNode
|
|
630
2155
|
});
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
2156
|
+
}
|
|
2157
|
+
};
|
|
2158
|
+
var handleText = (node, result) => {
|
|
2159
|
+
if (node.children && node.children.length > 0) {
|
|
2160
|
+
addTemplate(result, node.children.join(""), true);
|
|
2161
|
+
}
|
|
2162
|
+
};
|
|
2163
|
+
var handleElement = (node, result) => {
|
|
2164
|
+
let openTag = `<${node.tag} data-idx="${node.index}"`;
|
|
2165
|
+
const { staticAttrs, dynamicAttrs } = processAttributes(node.props || {});
|
|
2166
|
+
openTag += staticAttrs;
|
|
2167
|
+
addTemplate(result, openTag, true);
|
|
2168
|
+
addTemplate(result, node.selfClosing ? "/>" : ">", dynamicAttrs.length === 0);
|
|
2169
|
+
if (!node.selfClosing && node.children && node.children.length > 0) {
|
|
2170
|
+
processChildren2(node.children, result);
|
|
2171
|
+
}
|
|
2172
|
+
if (!node.selfClosing) {
|
|
2173
|
+
addTemplate(result, `</${node.tag}>`, true);
|
|
2174
|
+
}
|
|
2175
|
+
processDynamicAttributes(dynamicAttrs, result);
|
|
2176
|
+
};
|
|
2177
|
+
var processChildren2 = (children, result) => {
|
|
2178
|
+
children.forEach((child) => {
|
|
2179
|
+
if (isTreeNode(child)) {
|
|
2180
|
+
walkTreeNode(child, result);
|
|
2181
|
+
} else if (m(child)) {
|
|
2182
|
+
addTemplate(result, child, true);
|
|
2183
|
+
}
|
|
2184
|
+
});
|
|
2185
|
+
};
|
|
2186
|
+
function processAttributes(props) {
|
|
2187
|
+
let staticAttrs = "";
|
|
2188
|
+
const dynamicAttrs = [];
|
|
2189
|
+
for (const [prop, value] of Object.entries(props)) {
|
|
2190
|
+
if (prop.startsWith(EVENT_ATTR_NAME) || prop.startsWith(UPDATE_PREFIX)) {
|
|
2191
|
+
continue;
|
|
2192
|
+
}
|
|
2193
|
+
if (j(value)) {
|
|
2194
|
+
if (value === true) {
|
|
2195
|
+
staticAttrs += ` ${prop}`;
|
|
2196
|
+
} else if (A(value)) {
|
|
2197
|
+
staticAttrs += ` ${prop}="${value.toString()}"`;
|
|
2198
|
+
} else if (value !== false) {
|
|
2199
|
+
staticAttrs += ` ${prop}="${value}"`;
|
|
2200
|
+
}
|
|
2201
|
+
} else if (t9.isExpression(value)) {
|
|
2202
|
+
dynamicAttrs.push({ name: prop, value });
|
|
653
2203
|
}
|
|
654
2204
|
}
|
|
2205
|
+
return { staticAttrs, dynamicAttrs };
|
|
655
2206
|
}
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
2207
|
+
var processDynamicAttributes = (dynamicAttrs, result) => {
|
|
2208
|
+
dynamicAttrs.forEach((attr) => {
|
|
2209
|
+
const { state } = getContext();
|
|
2210
|
+
addImport(importMap.patchAttr);
|
|
2211
|
+
addImport(importMap.escapeHTML);
|
|
2212
|
+
result.dynamics.push({
|
|
2213
|
+
type: "attr",
|
|
2214
|
+
node: t9.callExpression(state.imports.patchAttr, [
|
|
2215
|
+
t9.stringLiteral(attr.name),
|
|
2216
|
+
t9.callExpression(state.imports.escapeHTML, [attr.value]),
|
|
2217
|
+
t9.booleanLiteral(false)
|
|
2218
|
+
]),
|
|
2219
|
+
attrName: attr.name
|
|
2220
|
+
});
|
|
2221
|
+
});
|
|
2222
|
+
};
|
|
2223
|
+
|
|
2224
|
+
// src/jsx/index.ts
|
|
2225
|
+
var transformStrategies = {
|
|
2226
|
+
["client" /* CLIENT */]: transformJSXToClient,
|
|
2227
|
+
["ssr" /* SSR */]: transformJSXToClient,
|
|
2228
|
+
["ssg" /* SSG */]: transformJSXToSSG
|
|
2229
|
+
};
|
|
2230
|
+
var getRenderingStrategy = (mode) => {
|
|
2231
|
+
const strategy = transformStrategies[mode];
|
|
2232
|
+
if (!strategy) {
|
|
2233
|
+
throw new Error(`Unsupported render mode: ${mode}`);
|
|
2234
|
+
}
|
|
2235
|
+
return strategy;
|
|
2236
|
+
};
|
|
2237
|
+
function transformJSX(path) {
|
|
2238
|
+
var _a, _b;
|
|
2239
|
+
try {
|
|
2240
|
+
const mode = ((_b = (_a = path.state) == null ? void 0 : _a.opts) == null ? void 0 : _b.mode) || "client" /* CLIENT */;
|
|
2241
|
+
const strategy = getRenderingStrategy(mode);
|
|
2242
|
+
const tree = createTree(path);
|
|
2243
|
+
setContext({ state: path.state, path, operationIndex: 0 });
|
|
2244
|
+
const result = strategy(path, tree);
|
|
2245
|
+
path.replaceWith(result);
|
|
2246
|
+
resetContext();
|
|
2247
|
+
} catch (error_) {
|
|
2248
|
+
console.warn("JSX transformation failed in test:", { error_ });
|
|
2249
|
+
resetContext();
|
|
2250
|
+
throw error_;
|
|
2251
|
+
}
|
|
666
2252
|
}
|
|
667
2253
|
|
|
668
2254
|
// src/index.ts
|
|
669
|
-
function
|
|
2255
|
+
function index_default() {
|
|
670
2256
|
return {
|
|
671
2257
|
name: "babel-plugin-essor",
|
|
672
|
-
manipulateOptions(
|
|
673
|
-
if (filename.endsWith(".ts") || filename.endsWith(".tsx")) {
|
|
674
|
-
parserOpts.plugins.push("typescript");
|
|
675
|
-
}
|
|
2258
|
+
manipulateOptions(_2, parserOpts) {
|
|
676
2259
|
parserOpts.plugins.push("jsx");
|
|
677
2260
|
},
|
|
678
2261
|
visitor: {
|
|
679
2262
|
Program: transformProgram,
|
|
680
|
-
|
|
681
|
-
|
|
2263
|
+
// props
|
|
2264
|
+
FunctionDeclaration: transformProps,
|
|
2265
|
+
ArrowFunctionExpression: transformProps,
|
|
2266
|
+
// Symbol
|
|
682
2267
|
VariableDeclarator: replaceSymbol,
|
|
683
|
-
|
|
2268
|
+
// let $x = 0 → let $x = signal(0)
|
|
684
2269
|
Identifier: symbolIdentifier,
|
|
2270
|
+
// $x → $x.value
|
|
2271
|
+
AssignmentExpression: symbolAssignment,
|
|
2272
|
+
// $x = 1 → $x.value = 1
|
|
2273
|
+
UpdateExpression: symbolUpdate,
|
|
2274
|
+
// $x++ → $x.value++
|
|
685
2275
|
ObjectPattern: symbolObjectPattern,
|
|
2276
|
+
// { $x } → handle nested patterns
|
|
686
2277
|
ArrayPattern: symbolArrayPattern,
|
|
2278
|
+
// [$x] → handle nested patterns
|
|
2279
|
+
// JSX
|
|
687
2280
|
JSXElement: transformJSX,
|
|
688
2281
|
JSXFragment: transformJSX
|
|
689
2282
|
}
|
|
690
2283
|
};
|
|
691
2284
|
}
|
|
692
2285
|
export {
|
|
693
|
-
|
|
2286
|
+
index_default as default
|
|
694
2287
|
};
|
|
695
2288
|
//# sourceMappingURL=index.js.map
|