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