babel-plugin-essor 0.0.14-beta.6 → 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 -289
- 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 -288
- 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,282 +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
|
-
result.props.children = childrenGenerator;
|
|
310
|
-
}
|
|
311
|
-
} else {
|
|
312
|
-
transformJSX(path);
|
|
313
|
-
replaceChild(path.node, result);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
function handleHTMLElement(path, result, tagName, isSelfClose, isSvg, props, hasExpression) {
|
|
317
|
-
if (isSvg) {
|
|
318
|
-
result.template = isSSG ? [`<svg _svg_ data-hk="${result.index}">`] : `<svg _svg_ >`;
|
|
319
|
-
}
|
|
320
|
-
addToTemplate(result, `<${tagName}${isSSG ? ` data-hk="${result.index}"` : ""}`, true);
|
|
321
|
-
handleAttributes(props, result);
|
|
322
|
-
addToTemplate(result, isSelfClose ? "/>" : ">", !hasExpression);
|
|
323
|
-
if (!isSelfClose) {
|
|
324
|
-
transformChildren(path, result);
|
|
325
|
-
if (hasSiblingElement(path) || isSSG) {
|
|
326
|
-
addToTemplate(result, `</${tagName}>`);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
function transformChildren(path, result) {
|
|
331
|
-
const parentIndex = result.index;
|
|
332
|
-
path.get("children").reduce((pre, cur) => {
|
|
333
|
-
if (isValidChild(cur)) {
|
|
334
|
-
const lastChild = pre.at(-1);
|
|
335
|
-
if (lastChild && isTextChild(cur) && isTextChild(lastChild)) {
|
|
336
|
-
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
|
+
}
|
|
337
344
|
} else {
|
|
338
|
-
|
|
345
|
+
transformJSX(path);
|
|
346
|
+
replaceChild(path.node);
|
|
339
347
|
}
|
|
340
|
-
}
|
|
341
|
-
return pre;
|
|
342
|
-
}, []).forEach((child, i, arr) => {
|
|
343
|
-
result.parentIndex = parentIndex;
|
|
344
|
-
result.isLastChild = i === arr.length - 1;
|
|
345
|
-
transformChild(child, result);
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
function transformChild(child, result) {
|
|
349
|
-
result.index++;
|
|
350
|
-
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
351
|
-
transformJSXElement(child, result, false);
|
|
352
|
-
} else if (child.isJSXExpressionContainer()) {
|
|
353
|
-
const expression = child.get("expression");
|
|
354
|
-
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
355
|
-
addToTemplate(result, `${expression.node.value}`, true);
|
|
356
|
-
} else if (expression.isExpression()) {
|
|
357
|
-
replaceChild(expression.node, result);
|
|
358
|
-
} else if (t3.isJSXEmptyExpression(expression.node)) {
|
|
359
348
|
} else {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
return replaceSpace(path.node);
|
|
371
|
-
}
|
|
372
|
-
if (path.isJSXExpressionContainer()) {
|
|
373
|
-
const expression = path.get("expression");
|
|
374
|
-
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
375
|
-
return String(expression.node.value);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
return "";
|
|
379
|
-
}
|
|
380
|
-
function isStyleClassName(name) {
|
|
381
|
-
return name === "class" || name === "style";
|
|
382
|
-
}
|
|
383
|
-
function handleAttributes(props, result) {
|
|
384
|
-
let klass = "";
|
|
385
|
-
let style = "";
|
|
386
|
-
for (const [prop, value] of Object.entries(props)) {
|
|
387
|
-
if (prop === "class" && typeof value === "string") {
|
|
388
|
-
klass += ` ${value}`;
|
|
389
|
-
delete props[prop];
|
|
390
|
-
} else if (prop === "style" && typeof value === "string") {
|
|
391
|
-
style += `${value}${value.at(-1) === ";" ? "" : ";"}`;
|
|
392
|
-
delete props[prop];
|
|
393
|
-
} else if (value === true) {
|
|
394
|
-
addToTemplate(result, ` ${prop}`);
|
|
395
|
-
delete props[prop];
|
|
396
|
-
} else if (value === false) {
|
|
397
|
-
delete props[prop];
|
|
398
|
-
} else if (typeof value === "string" || typeof value === "number") {
|
|
399
|
-
addToTemplate(result, ` ${prop}="${value}"`);
|
|
400
|
-
delete props[prop];
|
|
401
|
-
} else if (t3.isConditionalExpression(value)) {
|
|
402
|
-
props[prop] = t3.arrowFunctionExpression([], value);
|
|
403
|
-
} else if (t3.isObjectExpression(value)) {
|
|
404
|
-
const val = handleObjectExpression(prop, value, props, isStyleClassName(prop));
|
|
405
|
-
if (val) {
|
|
406
|
-
if (prop === "class") {
|
|
407
|
-
klass += ` ${val}`;
|
|
408
|
-
}
|
|
409
|
-
if (prop === "style") {
|
|
410
|
-
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}>`);
|
|
411
359
|
}
|
|
412
360
|
}
|
|
413
361
|
}
|
|
414
|
-
}
|
|
415
|
-
if (Object.keys(props).length > 0) {
|
|
416
|
-
result.props[result.index] = props;
|
|
417
|
-
}
|
|
418
|
-
if (klass.trim()) {
|
|
419
|
-
addToTemplate(result, ` class="${klass.trim()}"`);
|
|
420
|
-
}
|
|
421
|
-
if (style.trim()) {
|
|
422
|
-
addToTemplate(result, ` style="${style.trim()}"`);
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
function handleObjectExpression(prop, value, props, isCt = false) {
|
|
426
|
-
let ct = "";
|
|
427
|
-
const hasConditional = value.properties.some(
|
|
428
|
-
(property) => t3.isObjectProperty(property) && t3.isConditionalExpression(property.value)
|
|
429
|
-
);
|
|
430
|
-
if (hasConditional) {
|
|
431
|
-
props[prop] = t3.arrowFunctionExpression([], value);
|
|
432
|
-
} else if (isCt) {
|
|
433
|
-
value.properties.forEach((property) => {
|
|
434
|
-
if (t3.isObjectProperty(property)) {
|
|
435
|
-
ct += `${property.key.name || property.key.value}:${property.value.value};`;
|
|
436
|
-
}
|
|
437
|
-
});
|
|
438
|
-
delete props[prop];
|
|
439
|
-
}
|
|
440
|
-
return ct;
|
|
441
|
-
}
|
|
442
|
-
function replaceChild(node, result) {
|
|
443
|
-
var _a, _b, _c, _d, _e;
|
|
444
|
-
if (result.isLastChild) {
|
|
445
|
-
result.index--;
|
|
446
362
|
} else {
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
(_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
|
|
450
|
-
(_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
|
|
451
|
-
result.props[result.parentIndex].children.push(
|
|
452
|
-
t3.arrayExpression([
|
|
453
|
-
t3.arrowFunctionExpression([], node),
|
|
454
|
-
result.isLastChild ? t3.nullLiteral() : t3.identifier(String(result.index))
|
|
455
|
-
])
|
|
456
|
-
);
|
|
457
|
-
}
|
|
458
|
-
function getChildren(path) {
|
|
459
|
-
return path.get("children").filter((child) => isValidChild(child)).map((child) => {
|
|
460
|
-
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
461
|
-
transformJSX(child);
|
|
462
|
-
} else if (child.isJSXExpressionContainer()) {
|
|
463
|
-
child.replaceWith(child.get("expression"));
|
|
464
|
-
} else if (child.isJSXText()) {
|
|
465
|
-
child.replaceWith(t3.stringLiteral(replaceSpace(child.node)));
|
|
466
|
-
} else {
|
|
467
|
-
throw new Error("Unsupported child type");
|
|
468
|
-
}
|
|
469
|
-
return child.node;
|
|
470
|
-
});
|
|
471
|
-
}
|
|
472
|
-
function isValidChild(path) {
|
|
473
|
-
const regex = /^\s*$/;
|
|
474
|
-
if (path.isStringLiteral() || path.isJSXText()) {
|
|
475
|
-
return !regex.test(path.node.value);
|
|
363
|
+
currentResult.index--;
|
|
364
|
+
transformChildren(path);
|
|
476
365
|
}
|
|
477
|
-
return Object.keys(path.node).length > 0;
|
|
478
366
|
}
|
|
479
|
-
function getAttrProps(path) {
|
|
367
|
+
function getAttrProps(path, state) {
|
|
480
368
|
const props = {};
|
|
481
369
|
let hasExpression = false;
|
|
482
370
|
path.get("openingElement").get("attributes").forEach((attribute) => {
|
|
@@ -511,7 +399,10 @@ function getAttrProps(path) {
|
|
|
511
399
|
);
|
|
512
400
|
} else {
|
|
513
401
|
if (expression.isConditionalExpression()) {
|
|
514
|
-
|
|
402
|
+
imports.add("useComputed");
|
|
403
|
+
props[name] = t3.callExpression(state.useComputed, [
|
|
404
|
+
t3.arrowFunctionExpression([], expression.node)
|
|
405
|
+
]);
|
|
515
406
|
} else {
|
|
516
407
|
props[name] = expression.node;
|
|
517
408
|
}
|
|
@@ -534,16 +425,179 @@ function getAttrProps(path) {
|
|
|
534
425
|
hasExpression
|
|
535
426
|
};
|
|
536
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
|
+
}
|
|
537
581
|
|
|
538
582
|
// src/signal/symbol.ts
|
|
583
|
+
import { types as t5 } from "@babel/core";
|
|
584
|
+
|
|
585
|
+
// src/shared.ts
|
|
539
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
|
|
540
594
|
function replaceSymbol(path) {
|
|
541
595
|
const { init, id } = path.node;
|
|
542
596
|
const variableName = id.name;
|
|
543
|
-
if (
|
|
544
|
-
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";
|
|
545
599
|
const hookName = isComputed ? "useComputed" : "useSignal";
|
|
546
|
-
const newInit =
|
|
600
|
+
const newInit = t5.callExpression(t5.identifier(path.state[hookName].name), init ? [init] : []);
|
|
547
601
|
imports.add(hookName);
|
|
548
602
|
path.node.init = newInit;
|
|
549
603
|
}
|
|
@@ -553,31 +607,31 @@ function symbolIdentifier(path) {
|
|
|
553
607
|
const { node } = path;
|
|
554
608
|
if (!isSymbolStart(path, node.name)) return;
|
|
555
609
|
if (!path.findParent((p) => p.isMemberExpression() && p.node.property.name === "value")) {
|
|
556
|
-
path.replaceWith(
|
|
610
|
+
path.replaceWith(t5.memberExpression(t5.identifier(node.name), t5.identifier("value")));
|
|
557
611
|
}
|
|
558
612
|
}
|
|
559
613
|
function shouldProcessIdentifier(parentPath) {
|
|
560
|
-
return parentPath && !
|
|
614
|
+
return parentPath && !t5.isVariableDeclarator(parentPath) && !t5.isImportSpecifier(parentPath) && !t5.isObjectProperty(parentPath) && !t5.isArrayPattern(parentPath) && !t5.isObjectPattern(parentPath);
|
|
561
615
|
}
|
|
562
616
|
function symbolObjectPattern(path) {
|
|
563
617
|
path.node.properties.forEach((property) => {
|
|
564
|
-
if (
|
|
565
|
-
property.key =
|
|
618
|
+
if (t5.isObjectProperty(property) && t5.isIdentifier(property.key) && isSymbolStart(path, property.key.name)) {
|
|
619
|
+
property.key = t5.identifier(property.key.name);
|
|
566
620
|
}
|
|
567
621
|
});
|
|
568
622
|
}
|
|
569
623
|
function symbolArrayPattern(path) {
|
|
570
624
|
path.node.elements.forEach((element) => {
|
|
571
|
-
if (
|
|
572
|
-
element.name =
|
|
573
|
-
} else if (
|
|
625
|
+
if (t5.isIdentifier(element) && element.name.startsWith("$")) {
|
|
626
|
+
element.name = t5.identifier(element.name).name;
|
|
627
|
+
} else if (t5.isObjectPattern(element)) {
|
|
574
628
|
symbolObjectPattern({ node: element });
|
|
575
629
|
}
|
|
576
630
|
});
|
|
577
631
|
}
|
|
578
632
|
|
|
579
633
|
// src/signal/import.ts
|
|
580
|
-
import { types as
|
|
634
|
+
import { types as t6 } from "@babel/core";
|
|
581
635
|
function replaceImportDeclaration(path) {
|
|
582
636
|
path.node.specifiers.forEach((specifier) => {
|
|
583
637
|
const variableName = specifier.local.name;
|
|
@@ -591,11 +645,11 @@ function isVariableUsedAsObject(path, variableName) {
|
|
|
591
645
|
var _a;
|
|
592
646
|
const binding = path.scope.getBinding(variableName);
|
|
593
647
|
return ((_a = binding == null ? void 0 : binding.referencePaths) == null ? void 0 : _a.some((referencePath) => {
|
|
594
|
-
if (
|
|
648
|
+
if (t6.isMemberExpression(referencePath.parent)) {
|
|
595
649
|
const { object, property } = referencePath.parent;
|
|
596
|
-
if (
|
|
650
|
+
if (t6.isIdentifier(object, { name: variableName })) {
|
|
597
651
|
referencePath.parentPath.replaceWith(
|
|
598
|
-
|
|
652
|
+
t6.memberExpression(t6.memberExpression(object, t6.identifier("value")), property)
|
|
599
653
|
);
|
|
600
654
|
return true;
|
|
601
655
|
}
|
|
@@ -605,28 +659,30 @@ function isVariableUsedAsObject(path, variableName) {
|
|
|
605
659
|
}
|
|
606
660
|
|
|
607
661
|
// src/signal/props.ts
|
|
608
|
-
import { types as
|
|
662
|
+
import { types as t7 } from "@babel/core";
|
|
609
663
|
function replaceProps(path) {
|
|
610
664
|
var _a;
|
|
611
665
|
const state = path.state;
|
|
612
666
|
const firstParam = path.node.params[0];
|
|
613
|
-
if (!firstParam || !
|
|
667
|
+
if (!firstParam || !t7.isObjectPattern(firstParam)) return;
|
|
614
668
|
const returnStatement = path.get("body").get("body").find((statement) => statement.isReturnStatement());
|
|
615
|
-
if (!returnStatement || !
|
|
669
|
+
if (!returnStatement || !t7.isJSXElement((_a = returnStatement.node) == null ? void 0 : _a.argument)) return;
|
|
616
670
|
const replaceProperties = (properties2, parentPath) => {
|
|
617
671
|
properties2.forEach((property) => {
|
|
618
|
-
if (
|
|
672
|
+
if (t7.isObjectProperty(property) && t7.isIdentifier(property.key)) {
|
|
619
673
|
const keyName = property.key.name;
|
|
620
|
-
if (
|
|
674
|
+
if (t7.isIdentifier(property.value)) {
|
|
621
675
|
path.scope.rename(property.value.name, `${parentPath}${keyName}`);
|
|
622
|
-
} else if (
|
|
676
|
+
} else if (t7.isObjectPattern(property.value)) {
|
|
623
677
|
replaceProperties(property.value.properties, `${parentPath}${keyName}.`);
|
|
624
678
|
}
|
|
625
679
|
}
|
|
626
680
|
});
|
|
627
681
|
};
|
|
628
682
|
const properties = firstParam.properties;
|
|
629
|
-
const notRestProperties = properties.filter(
|
|
683
|
+
const notRestProperties = properties.filter(
|
|
684
|
+
(property) => !t7.isRestElement(property)
|
|
685
|
+
);
|
|
630
686
|
replaceProperties(notRestProperties, "__props.");
|
|
631
687
|
const notRestNames = notRestProperties.map((property) => property.key.name);
|
|
632
688
|
if (notRestNames.some((name) => startsWith(name, "$"))) {
|
|
@@ -636,12 +692,12 @@ function replaceProps(path) {
|
|
|
636
692
|
handleRestElement(path, state, properties, notRestNames);
|
|
637
693
|
}
|
|
638
694
|
function handleRestElement(path, state, properties, notRestNames) {
|
|
639
|
-
const restElement = properties.find((property) =>
|
|
640
|
-
path.node.params[0] =
|
|
695
|
+
const restElement = properties.find((property) => t7.isRestElement(property));
|
|
696
|
+
path.node.params[0] = t7.identifier("__props");
|
|
641
697
|
if (restElement) {
|
|
642
698
|
const restName = restElement.argument.name;
|
|
643
699
|
if (notRestNames.length === 0) {
|
|
644
|
-
path.node.params[0] =
|
|
700
|
+
path.node.params[0] = t7.identifier(restName);
|
|
645
701
|
} else {
|
|
646
702
|
const restVariableDeclaration = createRestVariableDeclaration(state, restName, notRestNames);
|
|
647
703
|
imports.add("useReactive");
|
|
@@ -650,12 +706,12 @@ function handleRestElement(path, state, properties, notRestNames) {
|
|
|
650
706
|
}
|
|
651
707
|
}
|
|
652
708
|
function createRestVariableDeclaration(state, restName, notRestNames) {
|
|
653
|
-
return
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
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)))
|
|
659
715
|
])
|
|
660
716
|
)
|
|
661
717
|
]);
|