babel-plugin-essor 0.0.5-beta.7 → 0.0.6-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -24,10 +24,50 @@ __export(src_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(src_exports);
26
26
 
27
- // src/transform.ts
27
+ // src/jsx/server.ts
28
+ var import_core3 = require("@babel/core");
29
+
30
+ // src/program.ts
28
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
+ }
29
69
 
30
- // src/constants.ts
70
+ // src/jsx/constants.ts
31
71
  var selfClosingTags = [
32
72
  "area",
33
73
  "base",
@@ -65,48 +105,10 @@ var svgTags = [
65
105
  "text",
66
106
  "use"
67
107
  ];
68
- var imports = /* @__PURE__ */ new Set();
69
-
70
- // src/transform.ts
71
- var transformProgram = {
72
- enter(path) {
73
- path.state = {
74
- h: path.scope.generateUidIdentifier("h$"),
75
- useSignal: path.scope.generateUidIdentifier("useSignal$"),
76
- useComputed: path.scope.generateUidIdentifier("useComputed$"),
77
- template: path.scope.generateUidIdentifier("template$"),
78
- signalObject: path.scope.generateUidIdentifier("signalObject&"),
79
- tmplDeclaration: import_core.types.variableDeclaration("const", [])
80
- };
81
- imports.clear();
82
- },
83
- exit(path) {
84
- const state = path.state;
85
- if (state.tmplDeclaration.declarations.length > 0) {
86
- const index = path.node.body.findIndex(
87
- (node) => !import_core.types.isImportDeclaration(node) && !import_core.types.isExportDeclaration(node)
88
- );
89
- path.node.body.splice(index, 0, state.tmplDeclaration);
90
- }
91
- if (imports.size > 0) {
92
- path.node.body.unshift(createImport(state, "essor"));
93
- }
94
- }
95
- };
96
- function createImport(state, from) {
97
- const ImportSpecifier = [];
98
- imports.forEach((name) => {
99
- const local = import_core.types.identifier(state[name].name);
100
- const imported = import_core.types.identifier(name);
101
- ImportSpecifier.push(import_core.types.importSpecifier(local, imported));
102
- });
103
- const importSource = import_core.types.stringLiteral(from);
104
- return import_core.types.importDeclaration(ImportSpecifier, importSource);
105
- }
106
108
 
107
- // src/jsx-transform.ts
109
+ // src/jsx/client.ts
108
110
  var import_core2 = require("@babel/core");
109
- function transformJSX(path) {
111
+ function transformJSXClient(path) {
110
112
  const result = {
111
113
  index: 1,
112
114
  isLastChild: false,
@@ -114,11 +116,11 @@ function transformJSX(path) {
114
116
  props: {},
115
117
  template: ""
116
118
  };
117
- transformElement(path, result, true);
119
+ transformJSXElement(path, result, true);
118
120
  path.replaceWith(createEssorNode(path, result));
119
121
  }
120
122
  function createEssorNode(path, result) {
121
- var _a, _b;
123
+ var _a;
122
124
  const state = path.state;
123
125
  let tmpl;
124
126
  if (path.isJSXElement() && isComponent(getTagName(path.node))) {
@@ -131,7 +133,7 @@ function createEssorNode(path, result) {
131
133
  imports.add("template");
132
134
  }
133
135
  const args = [tmpl, createProps(result.props)];
134
- const key = (_b = result.props.key) != null ? _b : (_a = result.props[0]) == null ? void 0 : _a.key;
136
+ const key = result.props.key || ((_a = result.props[0]) == null ? void 0 : _a.key);
135
137
  if (key) {
136
138
  args.push(key);
137
139
  }
@@ -139,119 +141,63 @@ function createEssorNode(path, result) {
139
141
  return import_core2.types.callExpression(state.h, args);
140
142
  }
141
143
  function createProps(props) {
142
- const result = [];
143
- for (const prop in props) {
144
- let value = props[prop];
145
- if (prop === "key") {
146
- continue;
147
- }
144
+ const toAstNode = (value) => {
148
145
  if (Array.isArray(value)) {
149
- value = import_core2.types.arrayExpression(value);
150
- }
151
- if (typeof value === "object" && value !== null && !import_core2.types.isNode(value)) {
152
- value = createProps(value);
153
- }
154
- if (typeof value === "string") {
155
- value = import_core2.types.stringLiteral(value);
156
- }
157
- if (typeof value === "number") {
158
- value = import_core2.types.numericLiteral(value);
159
- }
160
- if (typeof value === "boolean") {
161
- value = import_core2.types.booleanLiteral(value);
162
- }
163
- if (value === void 0) {
164
- value = import_core2.types.tsUndefinedKeyword();
165
- }
166
- if (value === null) {
167
- value = import_core2.types.nullLiteral();
146
+ return import_core2.types.arrayExpression(value.map(toAstNode));
168
147
  }
169
- if (prop === "_$spread$") {
170
- result.push(import_core2.types.spreadElement(value));
171
- } else {
172
- result.push(import_core2.types.objectProperty(import_core2.types.stringLiteral(prop), value));
148
+ if (value && typeof value === "object" && !import_core2.types.isNode(value)) {
149
+ return createProps(value);
173
150
  }
174
- }
175
- return import_core2.types.objectExpression(result);
176
- }
177
- function getAttrProps(path) {
178
- const props = {};
179
- path.get("openingElement").get("attributes").forEach((attribute) => {
180
- if (attribute.isJSXAttribute()) {
181
- const name = getAttrName(attribute.node);
182
- const value = attribute.get("value");
183
- if (!value.node) {
184
- props[name] = true;
185
- } else if (value.isStringLiteral()) {
186
- props[name] = value.node.value;
187
- } else {
188
- if (value.isJSXExpressionContainer()) {
189
- const expression = value.get("expression");
190
- if (expression.isStringLiteral()) {
191
- props[name] = expression.node.value;
192
- } else if (expression.isNumericLiteral()) {
193
- props[name] = expression.node.value;
194
- } else if (expression.isJSXElement() || expression.isJSXFragment()) {
195
- transformJSX(expression);
196
- props[name] = expression.node;
197
- } else if (expression.isExpression()) {
198
- if (/^key|ref|on.+$/.test(name)) {
199
- props[name] = expression.node;
200
- } else if (/^bind:.+/.test(name)) {
201
- props[name] = import_core2.types.arrowFunctionExpression([], expression.node);
202
- const value2 = path.scope.generateUidIdentifier("value");
203
- props[`update:${name.slice(5).toLocaleLowerCase()}`] = import_core2.types.arrowFunctionExpression(
204
- [value2],
205
- import_core2.types.assignmentExpression("=", expression.node, value2)
206
- );
207
- } else {
208
- if (expression.isConditionalExpression()) {
209
- props[name] = import_core2.types.arrowFunctionExpression([], expression.node);
210
- } else {
211
- props[name] = expression.node;
212
- }
213
- }
214
- }
215
- } else if (value.isJSXElement() || value.isJSXFragment()) {
216
- transformJSX(value);
217
- props[name] = value.node;
218
- }
219
- }
220
- } else if (attribute.isJSXSpreadAttribute()) {
221
- props._$spread$ = attribute.get("argument").node;
222
- } else {
223
- throw new Error("Unsupported attribute type");
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;
224
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);
225
171
  });
226
- return props;
172
+ return import_core2.types.objectExpression(result);
227
173
  }
228
- function transformElement(path, result, isRoot) {
174
+ function transformJSXElement(path, result, isRoot = false) {
229
175
  if (path.isJSXElement()) {
230
176
  const tagName = getTagName(path.node);
231
177
  const tagIsComponent = isComponent(tagName);
232
- const isSelfClosing = !tagIsComponent && selfClosingTags.includes(tagName);
233
- const isSvgTemplate = svgTags.includes(tagName) && result.index === 1;
178
+ const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
179
+ const isSvg = svgTags.includes(tagName) && result.index === 1;
234
180
  const props = getAttrProps(path);
235
181
  if (tagIsComponent) {
236
182
  if (isRoot) {
237
183
  result.props = props;
238
184
  const children = getChildren(path);
239
185
  if (children.length > 0) {
240
- const childrenGetter = children.length === 1 ? children[0] : import_core2.types.arrayExpression(children);
241
- result.props.children = childrenGetter;
186
+ const childrenGenerator = children.length === 1 ? children[0] : import_core2.types.arrayExpression(children);
187
+ result.props.children = childrenGenerator;
242
188
  }
243
189
  } else {
244
- transformJSX(path);
190
+ transformJSXClient(path);
245
191
  replaceChild(path.node, result);
246
192
  }
247
193
  } else {
248
- if (isSvgTemplate) {
249
- result.template += `<svg _svg_>`;
194
+ if (isSvg) {
195
+ result.template = "<svg _svg_>";
250
196
  }
251
197
  result.template += `<${tagName}`;
252
198
  handleAttributes(props, result);
253
- result.template += isSelfClosing ? "/>" : ">";
254
- if (!isSelfClosing) {
199
+ result.template += isSelfClose ? "/>" : ">";
200
+ if (!isSelfClose) {
255
201
  transformChildren(path, result);
256
202
  result.template += `</${tagName}>`;
257
203
  }
@@ -261,20 +207,78 @@ function transformElement(path, result, isRoot) {
261
207
  transformChildren(path, result);
262
208
  }
263
209
  }
264
- function getChildren(path) {
265
- return path.get("children").filter(isValidChild).map((child) => {
266
- if (child.isJSXElement() || child.isJSXFragment()) {
267
- transformJSX(child);
268
- } else if (child.isJSXExpressionContainer()) {
269
- child.replaceWith(child.get("expression"));
270
- } else if (child.isJSXText()) {
271
- child.replaceWith(import_core2.types.stringLiteral(child.node.value));
272
- } else {
273
- throw new Error("Unsupported JSX child");
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
+ }
274
220
  }
275
- return child.node;
221
+ return pre;
222
+ }, []).forEach((child, i, arr) => {
223
+ result.parentIndex = parentIndex;
224
+ result.isLastChild = i === arr.length - 1;
225
+ transformChild(child, result);
276
226
  });
277
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
+ }
278
282
  function handleAttributes(props, result) {
279
283
  let klass = "";
280
284
  let style = "";
@@ -332,49 +336,12 @@ function handleAttributes(props, result) {
332
336
  result.template += ` style="${style}"`;
333
337
  }
334
338
  }
335
- function transformChildren(path, result) {
336
- const parentIndex = result.index;
337
- path.get("children").reduce((acc, child) => {
338
- if (isValidChild(child)) {
339
- const lastChild = acc.at(-1);
340
- if (lastChild && isTextChild(child) && isTextChild(lastChild)) {
341
- setNodeText(lastChild, getNodeText(lastChild) + getNodeText(child));
342
- } else {
343
- acc.push(child);
344
- }
345
- }
346
- return acc;
347
- }, []).forEach((child, i, arr) => {
348
- result.parentIndex = parentIndex;
349
- result.isLastChild = i === arr.length - 1;
350
- transformChild(child, result);
351
- });
352
- }
353
- function transformChild(child, result) {
354
- result.index++;
355
- if (child.isJSXElement() || child.isJSXFragment()) {
356
- transformElement(child, result);
357
- } else if (child.isJSXExpressionContainer()) {
358
- const expression = child.get("expression");
359
- if (expression.isStringLiteral() || expression.isNumericLiteral()) {
360
- result.template += expression.node.value;
361
- } else if (expression.isExpression()) {
362
- replaceChild(expression.node, result);
363
- } else {
364
- throw new Error("Unsupported JSX child");
365
- }
366
- } else if (child.isJSXText()) {
367
- result.template += child.node.value;
368
- } else {
369
- throw new Error("Unsupported JSX child");
370
- }
371
- }
372
339
  function replaceChild(node, result) {
373
340
  var _a, _b, _c, _d, _e;
374
341
  if (result.isLastChild) {
375
342
  result.index--;
376
343
  } else {
377
- result.template += "<!>";
344
+ result.template += "<!-->";
378
345
  }
379
346
  (_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
380
347
  (_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
@@ -385,116 +352,469 @@ function replaceChild(node, result) {
385
352
  ])
386
353
  );
387
354
  }
388
- function getAttrName(attribute) {
389
- if (import_core2.types.isJSXIdentifier(attribute.name)) {
390
- return attribute.name.name;
391
- }
392
- if (import_core2.types.isJSXNamespacedName(attribute.name)) {
393
- return `${attribute.name.namespace.name}:${attribute.name.name.name}`;
394
- }
395
- throw new Error("Unsupported attribute type");
396
- }
397
- function getNodeText(path) {
398
- if (path.isJSXText()) {
399
- return path.node.value;
400
- }
401
- if (path.isJSXExpressionContainer()) {
402
- const expression = path.get("expression");
403
- if (expression.isStringLiteral() || expression.isNumericLiteral()) {
404
- return String(expression.node.value);
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");
405
365
  }
406
- }
407
- return "";
366
+ return child.node;
367
+ });
408
368
  }
409
- function setNodeText(path, text) {
410
- if (path.isJSXText()) {
411
- path.node.value = text;
369
+ function isValidChild(path) {
370
+ const regex = /^\s*$/;
371
+ if (path.isStringLiteral() || path.isJSXText()) {
372
+ return !regex.test(path.node.value);
412
373
  }
413
- if (path.isJSXExpressionContainer()) {
414
- const expression = path.get("expression");
415
- if (expression.isStringLiteral() || expression.isNumericLiteral()) {
416
- expression.replaceWith(import_core2.types.stringLiteral(text));
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");
417
418
  }
418
- }
419
+ });
420
+ return props;
419
421
  }
420
- function getTagName(node) {
421
- const jsxName = node.openingElement.name;
422
- return jsxElementNameToString(jsxName);
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");
423
430
  }
424
431
  function isComponent(tagName) {
425
432
  return tagName[0] && tagName[0].toLowerCase() !== tagName[0] || tagName.includes(".") || /[^A-Za-z]/.test(tagName[0]);
426
433
  }
427
- function isTextChild(path) {
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
+ }
428
601
  if (path.isJSXExpressionContainer()) {
429
602
  const expression = path.get("expression");
430
- if (expression.isJSXText() || expression.isStringLiteral() || expression.isNumericLiteral()) {
431
- return true;
603
+ if (expression.isStringLiteral() || expression.isNumericLiteral()) {
604
+ return String(expression.node.value);
432
605
  }
433
606
  }
434
- if (path.isJSXText() || path.isStringLiteral() || path.isNullLiteral()) {
435
- return true;
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
+ }
436
618
  }
437
- return false;
438
619
  }
439
- function isValidChild(path) {
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) {
440
706
  const regex = /^\s*$/;
441
707
  if (path.isStringLiteral() || path.isJSXText()) {
442
708
  return !regex.test(path.node.value);
443
709
  }
444
710
  return Object.keys(path.node).length > 0;
445
711
  }
446
- function jsxElementNameToString(node) {
447
- if (import_core2.types.isJSXMemberExpression(node)) {
448
- return `${jsxElementNameToString(node.object)}.${node.property.name}`;
449
- }
450
- if (import_core2.types.isJSXIdentifier(node) || import_core2.types.isIdentifier(node)) {
451
- return node.name;
452
- }
453
- return `${node.namespace.name}:${node.name.name}`;
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
+ };
454
763
  }
455
764
 
456
- // src/symbol-replace.ts
457
- var import_core3 = require("@babel/core");
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");
458
774
  var import_types = require("@babel/types");
459
775
  function replaceSymbol(path) {
460
776
  const init = path.node.init;
461
777
  const variableName = path.node.id.name;
462
- if (import_core3.types.isObjectPattern(path.node.id) || import_core3.types.isArrayPattern(path.node.id)) {
778
+ if (import_core4.types.isObjectPattern(path.node.id) || import_core4.types.isArrayPattern(path.node.id)) {
463
779
  return;
464
780
  }
465
781
  if (!variableName.startsWith("$")) {
466
782
  return;
467
783
  }
468
- if (init && (import_core3.types.isFunctionExpression(init) || import_core3.types.isArrowFunctionExpression(init)) && path.parent.kind === "const") {
469
- const newInit = import_core3.types.callExpression(import_core3.types.identifier(path.state.computed.name), init ? [init] : []);
470
- imports.add("computed");
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");
471
787
  path.node.init = newInit;
472
788
  } else {
473
789
  const originalImportDeclarationNodes = (0, import_types.cloneNode)(path.get("id").node, true);
474
- const newInit = import_core3.types.callExpression(import_core3.types.identifier(path.state.signal.name), init ? [init] : []);
475
- imports.add("signal");
790
+ const newInit = import_core4.types.callExpression(import_core4.types.identifier(path.state.useSignal.name), init ? [init] : []);
791
+ imports.add("useSignal");
476
792
  path.node.init = newInit;
477
793
  path.scope.rename(variableName, `${variableName}.value`);
478
794
  path.get("id").replaceWith(originalImportDeclarationNodes);
479
- path.scope.traverse(path.scope.block, {
480
- Identifier(innerPath) {
481
- if (import_core3.types.isExportSpecifier(innerPath.parent)) {
482
- const { name } = innerPath.node;
483
- if (name.endsWith(".value")) {
484
- innerPath.node.name = name.slice(0, -6);
485
- }
486
- }
487
- }
488
- });
489
795
  }
490
796
  }
491
797
 
492
- // src/import-replace.ts
493
- function replaceImport(path) {
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) {
494
814
  const imports2 = path.node.specifiers;
495
815
  imports2.forEach((specifier) => {
496
816
  const variableName = specifier.local.name;
497
- if (variableName.indexOf("$") === 0) {
817
+ if (variableName.indexOf("$") === 0 && !isVariableUsedAsObject(path, variableName)) {
498
818
  path.scope.rename(variableName, `${variableName}.value`);
499
819
  specifier.local.name = `${variableName}`;
500
820
  }
@@ -506,7 +826,7 @@ function src_default() {
506
826
  return {
507
827
  name: "babel-plugin-essor",
508
828
  manipulateOptions({ filename }, parserOpts) {
509
- if (filename.endsWith("ts") || filename.endsWith("tsx")) {
829
+ if (filename.endsWith(".ts") || filename.endsWith(".tsx")) {
510
830
  parserOpts.plugins.push("typescript");
511
831
  }
512
832
  parserOpts.plugins.push("jsx");
@@ -516,7 +836,7 @@ function src_default() {
516
836
  JSXElement: transformJSX,
517
837
  JSXFragment: transformJSX,
518
838
  VariableDeclarator: replaceSymbol,
519
- ImportDeclaration: replaceImport
839
+ ImportDeclaration: replaceImportDeclaration
520
840
  }
521
841
  };
522
842
  }