babel-plugin-essor 0.0.7-beta.6 → 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());
@@ -129,7 +113,7 @@ function jsxElementNameToString(node) {
129
113
  return `${node.namespace.name}:${node.name.name}`;
130
114
  }
131
115
  function isComponent(tagName) {
132
- return tagName[0] && tagName[0].toLowerCase() !== tagName[0] || tagName.includes(".") || /[^A-Za-z]/.test(tagName[0]);
116
+ return tagName[0] && tagName[0].toLowerCase() !== tagName[0] || tagName.includes(".") || /[^a-z]/i.test(tagName[0]);
133
117
  }
134
118
  function isTextChild(path) {
135
119
  if (path.isJSXExpressionContainer()) {
@@ -154,18 +138,71 @@ function setNodeText(path, text) {
154
138
  }
155
139
  }
156
140
  }
141
+ function isSymbolStart(path, name) {
142
+ const state = path.state;
143
+ const { symbol } = state.opts;
144
+ return startsWith(name, symbol);
145
+ }
157
146
 
158
- // src/jsx/server.ts
159
- function transformJSXService(path) {
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);
191
+ } else {
192
+ result.template += content;
193
+ }
194
+ }
195
+ function transformJSX(path) {
196
+ const state = path.state;
197
+ isSsg = state.opts.ssg;
160
198
  const result = {
161
- index: 0,
199
+ index: 1,
162
200
  isLastChild: false,
163
201
  parentIndex: 0,
164
202
  props: {},
165
- template: []
166
- // 修改为数组
203
+ template: isSsg ? [] : ""
167
204
  };
168
- transformJSXServiceElement(path, result, true);
205
+ transformJSXElement(path, result, true);
169
206
  path.replaceWith(createEssorNode(path, result));
170
207
  }
171
208
  function createEssorNode(path, result) {
@@ -176,61 +213,59 @@ function createEssorNode(path, result) {
176
213
  tmpl = t3.identifier(getTagName(path.node));
177
214
  } else {
178
215
  tmpl = path.scope.generateUidIdentifier("_tmpl$");
179
- const template = t3.arrayExpression(result.template.map(t3.stringLiteral));
216
+ const template = isSsg ? t3.arrayExpression(result.template.map(t3.stringLiteral)) : t3.callExpression(state.template, [t3.stringLiteral(result.template)]);
180
217
  const declarator = t3.variableDeclarator(tmpl, template);
181
218
  state.tmplDeclaration.declarations.push(declarator);
219
+ if (!isSsg) {
220
+ imports.add("template");
221
+ }
182
222
  }
183
223
  const args = [tmpl, createProps(result.props)];
184
224
  const key = result.props.key || ((_a = result.props[0]) == null ? void 0 : _a.key);
185
225
  if (key) {
186
226
  args.push(key);
187
227
  }
188
- imports.add("renderTemplate");
189
- return t3.callExpression(state.renderTemplate, args);
228
+ imports.add(isSsg ? "renderTemplate" : "h");
229
+ return t3.callExpression(isSsg ? state.renderTemplate : state.h, args);
190
230
  }
191
231
  function createProps(props) {
192
- const result = [];
193
- for (const prop in props) {
194
- let value = props[prop];
195
- if (prop === "key") {
196
- continue;
197
- }
232
+ const toAstNode = (value) => {
198
233
  if (Array.isArray(value)) {
199
- value = t3.arrayExpression(value);
200
- }
201
- if (typeof value === "object" && value !== null && !t3.isNode(value)) {
202
- value = createProps(value);
234
+ return t3.arrayExpression(value.map(toAstNode));
203
235
  }
204
- if (typeof value === "string") {
205
- value = t3.stringLiteral(value);
236
+ if (value && typeof value === "object" && !t3.isNode(value)) {
237
+ return createProps(value);
206
238
  }
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));
239
+ switch (typeof value) {
240
+ case "string":
241
+ return t3.stringLiteral(value);
242
+ case "number":
243
+ return t3.numericLiteral(value);
244
+ case "boolean":
245
+ return t3.booleanLiteral(value);
246
+ case "undefined":
247
+ return t3.tsUndefinedKeyword();
248
+ case void 0:
249
+ return t3.tsUndefinedKeyword();
250
+ case null:
251
+ return t3.nullLiteral();
252
+ default:
253
+ return value;
223
254
  }
224
- }
255
+ };
256
+ const result = Object.keys(props).filter((prop) => prop !== "key").map((prop) => {
257
+ const value = toAstNode(props[prop]);
258
+ return prop === "_$spread$" ? t3.spreadElement(value) : t3.objectProperty(t3.stringLiteral(prop), value);
259
+ });
225
260
  return t3.objectExpression(result);
226
261
  }
227
- function transformJSXServiceElement(path, result, isRoot = false) {
262
+ function transformJSXElement(path, result, isRoot = false) {
228
263
  if (path.isJSXElement()) {
229
264
  const tagName = getTagName(path.node);
230
265
  const tagIsComponent = isComponent(tagName);
231
266
  const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
232
267
  const isSvg = svgTags.includes(tagName) && result.index === 1;
233
- const { props, hasExpression } = getAttrProps(path);
268
+ const props = getAttrProps(path);
234
269
  if (tagIsComponent) {
235
270
  if (isRoot) {
236
271
  result.props = props;
@@ -240,24 +275,21 @@ function transformJSXServiceElement(path, result, isRoot = false) {
240
275
  result.props.children = childrenGenerator;
241
276
  }
242
277
  } else {
243
- transformJSXService(path);
278
+ transformJSX(path);
244
279
  replaceChild(path.node, result);
245
280
  }
246
281
  } else {
247
282
  if (isSvg) {
248
- result.template.push("<svg _svg_>");
283
+ result.template = isSsg ? ["<svg _svg_>"] : "<svg _svg_>";
249
284
  }
250
- result.template.push(`<${tagName}`);
285
+ addToTemplate(result, `<${tagName}`);
251
286
  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);
287
+ addToTemplate(result, isSelfClose ? "/>" : ">");
259
288
  if (!isSelfClose) {
260
- result.template.push(`</${tagName}>`);
289
+ transformChildren(path, result);
290
+ if (hasSiblingElement(path)) {
291
+ addToTemplate(result, `</${tagName}>`);
292
+ }
261
293
  }
262
294
  }
263
295
  } else {
@@ -266,7 +298,7 @@ function transformJSXServiceElement(path, result, isRoot = false) {
266
298
  }
267
299
  }
268
300
  function transformChildren(path, result) {
269
- const parentIndex = result.template.length;
301
+ const parentIndex = isSsg ? result.template.length : result.index;
270
302
  path.get("children").reduce((pre, cur) => {
271
303
  if (isValidChild(cur)) {
272
304
  const lastChild = pre.at(-1);
@@ -284,12 +316,13 @@ function transformChildren(path, result) {
284
316
  });
285
317
  }
286
318
  function transformChild(child, result) {
319
+ result.index++;
287
320
  if (child.isJSXElement() || child.isJSXFragment()) {
288
- transformJSXServiceElement(child, result, false);
321
+ transformJSXElement(child, result, false);
289
322
  } else if (child.isJSXExpressionContainer()) {
290
323
  const expression = child.get("expression");
291
324
  if (expression.isStringLiteral() || expression.isNumericLiteral()) {
292
- result.template[result.template.length - 1] += String(expression.node.value);
325
+ addToTemplate(result, String(expression.node.value));
293
326
  } else if (expression.isExpression()) {
294
327
  replaceChild(expression.node, result);
295
328
  } else if (t3.isJSXEmptyExpression(expression.node)) {
@@ -297,7 +330,7 @@ function transformChild(child, result) {
297
330
  throw new Error("Unsupported child type");
298
331
  }
299
332
  } else if (child.isJSXText()) {
300
- result.template.push(String(child.node.value));
333
+ addToTemplate(result, String(child.node.value));
301
334
  } else {
302
335
  throw new Error("Unsupported child type");
303
336
  }
@@ -318,7 +351,7 @@ function handleAttributes(props, result) {
318
351
  let klass = "";
319
352
  let style = "";
320
353
  for (const prop in props) {
321
- const value = props[prop];
354
+ let value = props[prop];
322
355
  if (prop === "class" && typeof value === "string") {
323
356
  klass += ` ${value}`;
324
357
  delete props[prop];
@@ -330,16 +363,42 @@ function handleAttributes(props, result) {
330
363
  continue;
331
364
  }
332
365
  if (value === true) {
333
- result.template[result.template.length - 1] += ` ${prop}`;
366
+ addToTemplate(result, ` ${prop}`);
334
367
  delete props[prop];
335
368
  }
336
369
  if (value === false) {
337
370
  delete props[prop];
338
371
  }
339
372
  if (typeof value === "string" || typeof value === "number") {
340
- result.template[result.template.length - 1] += ` ${prop}="${value}"`;
373
+ addToTemplate(result, ` ${prop}="${value}"`);
341
374
  delete props[prop];
342
375
  }
376
+ if (t3.isConditionalExpression(value)) {
377
+ const { test, consequent, alternate } = value;
378
+ value = t3.arrowFunctionExpression([], t3.conditionalExpression(test, consequent, alternate));
379
+ props[prop] = value;
380
+ }
381
+ if (t3.isObjectExpression(value)) {
382
+ let hasConditional = false;
383
+ value.properties.forEach((property) => {
384
+ if (t3.isObjectProperty(property) && t3.isConditionalExpression(property.value)) {
385
+ hasConditional = true;
386
+ }
387
+ });
388
+ if (hasConditional) {
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
+ }
400
+ }
401
+ }
343
402
  }
344
403
  if (Object.keys(props).length > 0) {
345
404
  result.props[result.index] = props;
@@ -347,30 +406,32 @@ function handleAttributes(props, result) {
347
406
  klass = klass.trim();
348
407
  style = style.trim();
349
408
  if (klass) {
350
- result.template[result.template.length - 1] += ` class="${klass}"`;
409
+ addToTemplate(result, ` class="${klass}"`);
351
410
  }
352
411
  if (style) {
353
- result.template[result.template.length - 1] += ` style="${style}"`;
412
+ addToTemplate(result, ` style="${style}"`);
354
413
  }
355
414
  }
356
415
  function replaceChild(node, result) {
357
416
  var _a, _b, _c, _d, _e;
358
417
  if (result.isLastChild) {
359
418
  result.index--;
419
+ } else {
420
+ addToTemplate(result, "<!>");
360
421
  }
361
422
  (_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
362
423
  (_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
363
424
  result.props[result.parentIndex].children.push(
364
425
  t3.arrayExpression([
365
426
  t3.arrowFunctionExpression([], node),
366
- t3.identifier(String(result.template.length))
427
+ result.isLastChild ? t3.nullLiteral() : t3.identifier(String(result.index))
367
428
  ])
368
429
  );
369
430
  }
370
431
  function getChildren(path) {
371
432
  return path.get("children").filter((child) => isValidChild(child)).map((child) => {
372
433
  if (child.isJSXElement() || child.isJSXFragment()) {
373
- transformJSXService(child);
434
+ transformJSX(child);
374
435
  } else if (child.isJSXExpressionContainer()) {
375
436
  child.replaceWith(child.get("expression"));
376
437
  } else if (child.isJSXText()) {
@@ -390,7 +451,6 @@ function isValidChild(path) {
390
451
  }
391
452
  function getAttrProps(path) {
392
453
  const props = {};
393
- let hasExpression = false;
394
454
  path.get("openingElement").get("attributes").forEach((attribute) => {
395
455
  if (attribute.isJSXAttribute()) {
396
456
  const name = getAttrName(attribute.node);
@@ -407,10 +467,9 @@ function getAttrProps(path) {
407
467
  } else if (expression.isNumericLiteral()) {
408
468
  props[name] = expression.node.value;
409
469
  } else if (expression.isJSXElement() || expression.isJSXFragment()) {
410
- transformJSXService(expression);
470
+ transformJSX(expression);
411
471
  props[name] = expression.node;
412
472
  } else if (expression.isExpression()) {
413
- hasExpression = true;
414
473
  if (/^key|ref|on.+$/.test(name)) {
415
474
  props[name] = expression.node;
416
475
  } else if (/^bind:.+/.test(name)) {
@@ -430,293 +489,7 @@ function getAttrProps(path) {
430
489
  }
431
490
  }
432
491
  } 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) {
453
- const result = {
454
- index: 1,
455
- isLastChild: false,
456
- parentIndex: 0,
457
- props: {},
458
- template: ""
459
- };
460
- transformJSXElement(path, result, true);
461
- path.replaceWith(createEssorNode2(path, result));
462
- }
463
- function createEssorNode2(path, result) {
464
- var _a;
465
- const state = path.state;
466
- let tmpl;
467
- if (path.isJSXElement() && isComponent(getTagName(path.node))) {
468
- tmpl = t4.identifier(getTagName(path.node));
469
- } else {
470
- tmpl = path.scope.generateUidIdentifier("_tmpl$");
471
- const template = t4.callExpression(state.template, [t4.stringLiteral(result.template)]);
472
- const declarator = t4.variableDeclarator(tmpl, template);
473
- state.tmplDeclaration.declarations.push(declarator);
474
- imports.add("template");
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("h");
482
- return t4.callExpression(state.h, args);
483
- }
484
- function createProps2(props) {
485
- const toAstNode = (value) => {
486
- if (Array.isArray(value)) {
487
- return t4.arrayExpression(value.map(toAstNode));
488
- }
489
- if (value && typeof value === "object" && !t4.isNode(value)) {
490
- return createProps2(value);
491
- }
492
- switch (typeof value) {
493
- case "string":
494
- return t4.stringLiteral(value);
495
- case "number":
496
- return t4.numericLiteral(value);
497
- case "boolean":
498
- return t4.booleanLiteral(value);
499
- case "undefined":
500
- return t4.tsUndefinedKeyword();
501
- case void 0:
502
- return t4.tsUndefinedKeyword();
503
- case null:
504
- return t4.nullLiteral();
505
- default:
506
- return value;
507
- }
508
- };
509
- const result = Object.keys(props).filter((prop) => prop !== "key").map((prop) => {
510
- const value = toAstNode(props[prop]);
511
- return prop === "_$spread$" ? t4.spreadElement(value) : t4.objectProperty(t4.stringLiteral(prop), value);
512
- });
513
- return t4.objectExpression(result);
514
- }
515
- function transformJSXElement(path, result, isRoot = false) {
516
- if (path.isJSXElement()) {
517
- const tagName = getTagName(path.node);
518
- const tagIsComponent = isComponent(tagName);
519
- const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
520
- const isSvg = svgTags.includes(tagName) && result.index === 1;
521
- const props = getAttrProps2(path);
522
- if (tagIsComponent) {
523
- if (isRoot) {
524
- result.props = props;
525
- const children = getChildren2(path);
526
- if (children.length > 0) {
527
- const childrenGenerator = children.length === 1 ? children[0] : t4.arrayExpression(children);
528
- result.props.children = childrenGenerator;
529
- }
530
- } else {
531
- transformJSXClient(path);
532
- replaceChild2(path.node, result);
533
- }
534
- } else {
535
- if (isSvg) {
536
- result.template = "<svg _svg_>";
537
- }
538
- result.template += `<${tagName}`;
539
- handleAttributes2(props, result);
540
- result.template += isSelfClose ? "/>" : ">";
541
- if (!isSelfClose) {
542
- transformChildren2(path, result);
543
- if (hasSiblingElement(path)) {
544
- result.template += `</${tagName}>`;
545
- }
546
- }
547
- }
548
- } else {
549
- result.index--;
550
- transformChildren2(path, result);
551
- }
552
- }
553
- function transformChildren2(path, result) {
554
- const parentIndex = result.index;
555
- path.get("children").reduce((pre, cur) => {
556
- if (isValidChild2(cur)) {
557
- const lastChild = pre.at(-1);
558
- if (lastChild && isTextChild(cur) && isTextChild(lastChild)) {
559
- setNodeText(lastChild, getNodeText2(lastChild) + getNodeText2(cur));
560
- } else {
561
- pre.push(cur);
562
- }
563
- }
564
- return pre;
565
- }, []).forEach((child, i, arr) => {
566
- result.parentIndex = parentIndex;
567
- result.isLastChild = i === arr.length - 1;
568
- transformChild2(child, result);
569
- });
570
- }
571
- function transformChild2(child, result) {
572
- result.index++;
573
- if (child.isJSXElement() || child.isJSXFragment()) {
574
- transformJSXElement(child, result, false);
575
- } else if (child.isJSXExpressionContainer()) {
576
- const expression = child.get("expression");
577
- if (expression.isStringLiteral() || expression.isNumericLiteral()) {
578
- result.template += String(expression.node.value);
579
- } else if (expression.isExpression()) {
580
- replaceChild2(expression.node, result);
581
- } else if (t4.isJSXEmptyExpression(expression.node)) {
582
- } else {
583
- throw new Error("Unsupported child type");
584
- }
585
- } else if (child.isJSXText()) {
586
- result.template += String(child.node.value);
587
- } else {
588
- throw new Error("Unsupported child type");
589
- }
590
- }
591
- function getNodeText2(path) {
592
- if (path.isJSXText()) {
593
- return path.node.value;
594
- }
595
- if (path.isJSXExpressionContainer()) {
596
- const expression = path.get("expression");
597
- if (expression.isStringLiteral() || expression.isNumericLiteral()) {
598
- return String(expression.node.value);
599
- }
600
- }
601
- return "";
602
- }
603
- function handleAttributes2(props, result) {
604
- let klass = "";
605
- let style = "";
606
- for (const prop in props) {
607
- const value = props[prop];
608
- if (prop === "class" && typeof value === "string") {
609
- klass += ` ${value}`;
610
- delete props[prop];
611
- continue;
612
- }
613
- if (prop === "style" && typeof value === "string") {
614
- style += `${value}${value.at(-1) === ";" ? "" : ";"}`;
615
- delete props[prop];
616
- continue;
617
- }
618
- if (value === true) {
619
- result.template += ` ${prop}`;
620
- delete props[prop];
621
- }
622
- if (value === false) {
623
- delete props[prop];
624
- }
625
- if (typeof value === "string" || typeof value === "number") {
626
- result.template += ` ${prop}="${value}"`;
627
- delete props[prop];
628
- }
629
- }
630
- if (Object.keys(props).length > 0) {
631
- result.props[result.index] = props;
632
- }
633
- klass = klass.trim();
634
- style = style.trim();
635
- if (klass) {
636
- result.template += ` class="${klass}"`;
637
- }
638
- if (style) {
639
- result.template += ` style="${style}"`;
640
- }
641
- }
642
- function replaceChild2(node, result) {
643
- var _a, _b, _c, _d, _e;
644
- if (result.isLastChild) {
645
- result.index--;
646
- } else {
647
- result.template += "<!>";
648
- }
649
- (_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
650
- (_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
651
- result.props[result.parentIndex].children.push(
652
- t4.arrayExpression([
653
- t4.arrowFunctionExpression([], node),
654
- result.isLastChild ? t4.nullLiteral() : t4.identifier(String(result.index))
655
- ])
656
- );
657
- }
658
- function getChildren2(path) {
659
- return path.get("children").filter((child) => isValidChild2(child)).map((child) => {
660
- if (child.isJSXElement() || child.isJSXFragment()) {
661
- transformJSXClient(child);
662
- } else if (child.isJSXExpressionContainer()) {
663
- child.replaceWith(child.get("expression"));
664
- } else if (child.isJSXText()) {
665
- child.replaceWith(t4.stringLiteral(child.node.value));
666
- } else {
667
- throw new Error("Unsupported child type");
668
- }
669
- return child.node;
670
- });
671
- }
672
- function isValidChild2(path) {
673
- const regex = /^\s*$/;
674
- if (path.isStringLiteral() || path.isJSXText()) {
675
- return !regex.test(path.node.value);
676
- }
677
- return Object.keys(path.node).length > 0;
678
- }
679
- function getAttrProps2(path) {
680
- const props = {};
681
- path.get("openingElement").get("attributes").forEach((attribute) => {
682
- if (attribute.isJSXAttribute()) {
683
- const name = getAttrName(attribute.node);
684
- const value = attribute.get("value");
685
- if (!value.node) {
686
- props[name] = true;
687
- } else if (value.isStringLiteral()) {
688
- props[name] = value.node.value;
689
- } else {
690
- if (value.isJSXExpressionContainer()) {
691
- const expression = value.get("expression");
692
- if (expression.isStringLiteral()) {
693
- props[name] = expression.node.value;
694
- } else if (expression.isNumericLiteral()) {
695
- props[name] = expression.node.value;
696
- } else if (expression.isJSXElement() || expression.isJSXFragment()) {
697
- transformJSXClient(expression);
698
- props[name] = expression.node;
699
- } else if (expression.isExpression()) {
700
- if (/^key|ref|on.+$/.test(name)) {
701
- props[name] = expression.node;
702
- } else if (/^bind:.+/.test(name)) {
703
- const value2 = path.scope.generateUidIdentifier("value");
704
- const bindName = name.slice(5).toLocaleLowerCase();
705
- props[bindName] = expression.node;
706
- props[`update${capitalizeFirstLetter(bindName)}`] = t4.arrowFunctionExpression(
707
- [value2],
708
- t4.assignmentExpression("=", expression.node, value2)
709
- );
710
- } else {
711
- if (expression.isConditionalExpression()) {
712
- props[name] = t4.arrowFunctionExpression([], expression.node);
713
- } else {
714
- props[name] = expression.node;
715
- }
716
- }
717
- }
718
- } else if (value.isJSXElement() || value.isJSXFragment()) {
719
- transformJSXClient(value);
492
+ transformJSX(value);
720
493
  props[name] = value.node;
721
494
  }
722
495
  }
@@ -729,41 +502,34 @@ function getAttrProps2(path) {
729
502
  return props;
730
503
  }
731
504
 
732
- // src/jsx/index.ts
733
- function transformJSX(path) {
734
- const state = path.state;
735
- const isSsg = state.opts.ssg;
736
- return isSsg ? transformJSXService(path) : transformJSXClient(path);
737
- }
738
-
739
505
  // src/signal/symbol.ts
740
- import { types as t5 } from "@babel/core";
506
+ import { types as t4 } from "@babel/core";
741
507
  function replaceSymbol(path) {
742
508
  const init = path.node.init;
743
509
  const variableName = path.node.id.name;
744
- if (t5.isObjectPattern(path.node.id) || t5.isArrayPattern(path.node.id)) {
510
+ if (t4.isObjectPattern(path.node.id) || t4.isArrayPattern(path.node.id)) {
745
511
  return;
746
512
  }
747
- if (!startsWith(variableName, "$")) {
513
+ if (!isSymbolStart(path, variableName)) {
748
514
  return;
749
515
  }
750
- if (init && (t5.isFunctionExpression(init) || t5.isArrowFunctionExpression(init)) && path.parent.kind === "const") {
751
- 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] : []);
752
518
  imports.add("useComputed");
753
519
  path.node.init = newInit;
754
520
  } else {
755
- 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] : []);
756
522
  imports.add("useSignal");
757
523
  path.node.init = newInit;
758
524
  }
759
525
  }
760
526
  function symbolIdentifier(path) {
761
527
  const parentPath = path.parentPath;
762
- 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)) {
763
529
  return;
764
530
  }
765
531
  const { node } = path;
766
- if (node.name.startsWith("$")) {
532
+ if (isSymbolStart(path, node.name)) {
767
533
  let currentPath = path;
768
534
  while (currentPath.parentPath && !currentPath.parentPath.isProgram()) {
769
535
  if (currentPath.parentPath.isMemberExpression() && currentPath.parentPath.node.property.name === "value") {
@@ -771,27 +537,27 @@ function symbolIdentifier(path) {
771
537
  }
772
538
  currentPath = currentPath.parentPath;
773
539
  }
774
- const newNode = t5.memberExpression(t5.identifier(node.name), t5.identifier("value"));
540
+ const newNode = t4.memberExpression(t4.identifier(node.name), t4.identifier("value"));
775
541
  path.replaceWith(newNode);
776
542
  }
777
543
  }
778
544
  function symbolObjectPattern(path) {
779
545
  path.node.properties.forEach((property) => {
780
- if (t5.isObjectProperty(property) && t5.isIdentifier(property.key) && property.key.name.startsWith("$")) {
781
- 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);
782
548
  property.key = newKey;
783
549
  }
784
550
  });
785
551
  }
786
552
  function symbolArrayPattern(path) {
787
553
  path.node.elements.forEach((element) => {
788
- if (t5.isIdentifier(element) && element.name.startsWith("$")) {
789
- const newElement = t5.identifier(element.name);
554
+ if (t4.isIdentifier(element) && element.name.startsWith("$")) {
555
+ const newElement = t4.identifier(element.name);
790
556
  element.name = newElement.name;
791
- } else if (t5.isObjectPattern(element)) {
557
+ } else if (t4.isObjectPattern(element)) {
792
558
  element.properties.forEach((property) => {
793
- if (t5.isObjectProperty(property) && t5.isIdentifier(property.key) && property.key.name.startsWith("$")) {
794
- 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);
795
561
  property.key = newKey;
796
562
  }
797
563
  });
@@ -800,7 +566,17 @@ function symbolArrayPattern(path) {
800
566
  }
801
567
 
802
568
  // src/signal/import.ts
803
- 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
+ }
804
580
  function isVariableUsedAsObject(path, variableName) {
805
581
  const binding = path.scope.getBinding(variableName);
806
582
  let isUsedObject = false;
@@ -808,30 +584,28 @@ function isVariableUsedAsObject(path, variableName) {
808
584
  return isUsedObject;
809
585
  }
810
586
  for (const referencePath of binding.referencePaths) {
811
- if (t6.isMemberExpression(referencePath.parent)) {
812
- 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
+ }
813
597
  }
814
598
  }
815
599
  return isUsedObject;
816
600
  }
817
- function replaceImportDeclaration(path) {
818
- const imports2 = path.node.specifiers;
819
- imports2.forEach((specifier) => {
820
- const variableName = specifier.local.name;
821
- if (startsWith(variableName, "$") && !isVariableUsedAsObject(path, variableName)) {
822
- path.scope.rename(variableName, `${variableName}.value`);
823
- specifier.local.name = `${variableName}`;
824
- }
825
- });
826
- }
827
601
 
828
602
  // src/signal/props.ts
829
- import { types as t7 } from "@babel/core";
603
+ import { types as t6 } from "@babel/core";
830
604
  function replaceProps(path) {
831
605
  var _a;
832
606
  const state = path.state;
833
607
  const firstParam = path.node.params[0];
834
- if (!firstParam || !t7.isObjectPattern(firstParam)) {
608
+ if (!firstParam || !t6.isObjectPattern(firstParam)) {
835
609
  return;
836
610
  }
837
611
  const returnStatement = path.get("body").get("body").find((statement) => statement.isReturnStatement());
@@ -839,18 +613,18 @@ function replaceProps(path) {
839
613
  return;
840
614
  }
841
615
  const returnValue = (_a = returnStatement.node) == null ? void 0 : _a.argument;
842
- if (!t7.isJSXElement(returnValue)) {
616
+ if (!t6.isJSXElement(returnValue)) {
843
617
  return;
844
618
  }
845
619
  function replaceProperties(properties2, parentPath) {
846
620
  properties2.forEach((property) => {
847
- if (t7.isObjectProperty(property)) {
621
+ if (t6.isObjectProperty(property)) {
848
622
  const keyName = property.key.name;
849
- if (t7.isIdentifier(property.value)) {
623
+ if (t6.isIdentifier(property.value)) {
850
624
  const propertyName = property.value.name;
851
625
  const newName = `${parentPath}${keyName}`;
852
626
  path.scope.rename(propertyName, newName);
853
- } else if (t7.isObjectPattern(property.value)) {
627
+ } else if (t6.isObjectPattern(property.value)) {
854
628
  replaceProperties(property.value.properties, `${parentPath}${keyName}.`);
855
629
  }
856
630
  }
@@ -858,10 +632,10 @@ function replaceProps(path) {
858
632
  }
859
633
  const properties = firstParam.properties;
860
634
  replaceProperties(
861
- properties.filter((property) => !t7.isRestElement(property)),
635
+ properties.filter((property) => !t6.isRestElement(property)),
862
636
  "__props."
863
637
  );
864
- const notRestProperties = properties.filter((property) => !t7.isRestElement(property));
638
+ const notRestProperties = properties.filter((property) => !t6.isRestElement(property));
865
639
  const notRestNames = notRestProperties.map(
866
640
  (property) => property.key.name
867
641
  );
@@ -869,19 +643,19 @@ function replaceProps(path) {
869
643
  console.warn("props name can not start with $");
870
644
  return;
871
645
  }
872
- const restElement = properties.find((property) => t7.isRestElement(property));
873
- path.node.params[0] = t7.identifier("__props");
646
+ const restElement = properties.find((property) => t6.isRestElement(property));
647
+ path.node.params[0] = t6.identifier("__props");
874
648
  if (restElement) {
875
649
  const restName = restElement.argument.name;
876
650
  if (notRestProperties.length === 0) {
877
- path.node.params[0] = t7.identifier(restName);
651
+ path.node.params[0] = t6.identifier(restName);
878
652
  } else {
879
- const restVariableDeclaration = t7.variableDeclaration("const", [
880
- t7.variableDeclarator(
881
- t7.identifier(restName),
882
- t7.callExpression(state.useReactive, [
883
- t7.identifier("__props"),
884
- 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)))
885
659
  ])
886
660
  )
887
661
  ]);
@@ -903,19 +677,24 @@ function src_default() {
903
677
  },
904
678
  visitor: {
905
679
  Program: transformProgram,
906
- JSXElement: transformJSX,
907
- JSXFragment: transformJSX,
908
680
  FunctionDeclaration: replaceProps,
909
681
  ArrowFunctionExpression: replaceProps,
910
682
  VariableDeclarator: replaceSymbol,
911
683
  ImportDeclaration: replaceImportDeclaration,
912
684
  Identifier: symbolIdentifier,
913
685
  ObjectPattern: symbolObjectPattern,
914
- ArrayPattern: symbolArrayPattern
686
+ ArrayPattern: symbolArrayPattern,
687
+ JSXElement: transformJSX,
688
+ JSXFragment: transformJSX
915
689
  }
916
690
  };
917
691
  }
918
692
  export {
919
693
  src_default as default
920
694
  };
695
+ /**
696
+ * @estjs/shared v0.0.10-beta.20
697
+ * (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
698
+ * @license MIT
699
+ **/
921
700
  //# sourceMappingURL=index.js.map