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