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