babel-plugin-essor 0.0.8 → 0.0.10-beta.21

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.js CHANGED
@@ -1,3 +1,23 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
4
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __spreadValues = (a, b) => {
7
+ for (var prop in b || (b = {}))
8
+ if (__hasOwnProp.call(b, prop))
9
+ __defNormalProp(a, prop, b[prop]);
10
+ if (__getOwnPropSymbols)
11
+ for (var prop of __getOwnPropSymbols(b)) {
12
+ if (__propIsEnum.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ }
15
+ return a;
16
+ };
17
+
18
+ // src/jsx/index.ts
19
+ import { types as t3 } from "@babel/core";
20
+
1
21
  // ../shared/dist/essor-shared.js
2
22
  var isArray = Array.isArray;
3
23
  function isString(val) {
@@ -14,15 +34,18 @@ var capitalizeFirstLetter = (inputString) => {
14
34
  return inputString.charAt(0).toUpperCase() + inputString.slice(1);
15
35
  };
16
36
 
17
- // src/jsx/server.ts
18
- import { types as t3 } from "@babel/core";
19
-
20
37
  // src/program.ts
21
38
  import { types as t } from "@babel/core";
22
39
  var imports = /* @__PURE__ */ new Set();
40
+ var defaultOption = {
41
+ ssg: false,
42
+ symbol: "$",
43
+ props: true
44
+ };
23
45
  var transformProgram = {
24
46
  enter(path, state) {
25
47
  imports.clear();
48
+ state.opts = __spreadValues(__spreadValues({}, defaultOption), state.opts);
26
49
  path.state = {
27
50
  h: path.scope.generateUidIdentifier("h$"),
28
51
  renderTemplate: path.scope.generateUidIdentifier("renderTemplate$"),
@@ -58,46 +81,7 @@ function createImport(state, from) {
58
81
  return t.importDeclaration(ImportSpecifier, importSource);
59
82
  }
60
83
 
61
- // src/jsx/constants.ts
62
- var selfClosingTags = [
63
- "area",
64
- "base",
65
- "br",
66
- "col",
67
- "embed",
68
- "hr",
69
- "img",
70
- "input",
71
- "link",
72
- "meta",
73
- "param",
74
- "source",
75
- "track",
76
- "wbr"
77
- ];
78
- var svgTags = [
79
- "circle",
80
- "clipPath",
81
- "defs",
82
- "ellipse",
83
- "filter",
84
- "g",
85
- "line",
86
- "linearGradient",
87
- "mask",
88
- "path",
89
- "pattern",
90
- "polygon",
91
- "polyline",
92
- "radialGradient",
93
- "rect",
94
- "stop",
95
- "symbol",
96
- "text",
97
- "use"
98
- ];
99
-
100
- // src/jsx/shared.ts
84
+ // src/shared.ts
101
85
  import { types as t2 } from "@babel/core";
102
86
  function hasSiblingElement(path) {
103
87
  const siblings = path.getAllPrevSiblings().concat(path.getAllNextSiblings());
@@ -154,363 +138,126 @@ function setNodeText(path, text) {
154
138
  }
155
139
  }
156
140
  }
157
-
158
- // src/jsx/server.ts
159
- function transformJSXService(path) {
160
- const result = {
161
- index: 0,
162
- isLastChild: false,
163
- parentIndex: 0,
164
- props: {},
165
- template: []
166
- // 修改为数组
167
- };
168
- transformJSXServiceElement(path, result, true);
169
- path.replaceWith(createEssorNode(path, result));
170
- }
171
- function createEssorNode(path, result) {
172
- var _a;
141
+ function isSymbolStart(path, name) {
173
142
  const state = path.state;
174
- let tmpl;
175
- if (path.isJSXElement() && isComponent(getTagName(path.node))) {
176
- tmpl = t3.identifier(getTagName(path.node));
177
- } else {
178
- tmpl = path.scope.generateUidIdentifier("_tmpl$");
179
- const template = t3.arrayExpression(result.template.map(t3.stringLiteral));
180
- const declarator = t3.variableDeclarator(tmpl, template);
181
- state.tmplDeclaration.declarations.push(declarator);
182
- }
183
- const args = [tmpl, createProps(result.props)];
184
- const key = result.props.key || ((_a = result.props[0]) == null ? void 0 : _a.key);
185
- if (key) {
186
- args.push(key);
187
- }
188
- imports.add("renderTemplate");
189
- return t3.callExpression(state.renderTemplate, args);
143
+ const { symbol } = state.opts;
144
+ return startsWith(name, symbol);
190
145
  }
191
- function createProps(props) {
192
- const result = [];
193
- for (const prop in props) {
194
- let value = props[prop];
195
- if (prop === "key") {
196
- continue;
197
- }
198
- if (Array.isArray(value)) {
199
- value = t3.arrayExpression(value);
200
- }
201
- if (typeof value === "object" && value !== null && !t3.isNode(value)) {
202
- value = createProps(value);
203
- }
204
- if (typeof value === "string") {
205
- value = t3.stringLiteral(value);
206
- }
207
- if (typeof value === "number") {
208
- value = t3.numericLiteral(value);
209
- }
210
- if (typeof value === "boolean") {
211
- value = t3.booleanLiteral(value);
212
- }
213
- if (value === void 0) {
214
- value = t3.tsUndefinedKeyword();
215
- }
216
- if (value === null) {
217
- value = t3.nullLiteral();
218
- }
219
- if (prop === "_$spread$") {
220
- result.push(t3.spreadElement(value));
221
- } else {
222
- result.push(t3.objectProperty(t3.stringLiteral(prop), value));
223
- }
224
- }
225
- return t3.objectExpression(result);
226
- }
227
- function transformJSXServiceElement(path, result, isRoot = false) {
228
- if (path.isJSXElement()) {
229
- const tagName = getTagName(path.node);
230
- const tagIsComponent = isComponent(tagName);
231
- const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
232
- const isSvg = svgTags.includes(tagName) && result.index === 1;
233
- const { props, hasExpression } = getAttrProps(path);
234
- if (tagIsComponent) {
235
- if (isRoot) {
236
- result.props = props;
237
- const children = getChildren(path);
238
- if (children.length > 0) {
239
- const childrenGenerator = children.length === 1 ? children[0] : t3.arrayExpression(children);
240
- result.props.children = childrenGenerator;
241
- }
242
- } else {
243
- transformJSXService(path);
244
- replaceChild(path.node, result);
245
- }
246
- } else {
247
- if (isSvg) {
248
- result.template.push("<svg _svg_>");
249
- }
250
- result.template.push(`<${tagName}`);
251
- handleAttributes(props, result);
252
- if (hasExpression) {
253
- result.template.push(isSelfClose ? "/>" : ">");
254
- result.props || (result.props = {});
255
- } else {
256
- result.template[result.template.length - 1] += isSelfClose ? "/>" : ">";
257
- }
258
- transformChildren(path, result);
259
- if (!isSelfClose) {
260
- result.template.push(`</${tagName}>`);
261
- }
262
- }
263
- } else {
264
- result.index--;
265
- transformChildren(path, result);
266
- }
267
- }
268
- function transformChildren(path, result) {
269
- const parentIndex = result.template.length;
270
- path.get("children").reduce((pre, cur) => {
271
- if (isValidChild(cur)) {
272
- const lastChild = pre.at(-1);
273
- if (lastChild && isTextChild(cur) && isTextChild(lastChild)) {
274
- setNodeText(lastChild, getNodeText(lastChild) + getNodeText(cur));
275
- } else {
276
- pre.push(cur);
277
- }
278
- }
279
- return pre;
280
- }, []).forEach((child, i, arr) => {
281
- result.parentIndex = parentIndex;
282
- result.isLastChild = i === arr.length - 1;
283
- transformChild(child, result);
284
- });
285
- }
286
- function transformChild(child, result) {
287
- if (child.isJSXElement() || child.isJSXFragment()) {
288
- transformJSXServiceElement(child, result, false);
289
- } else if (child.isJSXExpressionContainer()) {
290
- const expression = child.get("expression");
291
- if (expression.isStringLiteral() || expression.isNumericLiteral()) {
292
- result.template[result.template.length - 1] += String(expression.node.value);
293
- } else if (expression.isExpression()) {
294
- replaceChild(expression.node, result);
295
- } else if (t3.isJSXEmptyExpression(expression.node)) {
296
- } else {
297
- throw new Error("Unsupported child type");
298
- }
299
- } else if (child.isJSXText()) {
300
- result.template.push(String(child.node.value));
146
+
147
+ // src/jsx/constants.ts
148
+ var selfClosingTags = [
149
+ "area",
150
+ "base",
151
+ "br",
152
+ "col",
153
+ "embed",
154
+ "hr",
155
+ "img",
156
+ "input",
157
+ "link",
158
+ "meta",
159
+ "param",
160
+ "source",
161
+ "track",
162
+ "wbr"
163
+ ];
164
+ var svgTags = [
165
+ "circle",
166
+ "clipPath",
167
+ "defs",
168
+ "ellipse",
169
+ "filter",
170
+ "g",
171
+ "line",
172
+ "linearGradient",
173
+ "mask",
174
+ "path",
175
+ "pattern",
176
+ "polygon",
177
+ "polyline",
178
+ "radialGradient",
179
+ "rect",
180
+ "stop",
181
+ "symbol",
182
+ "text",
183
+ "use"
184
+ ];
185
+
186
+ // src/jsx/index.ts
187
+ var isSsg = false;
188
+ function addToTemplate(result, content) {
189
+ if (isSsg) {
190
+ result.template.push(content);
301
191
  } else {
302
- throw new Error("Unsupported child type");
192
+ result.template += content;
303
193
  }
304
194
  }
305
- function getNodeText(path) {
306
- if (path.isJSXText()) {
307
- return path.node.value;
308
- }
309
- if (path.isJSXExpressionContainer()) {
310
- const expression = path.get("expression");
311
- if (expression.isStringLiteral() || expression.isNumericLiteral()) {
312
- return String(expression.node.value);
313
- }
314
- }
315
- return "";
316
- }
317
- function handleAttributes(props, result) {
318
- let klass = "";
319
- let style = "";
320
- for (const prop in props) {
321
- const value = props[prop];
322
- if (prop === "class" && typeof value === "string") {
323
- klass += ` ${value}`;
324
- delete props[prop];
325
- continue;
326
- }
327
- if (prop === "style" && typeof value === "string") {
328
- style += `${value}${value.at(-1) === ";" ? "" : ";"}`;
329
- delete props[prop];
330
- continue;
331
- }
332
- if (value === true) {
333
- result.template[result.template.length - 1] += ` ${prop}`;
334
- delete props[prop];
335
- }
336
- if (value === false) {
337
- delete props[prop];
338
- }
339
- if (typeof value === "string" || typeof value === "number") {
340
- result.template[result.template.length - 1] += ` ${prop}="${value}"`;
341
- delete props[prop];
342
- }
343
- }
344
- if (Object.keys(props).length > 0) {
345
- result.props[result.index] = props;
346
- }
347
- klass = klass.trim();
348
- style = style.trim();
349
- if (klass) {
350
- result.template[result.template.length - 1] += ` class="${klass}"`;
351
- }
352
- if (style) {
353
- result.template[result.template.length - 1] += ` style="${style}"`;
354
- }
355
- }
356
- function replaceChild(node, result) {
357
- var _a, _b, _c, _d, _e;
358
- if (result.isLastChild) {
359
- result.index--;
360
- }
361
- (_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
362
- (_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
363
- result.props[result.parentIndex].children.push(
364
- t3.arrayExpression([
365
- t3.arrowFunctionExpression([], node),
366
- t3.identifier(String(result.template.length))
367
- ])
368
- );
369
- }
370
- function getChildren(path) {
371
- return path.get("children").filter((child) => isValidChild(child)).map((child) => {
372
- if (child.isJSXElement() || child.isJSXFragment()) {
373
- transformJSXService(child);
374
- } else if (child.isJSXExpressionContainer()) {
375
- child.replaceWith(child.get("expression"));
376
- } else if (child.isJSXText()) {
377
- child.replaceWith(t3.stringLiteral(child.node.value));
378
- } else {
379
- throw new Error("Unsupported child type");
380
- }
381
- return child.node;
382
- });
383
- }
384
- function isValidChild(path) {
385
- const regex = /^\s*$/;
386
- if (path.isStringLiteral() || path.isJSXText()) {
387
- return !regex.test(path.node.value);
388
- }
389
- return Object.keys(path.node).length > 0;
390
- }
391
- function getAttrProps(path) {
392
- const props = {};
393
- let hasExpression = false;
394
- path.get("openingElement").get("attributes").forEach((attribute) => {
395
- if (attribute.isJSXAttribute()) {
396
- const name = getAttrName(attribute.node);
397
- const value = attribute.get("value");
398
- if (!value.node) {
399
- props[name] = true;
400
- } else if (value.isStringLiteral()) {
401
- props[name] = value.node.value;
402
- } else {
403
- if (value.isJSXExpressionContainer()) {
404
- const expression = value.get("expression");
405
- if (expression.isStringLiteral()) {
406
- props[name] = expression.node.value;
407
- } else if (expression.isNumericLiteral()) {
408
- props[name] = expression.node.value;
409
- } else if (expression.isJSXElement() || expression.isJSXFragment()) {
410
- transformJSXService(expression);
411
- props[name] = expression.node;
412
- } else if (expression.isExpression()) {
413
- hasExpression = true;
414
- if (/^key|ref|on.+$/.test(name)) {
415
- props[name] = expression.node;
416
- } else if (/^bind:.+/.test(name)) {
417
- const value2 = path.scope.generateUidIdentifier("value");
418
- const bindName = name.slice(5).toLocaleLowerCase();
419
- props[bindName] = expression.node;
420
- props[`update${capitalizeFirstLetter(bindName)}`] = t3.arrowFunctionExpression(
421
- [value2],
422
- t3.assignmentExpression("=", expression.node, value2)
423
- );
424
- } else {
425
- if (expression.isConditionalExpression()) {
426
- props[name] = t3.arrowFunctionExpression([], expression.node);
427
- } else {
428
- props[name] = expression.node;
429
- }
430
- }
431
- }
432
- } else if (value.isJSXElement() || value.isJSXFragment()) {
433
- transformJSXService(value);
434
- props[name] = value.node;
435
- }
436
- }
437
- } else if (attribute.isJSXSpreadAttribute()) {
438
- props._$spread$ = attribute.get("argument").node;
439
- hasExpression = true;
440
- } else {
441
- throw new Error("Unsupported attribute type");
442
- }
443
- });
444
- return {
445
- props,
446
- hasExpression
447
- };
448
- }
449
-
450
- // src/jsx/client.ts
451
- import { types as t4 } from "@babel/core";
452
- function transformJSXClient(path) {
195
+ function transformJSX(path) {
196
+ const state = path.state;
197
+ isSsg = state.opts.ssg;
453
198
  const result = {
454
199
  index: 1,
455
200
  isLastChild: false,
456
201
  parentIndex: 0,
457
202
  props: {},
458
- template: ""
203
+ template: isSsg ? [] : ""
459
204
  };
460
205
  transformJSXElement(path, result, true);
461
- path.replaceWith(createEssorNode2(path, result));
206
+ path.replaceWith(createEssorNode(path, result));
462
207
  }
463
- function createEssorNode2(path, result) {
208
+ function createEssorNode(path, result) {
464
209
  var _a;
465
210
  const state = path.state;
466
211
  let tmpl;
467
212
  if (path.isJSXElement() && isComponent(getTagName(path.node))) {
468
- tmpl = t4.identifier(getTagName(path.node));
213
+ tmpl = t3.identifier(getTagName(path.node));
469
214
  } else {
470
215
  tmpl = path.scope.generateUidIdentifier("_tmpl$");
471
- const template = t4.callExpression(state.template, [t4.stringLiteral(result.template)]);
472
- const declarator = t4.variableDeclarator(tmpl, template);
216
+ const template = isSsg ? t3.arrayExpression(result.template.map(t3.stringLiteral)) : t3.callExpression(state.template, [t3.stringLiteral(result.template)]);
217
+ const declarator = t3.variableDeclarator(tmpl, template);
473
218
  state.tmplDeclaration.declarations.push(declarator);
474
- imports.add("template");
219
+ if (!isSsg) {
220
+ imports.add("template");
221
+ }
475
222
  }
476
- const args = [tmpl, createProps2(result.props)];
223
+ const args = [tmpl, createProps(result.props)];
477
224
  const key = result.props.key || ((_a = result.props[0]) == null ? void 0 : _a.key);
478
225
  if (key) {
479
226
  args.push(key);
480
227
  }
481
- imports.add("h");
482
- return t4.callExpression(state.h, args);
228
+ imports.add(isSsg ? "renderTemplate" : "h");
229
+ return t3.callExpression(isSsg ? state.renderTemplate : state.h, args);
483
230
  }
484
- function createProps2(props) {
231
+ function createProps(props) {
485
232
  const toAstNode = (value) => {
486
233
  if (Array.isArray(value)) {
487
- return t4.arrayExpression(value.map(toAstNode));
234
+ return t3.arrayExpression(value.map(toAstNode));
488
235
  }
489
- if (value && typeof value === "object" && !t4.isNode(value)) {
490
- return createProps2(value);
236
+ if (value && typeof value === "object" && !t3.isNode(value)) {
237
+ return createProps(value);
491
238
  }
492
239
  switch (typeof value) {
493
240
  case "string":
494
- return t4.stringLiteral(value);
241
+ return t3.stringLiteral(value);
495
242
  case "number":
496
- return t4.numericLiteral(value);
243
+ return t3.numericLiteral(value);
497
244
  case "boolean":
498
- return t4.booleanLiteral(value);
245
+ return t3.booleanLiteral(value);
499
246
  case "undefined":
500
- return t4.tsUndefinedKeyword();
247
+ return t3.tsUndefinedKeyword();
501
248
  case void 0:
502
- return t4.tsUndefinedKeyword();
249
+ return t3.tsUndefinedKeyword();
503
250
  case null:
504
- return t4.nullLiteral();
251
+ return t3.nullLiteral();
505
252
  default:
506
253
  return value;
507
254
  }
508
255
  };
509
256
  const result = Object.keys(props).filter((prop) => prop !== "key").map((prop) => {
510
257
  const value = toAstNode(props[prop]);
511
- return prop === "_$spread$" ? t4.spreadElement(value) : t4.objectProperty(t4.stringLiteral(prop), value);
258
+ return prop === "_$spread$" ? t3.spreadElement(value) : t3.objectProperty(t3.stringLiteral(prop), value);
512
259
  });
513
- return t4.objectExpression(result);
260
+ return t3.objectExpression(result);
514
261
  }
515
262
  function transformJSXElement(path, result, isRoot = false) {
516
263
  if (path.isJSXElement()) {
@@ -518,45 +265,45 @@ function transformJSXElement(path, result, isRoot = false) {
518
265
  const tagIsComponent = isComponent(tagName);
519
266
  const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
520
267
  const isSvg = svgTags.includes(tagName) && result.index === 1;
521
- const props = getAttrProps2(path);
268
+ const props = getAttrProps(path);
522
269
  if (tagIsComponent) {
523
270
  if (isRoot) {
524
271
  result.props = props;
525
- const children = getChildren2(path);
272
+ const children = getChildren(path);
526
273
  if (children.length > 0) {
527
- const childrenGenerator = children.length === 1 ? children[0] : t4.arrayExpression(children);
274
+ const childrenGenerator = children.length === 1 ? children[0] : t3.arrayExpression(children);
528
275
  result.props.children = childrenGenerator;
529
276
  }
530
277
  } else {
531
- transformJSXClient(path);
532
- replaceChild2(path.node, result);
278
+ transformJSX(path);
279
+ replaceChild(path.node, result);
533
280
  }
534
281
  } else {
535
282
  if (isSvg) {
536
- result.template = "<svg _svg_>";
283
+ result.template = isSsg ? ["<svg _svg_>"] : "<svg _svg_>";
537
284
  }
538
- result.template += `<${tagName}`;
539
- handleAttributes2(props, result);
540
- result.template += isSelfClose ? "/>" : ">";
285
+ addToTemplate(result, `<${tagName}`);
286
+ handleAttributes(props, result);
287
+ addToTemplate(result, isSelfClose ? "/>" : ">");
541
288
  if (!isSelfClose) {
542
- transformChildren2(path, result);
289
+ transformChildren(path, result);
543
290
  if (hasSiblingElement(path)) {
544
- result.template += `</${tagName}>`;
291
+ addToTemplate(result, `</${tagName}>`);
545
292
  }
546
293
  }
547
294
  }
548
295
  } else {
549
296
  result.index--;
550
- transformChildren2(path, result);
297
+ transformChildren(path, result);
551
298
  }
552
299
  }
553
- function transformChildren2(path, result) {
554
- const parentIndex = result.index;
300
+ function transformChildren(path, result) {
301
+ const parentIndex = isSsg ? result.template.length : result.index;
555
302
  path.get("children").reduce((pre, cur) => {
556
- if (isValidChild2(cur)) {
303
+ if (isValidChild(cur)) {
557
304
  const lastChild = pre.at(-1);
558
305
  if (lastChild && isTextChild(cur) && isTextChild(lastChild)) {
559
- setNodeText(lastChild, getNodeText2(lastChild) + getNodeText2(cur));
306
+ setNodeText(lastChild, getNodeText(lastChild) + getNodeText(cur));
560
307
  } else {
561
308
  pre.push(cur);
562
309
  }
@@ -565,30 +312,30 @@ function transformChildren2(path, result) {
565
312
  }, []).forEach((child, i, arr) => {
566
313
  result.parentIndex = parentIndex;
567
314
  result.isLastChild = i === arr.length - 1;
568
- transformChild2(child, result);
315
+ transformChild(child, result);
569
316
  });
570
317
  }
571
- function transformChild2(child, result) {
318
+ function transformChild(child, result) {
572
319
  result.index++;
573
320
  if (child.isJSXElement() || child.isJSXFragment()) {
574
321
  transformJSXElement(child, result, false);
575
322
  } else if (child.isJSXExpressionContainer()) {
576
323
  const expression = child.get("expression");
577
324
  if (expression.isStringLiteral() || expression.isNumericLiteral()) {
578
- result.template += String(expression.node.value);
325
+ addToTemplate(result, String(expression.node.value));
579
326
  } else if (expression.isExpression()) {
580
- replaceChild2(expression.node, result);
581
- } else if (t4.isJSXEmptyExpression(expression.node)) {
327
+ replaceChild(expression.node, result);
328
+ } else if (t3.isJSXEmptyExpression(expression.node)) {
582
329
  } else {
583
330
  throw new Error("Unsupported child type");
584
331
  }
585
332
  } else if (child.isJSXText()) {
586
- result.template += String(child.node.value);
333
+ addToTemplate(result, String(child.node.value));
587
334
  } else {
588
335
  throw new Error("Unsupported child type");
589
336
  }
590
337
  }
591
- function getNodeText2(path) {
338
+ function getNodeText(path) {
592
339
  if (path.isJSXText()) {
593
340
  return path.node.value;
594
341
  }
@@ -600,7 +347,7 @@ function getNodeText2(path) {
600
347
  }
601
348
  return "";
602
349
  }
603
- function handleAttributes2(props, result) {
350
+ function handleAttributes(props, result) {
604
351
  let klass = "";
605
352
  let style = "";
606
353
  for (const prop in props) {
@@ -616,32 +363,41 @@ function handleAttributes2(props, result) {
616
363
  continue;
617
364
  }
618
365
  if (value === true) {
619
- result.template += ` ${prop}`;
366
+ addToTemplate(result, ` ${prop}`);
620
367
  delete props[prop];
621
368
  }
622
369
  if (value === false) {
623
370
  delete props[prop];
624
371
  }
625
372
  if (typeof value === "string" || typeof value === "number") {
626
- result.template += ` ${prop}="${value}"`;
373
+ addToTemplate(result, ` ${prop}="${value}"`);
627
374
  delete props[prop];
628
375
  }
629
- if (t4.isConditionalExpression(value)) {
376
+ if (t3.isConditionalExpression(value)) {
630
377
  const { test, consequent, alternate } = value;
631
- value = t4.arrowFunctionExpression([], t4.conditionalExpression(test, consequent, alternate));
378
+ value = t3.arrowFunctionExpression([], t3.conditionalExpression(test, consequent, alternate));
632
379
  props[prop] = value;
633
380
  }
634
- if (t4.isObjectExpression(value)) {
381
+ if (t3.isObjectExpression(value)) {
635
382
  let hasConditional = false;
636
383
  value.properties.forEach((property) => {
637
- if (t4.isObjectProperty(property) && t4.isConditionalExpression(property.value)) {
384
+ if (t3.isObjectProperty(property) && t3.isConditionalExpression(property.value)) {
638
385
  hasConditional = true;
639
386
  }
640
387
  });
641
388
  if (hasConditional) {
642
- value = t4.arrowFunctionExpression([], value);
389
+ value = t3.arrowFunctionExpression([], value);
390
+ props[prop] = value;
391
+ } else {
392
+ if (prop === "style") {
393
+ value.properties.forEach((property) => {
394
+ if (t3.isObjectProperty(property)) {
395
+ style += `${property.key.name}:${property.value.value};`;
396
+ }
397
+ });
398
+ delete props[prop];
399
+ }
643
400
  }
644
- props[prop] = value;
645
401
  }
646
402
  }
647
403
  if (Object.keys(props).length > 0) {
@@ -650,50 +406,50 @@ function handleAttributes2(props, result) {
650
406
  klass = klass.trim();
651
407
  style = style.trim();
652
408
  if (klass) {
653
- result.template += ` class="${klass}"`;
409
+ addToTemplate(result, ` class="${klass}"`);
654
410
  }
655
411
  if (style) {
656
- result.template += ` style="${style}"`;
412
+ addToTemplate(result, ` style="${style}"`);
657
413
  }
658
414
  }
659
- function replaceChild2(node, result) {
415
+ function replaceChild(node, result) {
660
416
  var _a, _b, _c, _d, _e;
661
417
  if (result.isLastChild) {
662
418
  result.index--;
663
419
  } else {
664
- result.template += "<!>";
420
+ addToTemplate(result, "<!>");
665
421
  }
666
422
  (_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
667
423
  (_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
668
424
  result.props[result.parentIndex].children.push(
669
- t4.arrayExpression([
670
- t4.arrowFunctionExpression([], node),
671
- result.isLastChild ? t4.nullLiteral() : t4.identifier(String(result.index))
425
+ t3.arrayExpression([
426
+ t3.arrowFunctionExpression([], node),
427
+ result.isLastChild ? t3.nullLiteral() : t3.identifier(String(result.index))
672
428
  ])
673
429
  );
674
430
  }
675
- function getChildren2(path) {
676
- return path.get("children").filter((child) => isValidChild2(child)).map((child) => {
431
+ function getChildren(path) {
432
+ return path.get("children").filter((child) => isValidChild(child)).map((child) => {
677
433
  if (child.isJSXElement() || child.isJSXFragment()) {
678
- transformJSXClient(child);
434
+ transformJSX(child);
679
435
  } else if (child.isJSXExpressionContainer()) {
680
436
  child.replaceWith(child.get("expression"));
681
437
  } else if (child.isJSXText()) {
682
- child.replaceWith(t4.stringLiteral(child.node.value));
438
+ child.replaceWith(t3.stringLiteral(child.node.value));
683
439
  } else {
684
440
  throw new Error("Unsupported child type");
685
441
  }
686
442
  return child.node;
687
443
  });
688
444
  }
689
- function isValidChild2(path) {
445
+ function isValidChild(path) {
690
446
  const regex = /^\s*$/;
691
447
  if (path.isStringLiteral() || path.isJSXText()) {
692
448
  return !regex.test(path.node.value);
693
449
  }
694
450
  return Object.keys(path.node).length > 0;
695
451
  }
696
- function getAttrProps2(path) {
452
+ function getAttrProps(path) {
697
453
  const props = {};
698
454
  path.get("openingElement").get("attributes").forEach((attribute) => {
699
455
  if (attribute.isJSXAttribute()) {
@@ -711,7 +467,7 @@ function getAttrProps2(path) {
711
467
  } else if (expression.isNumericLiteral()) {
712
468
  props[name] = expression.node.value;
713
469
  } else if (expression.isJSXElement() || expression.isJSXFragment()) {
714
- transformJSXClient(expression);
470
+ transformJSX(expression);
715
471
  props[name] = expression.node;
716
472
  } else if (expression.isExpression()) {
717
473
  if (/^key|ref|on.+$/.test(name)) {
@@ -720,20 +476,20 @@ function getAttrProps2(path) {
720
476
  const value2 = path.scope.generateUidIdentifier("value");
721
477
  const bindName = name.slice(5).toLocaleLowerCase();
722
478
  props[bindName] = expression.node;
723
- props[`update${capitalizeFirstLetter(bindName)}`] = t4.arrowFunctionExpression(
479
+ props[`update${capitalizeFirstLetter(bindName)}`] = t3.arrowFunctionExpression(
724
480
  [value2],
725
- t4.assignmentExpression("=", expression.node, value2)
481
+ t3.assignmentExpression("=", expression.node, value2)
726
482
  );
727
483
  } else {
728
484
  if (expression.isConditionalExpression()) {
729
- props[name] = t4.arrowFunctionExpression([], expression.node);
485
+ props[name] = t3.arrowFunctionExpression([], expression.node);
730
486
  } else {
731
487
  props[name] = expression.node;
732
488
  }
733
489
  }
734
490
  }
735
491
  } else if (value.isJSXElement() || value.isJSXFragment()) {
736
- transformJSXClient(value);
492
+ transformJSX(value);
737
493
  props[name] = value.node;
738
494
  }
739
495
  }
@@ -746,41 +502,34 @@ function getAttrProps2(path) {
746
502
  return props;
747
503
  }
748
504
 
749
- // src/jsx/index.ts
750
- function transformJSX(path) {
751
- const state = path.state;
752
- const isSsg = state.opts.ssg;
753
- return isSsg ? transformJSXService(path) : transformJSXClient(path);
754
- }
755
-
756
505
  // src/signal/symbol.ts
757
- import { types as t5 } from "@babel/core";
506
+ import { types as t4 } from "@babel/core";
758
507
  function replaceSymbol(path) {
759
508
  const init = path.node.init;
760
509
  const variableName = path.node.id.name;
761
- if (t5.isObjectPattern(path.node.id) || t5.isArrayPattern(path.node.id)) {
510
+ if (t4.isObjectPattern(path.node.id) || t4.isArrayPattern(path.node.id)) {
762
511
  return;
763
512
  }
764
- if (!startsWith(variableName, "$")) {
513
+ if (!isSymbolStart(path, variableName)) {
765
514
  return;
766
515
  }
767
- if (init && (t5.isFunctionExpression(init) || t5.isArrowFunctionExpression(init)) && path.parent.kind === "const") {
768
- const newInit = t5.callExpression(t5.identifier(path.state.useComputed.name), init ? [init] : []);
516
+ if (init && (t4.isFunctionExpression(init) || t4.isArrowFunctionExpression(init)) && path.parent.kind === "const") {
517
+ const newInit = t4.callExpression(t4.identifier(path.state.useComputed.name), init ? [init] : []);
769
518
  imports.add("useComputed");
770
519
  path.node.init = newInit;
771
520
  } else {
772
- const newInit = t5.callExpression(t5.identifier(path.state.useSignal.name), init ? [init] : []);
521
+ const newInit = t4.callExpression(t4.identifier(path.state.useSignal.name), init ? [init] : []);
773
522
  imports.add("useSignal");
774
523
  path.node.init = newInit;
775
524
  }
776
525
  }
777
526
  function symbolIdentifier(path) {
778
527
  const parentPath = path.parentPath;
779
- if (!parentPath || t5.isVariableDeclarator(parentPath) || t5.isImportSpecifier(parentPath) || t5.isObjectProperty(parentPath) || t5.isArrayPattern(parentPath) || t5.isObjectPattern(parentPath)) {
528
+ if (!parentPath || t4.isVariableDeclarator(parentPath) || t4.isImportSpecifier(parentPath) || t4.isObjectProperty(parentPath) || t4.isArrayPattern(parentPath) || t4.isObjectPattern(parentPath)) {
780
529
  return;
781
530
  }
782
531
  const { node } = path;
783
- if (node.name.startsWith("$")) {
532
+ if (isSymbolStart(path, node.name)) {
784
533
  let currentPath = path;
785
534
  while (currentPath.parentPath && !currentPath.parentPath.isProgram()) {
786
535
  if (currentPath.parentPath.isMemberExpression() && currentPath.parentPath.node.property.name === "value") {
@@ -788,27 +537,27 @@ function symbolIdentifier(path) {
788
537
  }
789
538
  currentPath = currentPath.parentPath;
790
539
  }
791
- const newNode = t5.memberExpression(t5.identifier(node.name), t5.identifier("value"));
540
+ const newNode = t4.memberExpression(t4.identifier(node.name), t4.identifier("value"));
792
541
  path.replaceWith(newNode);
793
542
  }
794
543
  }
795
544
  function symbolObjectPattern(path) {
796
545
  path.node.properties.forEach((property) => {
797
- if (t5.isObjectProperty(property) && t5.isIdentifier(property.key) && property.key.name.startsWith("$")) {
798
- const newKey = t5.identifier(property.key.name);
546
+ if (t4.isObjectProperty(property) && t4.isIdentifier(property.key) && isSymbolStart(path, property.key.name)) {
547
+ const newKey = t4.identifier(property.key.name);
799
548
  property.key = newKey;
800
549
  }
801
550
  });
802
551
  }
803
552
  function symbolArrayPattern(path) {
804
553
  path.node.elements.forEach((element) => {
805
- if (t5.isIdentifier(element) && element.name.startsWith("$")) {
806
- const newElement = t5.identifier(element.name);
554
+ if (t4.isIdentifier(element) && element.name.startsWith("$")) {
555
+ const newElement = t4.identifier(element.name);
807
556
  element.name = newElement.name;
808
- } else if (t5.isObjectPattern(element)) {
557
+ } else if (t4.isObjectPattern(element)) {
809
558
  element.properties.forEach((property) => {
810
- if (t5.isObjectProperty(property) && t5.isIdentifier(property.key) && property.key.name.startsWith("$")) {
811
- const newKey = t5.identifier(property.key.name);
559
+ if (t4.isObjectProperty(property) && t4.isIdentifier(property.key) && isSymbolStart(path, property.key.name)) {
560
+ const newKey = t4.identifier(property.key.name);
812
561
  property.key = newKey;
813
562
  }
814
563
  });
@@ -817,7 +566,17 @@ function symbolArrayPattern(path) {
817
566
  }
818
567
 
819
568
  // src/signal/import.ts
820
- import { types as t6 } from "@babel/core";
569
+ import { types as t5 } from "@babel/core";
570
+ function replaceImportDeclaration(path) {
571
+ const imports2 = path.node.specifiers;
572
+ imports2.forEach((specifier) => {
573
+ const variableName = specifier.local.name;
574
+ if (startsWith(variableName, "$") && !isVariableUsedAsObject(path, variableName)) {
575
+ path.scope.rename(variableName, `${variableName}.value`);
576
+ specifier.local.name = `${variableName}`;
577
+ }
578
+ });
579
+ }
821
580
  function isVariableUsedAsObject(path, variableName) {
822
581
  const binding = path.scope.getBinding(variableName);
823
582
  let isUsedObject = false;
@@ -825,30 +584,28 @@ function isVariableUsedAsObject(path, variableName) {
825
584
  return isUsedObject;
826
585
  }
827
586
  for (const referencePath of binding.referencePaths) {
828
- if (t6.isMemberExpression(referencePath.parent)) {
829
- isUsedObject = true;
587
+ if (t5.isMemberExpression(referencePath.parent)) {
588
+ const memberExprParent = referencePath.parent;
589
+ if (t5.isIdentifier(memberExprParent.object, { name: variableName })) {
590
+ const newMemberExpr = t5.memberExpression(
591
+ t5.memberExpression(memberExprParent.object, t5.identifier("value")),
592
+ memberExprParent.property
593
+ );
594
+ referencePath.parentPath.replaceWith(newMemberExpr);
595
+ isUsedObject = true;
596
+ }
830
597
  }
831
598
  }
832
599
  return isUsedObject;
833
600
  }
834
- function replaceImportDeclaration(path) {
835
- const imports2 = path.node.specifiers;
836
- imports2.forEach((specifier) => {
837
- const variableName = specifier.local.name;
838
- if (startsWith(variableName, "$") && !isVariableUsedAsObject(path, variableName)) {
839
- path.scope.rename(variableName, `${variableName}.value`);
840
- specifier.local.name = `${variableName}`;
841
- }
842
- });
843
- }
844
601
 
845
602
  // src/signal/props.ts
846
- import { types as t7 } from "@babel/core";
603
+ import { types as t6 } from "@babel/core";
847
604
  function replaceProps(path) {
848
605
  var _a;
849
606
  const state = path.state;
850
607
  const firstParam = path.node.params[0];
851
- if (!firstParam || !t7.isObjectPattern(firstParam)) {
608
+ if (!firstParam || !t6.isObjectPattern(firstParam)) {
852
609
  return;
853
610
  }
854
611
  const returnStatement = path.get("body").get("body").find((statement) => statement.isReturnStatement());
@@ -856,18 +613,18 @@ function replaceProps(path) {
856
613
  return;
857
614
  }
858
615
  const returnValue = (_a = returnStatement.node) == null ? void 0 : _a.argument;
859
- if (!t7.isJSXElement(returnValue)) {
616
+ if (!t6.isJSXElement(returnValue)) {
860
617
  return;
861
618
  }
862
619
  function replaceProperties(properties2, parentPath) {
863
620
  properties2.forEach((property) => {
864
- if (t7.isObjectProperty(property)) {
621
+ if (t6.isObjectProperty(property)) {
865
622
  const keyName = property.key.name;
866
- if (t7.isIdentifier(property.value)) {
623
+ if (t6.isIdentifier(property.value)) {
867
624
  const propertyName = property.value.name;
868
625
  const newName = `${parentPath}${keyName}`;
869
626
  path.scope.rename(propertyName, newName);
870
- } else if (t7.isObjectPattern(property.value)) {
627
+ } else if (t6.isObjectPattern(property.value)) {
871
628
  replaceProperties(property.value.properties, `${parentPath}${keyName}.`);
872
629
  }
873
630
  }
@@ -875,10 +632,10 @@ function replaceProps(path) {
875
632
  }
876
633
  const properties = firstParam.properties;
877
634
  replaceProperties(
878
- properties.filter((property) => !t7.isRestElement(property)),
635
+ properties.filter((property) => !t6.isRestElement(property)),
879
636
  "__props."
880
637
  );
881
- const notRestProperties = properties.filter((property) => !t7.isRestElement(property));
638
+ const notRestProperties = properties.filter((property) => !t6.isRestElement(property));
882
639
  const notRestNames = notRestProperties.map(
883
640
  (property) => property.key.name
884
641
  );
@@ -886,19 +643,19 @@ function replaceProps(path) {
886
643
  console.warn("props name can not start with $");
887
644
  return;
888
645
  }
889
- const restElement = properties.find((property) => t7.isRestElement(property));
890
- path.node.params[0] = t7.identifier("__props");
646
+ const restElement = properties.find((property) => t6.isRestElement(property));
647
+ path.node.params[0] = t6.identifier("__props");
891
648
  if (restElement) {
892
649
  const restName = restElement.argument.name;
893
650
  if (notRestProperties.length === 0) {
894
- path.node.params[0] = t7.identifier(restName);
651
+ path.node.params[0] = t6.identifier(restName);
895
652
  } else {
896
- const restVariableDeclaration = t7.variableDeclaration("const", [
897
- t7.variableDeclarator(
898
- t7.identifier(restName),
899
- t7.callExpression(state.useReactive, [
900
- t7.identifier("__props"),
901
- t7.arrayExpression(notRestNames.map((name) => t7.stringLiteral(name)))
653
+ const restVariableDeclaration = t6.variableDeclaration("const", [
654
+ t6.variableDeclarator(
655
+ t6.identifier(restName),
656
+ t6.callExpression(state.useReactive, [
657
+ t6.identifier("__props"),
658
+ t6.arrayExpression(notRestNames.map((name) => t6.stringLiteral(name)))
902
659
  ])
903
660
  )
904
661
  ]);
@@ -920,19 +677,24 @@ function src_default() {
920
677
  },
921
678
  visitor: {
922
679
  Program: transformProgram,
923
- JSXElement: transformJSX,
924
- JSXFragment: transformJSX,
925
680
  FunctionDeclaration: replaceProps,
926
681
  ArrowFunctionExpression: replaceProps,
927
682
  VariableDeclarator: replaceSymbol,
928
683
  ImportDeclaration: replaceImportDeclaration,
929
684
  Identifier: symbolIdentifier,
930
685
  ObjectPattern: symbolObjectPattern,
931
- ArrayPattern: symbolArrayPattern
686
+ ArrayPattern: symbolArrayPattern,
687
+ JSXElement: transformJSX,
688
+ JSXFragment: transformJSX
932
689
  }
933
690
  };
934
691
  }
935
692
  export {
936
693
  src_default as default
937
694
  };
695
+ /**
696
+ * @estjs/shared v0.0.10-beta.20
697
+ * (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
698
+ * @license MIT
699
+ **/
938
700
  //# sourceMappingURL=index.js.map