babel-plugin-essor 0.0.6-beta.13 → 0.0.6-beta.14
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 +234 -230
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +234 -230
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -116,17 +116,78 @@ var svgTags = [
|
|
|
116
116
|
"use"
|
|
117
117
|
];
|
|
118
118
|
|
|
119
|
-
// src/jsx/
|
|
119
|
+
// src/jsx/shared.ts
|
|
120
120
|
var import_core2 = require("@babel/core");
|
|
121
|
-
function
|
|
121
|
+
function hasSiblingElement(path) {
|
|
122
|
+
function isSelfClosing(element) {
|
|
123
|
+
return element.openingElement.selfClosing;
|
|
124
|
+
}
|
|
125
|
+
const siblings = path.getAllPrevSiblings().concat(path.getAllNextSiblings());
|
|
126
|
+
const hasNonSelfClosingSibling = siblings.some(
|
|
127
|
+
(siblingPath) => siblingPath.isJSXElement() && !isSelfClosing(siblingPath.node)
|
|
128
|
+
);
|
|
129
|
+
return hasNonSelfClosingSibling;
|
|
130
|
+
}
|
|
131
|
+
function getAttrName(attribute) {
|
|
132
|
+
if (import_core2.types.isJSXIdentifier(attribute.name)) {
|
|
133
|
+
return attribute.name.name;
|
|
134
|
+
}
|
|
135
|
+
if (import_core2.types.isJSXNamespacedName(attribute.name)) {
|
|
136
|
+
return `${attribute.name.namespace.name}:${attribute.name.name.name}`;
|
|
137
|
+
}
|
|
138
|
+
throw new Error("Unsupported attribute type");
|
|
139
|
+
}
|
|
140
|
+
function getTagName(node) {
|
|
141
|
+
const tag = node.openingElement.name;
|
|
142
|
+
return jsxElementNameToString(tag);
|
|
143
|
+
}
|
|
144
|
+
function jsxElementNameToString(node) {
|
|
145
|
+
if (import_core2.types.isJSXMemberExpression(node)) {
|
|
146
|
+
return `${jsxElementNameToString(node.object)}.${jsxElementNameToString(node.property)}`;
|
|
147
|
+
}
|
|
148
|
+
if (import_core2.types.isJSXIdentifier(node) || import_core2.types.isIdentifier(node)) {
|
|
149
|
+
return node.name;
|
|
150
|
+
}
|
|
151
|
+
return `${node.namespace.name}:${node.name.name}`;
|
|
152
|
+
}
|
|
153
|
+
function isComponent(tagName) {
|
|
154
|
+
return tagName[0] && tagName[0].toLowerCase() !== tagName[0] || tagName.includes(".") || /[^A-Za-z]/.test(tagName[0]);
|
|
155
|
+
}
|
|
156
|
+
function isTextChild(path) {
|
|
157
|
+
if (path.isJSXExpressionContainer()) {
|
|
158
|
+
const expression = path.get("expression");
|
|
159
|
+
if (expression.isJSXText() || expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (path.isJSXText() || path.isStringLiteral() || path.isNullLiteral()) {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
function setNodeText(path, text) {
|
|
169
|
+
if (path.isJSXText()) {
|
|
170
|
+
path.node.value = text;
|
|
171
|
+
}
|
|
172
|
+
if (path.isJSXExpressionContainer()) {
|
|
173
|
+
const expression = path.get("expression");
|
|
174
|
+
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
175
|
+
expression.replaceWith(import_core2.types.stringLiteral(text));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// src/jsx/server.ts
|
|
181
|
+
function transformJSXService(path) {
|
|
122
182
|
const result = {
|
|
123
|
-
index:
|
|
183
|
+
index: 0,
|
|
124
184
|
isLastChild: false,
|
|
125
185
|
parentIndex: 0,
|
|
126
186
|
props: {},
|
|
127
|
-
template:
|
|
187
|
+
template: []
|
|
188
|
+
// 修改为数组
|
|
128
189
|
};
|
|
129
|
-
|
|
190
|
+
transformJSXServiceElement(path, result, true);
|
|
130
191
|
path.replaceWith(createEssorNode(path, result));
|
|
131
192
|
}
|
|
132
193
|
function createEssorNode(path, result) {
|
|
@@ -134,82 +195,94 @@ function createEssorNode(path, result) {
|
|
|
134
195
|
const state = path.state;
|
|
135
196
|
let tmpl;
|
|
136
197
|
if (path.isJSXElement() && isComponent(getTagName(path.node))) {
|
|
137
|
-
tmpl =
|
|
198
|
+
tmpl = import_core3.types.identifier(getTagName(path.node));
|
|
138
199
|
} else {
|
|
139
200
|
tmpl = path.scope.generateUidIdentifier("_tmpl$");
|
|
140
|
-
const template =
|
|
141
|
-
|
|
201
|
+
const template = import_core3.types.callExpression(state.ssrtmpl, [
|
|
202
|
+
import_core3.types.arrayExpression(result.template.map(import_core3.types.stringLiteral))
|
|
203
|
+
]);
|
|
204
|
+
const declarator = import_core3.types.variableDeclarator(tmpl, template);
|
|
142
205
|
state.tmplDeclaration.declarations.push(declarator);
|
|
143
|
-
imports.add("
|
|
206
|
+
imports.add("ssrtmpl");
|
|
144
207
|
}
|
|
145
208
|
const args = [tmpl, createProps(result.props)];
|
|
146
209
|
const key = result.props.key || ((_a = result.props[0]) == null ? void 0 : _a.key);
|
|
147
210
|
if (key) {
|
|
148
211
|
args.push(key);
|
|
149
212
|
}
|
|
150
|
-
imports.add("
|
|
151
|
-
return
|
|
213
|
+
imports.add("ssr");
|
|
214
|
+
return import_core3.types.callExpression(state.ssr, args);
|
|
152
215
|
}
|
|
153
216
|
function createProps(props) {
|
|
154
|
-
const
|
|
217
|
+
const result = [];
|
|
218
|
+
for (const prop in props) {
|
|
219
|
+
let value = props[prop];
|
|
220
|
+
if (prop === "key") {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
155
223
|
if (Array.isArray(value)) {
|
|
156
|
-
|
|
224
|
+
value = import_core3.types.arrayExpression(value);
|
|
157
225
|
}
|
|
158
|
-
if (
|
|
159
|
-
|
|
226
|
+
if (typeof value === "object" && value !== null && !import_core3.types.isNode(value)) {
|
|
227
|
+
value = createProps(value);
|
|
160
228
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
return import_core2.types.stringLiteral(value);
|
|
164
|
-
case "number":
|
|
165
|
-
return import_core2.types.numericLiteral(value);
|
|
166
|
-
case "boolean":
|
|
167
|
-
return import_core2.types.booleanLiteral(value);
|
|
168
|
-
case "undefined":
|
|
169
|
-
return import_core2.types.tsUndefinedKeyword();
|
|
170
|
-
case void 0:
|
|
171
|
-
return import_core2.types.tsUndefinedKeyword();
|
|
172
|
-
case null:
|
|
173
|
-
return import_core2.types.nullLiteral();
|
|
174
|
-
default:
|
|
175
|
-
return value;
|
|
229
|
+
if (typeof value === "string") {
|
|
230
|
+
value = import_core3.types.stringLiteral(value);
|
|
176
231
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
232
|
+
if (typeof value === "number") {
|
|
233
|
+
value = import_core3.types.numericLiteral(value);
|
|
234
|
+
}
|
|
235
|
+
if (typeof value === "boolean") {
|
|
236
|
+
value = import_core3.types.booleanLiteral(value);
|
|
237
|
+
}
|
|
238
|
+
if (value === void 0) {
|
|
239
|
+
value = import_core3.types.tsUndefinedKeyword();
|
|
240
|
+
}
|
|
241
|
+
if (value === null) {
|
|
242
|
+
value = import_core3.types.nullLiteral();
|
|
243
|
+
}
|
|
244
|
+
if (prop === "_$spread$") {
|
|
245
|
+
result.push(import_core3.types.spreadElement(value));
|
|
246
|
+
} else {
|
|
247
|
+
result.push(import_core3.types.objectProperty(import_core3.types.stringLiteral(prop), value));
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return import_core3.types.objectExpression(result);
|
|
183
251
|
}
|
|
184
|
-
function
|
|
252
|
+
function transformJSXServiceElement(path, result, isRoot = false) {
|
|
185
253
|
if (path.isJSXElement()) {
|
|
186
254
|
const tagName = getTagName(path.node);
|
|
187
255
|
const tagIsComponent = isComponent(tagName);
|
|
188
256
|
const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
|
|
189
257
|
const isSvg = svgTags.includes(tagName) && result.index === 1;
|
|
190
|
-
const props = getAttrProps(path);
|
|
258
|
+
const { props, hasExpression } = getAttrProps(path);
|
|
191
259
|
if (tagIsComponent) {
|
|
192
260
|
if (isRoot) {
|
|
193
261
|
result.props = props;
|
|
194
262
|
const children = getChildren(path);
|
|
195
263
|
if (children.length > 0) {
|
|
196
|
-
const childrenGenerator = children.length === 1 ? children[0] :
|
|
264
|
+
const childrenGenerator = children.length === 1 ? children[0] : import_core3.types.arrayExpression(children);
|
|
197
265
|
result.props.children = childrenGenerator;
|
|
198
266
|
}
|
|
199
267
|
} else {
|
|
200
|
-
|
|
268
|
+
transformJSXService(path);
|
|
201
269
|
replaceChild(path.node, result);
|
|
202
270
|
}
|
|
203
271
|
} else {
|
|
204
272
|
if (isSvg) {
|
|
205
|
-
result.template
|
|
273
|
+
result.template.push("<svg _svg_>");
|
|
206
274
|
}
|
|
207
|
-
result.template
|
|
275
|
+
result.template.push(`<${tagName}`);
|
|
208
276
|
handleAttributes(props, result);
|
|
209
|
-
|
|
277
|
+
if (hasExpression) {
|
|
278
|
+
result.template.push(isSelfClose ? "/>" : ">");
|
|
279
|
+
result.props || (result.props = {});
|
|
280
|
+
} else {
|
|
281
|
+
result.template[result.template.length - 1] += isSelfClose ? "/>" : ">";
|
|
282
|
+
}
|
|
283
|
+
transformChildren(path, result);
|
|
210
284
|
if (!isSelfClose) {
|
|
211
|
-
|
|
212
|
-
result.template += `</${tagName}>`;
|
|
285
|
+
result.template.push(`</${tagName}>`);
|
|
213
286
|
}
|
|
214
287
|
}
|
|
215
288
|
} else {
|
|
@@ -218,7 +291,7 @@ function transformJSXElement(path, result, isRoot = false) {
|
|
|
218
291
|
}
|
|
219
292
|
}
|
|
220
293
|
function transformChildren(path, result) {
|
|
221
|
-
const parentIndex = result.
|
|
294
|
+
const parentIndex = result.template.length;
|
|
222
295
|
path.get("children").reduce((pre, cur) => {
|
|
223
296
|
if (isValidChild(cur)) {
|
|
224
297
|
const lastChild = pre.at(-1);
|
|
@@ -236,21 +309,20 @@ function transformChildren(path, result) {
|
|
|
236
309
|
});
|
|
237
310
|
}
|
|
238
311
|
function transformChild(child, result) {
|
|
239
|
-
result.index++;
|
|
240
312
|
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
241
|
-
|
|
313
|
+
transformJSXServiceElement(child, result, false);
|
|
242
314
|
} else if (child.isJSXExpressionContainer()) {
|
|
243
315
|
const expression = child.get("expression");
|
|
244
316
|
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
245
|
-
result.template += String(expression.node.value);
|
|
317
|
+
result.template[result.template.length - 1] += String(expression.node.value);
|
|
246
318
|
} else if (expression.isExpression()) {
|
|
247
319
|
replaceChild(expression.node, result);
|
|
248
|
-
} else if (
|
|
320
|
+
} else if (import_core3.types.isJSXEmptyExpression(expression.node)) {
|
|
249
321
|
} else {
|
|
250
322
|
throw new Error("Unsupported child type");
|
|
251
323
|
}
|
|
252
324
|
} else if (child.isJSXText()) {
|
|
253
|
-
result.template
|
|
325
|
+
result.template.push(String(child.node.value));
|
|
254
326
|
} else {
|
|
255
327
|
throw new Error("Unsupported child type");
|
|
256
328
|
}
|
|
@@ -267,29 +339,6 @@ function getNodeText(path) {
|
|
|
267
339
|
}
|
|
268
340
|
return "";
|
|
269
341
|
}
|
|
270
|
-
function setNodeText(path, text) {
|
|
271
|
-
if (path.isJSXText()) {
|
|
272
|
-
path.node.value = text;
|
|
273
|
-
}
|
|
274
|
-
if (path.isJSXExpressionContainer()) {
|
|
275
|
-
const expression = path.get("expression");
|
|
276
|
-
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
277
|
-
expression.replaceWith(import_core2.types.stringLiteral(text));
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
function isTextChild(path) {
|
|
282
|
-
if (path.isJSXExpressionContainer()) {
|
|
283
|
-
const expression = path.get("expression");
|
|
284
|
-
if (expression.isJSXText() || expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
285
|
-
return true;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
if (path.isJSXText() || path.isStringLiteral() || path.isNullLiteral()) {
|
|
289
|
-
return true;
|
|
290
|
-
}
|
|
291
|
-
return false;
|
|
292
|
-
}
|
|
293
342
|
function handleAttributes(props, result) {
|
|
294
343
|
let klass = "";
|
|
295
344
|
let style = "";
|
|
@@ -324,14 +373,14 @@ function handleAttributes(props, result) {
|
|
|
324
373
|
continue;
|
|
325
374
|
}
|
|
326
375
|
if (value === true) {
|
|
327
|
-
result.template += ` ${prop}`;
|
|
376
|
+
result.template[result.template.length - 1] += ` ${prop}`;
|
|
328
377
|
delete props[prop];
|
|
329
378
|
}
|
|
330
379
|
if (value === false) {
|
|
331
380
|
delete props[prop];
|
|
332
381
|
}
|
|
333
382
|
if (typeof value === "string" || typeof value === "number") {
|
|
334
|
-
result.template += ` ${prop}="${value}"`;
|
|
383
|
+
result.template[result.template.length - 1] += ` ${prop}="${value}"`;
|
|
335
384
|
delete props[prop];
|
|
336
385
|
}
|
|
337
386
|
}
|
|
@@ -341,36 +390,34 @@ function handleAttributes(props, result) {
|
|
|
341
390
|
klass = klass.trim();
|
|
342
391
|
style = style.trim();
|
|
343
392
|
if (klass) {
|
|
344
|
-
result.template += ` class="${klass}"`;
|
|
393
|
+
result.template[result.template.length - 1] += ` class="${klass}"`;
|
|
345
394
|
}
|
|
346
395
|
if (style) {
|
|
347
|
-
result.template += ` style="${style}"`;
|
|
396
|
+
result.template[result.template.length - 1] += ` style="${style}"`;
|
|
348
397
|
}
|
|
349
398
|
}
|
|
350
399
|
function replaceChild(node, result) {
|
|
351
400
|
var _a, _b, _c, _d, _e;
|
|
352
401
|
if (result.isLastChild) {
|
|
353
402
|
result.index--;
|
|
354
|
-
} else {
|
|
355
|
-
result.template += "<!-->";
|
|
356
403
|
}
|
|
357
404
|
(_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
|
|
358
405
|
(_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
|
|
359
406
|
result.props[result.parentIndex].children.push(
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
407
|
+
import_core3.types.arrayExpression([
|
|
408
|
+
import_core3.types.arrowFunctionExpression([], node),
|
|
409
|
+
import_core3.types.identifier(String(result.template.length))
|
|
363
410
|
])
|
|
364
411
|
);
|
|
365
412
|
}
|
|
366
413
|
function getChildren(path) {
|
|
367
414
|
return path.get("children").filter((child) => isValidChild(child)).map((child) => {
|
|
368
415
|
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
369
|
-
|
|
416
|
+
transformJSXService(child);
|
|
370
417
|
} else if (child.isJSXExpressionContainer()) {
|
|
371
418
|
child.replaceWith(child.get("expression"));
|
|
372
419
|
} else if (child.isJSXText()) {
|
|
373
|
-
child.replaceWith(
|
|
420
|
+
child.replaceWith(import_core3.types.stringLiteral(child.node.value));
|
|
374
421
|
} else {
|
|
375
422
|
throw new Error("Unsupported child type");
|
|
376
423
|
}
|
|
@@ -386,6 +433,7 @@ function isValidChild(path) {
|
|
|
386
433
|
}
|
|
387
434
|
function getAttrProps(path) {
|
|
388
435
|
const props = {};
|
|
436
|
+
let hasExpression = false;
|
|
389
437
|
path.get("openingElement").get("attributes").forEach((attribute) => {
|
|
390
438
|
if (attribute.isJSXAttribute()) {
|
|
391
439
|
const name = getAttrName(attribute.node);
|
|
@@ -402,77 +450,57 @@ function getAttrProps(path) {
|
|
|
402
450
|
} else if (expression.isNumericLiteral()) {
|
|
403
451
|
props[name] = expression.node.value;
|
|
404
452
|
} else if (expression.isJSXElement() || expression.isJSXFragment()) {
|
|
405
|
-
|
|
453
|
+
transformJSXService(expression);
|
|
406
454
|
props[name] = expression.node;
|
|
407
455
|
} else if (expression.isExpression()) {
|
|
456
|
+
hasExpression = true;
|
|
408
457
|
if (/^key|ref|on.+$/.test(name)) {
|
|
409
458
|
props[name] = expression.node;
|
|
410
459
|
} else if (/^bind:.+/.test(name)) {
|
|
411
460
|
const value2 = path.scope.generateUidIdentifier("value");
|
|
412
461
|
const bindName = name.slice(5).toLocaleLowerCase();
|
|
413
462
|
props[bindName] = expression.node;
|
|
414
|
-
props[`update${capitalizeFirstLetter(bindName)}`] =
|
|
463
|
+
props[`update${capitalizeFirstLetter(bindName)}`] = import_core3.types.arrowFunctionExpression(
|
|
415
464
|
[value2],
|
|
416
|
-
|
|
465
|
+
import_core3.types.assignmentExpression("=", expression.node, value2)
|
|
417
466
|
);
|
|
418
467
|
} else {
|
|
419
468
|
if (expression.isConditionalExpression()) {
|
|
420
|
-
props[name] =
|
|
469
|
+
props[name] = import_core3.types.arrowFunctionExpression([], expression.node);
|
|
421
470
|
} else {
|
|
422
471
|
props[name] = expression.node;
|
|
423
472
|
}
|
|
424
473
|
}
|
|
425
474
|
}
|
|
426
475
|
} else if (value.isJSXElement() || value.isJSXFragment()) {
|
|
427
|
-
|
|
476
|
+
transformJSXService(value);
|
|
428
477
|
props[name] = value.node;
|
|
429
478
|
}
|
|
430
479
|
}
|
|
431
480
|
} else if (attribute.isJSXSpreadAttribute()) {
|
|
432
481
|
props._$spread$ = attribute.get("argument").node;
|
|
482
|
+
hasExpression = true;
|
|
433
483
|
} else {
|
|
434
484
|
throw new Error("Unsupported attribute type");
|
|
435
485
|
}
|
|
436
486
|
});
|
|
437
|
-
return
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
return attribute.name.name;
|
|
442
|
-
}
|
|
443
|
-
if (import_core2.types.isJSXNamespacedName(attribute.name)) {
|
|
444
|
-
return `${attribute.name.namespace.name}:${attribute.name.name.name}`;
|
|
445
|
-
}
|
|
446
|
-
throw new Error("Unsupported attribute type");
|
|
447
|
-
}
|
|
448
|
-
function isComponent(tagName) {
|
|
449
|
-
return tagName[0] && tagName[0].toLowerCase() !== tagName[0] || tagName.includes(".") || /[^A-Za-z]/.test(tagName[0]);
|
|
450
|
-
}
|
|
451
|
-
function getTagName(node) {
|
|
452
|
-
const tag = node.openingElement.name;
|
|
453
|
-
return jsxElementNameToString(tag);
|
|
454
|
-
}
|
|
455
|
-
function jsxElementNameToString(node) {
|
|
456
|
-
if (import_core2.types.isJSXMemberExpression(node)) {
|
|
457
|
-
return `${jsxElementNameToString(node.object)}.${jsxElementNameToString(node.property)}`;
|
|
458
|
-
}
|
|
459
|
-
if (import_core2.types.isJSXIdentifier(node) || import_core2.types.isIdentifier(node)) {
|
|
460
|
-
return node.name;
|
|
461
|
-
}
|
|
462
|
-
return `${node.namespace.name}:${node.name.name}`;
|
|
487
|
+
return {
|
|
488
|
+
props,
|
|
489
|
+
hasExpression
|
|
490
|
+
};
|
|
463
491
|
}
|
|
464
492
|
|
|
465
|
-
// src/jsx/
|
|
466
|
-
|
|
493
|
+
// src/jsx/client.ts
|
|
494
|
+
var import_core4 = require("@babel/core");
|
|
495
|
+
function transformJSXClient(path) {
|
|
467
496
|
const result = {
|
|
468
|
-
index:
|
|
497
|
+
index: 1,
|
|
469
498
|
isLastChild: false,
|
|
470
499
|
parentIndex: 0,
|
|
471
500
|
props: {},
|
|
472
|
-
template:
|
|
473
|
-
// 修改为数组
|
|
501
|
+
template: ""
|
|
474
502
|
};
|
|
475
|
-
|
|
503
|
+
transformJSXElement(path, result, true);
|
|
476
504
|
path.replaceWith(createEssorNode2(path, result));
|
|
477
505
|
}
|
|
478
506
|
function createEssorNode2(path, result) {
|
|
@@ -480,94 +508,84 @@ function createEssorNode2(path, result) {
|
|
|
480
508
|
const state = path.state;
|
|
481
509
|
let tmpl;
|
|
482
510
|
if (path.isJSXElement() && isComponent(getTagName(path.node))) {
|
|
483
|
-
tmpl =
|
|
511
|
+
tmpl = import_core4.types.identifier(getTagName(path.node));
|
|
484
512
|
} else {
|
|
485
513
|
tmpl = path.scope.generateUidIdentifier("_tmpl$");
|
|
486
|
-
const template =
|
|
487
|
-
|
|
488
|
-
]);
|
|
489
|
-
const declarator = import_core3.types.variableDeclarator(tmpl, template);
|
|
514
|
+
const template = import_core4.types.callExpression(state.template, [import_core4.types.stringLiteral(result.template)]);
|
|
515
|
+
const declarator = import_core4.types.variableDeclarator(tmpl, template);
|
|
490
516
|
state.tmplDeclaration.declarations.push(declarator);
|
|
491
|
-
imports.add("
|
|
517
|
+
imports.add("template");
|
|
492
518
|
}
|
|
493
519
|
const args = [tmpl, createProps2(result.props)];
|
|
494
520
|
const key = result.props.key || ((_a = result.props[0]) == null ? void 0 : _a.key);
|
|
495
521
|
if (key) {
|
|
496
522
|
args.push(key);
|
|
497
523
|
}
|
|
498
|
-
imports.add("
|
|
499
|
-
return
|
|
524
|
+
imports.add("h");
|
|
525
|
+
return import_core4.types.callExpression(state.h, args);
|
|
500
526
|
}
|
|
501
527
|
function createProps2(props) {
|
|
502
|
-
const
|
|
503
|
-
for (const prop in props) {
|
|
504
|
-
let value = props[prop];
|
|
505
|
-
if (prop === "key") {
|
|
506
|
-
continue;
|
|
507
|
-
}
|
|
528
|
+
const toAstNode = (value) => {
|
|
508
529
|
if (Array.isArray(value)) {
|
|
509
|
-
|
|
510
|
-
}
|
|
511
|
-
if (typeof value === "object" && value !== null && !import_core3.types.isNode(value)) {
|
|
512
|
-
value = createProps2(value);
|
|
513
|
-
}
|
|
514
|
-
if (typeof value === "string") {
|
|
515
|
-
value = import_core3.types.stringLiteral(value);
|
|
516
|
-
}
|
|
517
|
-
if (typeof value === "number") {
|
|
518
|
-
value = import_core3.types.numericLiteral(value);
|
|
519
|
-
}
|
|
520
|
-
if (typeof value === "boolean") {
|
|
521
|
-
value = import_core3.types.booleanLiteral(value);
|
|
530
|
+
return import_core4.types.arrayExpression(value.map(toAstNode));
|
|
522
531
|
}
|
|
523
|
-
if (value ===
|
|
524
|
-
|
|
532
|
+
if (value && typeof value === "object" && !import_core4.types.isNode(value)) {
|
|
533
|
+
return createProps2(value);
|
|
525
534
|
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
535
|
+
switch (typeof value) {
|
|
536
|
+
case "string":
|
|
537
|
+
return import_core4.types.stringLiteral(value);
|
|
538
|
+
case "number":
|
|
539
|
+
return import_core4.types.numericLiteral(value);
|
|
540
|
+
case "boolean":
|
|
541
|
+
return import_core4.types.booleanLiteral(value);
|
|
542
|
+
case "undefined":
|
|
543
|
+
return import_core4.types.tsUndefinedKeyword();
|
|
544
|
+
case void 0:
|
|
545
|
+
return import_core4.types.tsUndefinedKeyword();
|
|
546
|
+
case null:
|
|
547
|
+
return import_core4.types.nullLiteral();
|
|
548
|
+
default:
|
|
549
|
+
return value;
|
|
533
550
|
}
|
|
534
|
-
}
|
|
535
|
-
|
|
551
|
+
};
|
|
552
|
+
const result = Object.keys(props).filter((prop) => prop !== "key").map((prop) => {
|
|
553
|
+
const value = toAstNode(props[prop]);
|
|
554
|
+
return prop === "_$spread$" ? import_core4.types.spreadElement(value) : import_core4.types.objectProperty(import_core4.types.stringLiteral(prop), value);
|
|
555
|
+
});
|
|
556
|
+
return import_core4.types.objectExpression(result);
|
|
536
557
|
}
|
|
537
|
-
function
|
|
558
|
+
function transformJSXElement(path, result, isRoot = false) {
|
|
538
559
|
if (path.isJSXElement()) {
|
|
539
560
|
const tagName = getTagName(path.node);
|
|
540
561
|
const tagIsComponent = isComponent(tagName);
|
|
541
562
|
const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
|
|
542
563
|
const isSvg = svgTags.includes(tagName) && result.index === 1;
|
|
543
|
-
const
|
|
564
|
+
const props = getAttrProps2(path);
|
|
544
565
|
if (tagIsComponent) {
|
|
545
566
|
if (isRoot) {
|
|
546
567
|
result.props = props;
|
|
547
568
|
const children = getChildren2(path);
|
|
548
569
|
if (children.length > 0) {
|
|
549
|
-
const childrenGenerator = children.length === 1 ? children[0] :
|
|
570
|
+
const childrenGenerator = children.length === 1 ? children[0] : import_core4.types.arrayExpression(children);
|
|
550
571
|
result.props.children = childrenGenerator;
|
|
551
572
|
}
|
|
552
573
|
} else {
|
|
553
|
-
|
|
574
|
+
transformJSXClient(path);
|
|
554
575
|
replaceChild2(path.node, result);
|
|
555
576
|
}
|
|
556
577
|
} else {
|
|
557
578
|
if (isSvg) {
|
|
558
|
-
result.template
|
|
579
|
+
result.template = "<svg _svg_>";
|
|
559
580
|
}
|
|
560
|
-
result.template
|
|
581
|
+
result.template += `<${tagName}`;
|
|
561
582
|
handleAttributes2(props, result);
|
|
562
|
-
|
|
563
|
-
result.template.push(isSelfClose ? "/>" : ">");
|
|
564
|
-
result.props || (result.props = {});
|
|
565
|
-
} else {
|
|
566
|
-
result.template[result.template.length - 1] += isSelfClose ? "/>" : ">";
|
|
567
|
-
}
|
|
568
|
-
transformChildren2(path, result);
|
|
583
|
+
result.template += isSelfClose ? "/>" : ">";
|
|
569
584
|
if (!isSelfClose) {
|
|
570
|
-
result
|
|
585
|
+
transformChildren2(path, result);
|
|
586
|
+
if (hasSiblingElement(path)) {
|
|
587
|
+
result.template += `</${tagName}>`;
|
|
588
|
+
}
|
|
571
589
|
}
|
|
572
590
|
}
|
|
573
591
|
} else {
|
|
@@ -576,12 +594,12 @@ function transformJSXServiceElement(path, result, isRoot = false) {
|
|
|
576
594
|
}
|
|
577
595
|
}
|
|
578
596
|
function transformChildren2(path, result) {
|
|
579
|
-
const parentIndex = result.
|
|
597
|
+
const parentIndex = result.index;
|
|
580
598
|
path.get("children").reduce((pre, cur) => {
|
|
581
599
|
if (isValidChild2(cur)) {
|
|
582
600
|
const lastChild = pre.at(-1);
|
|
583
601
|
if (lastChild && isTextChild(cur) && isTextChild(lastChild)) {
|
|
584
|
-
|
|
602
|
+
setNodeText(lastChild, getNodeText2(lastChild) + getNodeText2(cur));
|
|
585
603
|
} else {
|
|
586
604
|
pre.push(cur);
|
|
587
605
|
}
|
|
@@ -594,20 +612,21 @@ function transformChildren2(path, result) {
|
|
|
594
612
|
});
|
|
595
613
|
}
|
|
596
614
|
function transformChild2(child, result) {
|
|
615
|
+
result.index++;
|
|
597
616
|
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
598
|
-
|
|
617
|
+
transformJSXElement(child, result, false);
|
|
599
618
|
} else if (child.isJSXExpressionContainer()) {
|
|
600
619
|
const expression = child.get("expression");
|
|
601
620
|
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
602
|
-
result.template
|
|
621
|
+
result.template += String(expression.node.value);
|
|
603
622
|
} else if (expression.isExpression()) {
|
|
604
623
|
replaceChild2(expression.node, result);
|
|
605
|
-
} else if (
|
|
624
|
+
} else if (import_core4.types.isJSXEmptyExpression(expression.node)) {
|
|
606
625
|
} else {
|
|
607
626
|
throw new Error("Unsupported child type");
|
|
608
627
|
}
|
|
609
628
|
} else if (child.isJSXText()) {
|
|
610
|
-
result.template
|
|
629
|
+
result.template += String(child.node.value);
|
|
611
630
|
} else {
|
|
612
631
|
throw new Error("Unsupported child type");
|
|
613
632
|
}
|
|
@@ -624,17 +643,6 @@ function getNodeText2(path) {
|
|
|
624
643
|
}
|
|
625
644
|
return "";
|
|
626
645
|
}
|
|
627
|
-
function setNodeText2(path, text) {
|
|
628
|
-
if (path.isJSXText()) {
|
|
629
|
-
path.node.value = text;
|
|
630
|
-
}
|
|
631
|
-
if (path.isJSXExpressionContainer()) {
|
|
632
|
-
const expression = path.get("expression");
|
|
633
|
-
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
634
|
-
expression.replaceWith(import_core3.types.stringLiteral(text));
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
646
|
function handleAttributes2(props, result) {
|
|
639
647
|
let klass = "";
|
|
640
648
|
let style = "";
|
|
@@ -669,14 +677,14 @@ function handleAttributes2(props, result) {
|
|
|
669
677
|
continue;
|
|
670
678
|
}
|
|
671
679
|
if (value === true) {
|
|
672
|
-
result.template
|
|
680
|
+
result.template += ` ${prop}`;
|
|
673
681
|
delete props[prop];
|
|
674
682
|
}
|
|
675
683
|
if (value === false) {
|
|
676
684
|
delete props[prop];
|
|
677
685
|
}
|
|
678
686
|
if (typeof value === "string" || typeof value === "number") {
|
|
679
|
-
result.template
|
|
687
|
+
result.template += ` ${prop}="${value}"`;
|
|
680
688
|
delete props[prop];
|
|
681
689
|
}
|
|
682
690
|
}
|
|
@@ -686,34 +694,36 @@ function handleAttributes2(props, result) {
|
|
|
686
694
|
klass = klass.trim();
|
|
687
695
|
style = style.trim();
|
|
688
696
|
if (klass) {
|
|
689
|
-
result.template
|
|
697
|
+
result.template += ` class="${klass}"`;
|
|
690
698
|
}
|
|
691
699
|
if (style) {
|
|
692
|
-
result.template
|
|
700
|
+
result.template += ` style="${style}"`;
|
|
693
701
|
}
|
|
694
702
|
}
|
|
695
703
|
function replaceChild2(node, result) {
|
|
696
704
|
var _a, _b, _c, _d, _e;
|
|
697
705
|
if (result.isLastChild) {
|
|
698
706
|
result.index--;
|
|
707
|
+
} else {
|
|
708
|
+
result.template += "<!>";
|
|
699
709
|
}
|
|
700
710
|
(_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
|
|
701
711
|
(_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
|
|
702
712
|
result.props[result.parentIndex].children.push(
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
713
|
+
import_core4.types.arrayExpression([
|
|
714
|
+
import_core4.types.arrowFunctionExpression([], node),
|
|
715
|
+
result.isLastChild ? import_core4.types.nullLiteral() : import_core4.types.identifier(String(result.index))
|
|
706
716
|
])
|
|
707
717
|
);
|
|
708
718
|
}
|
|
709
719
|
function getChildren2(path) {
|
|
710
720
|
return path.get("children").filter((child) => isValidChild2(child)).map((child) => {
|
|
711
721
|
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
712
|
-
|
|
722
|
+
transformJSXClient(child);
|
|
713
723
|
} else if (child.isJSXExpressionContainer()) {
|
|
714
724
|
child.replaceWith(child.get("expression"));
|
|
715
725
|
} else if (child.isJSXText()) {
|
|
716
|
-
child.replaceWith(
|
|
726
|
+
child.replaceWith(import_core4.types.stringLiteral(child.node.value));
|
|
717
727
|
} else {
|
|
718
728
|
throw new Error("Unsupported child type");
|
|
719
729
|
}
|
|
@@ -729,7 +739,6 @@ function isValidChild2(path) {
|
|
|
729
739
|
}
|
|
730
740
|
function getAttrProps2(path) {
|
|
731
741
|
const props = {};
|
|
732
|
-
let hasExpression = false;
|
|
733
742
|
path.get("openingElement").get("attributes").forEach((attribute) => {
|
|
734
743
|
if (attribute.isJSXAttribute()) {
|
|
735
744
|
const name = getAttrName(attribute.node);
|
|
@@ -746,44 +755,39 @@ function getAttrProps2(path) {
|
|
|
746
755
|
} else if (expression.isNumericLiteral()) {
|
|
747
756
|
props[name] = expression.node.value;
|
|
748
757
|
} else if (expression.isJSXElement() || expression.isJSXFragment()) {
|
|
749
|
-
|
|
758
|
+
transformJSXClient(expression);
|
|
750
759
|
props[name] = expression.node;
|
|
751
760
|
} else if (expression.isExpression()) {
|
|
752
|
-
hasExpression = true;
|
|
753
761
|
if (/^key|ref|on.+$/.test(name)) {
|
|
754
762
|
props[name] = expression.node;
|
|
755
763
|
} else if (/^bind:.+/.test(name)) {
|
|
756
764
|
const value2 = path.scope.generateUidIdentifier("value");
|
|
757
765
|
const bindName = name.slice(5).toLocaleLowerCase();
|
|
758
766
|
props[bindName] = expression.node;
|
|
759
|
-
props[`update${capitalizeFirstLetter(bindName)}`] =
|
|
767
|
+
props[`update${capitalizeFirstLetter(bindName)}`] = import_core4.types.arrowFunctionExpression(
|
|
760
768
|
[value2],
|
|
761
|
-
|
|
769
|
+
import_core4.types.assignmentExpression("=", expression.node, value2)
|
|
762
770
|
);
|
|
763
771
|
} else {
|
|
764
772
|
if (expression.isConditionalExpression()) {
|
|
765
|
-
props[name] =
|
|
773
|
+
props[name] = import_core4.types.arrowFunctionExpression([], expression.node);
|
|
766
774
|
} else {
|
|
767
775
|
props[name] = expression.node;
|
|
768
776
|
}
|
|
769
777
|
}
|
|
770
778
|
}
|
|
771
779
|
} else if (value.isJSXElement() || value.isJSXFragment()) {
|
|
772
|
-
|
|
780
|
+
transformJSXClient(value);
|
|
773
781
|
props[name] = value.node;
|
|
774
782
|
}
|
|
775
783
|
}
|
|
776
784
|
} else if (attribute.isJSXSpreadAttribute()) {
|
|
777
785
|
props._$spread$ = attribute.get("argument").node;
|
|
778
|
-
hasExpression = true;
|
|
779
786
|
} else {
|
|
780
787
|
throw new Error("Unsupported attribute type");
|
|
781
788
|
}
|
|
782
789
|
});
|
|
783
|
-
return
|
|
784
|
-
props,
|
|
785
|
-
hasExpression
|
|
786
|
-
};
|
|
790
|
+
return props;
|
|
787
791
|
}
|
|
788
792
|
|
|
789
793
|
// src/jsx/index.ts
|
|
@@ -794,24 +798,24 @@ function transformJSX(path) {
|
|
|
794
798
|
}
|
|
795
799
|
|
|
796
800
|
// src/signal/symbol.ts
|
|
797
|
-
var
|
|
801
|
+
var import_core5 = require("@babel/core");
|
|
798
802
|
var import_types = require("@babel/types");
|
|
799
803
|
function replaceSymbol(path) {
|
|
800
804
|
const init = path.node.init;
|
|
801
805
|
const variableName = path.node.id.name;
|
|
802
|
-
if (
|
|
806
|
+
if (import_core5.types.isObjectPattern(path.node.id) || import_core5.types.isArrayPattern(path.node.id)) {
|
|
803
807
|
return;
|
|
804
808
|
}
|
|
805
809
|
if (!startsWith(variableName, "$")) {
|
|
806
810
|
return;
|
|
807
811
|
}
|
|
808
|
-
if (init && (
|
|
809
|
-
const newInit =
|
|
812
|
+
if (init && (import_core5.types.isFunctionExpression(init) || import_core5.types.isArrowFunctionExpression(init)) && path.parent.kind === "const") {
|
|
813
|
+
const newInit = import_core5.types.callExpression(import_core5.types.identifier(path.state.useComputed.name), init ? [init] : []);
|
|
810
814
|
imports.add("useComputed");
|
|
811
815
|
path.node.init = newInit;
|
|
812
816
|
} else {
|
|
813
817
|
const originalImportDeclarationNodes = (0, import_types.cloneNode)(path.get("id").node, true);
|
|
814
|
-
const newInit =
|
|
818
|
+
const newInit = import_core5.types.callExpression(import_core5.types.identifier(path.state.useSignal.name), init ? [init] : []);
|
|
815
819
|
imports.add("useSignal");
|
|
816
820
|
path.node.init = newInit;
|
|
817
821
|
path.scope.rename(variableName, `${variableName}.value`);
|
|
@@ -820,7 +824,7 @@ function replaceSymbol(path) {
|
|
|
820
824
|
}
|
|
821
825
|
|
|
822
826
|
// src/signal/import.ts
|
|
823
|
-
var
|
|
827
|
+
var import_core6 = require("@babel/core");
|
|
824
828
|
function isVariableUsedAsObject(path, variableName) {
|
|
825
829
|
const binding = path.scope.getBinding(variableName);
|
|
826
830
|
let isUsedObject = false;
|
|
@@ -828,7 +832,7 @@ function isVariableUsedAsObject(path, variableName) {
|
|
|
828
832
|
return isUsedObject;
|
|
829
833
|
}
|
|
830
834
|
for (const referencePath of binding.referencePaths) {
|
|
831
|
-
if (
|
|
835
|
+
if (import_core6.types.isMemberExpression(referencePath.parent)) {
|
|
832
836
|
isUsedObject = true;
|
|
833
837
|
}
|
|
834
838
|
}
|
|
@@ -846,11 +850,11 @@ function replaceImportDeclaration(path) {
|
|
|
846
850
|
}
|
|
847
851
|
|
|
848
852
|
// src/signal/props.ts
|
|
849
|
-
var
|
|
853
|
+
var import_core7 = require("@babel/core");
|
|
850
854
|
function replaceProps(path) {
|
|
851
855
|
var _a;
|
|
852
856
|
const firstParam = path.node.params[0];
|
|
853
|
-
if (!firstParam || !
|
|
857
|
+
if (!firstParam || !import_core7.types.isObjectPattern(firstParam)) {
|
|
854
858
|
return;
|
|
855
859
|
}
|
|
856
860
|
const returnStatement = path.get("body").get("body").find((statement) => statement.isReturnStatement());
|
|
@@ -858,18 +862,18 @@ function replaceProps(path) {
|
|
|
858
862
|
return;
|
|
859
863
|
}
|
|
860
864
|
const returnValue = (_a = returnStatement.node) == null ? void 0 : _a.argument;
|
|
861
|
-
if (!
|
|
865
|
+
if (!import_core7.types.isJSXElement(returnValue)) {
|
|
862
866
|
return;
|
|
863
867
|
}
|
|
864
868
|
function replaceProperties(properties2, parentPath) {
|
|
865
869
|
properties2.forEach((property) => {
|
|
866
|
-
if (
|
|
870
|
+
if (import_core7.types.isObjectProperty(property)) {
|
|
867
871
|
const keyName = property.key.name;
|
|
868
|
-
if (
|
|
872
|
+
if (import_core7.types.isIdentifier(property.value)) {
|
|
869
873
|
const propertyName = property.value.name;
|
|
870
874
|
const newName = `${parentPath}${keyName}`;
|
|
871
875
|
path.scope.rename(propertyName, newName);
|
|
872
|
-
} else if (
|
|
876
|
+
} else if (import_core7.types.isObjectPattern(property.value)) {
|
|
873
877
|
replaceProperties(property.value.properties, `${parentPath}${keyName}.`);
|
|
874
878
|
}
|
|
875
879
|
}
|
|
@@ -877,18 +881,18 @@ function replaceProps(path) {
|
|
|
877
881
|
}
|
|
878
882
|
const properties = firstParam.properties;
|
|
879
883
|
replaceProperties(
|
|
880
|
-
properties.filter((property) => !
|
|
884
|
+
properties.filter((property) => !import_core7.types.isRestElement(property)),
|
|
881
885
|
"__props."
|
|
882
886
|
);
|
|
883
|
-
const restElement = properties.find((property) =>
|
|
887
|
+
const restElement = properties.find((property) => import_core7.types.isRestElement(property));
|
|
884
888
|
if (restElement) {
|
|
885
889
|
const restName = restElement.argument.name;
|
|
886
|
-
const restVariableDeclaration =
|
|
887
|
-
|
|
890
|
+
const restVariableDeclaration = import_core7.types.variableDeclaration("const", [
|
|
891
|
+
import_core7.types.variableDeclarator(import_core7.types.identifier(restName), import_core7.types.identifier("__props"))
|
|
888
892
|
]);
|
|
889
893
|
path.node.body.body.unshift(restVariableDeclaration);
|
|
890
894
|
}
|
|
891
|
-
path.node.params[0] =
|
|
895
|
+
path.node.params[0] = import_core7.types.identifier("__props");
|
|
892
896
|
}
|
|
893
897
|
|
|
894
898
|
// src/index.ts
|