babel-plugin-essor 0.0.14-beta.9 → 0.0.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 +345 -293
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +344 -292
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -55,7 +55,7 @@ var capitalize = cacheStringFunction(
|
|
|
55
55
|
import { types as t } from "@babel/core";
|
|
56
56
|
var imports = /* @__PURE__ */ new Set();
|
|
57
57
|
var defaultOption = {
|
|
58
|
-
|
|
58
|
+
server: false,
|
|
59
59
|
symbol: "$",
|
|
60
60
|
props: true
|
|
61
61
|
};
|
|
@@ -67,9 +67,10 @@ var transformProgram = {
|
|
|
67
67
|
h: path.scope.generateUidIdentifier("h$"),
|
|
68
68
|
template: path.scope.generateUidIdentifier("template$"),
|
|
69
69
|
ssg: path.scope.generateUidIdentifier("ssg$"),
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
70
|
+
Fragment: path.scope.generateUidIdentifier("fragment$"),
|
|
71
|
+
useSignal: path.scope.generateUidIdentifier("useSignal$"),
|
|
72
|
+
useComputed: path.scope.generateUidIdentifier("useComputed$"),
|
|
73
|
+
useReactive: path.scope.generateUidIdentifier("useReactive$"),
|
|
73
74
|
tmplDeclaration: t.variableDeclaration("const", []),
|
|
74
75
|
opts: state.opts
|
|
75
76
|
};
|
|
@@ -98,8 +99,26 @@ function createImport(state, from) {
|
|
|
98
99
|
return t.importDeclaration(ImportSpecifier, importSource);
|
|
99
100
|
}
|
|
100
101
|
|
|
101
|
-
// src/shared.ts
|
|
102
|
+
// src/jsx/shared.ts
|
|
102
103
|
import { types as t2 } from "@babel/core";
|
|
104
|
+
function createClientResult() {
|
|
105
|
+
return {
|
|
106
|
+
index: 1,
|
|
107
|
+
isLastChild: false,
|
|
108
|
+
parentIndex: 0,
|
|
109
|
+
props: {},
|
|
110
|
+
template: ""
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function createServerResult() {
|
|
114
|
+
return {
|
|
115
|
+
index: 1,
|
|
116
|
+
isLastChild: false,
|
|
117
|
+
parentIndex: 0,
|
|
118
|
+
props: {},
|
|
119
|
+
template: []
|
|
120
|
+
};
|
|
121
|
+
}
|
|
103
122
|
function hasSiblingElement(path) {
|
|
104
123
|
const siblings = path.getAllPrevSiblings().concat(path.getAllNextSiblings());
|
|
105
124
|
const hasSibling = siblings.some(
|
|
@@ -129,7 +148,7 @@ function jsxElementNameToString(node) {
|
|
|
129
148
|
}
|
|
130
149
|
return `${node.namespace.name}:${node.name.name}`;
|
|
131
150
|
}
|
|
132
|
-
function
|
|
151
|
+
function isComponentName(tagName) {
|
|
133
152
|
return tagName[0] && tagName[0].toLowerCase() !== tagName[0] || tagName.includes(".") || /[^a-z]/i.test(tagName[0]);
|
|
134
153
|
}
|
|
135
154
|
function isTextChild(path) {
|
|
@@ -155,10 +174,45 @@ function setNodeText(path, text) {
|
|
|
155
174
|
}
|
|
156
175
|
}
|
|
157
176
|
}
|
|
158
|
-
function
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
177
|
+
function replaceSpace(node) {
|
|
178
|
+
return node.value.replaceAll(/\s+/g, " ").trim();
|
|
179
|
+
}
|
|
180
|
+
function isValidChild(path) {
|
|
181
|
+
const regex = /^\s*$/;
|
|
182
|
+
if (path.isStringLiteral() || path.isJSXText()) {
|
|
183
|
+
return !regex.test(path.node.value);
|
|
184
|
+
}
|
|
185
|
+
return Object.keys(path.node).length > 0;
|
|
186
|
+
}
|
|
187
|
+
function hasObjectExpression(prop, value, props, state, isCt = false) {
|
|
188
|
+
let ct = "";
|
|
189
|
+
const hasConditional = value.properties.some(
|
|
190
|
+
(property) => t2.isObjectProperty(property) && t2.isConditionalExpression(property.value)
|
|
191
|
+
);
|
|
192
|
+
if (hasConditional) {
|
|
193
|
+
imports.add("useComputed");
|
|
194
|
+
props[prop] = t2.callExpression(state.useComputed, [t2.arrowFunctionExpression([], value)]);
|
|
195
|
+
} else if (isCt) {
|
|
196
|
+
value.properties.forEach((property) => {
|
|
197
|
+
if (t2.isObjectProperty(property)) {
|
|
198
|
+
ct += `${property.key.name || property.key.value}:${property.value.value};`;
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
delete props[prop];
|
|
202
|
+
}
|
|
203
|
+
return ct;
|
|
204
|
+
}
|
|
205
|
+
function getNodeText(path) {
|
|
206
|
+
if (path.isJSXText()) {
|
|
207
|
+
return replaceSpace(path.node);
|
|
208
|
+
}
|
|
209
|
+
if (path.isJSXExpressionContainer()) {
|
|
210
|
+
const expression = path.get("expression");
|
|
211
|
+
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
212
|
+
return String(expression.node.value);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return "";
|
|
162
216
|
}
|
|
163
217
|
|
|
164
218
|
// src/jsx/constants.ts
|
|
@@ -201,286 +255,116 @@ var svgTags = [
|
|
|
201
255
|
];
|
|
202
256
|
|
|
203
257
|
// src/jsx/index.ts
|
|
204
|
-
var
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
258
|
+
var currentResult;
|
|
259
|
+
var isServer = false;
|
|
260
|
+
function addTemplate(content, join = false) {
|
|
261
|
+
if (isServer) {
|
|
262
|
+
if (join && currentResult.template.length > 0) {
|
|
263
|
+
currentResult.template[currentResult.template.length - 1] += content;
|
|
209
264
|
} else {
|
|
210
|
-
|
|
265
|
+
currentResult.template.push(content);
|
|
211
266
|
}
|
|
212
267
|
} else {
|
|
213
|
-
|
|
268
|
+
currentResult.template += content;
|
|
214
269
|
}
|
|
215
270
|
}
|
|
216
271
|
function transformJSX(path) {
|
|
272
|
+
var _a, _b;
|
|
273
|
+
isServer = (_b = (_a = path.state) == null ? void 0 : _a.opts) == null ? void 0 : _b.server;
|
|
274
|
+
const preCurrentResult = currentResult;
|
|
275
|
+
currentResult = isServer ? createServerResult() : createClientResult();
|
|
276
|
+
transformJSXElement(path, isServer, true);
|
|
277
|
+
path.replaceWith(isServer ? createServerNode(path) : createClientNode(path));
|
|
278
|
+
currentResult = preCurrentResult;
|
|
279
|
+
}
|
|
280
|
+
function createClientNode(path) {
|
|
281
|
+
var _a, _b;
|
|
217
282
|
const state = path.state;
|
|
218
|
-
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
|
|
283
|
+
const isJSXFragment = path.isJSXFragment();
|
|
284
|
+
const isComponent = path.isJSXElement() && isComponentName(getTagName(path.node));
|
|
285
|
+
const tmpl = isComponent ? t3.identifier(getTagName(path.node)) : path.scope.generateUidIdentifier("_tmpl$");
|
|
286
|
+
let templateNode;
|
|
287
|
+
if (!isComponent) {
|
|
288
|
+
templateNode = t3.callExpression(state.template, [
|
|
289
|
+
t3.stringLiteral(currentResult.template)
|
|
290
|
+
]);
|
|
291
|
+
state.tmplDeclaration.declarations.push(t3.variableDeclarator(tmpl, templateNode));
|
|
292
|
+
imports.add("template");
|
|
293
|
+
}
|
|
294
|
+
const key = (_b = currentResult.props.key) != null ? _b : (_a = currentResult.props[0]) == null ? void 0 : _a.key;
|
|
295
|
+
const propsArg = createProps(currentResult.props);
|
|
296
|
+
const args = isComponent && getTagName(path.node) === "Fragment" ? [t3.stringLiteral(""), propsArg] : [tmpl, propsArg];
|
|
297
|
+
if (key) {
|
|
298
|
+
args.push(key);
|
|
299
|
+
}
|
|
300
|
+
const fnName = isJSXFragment || isComponent && getTagName(path.node) === "Fragment" ? "Fragment" : "h";
|
|
301
|
+
imports.add(fnName);
|
|
302
|
+
return t3.callExpression(state[fnName], args);
|
|
231
303
|
}
|
|
232
|
-
function
|
|
304
|
+
function createServerNode(path) {
|
|
305
|
+
var _a, _b;
|
|
233
306
|
const state = path.state;
|
|
234
|
-
const
|
|
235
|
-
const tmpl =
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
imports.add("template");
|
|
241
|
-
}
|
|
307
|
+
const isComponent = path.isJSXElement() && isComponentName(getTagName(path.node));
|
|
308
|
+
const tmpl = isComponent ? t3.identifier(getTagName(path.node)) : path.scope.generateUidIdentifier("_tmpl$");
|
|
309
|
+
let templateNode;
|
|
310
|
+
if (!isComponent) {
|
|
311
|
+
templateNode = t3.arrayExpression(currentResult.template.map(t3.stringLiteral));
|
|
312
|
+
state.tmplDeclaration.declarations.push(t3.variableDeclarator(tmpl, templateNode));
|
|
242
313
|
}
|
|
243
|
-
const key =
|
|
244
|
-
|
|
245
|
-
const args =
|
|
314
|
+
const key = (_b = currentResult.props.key) != null ? _b : (_a = currentResult.props[0]) == null ? void 0 : _a.key;
|
|
315
|
+
const propsArg = createProps(currentResult.props);
|
|
316
|
+
const args = isComponent && getTagName(path.node) === "Fragment" ? [t3.stringLiteral(""), propsArg] : [tmpl, propsArg];
|
|
246
317
|
if (key) {
|
|
247
318
|
args.push(key);
|
|
248
319
|
}
|
|
249
|
-
const fnName =
|
|
320
|
+
const fnName = "ssg";
|
|
250
321
|
imports.add(fnName);
|
|
251
322
|
return t3.callExpression(state[fnName], args);
|
|
252
323
|
}
|
|
253
|
-
function
|
|
254
|
-
const
|
|
255
|
-
if (isArray(value)) {
|
|
256
|
-
return t3.arrayExpression(value.map(toAstNode));
|
|
257
|
-
}
|
|
258
|
-
if (value && typeof value === "object" && !t3.isNode(value)) {
|
|
259
|
-
return createProps(value);
|
|
260
|
-
}
|
|
261
|
-
switch (typeof value) {
|
|
262
|
-
case "string":
|
|
263
|
-
return t3.stringLiteral(value);
|
|
264
|
-
case "number":
|
|
265
|
-
return t3.numericLiteral(value);
|
|
266
|
-
case "boolean":
|
|
267
|
-
return t3.booleanLiteral(value);
|
|
268
|
-
case "undefined":
|
|
269
|
-
case void 0:
|
|
270
|
-
return t3.tsUndefinedKeyword();
|
|
271
|
-
case null:
|
|
272
|
-
return t3.nullLiteral();
|
|
273
|
-
default:
|
|
274
|
-
return value;
|
|
275
|
-
}
|
|
276
|
-
};
|
|
277
|
-
const result = Object.entries(props).map(
|
|
278
|
-
([prop, value]) => prop === "_$spread$" ? t3.spreadElement(toAstNode(value)) : t3.objectProperty(t3.stringLiteral(prop), toAstNode(value))
|
|
279
|
-
);
|
|
280
|
-
return t3.objectExpression(result);
|
|
281
|
-
}
|
|
282
|
-
function transformJSXElement(path, result, isRoot = false) {
|
|
324
|
+
function transformJSXElement(path, isServer2 = false, isRoot = false) {
|
|
325
|
+
const state = path.state;
|
|
283
326
|
if (path.isJSXElement()) {
|
|
284
327
|
const tagName = getTagName(path.node);
|
|
285
|
-
const tagIsComponent =
|
|
328
|
+
const tagIsComponent = isComponentName(tagName);
|
|
286
329
|
const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
|
|
287
|
-
const isSvg = svgTags.includes(tagName) &&
|
|
288
|
-
const { props, hasExpression } = getAttrProps(path);
|
|
289
|
-
if (props.key) {
|
|
290
|
-
result.props.key = props.key;
|
|
291
|
-
delete props.key;
|
|
292
|
-
}
|
|
330
|
+
const isSvg = svgTags.includes(tagName) && currentResult.index === 1;
|
|
331
|
+
const { props, hasExpression } = getAttrProps(path, state);
|
|
293
332
|
if (tagIsComponent) {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
result.props = props;
|
|
306
|
-
const children = getChildren(path);
|
|
307
|
-
if (children.length > 0) {
|
|
308
|
-
const childrenGenerator = children.length === 1 ? children[0] : t3.arrayExpression(children);
|
|
309
|
-
if (t3.isConditionalExpression(childrenGenerator)) {
|
|
310
|
-
result.props.children = t3.arrowFunctionExpression([], childrenGenerator);
|
|
311
|
-
} else {
|
|
312
|
-
result.props.children = childrenGenerator;
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
} else {
|
|
316
|
-
transformJSX(path);
|
|
317
|
-
replaceChild(path.node, result);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
function handleHTMLElement(path, result, tagName, isSelfClose, isSvg, props, hasExpression) {
|
|
321
|
-
if (isSvg) {
|
|
322
|
-
result.template = isSSG ? [`<svg _svg_ data-hk="${result.index}">`] : `<svg _svg_ >`;
|
|
323
|
-
}
|
|
324
|
-
addToTemplate(result, `<${tagName}${isSSG ? ` data-hk="${result.index}"` : ""}`, true);
|
|
325
|
-
handleAttributes(props, result);
|
|
326
|
-
addToTemplate(result, isSelfClose ? "/>" : ">", !hasExpression);
|
|
327
|
-
if (!isSelfClose) {
|
|
328
|
-
transformChildren(path, result);
|
|
329
|
-
if (hasSiblingElement(path) || isSSG) {
|
|
330
|
-
addToTemplate(result, `</${tagName}>`);
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
function transformChildren(path, result) {
|
|
335
|
-
const parentIndex = result.index;
|
|
336
|
-
path.get("children").reduce((pre, cur) => {
|
|
337
|
-
if (isValidChild(cur)) {
|
|
338
|
-
const lastChild = pre.at(-1);
|
|
339
|
-
if (lastChild && isTextChild(cur) && isTextChild(lastChild)) {
|
|
340
|
-
setNodeText(lastChild, getNodeText(lastChild) + getNodeText(cur));
|
|
333
|
+
if (isRoot) {
|
|
334
|
+
currentResult.props = props;
|
|
335
|
+
const children = getChildren(path);
|
|
336
|
+
if (children.length > 0) {
|
|
337
|
+
const childrenGenerator = children.length === 1 ? children[0] : t3.arrayExpression(children);
|
|
338
|
+
if (t3.isConditionalExpression(childrenGenerator)) {
|
|
339
|
+
currentResult.props.children = t3.arrowFunctionExpression([], childrenGenerator);
|
|
340
|
+
} else {
|
|
341
|
+
currentResult.props.children = childrenGenerator;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
341
344
|
} else {
|
|
342
|
-
|
|
345
|
+
transformJSX(path);
|
|
346
|
+
replaceChild(path.node);
|
|
343
347
|
}
|
|
344
|
-
}
|
|
345
|
-
return pre;
|
|
346
|
-
}, []).forEach((child, i, arr) => {
|
|
347
|
-
result.parentIndex = parentIndex;
|
|
348
|
-
result.isLastChild = i === arr.length - 1;
|
|
349
|
-
transformChild(child, result);
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
function transformChild(child, result) {
|
|
353
|
-
result.index++;
|
|
354
|
-
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
355
|
-
transformJSXElement(child, result, false);
|
|
356
|
-
} else if (child.isJSXExpressionContainer()) {
|
|
357
|
-
const expression = child.get("expression");
|
|
358
|
-
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
359
|
-
addToTemplate(result, `${expression.node.value}`, true);
|
|
360
|
-
} else if (expression.isExpression()) {
|
|
361
|
-
replaceChild(expression.node, result);
|
|
362
|
-
} else if (t3.isJSXEmptyExpression(expression.node)) {
|
|
363
348
|
} else {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
return replaceSpace(path.node);
|
|
375
|
-
}
|
|
376
|
-
if (path.isJSXExpressionContainer()) {
|
|
377
|
-
const expression = path.get("expression");
|
|
378
|
-
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
379
|
-
return String(expression.node.value);
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
return "";
|
|
383
|
-
}
|
|
384
|
-
function isStyleClassName(name) {
|
|
385
|
-
return name === "class" || name === "style";
|
|
386
|
-
}
|
|
387
|
-
function handleAttributes(props, result) {
|
|
388
|
-
let klass = "";
|
|
389
|
-
let style = "";
|
|
390
|
-
for (const [prop, value] of Object.entries(props)) {
|
|
391
|
-
if (prop === "class" && typeof value === "string") {
|
|
392
|
-
klass += ` ${value}`;
|
|
393
|
-
delete props[prop];
|
|
394
|
-
} else if (prop === "style" && typeof value === "string") {
|
|
395
|
-
style += `${value}${value.at(-1) === ";" ? "" : ";"}`;
|
|
396
|
-
delete props[prop];
|
|
397
|
-
} else if (value === true) {
|
|
398
|
-
addToTemplate(result, ` ${prop}`);
|
|
399
|
-
delete props[prop];
|
|
400
|
-
} else if (value === false) {
|
|
401
|
-
delete props[prop];
|
|
402
|
-
} else if (typeof value === "string" || typeof value === "number") {
|
|
403
|
-
addToTemplate(result, ` ${prop}="${value}"`);
|
|
404
|
-
delete props[prop];
|
|
405
|
-
} else if (t3.isConditionalExpression(value)) {
|
|
406
|
-
props[prop] = t3.arrowFunctionExpression([], value);
|
|
407
|
-
} else if (t3.isObjectExpression(value)) {
|
|
408
|
-
const val = handleObjectExpression(prop, value, props, isStyleClassName(prop));
|
|
409
|
-
if (val) {
|
|
410
|
-
if (prop === "class") {
|
|
411
|
-
klass += ` ${val}`;
|
|
412
|
-
}
|
|
413
|
-
if (prop === "style") {
|
|
414
|
-
style += `${val}${val.at(-1) === ";" ? "" : ";"}`;
|
|
349
|
+
if (isSvg) {
|
|
350
|
+
currentResult.template = isServer2 ? [`<svg _svg_ data-hk="${currentResult.index}">`] : `<svg _svg_ >`;
|
|
351
|
+
}
|
|
352
|
+
addTemplate(`<${tagName}${isServer2 ? ` data-hk="${currentResult.index}"` : ""}`, true);
|
|
353
|
+
handleAttributes(props, state);
|
|
354
|
+
addTemplate(isSelfClose ? "/>" : ">", !hasExpression);
|
|
355
|
+
if (!isSelfClose) {
|
|
356
|
+
transformChildren(path);
|
|
357
|
+
if (hasSiblingElement(path) || isServer2) {
|
|
358
|
+
addTemplate(`</${tagName}>`);
|
|
415
359
|
}
|
|
416
360
|
}
|
|
417
361
|
}
|
|
418
|
-
}
|
|
419
|
-
if (Object.keys(props).length > 0) {
|
|
420
|
-
result.props[result.index] = props;
|
|
421
|
-
}
|
|
422
|
-
if (klass.trim()) {
|
|
423
|
-
addToTemplate(result, ` class="${klass.trim()}"`);
|
|
424
|
-
}
|
|
425
|
-
if (style.trim()) {
|
|
426
|
-
addToTemplate(result, ` style="${style.trim()}"`);
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
function handleObjectExpression(prop, value, props, isCt = false) {
|
|
430
|
-
let ct = "";
|
|
431
|
-
const hasConditional = value.properties.some(
|
|
432
|
-
(property) => t3.isObjectProperty(property) && t3.isConditionalExpression(property.value)
|
|
433
|
-
);
|
|
434
|
-
if (hasConditional) {
|
|
435
|
-
props[prop] = t3.arrowFunctionExpression([], value);
|
|
436
|
-
} else if (isCt) {
|
|
437
|
-
value.properties.forEach((property) => {
|
|
438
|
-
if (t3.isObjectProperty(property)) {
|
|
439
|
-
ct += `${property.key.name || property.key.value}:${property.value.value};`;
|
|
440
|
-
}
|
|
441
|
-
});
|
|
442
|
-
delete props[prop];
|
|
443
|
-
}
|
|
444
|
-
return ct;
|
|
445
|
-
}
|
|
446
|
-
function replaceChild(node, result) {
|
|
447
|
-
var _a, _b, _c, _d, _e;
|
|
448
|
-
if (result.isLastChild) {
|
|
449
|
-
result.index--;
|
|
450
362
|
} else {
|
|
451
|
-
|
|
363
|
+
currentResult.index--;
|
|
364
|
+
transformChildren(path);
|
|
452
365
|
}
|
|
453
|
-
(_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
|
|
454
|
-
(_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
|
|
455
|
-
result.props[result.parentIndex].children.push(
|
|
456
|
-
t3.arrayExpression([
|
|
457
|
-
t3.arrowFunctionExpression([], node),
|
|
458
|
-
result.isLastChild ? t3.nullLiteral() : t3.identifier(String(result.index))
|
|
459
|
-
])
|
|
460
|
-
);
|
|
461
|
-
}
|
|
462
|
-
function getChildren(path) {
|
|
463
|
-
return path.get("children").filter((child) => isValidChild(child)).map((child) => {
|
|
464
|
-
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
465
|
-
transformJSX(child);
|
|
466
|
-
} else if (child.isJSXExpressionContainer()) {
|
|
467
|
-
child.replaceWith(child.get("expression"));
|
|
468
|
-
} else if (child.isJSXText()) {
|
|
469
|
-
child.replaceWith(t3.stringLiteral(replaceSpace(child.node)));
|
|
470
|
-
} else {
|
|
471
|
-
throw new Error("Unsupported child type");
|
|
472
|
-
}
|
|
473
|
-
return child.node;
|
|
474
|
-
});
|
|
475
366
|
}
|
|
476
|
-
function
|
|
477
|
-
const regex = /^\s*$/;
|
|
478
|
-
if (path.isStringLiteral() || path.isJSXText()) {
|
|
479
|
-
return !regex.test(path.node.value);
|
|
480
|
-
}
|
|
481
|
-
return Object.keys(path.node).length > 0;
|
|
482
|
-
}
|
|
483
|
-
function getAttrProps(path) {
|
|
367
|
+
function getAttrProps(path, state) {
|
|
484
368
|
const props = {};
|
|
485
369
|
let hasExpression = false;
|
|
486
370
|
path.get("openingElement").get("attributes").forEach((attribute) => {
|
|
@@ -515,7 +399,10 @@ function getAttrProps(path) {
|
|
|
515
399
|
);
|
|
516
400
|
} else {
|
|
517
401
|
if (expression.isConditionalExpression()) {
|
|
518
|
-
|
|
402
|
+
imports.add("useComputed");
|
|
403
|
+
props[name] = t3.callExpression(state.useComputed, [
|
|
404
|
+
t3.arrowFunctionExpression([], expression.node)
|
|
405
|
+
]);
|
|
519
406
|
} else {
|
|
520
407
|
props[name] = expression.node;
|
|
521
408
|
}
|
|
@@ -538,16 +425,179 @@ function getAttrProps(path) {
|
|
|
538
425
|
hasExpression
|
|
539
426
|
};
|
|
540
427
|
}
|
|
428
|
+
function getChildren(path) {
|
|
429
|
+
return path.get("children").filter((child) => isValidChild(child)).map((child) => {
|
|
430
|
+
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
431
|
+
transformJSX(child);
|
|
432
|
+
} else if (child.isJSXExpressionContainer()) {
|
|
433
|
+
child.replaceWith(child.get("expression"));
|
|
434
|
+
} else if (child.isJSXText()) {
|
|
435
|
+
child.replaceWith(t3.stringLiteral(replaceSpace(child.node)));
|
|
436
|
+
} else {
|
|
437
|
+
throw new Error("Unsupported child type");
|
|
438
|
+
}
|
|
439
|
+
return child.node;
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
function replaceChild(node) {
|
|
443
|
+
var _a, _b, _c, _d, _e;
|
|
444
|
+
if (currentResult.isLastChild) {
|
|
445
|
+
currentResult.index--;
|
|
446
|
+
} else {
|
|
447
|
+
addTemplate("<!>");
|
|
448
|
+
}
|
|
449
|
+
(_c = (_a = currentResult.props)[_b = currentResult.parentIndex]) != null ? _c : _a[_b] = {};
|
|
450
|
+
(_e = (_d = currentResult.props[currentResult.parentIndex]).children) != null ? _e : _d.children = [];
|
|
451
|
+
currentResult.props[currentResult.parentIndex].children.push(
|
|
452
|
+
t3.arrayExpression([
|
|
453
|
+
t3.arrowFunctionExpression([], node),
|
|
454
|
+
currentResult.isLastChild ? t3.nullLiteral() : t3.identifier(String(currentResult.index))
|
|
455
|
+
])
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
function handleAttributes(props, state) {
|
|
459
|
+
let klass = "";
|
|
460
|
+
let style = "";
|
|
461
|
+
for (const [prop, value] of Object.entries(props)) {
|
|
462
|
+
if (prop === "class" && typeof value === "string") {
|
|
463
|
+
klass += ` ${value}`;
|
|
464
|
+
delete props[prop];
|
|
465
|
+
} else if (prop === "style" && typeof value === "string") {
|
|
466
|
+
style += `${value}${value.at(-1) === ";" ? "" : ";"}`;
|
|
467
|
+
delete props[prop];
|
|
468
|
+
} else if (value === true) {
|
|
469
|
+
addTemplate(` ${prop}`);
|
|
470
|
+
delete props[prop];
|
|
471
|
+
} else if (value === false) {
|
|
472
|
+
delete props[prop];
|
|
473
|
+
} else if (typeof value === "string" || typeof value === "number") {
|
|
474
|
+
addTemplate(` ${prop}="${value}"`);
|
|
475
|
+
delete props[prop];
|
|
476
|
+
} else if (t3.isConditionalExpression(value)) {
|
|
477
|
+
imports.add("useComputed");
|
|
478
|
+
props[prop] = t3.callExpression(state.useComputed, [t3.arrowFunctionExpression([], value)]);
|
|
479
|
+
} else if (t3.isObjectExpression(value)) {
|
|
480
|
+
const val = hasObjectExpression(
|
|
481
|
+
prop,
|
|
482
|
+
value,
|
|
483
|
+
props,
|
|
484
|
+
state,
|
|
485
|
+
prop === "class" || prop === "style"
|
|
486
|
+
);
|
|
487
|
+
if (val) {
|
|
488
|
+
if (prop === "class") {
|
|
489
|
+
klass += ` ${val}`;
|
|
490
|
+
}
|
|
491
|
+
if (prop === "style") {
|
|
492
|
+
style += `${val}${val.at(-1) === ";" ? "" : ";"}`;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
if (Object.keys(props).length > 0) {
|
|
498
|
+
currentResult.props[currentResult.index] = props;
|
|
499
|
+
}
|
|
500
|
+
if (klass.trim()) {
|
|
501
|
+
addTemplate(` class="${klass.trim()}"`);
|
|
502
|
+
}
|
|
503
|
+
if (style.trim()) {
|
|
504
|
+
addTemplate(` style="${style.trim()}"`);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
function transformChildren(path) {
|
|
508
|
+
const parentIndex = currentResult.index;
|
|
509
|
+
path.get("children").reduce((pre, cur) => {
|
|
510
|
+
if (isValidChild(cur)) {
|
|
511
|
+
const lastChild = pre.at(-1);
|
|
512
|
+
if (lastChild && isTextChild(cur) && isTextChild(lastChild)) {
|
|
513
|
+
setNodeText(lastChild, getNodeText(lastChild) + getNodeText(cur));
|
|
514
|
+
} else {
|
|
515
|
+
pre.push(cur);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return pre;
|
|
519
|
+
}, []).forEach((child, i, arr) => {
|
|
520
|
+
currentResult.parentIndex = parentIndex;
|
|
521
|
+
currentResult.isLastChild = i === arr.length - 1;
|
|
522
|
+
transformChild(child);
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
function transformChild(child) {
|
|
526
|
+
currentResult.index++;
|
|
527
|
+
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
528
|
+
transformJSXElement(child, false);
|
|
529
|
+
} else if (child.isJSXExpressionContainer()) {
|
|
530
|
+
const expression = child.get("expression");
|
|
531
|
+
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
532
|
+
addTemplate(`${expression.node.value}`, true);
|
|
533
|
+
} else if (expression.isExpression()) {
|
|
534
|
+
replaceChild(expression.node);
|
|
535
|
+
} else if (t3.isJSXEmptyExpression(expression.node)) {
|
|
536
|
+
} else {
|
|
537
|
+
throw new Error("Unsupported child type");
|
|
538
|
+
}
|
|
539
|
+
} else if (child.isJSXText()) {
|
|
540
|
+
addTemplate(replaceSpace(child.node), true);
|
|
541
|
+
} else {
|
|
542
|
+
throw new Error("Unsupported child type");
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
function createProps(props) {
|
|
546
|
+
const result = [];
|
|
547
|
+
for (const prop in props) {
|
|
548
|
+
let value = props[prop];
|
|
549
|
+
if (prop === "key") {
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
if (Array.isArray(value)) {
|
|
553
|
+
value = t3.arrayExpression(value);
|
|
554
|
+
}
|
|
555
|
+
if (typeof value === "object" && value !== null && !t3.isNode(value)) {
|
|
556
|
+
value = createProps(value);
|
|
557
|
+
}
|
|
558
|
+
if (typeof value === "string") {
|
|
559
|
+
value = t3.stringLiteral(value);
|
|
560
|
+
}
|
|
561
|
+
if (typeof value === "number") {
|
|
562
|
+
value = t3.numericLiteral(value);
|
|
563
|
+
}
|
|
564
|
+
if (typeof value === "boolean") {
|
|
565
|
+
value = t3.booleanLiteral(value);
|
|
566
|
+
}
|
|
567
|
+
if (value === void 0) {
|
|
568
|
+
value = t3.tsUndefinedKeyword();
|
|
569
|
+
}
|
|
570
|
+
if (value === null) {
|
|
571
|
+
value = t3.nullLiteral();
|
|
572
|
+
}
|
|
573
|
+
if (prop === "_$spread$") {
|
|
574
|
+
result.push(t3.spreadElement(value));
|
|
575
|
+
} else {
|
|
576
|
+
result.push(t3.objectProperty(t3.stringLiteral(prop), value));
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return t3.objectExpression(result);
|
|
580
|
+
}
|
|
541
581
|
|
|
542
582
|
// src/signal/symbol.ts
|
|
583
|
+
import { types as t5 } from "@babel/core";
|
|
584
|
+
|
|
585
|
+
// src/shared.ts
|
|
543
586
|
import { types as t4 } from "@babel/core";
|
|
587
|
+
function isSymbolStart(path, name) {
|
|
588
|
+
const state = path.state;
|
|
589
|
+
const { symbol } = (state == null ? void 0 : state.opts) || "$";
|
|
590
|
+
return startsWith(name, symbol);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// src/signal/symbol.ts
|
|
544
594
|
function replaceSymbol(path) {
|
|
545
595
|
const { init, id } = path.node;
|
|
546
596
|
const variableName = id.name;
|
|
547
|
-
if (
|
|
548
|
-
const isComputed = init && (
|
|
597
|
+
if (t5.isObjectPattern(id) || t5.isArrayPattern(id) || !isSymbolStart(path, variableName)) return;
|
|
598
|
+
const isComputed = init && (t5.isFunctionExpression(init) || t5.isArrowFunctionExpression(init)) && path.parent.kind === "const";
|
|
549
599
|
const hookName = isComputed ? "useComputed" : "useSignal";
|
|
550
|
-
const newInit =
|
|
600
|
+
const newInit = t5.callExpression(t5.identifier(path.state[hookName].name), init ? [init] : []);
|
|
551
601
|
imports.add(hookName);
|
|
552
602
|
path.node.init = newInit;
|
|
553
603
|
}
|
|
@@ -557,31 +607,31 @@ function symbolIdentifier(path) {
|
|
|
557
607
|
const { node } = path;
|
|
558
608
|
if (!isSymbolStart(path, node.name)) return;
|
|
559
609
|
if (!path.findParent((p) => p.isMemberExpression() && p.node.property.name === "value")) {
|
|
560
|
-
path.replaceWith(
|
|
610
|
+
path.replaceWith(t5.memberExpression(t5.identifier(node.name), t5.identifier("value")));
|
|
561
611
|
}
|
|
562
612
|
}
|
|
563
613
|
function shouldProcessIdentifier(parentPath) {
|
|
564
|
-
return parentPath && !
|
|
614
|
+
return parentPath && !t5.isVariableDeclarator(parentPath) && !t5.isImportSpecifier(parentPath) && !t5.isObjectProperty(parentPath) && !t5.isArrayPattern(parentPath) && !t5.isObjectPattern(parentPath);
|
|
565
615
|
}
|
|
566
616
|
function symbolObjectPattern(path) {
|
|
567
617
|
path.node.properties.forEach((property) => {
|
|
568
|
-
if (
|
|
569
|
-
property.key =
|
|
618
|
+
if (t5.isObjectProperty(property) && t5.isIdentifier(property.key) && isSymbolStart(path, property.key.name)) {
|
|
619
|
+
property.key = t5.identifier(property.key.name);
|
|
570
620
|
}
|
|
571
621
|
});
|
|
572
622
|
}
|
|
573
623
|
function symbolArrayPattern(path) {
|
|
574
624
|
path.node.elements.forEach((element) => {
|
|
575
|
-
if (
|
|
576
|
-
element.name =
|
|
577
|
-
} else if (
|
|
625
|
+
if (t5.isIdentifier(element) && element.name.startsWith("$")) {
|
|
626
|
+
element.name = t5.identifier(element.name).name;
|
|
627
|
+
} else if (t5.isObjectPattern(element)) {
|
|
578
628
|
symbolObjectPattern({ node: element });
|
|
579
629
|
}
|
|
580
630
|
});
|
|
581
631
|
}
|
|
582
632
|
|
|
583
633
|
// src/signal/import.ts
|
|
584
|
-
import { types as
|
|
634
|
+
import { types as t6 } from "@babel/core";
|
|
585
635
|
function replaceImportDeclaration(path) {
|
|
586
636
|
path.node.specifiers.forEach((specifier) => {
|
|
587
637
|
const variableName = specifier.local.name;
|
|
@@ -595,11 +645,11 @@ function isVariableUsedAsObject(path, variableName) {
|
|
|
595
645
|
var _a;
|
|
596
646
|
const binding = path.scope.getBinding(variableName);
|
|
597
647
|
return ((_a = binding == null ? void 0 : binding.referencePaths) == null ? void 0 : _a.some((referencePath) => {
|
|
598
|
-
if (
|
|
648
|
+
if (t6.isMemberExpression(referencePath.parent)) {
|
|
599
649
|
const { object, property } = referencePath.parent;
|
|
600
|
-
if (
|
|
650
|
+
if (t6.isIdentifier(object, { name: variableName })) {
|
|
601
651
|
referencePath.parentPath.replaceWith(
|
|
602
|
-
|
|
652
|
+
t6.memberExpression(t6.memberExpression(object, t6.identifier("value")), property)
|
|
603
653
|
);
|
|
604
654
|
return true;
|
|
605
655
|
}
|
|
@@ -609,28 +659,30 @@ function isVariableUsedAsObject(path, variableName) {
|
|
|
609
659
|
}
|
|
610
660
|
|
|
611
661
|
// src/signal/props.ts
|
|
612
|
-
import { types as
|
|
662
|
+
import { types as t7 } from "@babel/core";
|
|
613
663
|
function replaceProps(path) {
|
|
614
664
|
var _a;
|
|
615
665
|
const state = path.state;
|
|
616
666
|
const firstParam = path.node.params[0];
|
|
617
|
-
if (!firstParam || !
|
|
667
|
+
if (!firstParam || !t7.isObjectPattern(firstParam)) return;
|
|
618
668
|
const returnStatement = path.get("body").get("body").find((statement) => statement.isReturnStatement());
|
|
619
|
-
if (!returnStatement || !
|
|
669
|
+
if (!returnStatement || !t7.isJSXElement((_a = returnStatement.node) == null ? void 0 : _a.argument)) return;
|
|
620
670
|
const replaceProperties = (properties2, parentPath) => {
|
|
621
671
|
properties2.forEach((property) => {
|
|
622
|
-
if (
|
|
672
|
+
if (t7.isObjectProperty(property) && t7.isIdentifier(property.key)) {
|
|
623
673
|
const keyName = property.key.name;
|
|
624
|
-
if (
|
|
674
|
+
if (t7.isIdentifier(property.value)) {
|
|
625
675
|
path.scope.rename(property.value.name, `${parentPath}${keyName}`);
|
|
626
|
-
} else if (
|
|
676
|
+
} else if (t7.isObjectPattern(property.value)) {
|
|
627
677
|
replaceProperties(property.value.properties, `${parentPath}${keyName}.`);
|
|
628
678
|
}
|
|
629
679
|
}
|
|
630
680
|
});
|
|
631
681
|
};
|
|
632
682
|
const properties = firstParam.properties;
|
|
633
|
-
const notRestProperties = properties.filter(
|
|
683
|
+
const notRestProperties = properties.filter(
|
|
684
|
+
(property) => !t7.isRestElement(property)
|
|
685
|
+
);
|
|
634
686
|
replaceProperties(notRestProperties, "__props.");
|
|
635
687
|
const notRestNames = notRestProperties.map((property) => property.key.name);
|
|
636
688
|
if (notRestNames.some((name) => startsWith(name, "$"))) {
|
|
@@ -640,12 +692,12 @@ function replaceProps(path) {
|
|
|
640
692
|
handleRestElement(path, state, properties, notRestNames);
|
|
641
693
|
}
|
|
642
694
|
function handleRestElement(path, state, properties, notRestNames) {
|
|
643
|
-
const restElement = properties.find((property) =>
|
|
644
|
-
path.node.params[0] =
|
|
695
|
+
const restElement = properties.find((property) => t7.isRestElement(property));
|
|
696
|
+
path.node.params[0] = t7.identifier("__props");
|
|
645
697
|
if (restElement) {
|
|
646
698
|
const restName = restElement.argument.name;
|
|
647
699
|
if (notRestNames.length === 0) {
|
|
648
|
-
path.node.params[0] =
|
|
700
|
+
path.node.params[0] = t7.identifier(restName);
|
|
649
701
|
} else {
|
|
650
702
|
const restVariableDeclaration = createRestVariableDeclaration(state, restName, notRestNames);
|
|
651
703
|
imports.add("useReactive");
|
|
@@ -654,12 +706,12 @@ function handleRestElement(path, state, properties, notRestNames) {
|
|
|
654
706
|
}
|
|
655
707
|
}
|
|
656
708
|
function createRestVariableDeclaration(state, restName, notRestNames) {
|
|
657
|
-
return
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
709
|
+
return t7.variableDeclaration("const", [
|
|
710
|
+
t7.variableDeclarator(
|
|
711
|
+
t7.identifier(restName),
|
|
712
|
+
t7.callExpression(state.useReactive, [
|
|
713
|
+
t7.identifier("__props"),
|
|
714
|
+
t7.arrayExpression(notRestNames.map((name) => t7.stringLiteral(name)))
|
|
663
715
|
])
|
|
664
716
|
)
|
|
665
717
|
]);
|