babel-plugin-essor 0.0.5 → 0.0.6-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -20,15 +20,824 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
- one: () => one,
24
- two: () => two
23
+ default: () => src_default
25
24
  });
26
25
  module.exports = __toCommonJS(src_exports);
27
- var one = 1;
28
- var two = 2;
29
- // Annotate the CommonJS export names for ESM import in node:
30
- 0 && (module.exports = {
31
- one,
32
- two
33
- });
26
+
27
+ // src/jsx/server.ts
28
+ var import_core3 = require("@babel/core");
29
+
30
+ // src/program.ts
31
+ var import_core = require("@babel/core");
32
+ var imports = /* @__PURE__ */ new Set();
33
+ var transformProgram = {
34
+ enter(path, state) {
35
+ path.state = {
36
+ h: path.scope.generateUidIdentifier("h$"),
37
+ ssrtmpl: path.scope.generateUidIdentifier("ssrtmpl$"),
38
+ ssr: path.scope.generateUidIdentifier("ssr$"),
39
+ template: path.scope.generateUidIdentifier("template$"),
40
+ useSignal: path.scope.generateUidIdentifier("signal$"),
41
+ useComputed: path.scope.generateUidIdentifier("computed$"),
42
+ tmplDeclaration: import_core.types.variableDeclaration("const", []),
43
+ opts: state.opts
44
+ };
45
+ },
46
+ exit(path) {
47
+ const state = path.state;
48
+ if (state.tmplDeclaration.declarations.length > 0) {
49
+ const index = path.node.body.findIndex(
50
+ (node) => !import_core.types.isImportDeclaration(node) && !import_core.types.isExportDeclaration(node)
51
+ );
52
+ path.node.body.splice(index, 0, state.tmplDeclaration);
53
+ }
54
+ if (imports.size > 0) {
55
+ path.node.body.unshift(createImport(state, "essor"));
56
+ }
57
+ }
58
+ };
59
+ function createImport(state, from) {
60
+ const ImportSpecifier = [];
61
+ imports.forEach((name) => {
62
+ const local = import_core.types.identifier(state[name].name);
63
+ const imported = import_core.types.identifier(name);
64
+ ImportSpecifier.push(import_core.types.importSpecifier(local, imported));
65
+ });
66
+ const importSource = import_core.types.stringLiteral(from);
67
+ return import_core.types.importDeclaration(ImportSpecifier, importSource);
68
+ }
69
+
70
+ // src/jsx/constants.ts
71
+ var selfClosingTags = [
72
+ "area",
73
+ "base",
74
+ "br",
75
+ "col",
76
+ "embed",
77
+ "hr",
78
+ "img",
79
+ "input",
80
+ "link",
81
+ "meta",
82
+ "param",
83
+ "source",
84
+ "track",
85
+ "wbr"
86
+ ];
87
+ var svgTags = [
88
+ "circle",
89
+ "clipPath",
90
+ "defs",
91
+ "ellipse",
92
+ "filter",
93
+ "g",
94
+ "line",
95
+ "linearGradient",
96
+ "mask",
97
+ "path",
98
+ "pattern",
99
+ "polygon",
100
+ "polyline",
101
+ "radialGradient",
102
+ "rect",
103
+ "stop",
104
+ "symbol",
105
+ "text",
106
+ "use"
107
+ ];
108
+
109
+ // src/jsx/client.ts
110
+ var import_core2 = require("@babel/core");
111
+ function transformJSXClient(path) {
112
+ const result = {
113
+ index: 1,
114
+ isLastChild: false,
115
+ parentIndex: 0,
116
+ props: {},
117
+ template: ""
118
+ };
119
+ transformJSXElement(path, result, true);
120
+ path.replaceWith(createEssorNode(path, result));
121
+ }
122
+ function createEssorNode(path, result) {
123
+ var _a;
124
+ const state = path.state;
125
+ let tmpl;
126
+ if (path.isJSXElement() && isComponent(getTagName(path.node))) {
127
+ tmpl = import_core2.types.identifier(getTagName(path.node));
128
+ } else {
129
+ tmpl = path.scope.generateUidIdentifier("_tmpl$");
130
+ const template = import_core2.types.callExpression(state.template, [import_core2.types.stringLiteral(result.template)]);
131
+ const declarator = import_core2.types.variableDeclarator(tmpl, template);
132
+ state.tmplDeclaration.declarations.push(declarator);
133
+ imports.add("template");
134
+ }
135
+ const args = [tmpl, createProps(result.props)];
136
+ const key = result.props.key || ((_a = result.props[0]) == null ? void 0 : _a.key);
137
+ if (key) {
138
+ args.push(key);
139
+ }
140
+ imports.add("h");
141
+ return import_core2.types.callExpression(state.h, args);
142
+ }
143
+ function createProps(props) {
144
+ const toAstNode = (value) => {
145
+ if (Array.isArray(value)) {
146
+ return import_core2.types.arrayExpression(value.map(toAstNode));
147
+ }
148
+ if (value && typeof value === "object" && !import_core2.types.isNode(value)) {
149
+ return createProps(value);
150
+ }
151
+ switch (typeof value) {
152
+ case "string":
153
+ return import_core2.types.stringLiteral(value);
154
+ case "number":
155
+ return import_core2.types.numericLiteral(value);
156
+ case "boolean":
157
+ return import_core2.types.booleanLiteral(value);
158
+ case "undefined":
159
+ return import_core2.types.tsUndefinedKeyword();
160
+ case void 0:
161
+ return import_core2.types.tsUndefinedKeyword();
162
+ case null:
163
+ return import_core2.types.nullLiteral();
164
+ default:
165
+ return value;
166
+ }
167
+ };
168
+ const result = Object.keys(props).filter((prop) => prop !== "key").map((prop) => {
169
+ const value = toAstNode(props[prop]);
170
+ return prop === "_$spread$" ? import_core2.types.spreadElement(value) : import_core2.types.objectProperty(import_core2.types.stringLiteral(prop), value);
171
+ });
172
+ return import_core2.types.objectExpression(result);
173
+ }
174
+ function transformJSXElement(path, result, isRoot = false) {
175
+ if (path.isJSXElement()) {
176
+ const tagName = getTagName(path.node);
177
+ const tagIsComponent = isComponent(tagName);
178
+ const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
179
+ const isSvg = svgTags.includes(tagName) && result.index === 1;
180
+ const props = getAttrProps(path);
181
+ if (tagIsComponent) {
182
+ if (isRoot) {
183
+ result.props = props;
184
+ const children = getChildren(path);
185
+ if (children.length > 0) {
186
+ const childrenGenerator = children.length === 1 ? children[0] : import_core2.types.arrayExpression(children);
187
+ result.props.children = childrenGenerator;
188
+ }
189
+ } else {
190
+ transformJSXClient(path);
191
+ replaceChild(path.node, result);
192
+ }
193
+ } else {
194
+ if (isSvg) {
195
+ result.template = "<svg _svg_>";
196
+ }
197
+ result.template += `<${tagName}`;
198
+ handleAttributes(props, result);
199
+ result.template += isSelfClose ? "/>" : ">";
200
+ if (!isSelfClose) {
201
+ transformChildren(path, result);
202
+ result.template += `</${tagName}>`;
203
+ }
204
+ }
205
+ } else {
206
+ result.index--;
207
+ transformChildren(path, result);
208
+ }
209
+ }
210
+ function transformChildren(path, result) {
211
+ const parentIndex = result.index;
212
+ path.get("children").reduce((pre, cur) => {
213
+ if (isValidChild(cur)) {
214
+ const lastChild = pre.at(-1);
215
+ if (lastChild && isTextChild(cur) && isTextChild(lastChild)) {
216
+ setNodeText(lastChild, getNodeText(lastChild) + getNodeText(cur));
217
+ } else {
218
+ pre.push(cur);
219
+ }
220
+ }
221
+ return pre;
222
+ }, []).forEach((child, i, arr) => {
223
+ result.parentIndex = parentIndex;
224
+ result.isLastChild = i === arr.length - 1;
225
+ transformChild(child, result);
226
+ });
227
+ }
228
+ function transformChild(child, result) {
229
+ result.index++;
230
+ if (child.isJSXElement() || child.isJSXFragment()) {
231
+ transformJSXElement(child, result, false);
232
+ } else if (child.isJSXExpressionContainer()) {
233
+ const expression = child.get("expression");
234
+ if (expression.isStringLiteral() || expression.isNumericLiteral()) {
235
+ result.template += String(expression.node.value);
236
+ } else if (expression.isExpression()) {
237
+ replaceChild(expression.node, result);
238
+ } else {
239
+ throw new Error("Unsupported child type");
240
+ }
241
+ } else if (child.isJSXText()) {
242
+ result.template += String(child.node.value);
243
+ } else {
244
+ throw new Error("Unsupported child type");
245
+ }
246
+ }
247
+ function getNodeText(path) {
248
+ if (path.isJSXText()) {
249
+ return path.node.value;
250
+ }
251
+ if (path.isJSXExpressionContainer()) {
252
+ const expression = path.get("expression");
253
+ if (expression.isStringLiteral() || expression.isNumericLiteral()) {
254
+ return String(expression.node.value);
255
+ }
256
+ }
257
+ return "";
258
+ }
259
+ function setNodeText(path, text) {
260
+ if (path.isJSXText()) {
261
+ path.node.value = text;
262
+ }
263
+ if (path.isJSXExpressionContainer()) {
264
+ const expression = path.get("expression");
265
+ if (expression.isStringLiteral() || expression.isNumericLiteral()) {
266
+ expression.replaceWith(import_core2.types.stringLiteral(text));
267
+ }
268
+ }
269
+ }
270
+ function isTextChild(path) {
271
+ if (path.isJSXExpressionContainer()) {
272
+ const expression = path.get("expression");
273
+ if (expression.isJSXText() || expression.isStringLiteral() || expression.isNumericLiteral()) {
274
+ return true;
275
+ }
276
+ }
277
+ if (path.isJSXText() || path.isStringLiteral() || path.isNullLiteral()) {
278
+ return true;
279
+ }
280
+ return false;
281
+ }
282
+ function handleAttributes(props, result) {
283
+ let klass = "";
284
+ let style = "";
285
+ for (const prop in props) {
286
+ const value = props[prop];
287
+ if (prop === "class" && typeof value === "string") {
288
+ klass += ` ${value}`;
289
+ delete props[prop];
290
+ continue;
291
+ }
292
+ if (prop.indexOf("class:") === 0) {
293
+ if (value === true) {
294
+ const name = prop.replace(/^class:/, "");
295
+ klass += ` ${name}`;
296
+ delete props[prop];
297
+ continue;
298
+ }
299
+ if (value === false) {
300
+ delete props[prop];
301
+ continue;
302
+ }
303
+ }
304
+ if (prop === "style" && typeof value === "string") {
305
+ style += `${value}${value.at(-1) === ";" ? "" : ";"}`;
306
+ delete props[prop];
307
+ continue;
308
+ }
309
+ if (prop.indexOf("style:") === 0 && (typeof value === "string" || typeof value === "number")) {
310
+ const name = prop.replace(/^style:/, "");
311
+ style += `${name}:${value};`;
312
+ delete props[prop];
313
+ continue;
314
+ }
315
+ if (value === true) {
316
+ result.template += ` ${prop}`;
317
+ delete props[prop];
318
+ }
319
+ if (value === false) {
320
+ delete props[prop];
321
+ }
322
+ if (typeof value === "string" || typeof value === "number") {
323
+ result.template += ` ${prop}="${value}"`;
324
+ delete props[prop];
325
+ }
326
+ }
327
+ if (Object.keys(props).length > 0) {
328
+ result.props[result.index] = props;
329
+ }
330
+ klass = klass.trim();
331
+ style = style.trim();
332
+ if (klass) {
333
+ result.template += ` class="${klass}"`;
334
+ }
335
+ if (style) {
336
+ result.template += ` style="${style}"`;
337
+ }
338
+ }
339
+ function replaceChild(node, result) {
340
+ var _a, _b, _c, _d, _e;
341
+ if (result.isLastChild) {
342
+ result.index--;
343
+ } else {
344
+ result.template += "<!-->";
345
+ }
346
+ (_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
347
+ (_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
348
+ result.props[result.parentIndex].children.push(
349
+ import_core2.types.arrayExpression([
350
+ import_core2.types.arrowFunctionExpression([], node),
351
+ result.isLastChild ? import_core2.types.nullLiteral() : import_core2.types.identifier(String(result.index))
352
+ ])
353
+ );
354
+ }
355
+ function getChildren(path) {
356
+ return path.get("children").filter((child) => isValidChild(child)).map((child) => {
357
+ if (child.isJSXElement() || child.isJSXFragment()) {
358
+ transformJSXClient(child);
359
+ } else if (child.isJSXExpressionContainer()) {
360
+ child.replaceWith(child.get("expression"));
361
+ } else if (child.isJSXText()) {
362
+ child.replaceWith(import_core2.types.stringLiteral(child.node.value));
363
+ } else {
364
+ throw new Error("Unsupported child type");
365
+ }
366
+ return child.node;
367
+ });
368
+ }
369
+ function isValidChild(path) {
370
+ const regex = /^\s*$/;
371
+ if (path.isStringLiteral() || path.isJSXText()) {
372
+ return !regex.test(path.node.value);
373
+ }
374
+ return Object.keys(path.node).length > 0;
375
+ }
376
+ function getAttrProps(path) {
377
+ const props = {};
378
+ path.get("openingElement").get("attributes").forEach((attribute) => {
379
+ if (attribute.isJSXAttribute()) {
380
+ const name = getAttrName(attribute.node);
381
+ const value = attribute.get("value");
382
+ if (!value.node) {
383
+ props[name] = true;
384
+ } else if (value.isStringLiteral()) {
385
+ props[name] = value.node.value;
386
+ } else {
387
+ if (value.isJSXExpressionContainer()) {
388
+ const expression = value.get("expression");
389
+ if (expression.isStringLiteral()) {
390
+ props[name] = expression.node.value;
391
+ } else if (expression.isNumericLiteral()) {
392
+ props[name] = expression.node.value;
393
+ } else if (expression.isJSXElement() || expression.isJSXFragment()) {
394
+ transformJSXClient(expression);
395
+ props[name] = expression.node;
396
+ } else if (expression.isExpression()) {
397
+ if (/^key|ref|on.+$/.test(name)) {
398
+ props[name] = expression.node;
399
+ } else if (/^bind:.+/.test(name)) {
400
+ props[name] = import_core2.types.arrowFunctionExpression([], expression.node);
401
+ } else {
402
+ if (expression.isConditionalExpression()) {
403
+ props[name] = import_core2.types.arrowFunctionExpression([], expression.node);
404
+ } else {
405
+ props[name] = expression.node;
406
+ }
407
+ }
408
+ }
409
+ } else if (value.isJSXElement() || value.isJSXFragment()) {
410
+ transformJSXClient(value);
411
+ props[name] = value.node;
412
+ }
413
+ }
414
+ } else if (attribute.isJSXSpreadAttribute()) {
415
+ props._$spread$ = attribute.get("argument").node;
416
+ } else {
417
+ throw new Error("Unsupported attribute type");
418
+ }
419
+ });
420
+ return props;
421
+ }
422
+ function getAttrName(attribute) {
423
+ if (import_core2.types.isJSXIdentifier(attribute.name)) {
424
+ return attribute.name.name;
425
+ }
426
+ if (import_core2.types.isJSXNamespacedName(attribute.name)) {
427
+ return `${attribute.name.namespace.name}:${attribute.name.name.name}`;
428
+ }
429
+ throw new Error("Unsupported attribute type");
430
+ }
431
+ function isComponent(tagName) {
432
+ return tagName[0] && tagName[0].toLowerCase() !== tagName[0] || tagName.includes(".") || /[^A-Za-z]/.test(tagName[0]);
433
+ }
434
+ function getTagName(node) {
435
+ const tag = node.openingElement.name;
436
+ return jsxElementNameToString(tag);
437
+ }
438
+ function jsxElementNameToString(node) {
439
+ if (import_core2.types.isJSXMemberExpression(node)) {
440
+ return `${jsxElementNameToString(node.object)}.${jsxElementNameToString(node.property)}`;
441
+ }
442
+ if (import_core2.types.isJSXIdentifier(node) || import_core2.types.isIdentifier(node)) {
443
+ return node.name;
444
+ }
445
+ return `${node.namespace.name}:${node.name.name}`;
446
+ }
447
+
448
+ // src/jsx/server.ts
449
+ function transformJSXService(path) {
450
+ const result = {
451
+ index: 0,
452
+ isLastChild: false,
453
+ parentIndex: 0,
454
+ props: {},
455
+ template: []
456
+ // 修改为数组
457
+ };
458
+ transformJSXServiceElement(path, result, true);
459
+ path.replaceWith(createEssorNode2(path, result));
460
+ }
461
+ function createEssorNode2(path, result) {
462
+ var _a;
463
+ const state = path.state;
464
+ let tmpl;
465
+ if (path.isJSXElement() && isComponent(getTagName(path.node))) {
466
+ tmpl = import_core3.types.identifier(getTagName(path.node));
467
+ } else {
468
+ tmpl = path.scope.generateUidIdentifier("_tmpl$");
469
+ const template = import_core3.types.callExpression(state.ssrtmpl, [
470
+ import_core3.types.arrayExpression(result.template.map(import_core3.types.stringLiteral))
471
+ ]);
472
+ const declarator = import_core3.types.variableDeclarator(tmpl, template);
473
+ state.tmplDeclaration.declarations.push(declarator);
474
+ imports.add("ssrtmpl");
475
+ }
476
+ const args = [tmpl, createProps2(result.props)];
477
+ const key = result.props.key || ((_a = result.props[0]) == null ? void 0 : _a.key);
478
+ if (key) {
479
+ args.push(key);
480
+ }
481
+ imports.add("ssr");
482
+ return import_core3.types.callExpression(state.ssr, args);
483
+ }
484
+ function createProps2(props) {
485
+ const result = [];
486
+ for (const prop in props) {
487
+ let value = props[prop];
488
+ if (prop === "key") {
489
+ continue;
490
+ }
491
+ if (Array.isArray(value)) {
492
+ value = import_core3.types.arrayExpression(value);
493
+ }
494
+ if (typeof value === "object" && value !== null && !import_core3.types.isNode(value)) {
495
+ value = createProps2(value);
496
+ }
497
+ if (typeof value === "string") {
498
+ value = import_core3.types.stringLiteral(value);
499
+ }
500
+ if (typeof value === "number") {
501
+ value = import_core3.types.numericLiteral(value);
502
+ }
503
+ if (typeof value === "boolean") {
504
+ value = import_core3.types.booleanLiteral(value);
505
+ }
506
+ if (value === void 0) {
507
+ value = import_core3.types.tsUndefinedKeyword();
508
+ }
509
+ if (value === null) {
510
+ value = import_core3.types.nullLiteral();
511
+ }
512
+ if (prop === "_$spread$") {
513
+ result.push(import_core3.types.spreadElement(value));
514
+ } else {
515
+ result.push(import_core3.types.objectProperty(import_core3.types.stringLiteral(prop), value));
516
+ }
517
+ }
518
+ return import_core3.types.objectExpression(result);
519
+ }
520
+ function transformJSXServiceElement(path, result, isRoot = false) {
521
+ if (path.isJSXElement()) {
522
+ const tagName = getTagName(path.node);
523
+ const tagIsComponent = isComponent(tagName);
524
+ const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
525
+ const isSvg = svgTags.includes(tagName) && result.index === 1;
526
+ const { props, hasExpression } = getAttrProps2(path);
527
+ if (tagIsComponent) {
528
+ if (isRoot) {
529
+ result.props = props;
530
+ const children = getChildren2(path);
531
+ if (children.length > 0) {
532
+ const childrenGenerator = children.length === 1 ? children[0] : import_core3.types.arrayExpression(children);
533
+ result.props.children = childrenGenerator;
534
+ }
535
+ } else {
536
+ transformJSXService(path);
537
+ replaceChild2(path.node, result);
538
+ }
539
+ } else {
540
+ if (isSvg) {
541
+ result.template.push("<svg _svg_>");
542
+ }
543
+ result.template.push(`<${tagName}`);
544
+ handleAttributes2(props, result);
545
+ if (hasExpression) {
546
+ result.template.push(isSelfClose ? "/>" : ">");
547
+ result.props || (result.props = {});
548
+ } else {
549
+ result.template[result.template.length - 1] += isSelfClose ? "/>" : ">";
550
+ }
551
+ transformChildren2(path, result);
552
+ if (!isSelfClose) {
553
+ result.template.push(`</${tagName}>`);
554
+ }
555
+ }
556
+ } else {
557
+ result.index--;
558
+ transformChildren2(path, result);
559
+ }
560
+ }
561
+ function transformChildren2(path, result) {
562
+ const parentIndex = result.template.length;
563
+ path.get("children").reduce((pre, cur) => {
564
+ if (isValidChild2(cur)) {
565
+ const lastChild = pre.at(-1);
566
+ if (lastChild && isTextChild(cur) && isTextChild(lastChild)) {
567
+ setNodeText2(lastChild, getNodeText2(lastChild) + getNodeText2(cur));
568
+ } else {
569
+ pre.push(cur);
570
+ }
571
+ }
572
+ return pre;
573
+ }, []).forEach((child, i, arr) => {
574
+ result.parentIndex = parentIndex;
575
+ result.isLastChild = i === arr.length - 1;
576
+ transformChild2(child, result);
577
+ });
578
+ }
579
+ function transformChild2(child, result) {
580
+ if (child.isJSXElement() || child.isJSXFragment()) {
581
+ transformJSXServiceElement(child, result, false);
582
+ } else if (child.isJSXExpressionContainer()) {
583
+ const expression = child.get("expression");
584
+ if (expression.isStringLiteral() || expression.isNumericLiteral()) {
585
+ result.template[result.template.length - 1] += String(expression.node.value);
586
+ } else if (expression.isExpression()) {
587
+ replaceChild2(expression.node, result);
588
+ } else {
589
+ throw new Error("Unsupported child type");
590
+ }
591
+ } else if (child.isJSXText()) {
592
+ result.template.push(String(child.node.value));
593
+ } else {
594
+ throw new Error("Unsupported child type");
595
+ }
596
+ }
597
+ function getNodeText2(path) {
598
+ if (path.isJSXText()) {
599
+ return path.node.value;
600
+ }
601
+ if (path.isJSXExpressionContainer()) {
602
+ const expression = path.get("expression");
603
+ if (expression.isStringLiteral() || expression.isNumericLiteral()) {
604
+ return String(expression.node.value);
605
+ }
606
+ }
607
+ return "";
608
+ }
609
+ function setNodeText2(path, text) {
610
+ if (path.isJSXText()) {
611
+ path.node.value = text;
612
+ }
613
+ if (path.isJSXExpressionContainer()) {
614
+ const expression = path.get("expression");
615
+ if (expression.isStringLiteral() || expression.isNumericLiteral()) {
616
+ expression.replaceWith(import_core3.types.stringLiteral(text));
617
+ }
618
+ }
619
+ }
620
+ function handleAttributes2(props, result) {
621
+ let klass = "";
622
+ let style = "";
623
+ for (const prop in props) {
624
+ const value = props[prop];
625
+ if (prop === "class" && typeof value === "string") {
626
+ klass += ` ${value}`;
627
+ delete props[prop];
628
+ continue;
629
+ }
630
+ if (prop.indexOf("class:") === 0) {
631
+ if (value === true) {
632
+ const name = prop.replace(/^class:/, "");
633
+ klass += ` ${name}`;
634
+ delete props[prop];
635
+ continue;
636
+ }
637
+ if (value === false) {
638
+ delete props[prop];
639
+ continue;
640
+ }
641
+ }
642
+ if (prop === "style" && typeof value === "string") {
643
+ style += `${value}${value.at(-1) === ";" ? "" : ";"}`;
644
+ delete props[prop];
645
+ continue;
646
+ }
647
+ if (prop.indexOf("style:") === 0 && (typeof value === "string" || typeof value === "number")) {
648
+ const name = prop.replace(/^style:/, "");
649
+ style += `${name}:${value};`;
650
+ delete props[prop];
651
+ continue;
652
+ }
653
+ if (value === true) {
654
+ result.template[result.template.length - 1] += ` ${prop}`;
655
+ delete props[prop];
656
+ }
657
+ if (value === false) {
658
+ delete props[prop];
659
+ }
660
+ if (typeof value === "string" || typeof value === "number") {
661
+ result.template[result.template.length - 1] += ` ${prop}="${value}"`;
662
+ delete props[prop];
663
+ }
664
+ }
665
+ if (Object.keys(props).length > 0) {
666
+ result.props[result.index] = props;
667
+ }
668
+ klass = klass.trim();
669
+ style = style.trim();
670
+ if (klass) {
671
+ result.template[result.template.length - 1] += ` class="${klass}"`;
672
+ }
673
+ if (style) {
674
+ result.template[result.template.length - 1] += ` style="${style}"`;
675
+ }
676
+ }
677
+ function replaceChild2(node, result) {
678
+ var _a, _b, _c, _d, _e;
679
+ if (result.isLastChild) {
680
+ result.index--;
681
+ }
682
+ (_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
683
+ (_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
684
+ result.props[result.parentIndex].children.push(
685
+ import_core3.types.arrayExpression([
686
+ import_core3.types.arrowFunctionExpression([], node),
687
+ import_core3.types.identifier(String(result.template.length))
688
+ ])
689
+ );
690
+ }
691
+ function getChildren2(path) {
692
+ return path.get("children").filter((child) => isValidChild2(child)).map((child) => {
693
+ if (child.isJSXElement() || child.isJSXFragment()) {
694
+ transformJSXService(child);
695
+ } else if (child.isJSXExpressionContainer()) {
696
+ child.replaceWith(child.get("expression"));
697
+ } else if (child.isJSXText()) {
698
+ child.replaceWith(import_core3.types.stringLiteral(child.node.value));
699
+ } else {
700
+ throw new Error("Unsupported child type");
701
+ }
702
+ return child.node;
703
+ });
704
+ }
705
+ function isValidChild2(path) {
706
+ const regex = /^\s*$/;
707
+ if (path.isStringLiteral() || path.isJSXText()) {
708
+ return !regex.test(path.node.value);
709
+ }
710
+ return Object.keys(path.node).length > 0;
711
+ }
712
+ function getAttrProps2(path) {
713
+ const props = {};
714
+ let hasExpression = false;
715
+ path.get("openingElement").get("attributes").forEach((attribute) => {
716
+ if (attribute.isJSXAttribute()) {
717
+ const name = getAttrName(attribute.node);
718
+ const value = attribute.get("value");
719
+ if (!value.node) {
720
+ props[name] = true;
721
+ } else if (value.isStringLiteral()) {
722
+ props[name] = value.node.value;
723
+ } else {
724
+ if (value.isJSXExpressionContainer()) {
725
+ const expression = value.get("expression");
726
+ if (expression.isStringLiteral()) {
727
+ props[name] = expression.node.value;
728
+ } else if (expression.isNumericLiteral()) {
729
+ props[name] = expression.node.value;
730
+ } else if (expression.isJSXElement() || expression.isJSXFragment()) {
731
+ transformJSXService(expression);
732
+ props[name] = expression.node;
733
+ } else if (expression.isExpression()) {
734
+ hasExpression = true;
735
+ if (/^key|ref|on.+$/.test(name)) {
736
+ props[name] = expression.node;
737
+ } else if (/^bind:.+/.test(name)) {
738
+ props[name] = import_core3.types.arrowFunctionExpression([], expression.node);
739
+ } else {
740
+ if (expression.isConditionalExpression()) {
741
+ props[name] = import_core3.types.arrowFunctionExpression([], expression.node);
742
+ } else {
743
+ props[name] = expression.node;
744
+ }
745
+ }
746
+ }
747
+ } else if (value.isJSXElement() || value.isJSXFragment()) {
748
+ transformJSXService(value);
749
+ props[name] = value.node;
750
+ }
751
+ }
752
+ } else if (attribute.isJSXSpreadAttribute()) {
753
+ props._$spread$ = attribute.get("argument").node;
754
+ hasExpression = true;
755
+ } else {
756
+ throw new Error("Unsupported attribute type");
757
+ }
758
+ });
759
+ return {
760
+ props,
761
+ hasExpression
762
+ };
763
+ }
764
+
765
+ // src/jsx/index.ts
766
+ function transformJSX(path) {
767
+ const state = path.state;
768
+ const isSsr = state.opts.ssr;
769
+ return isSsr ? transformJSXService(path) : transformJSXClient(path);
770
+ }
771
+
772
+ // src/signal/symbol.ts
773
+ var import_core4 = require("@babel/core");
774
+ var import_types = require("@babel/types");
775
+ function replaceSymbol(path) {
776
+ const init = path.node.init;
777
+ const variableName = path.node.id.name;
778
+ if (import_core4.types.isObjectPattern(path.node.id) || import_core4.types.isArrayPattern(path.node.id)) {
779
+ return;
780
+ }
781
+ if (!variableName.startsWith("$")) {
782
+ return;
783
+ }
784
+ if (init && (import_core4.types.isFunctionExpression(init) || import_core4.types.isArrowFunctionExpression(init)) && path.parent.kind === "const") {
785
+ const newInit = import_core4.types.callExpression(import_core4.types.identifier(path.state.useComputed.name), init ? [init] : []);
786
+ imports.add("useComputed");
787
+ path.node.init = newInit;
788
+ } else {
789
+ const originalImportDeclarationNodes = (0, import_types.cloneNode)(path.get("id").node, true);
790
+ const newInit = import_core4.types.callExpression(import_core4.types.identifier(path.state.useSignal.name), init ? [init] : []);
791
+ imports.add("useSignal");
792
+ path.node.init = newInit;
793
+ path.scope.rename(variableName, `${variableName}.value`);
794
+ path.get("id").replaceWith(originalImportDeclarationNodes);
795
+ }
796
+ }
797
+
798
+ // src/signal/import.ts
799
+ var import_core5 = require("@babel/core");
800
+ function isVariableUsedAsObject(path, variableName) {
801
+ const binding = path.scope.getBinding(variableName);
802
+ let isUsedObject = false;
803
+ if (!binding || !binding.referencePaths) {
804
+ return isUsedObject;
805
+ }
806
+ for (const referencePath of binding.referencePaths) {
807
+ if (import_core5.types.isMemberExpression(referencePath.parent)) {
808
+ isUsedObject = true;
809
+ }
810
+ }
811
+ return isUsedObject;
812
+ }
813
+ function replaceImportDeclaration(path) {
814
+ const imports2 = path.node.specifiers;
815
+ imports2.forEach((specifier) => {
816
+ const variableName = specifier.local.name;
817
+ if (variableName.indexOf("$") === 0 && !isVariableUsedAsObject(path, variableName)) {
818
+ path.scope.rename(variableName, `${variableName}.value`);
819
+ specifier.local.name = `${variableName}`;
820
+ }
821
+ });
822
+ }
823
+
824
+ // src/index.ts
825
+ function src_default() {
826
+ return {
827
+ name: "babel-plugin-essor",
828
+ manipulateOptions({ filename }, parserOpts) {
829
+ if (filename.endsWith(".ts") || filename.endsWith(".tsx")) {
830
+ parserOpts.plugins.push("typescript");
831
+ }
832
+ parserOpts.plugins.push("jsx");
833
+ },
834
+ visitor: {
835
+ Program: transformProgram,
836
+ JSXElement: transformJSX,
837
+ JSXFragment: transformJSX,
838
+ VariableDeclarator: replaceSymbol,
839
+ ImportDeclaration: replaceImportDeclaration
840
+ }
841
+ };
842
+ }
34
843
  //# sourceMappingURL=index.cjs.map