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