babel-plugin-essor 0.0.8 → 0.0.10-beta.22
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/README.md +20 -6
- package/package.json +3 -3
- package/dist/index.cjs +0 -961
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -21
- package/dist/index.d.ts +0 -21
- package/dist/index.js +0 -938
- package/dist/index.js.map +0 -1
package/dist/index.cjs
DELETED
|
@@ -1,961 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
default: () => src_default
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
|
|
27
|
-
// ../shared/dist/essor-shared.js
|
|
28
|
-
var isArray = Array.isArray;
|
|
29
|
-
function isString(val) {
|
|
30
|
-
return typeof val === "string";
|
|
31
|
-
}
|
|
32
|
-
var noop = Function.prototype;
|
|
33
|
-
function startsWith(str, searchString) {
|
|
34
|
-
if (!isString(str)) {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
return str.indexOf(searchString) === 0;
|
|
38
|
-
}
|
|
39
|
-
var capitalizeFirstLetter = (inputString) => {
|
|
40
|
-
return inputString.charAt(0).toUpperCase() + inputString.slice(1);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
// src/jsx/server.ts
|
|
44
|
-
var import_core3 = require("@babel/core");
|
|
45
|
-
|
|
46
|
-
// src/program.ts
|
|
47
|
-
var import_core = require("@babel/core");
|
|
48
|
-
var imports = /* @__PURE__ */ new Set();
|
|
49
|
-
var transformProgram = {
|
|
50
|
-
enter(path, state) {
|
|
51
|
-
imports.clear();
|
|
52
|
-
path.state = {
|
|
53
|
-
h: path.scope.generateUidIdentifier("h$"),
|
|
54
|
-
renderTemplate: path.scope.generateUidIdentifier("renderTemplate$"),
|
|
55
|
-
template: path.scope.generateUidIdentifier("template$"),
|
|
56
|
-
useSignal: path.scope.generateUidIdentifier("signal$"),
|
|
57
|
-
useComputed: path.scope.generateUidIdentifier("computed$"),
|
|
58
|
-
useReactive: path.scope.generateUidIdentifier("reactive$"),
|
|
59
|
-
tmplDeclaration: import_core.types.variableDeclaration("const", []),
|
|
60
|
-
opts: state.opts
|
|
61
|
-
};
|
|
62
|
-
},
|
|
63
|
-
exit(path) {
|
|
64
|
-
const state = path.state;
|
|
65
|
-
if (state.tmplDeclaration.declarations.length > 0) {
|
|
66
|
-
const index = path.node.body.findIndex(
|
|
67
|
-
(node) => !import_core.types.isImportDeclaration(node) && !import_core.types.isExportDeclaration(node)
|
|
68
|
-
);
|
|
69
|
-
path.node.body.splice(index, 0, state.tmplDeclaration);
|
|
70
|
-
}
|
|
71
|
-
if (imports.size > 0) {
|
|
72
|
-
path.node.body.unshift(createImport(state, "essor"));
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
function createImport(state, from) {
|
|
77
|
-
const ImportSpecifier = [];
|
|
78
|
-
imports.forEach((name) => {
|
|
79
|
-
const local = import_core.types.identifier(state[name].name);
|
|
80
|
-
const imported = import_core.types.identifier(name);
|
|
81
|
-
ImportSpecifier.push(import_core.types.importSpecifier(local, imported));
|
|
82
|
-
});
|
|
83
|
-
const importSource = import_core.types.stringLiteral(from);
|
|
84
|
-
return import_core.types.importDeclaration(ImportSpecifier, importSource);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// src/jsx/constants.ts
|
|
88
|
-
var selfClosingTags = [
|
|
89
|
-
"area",
|
|
90
|
-
"base",
|
|
91
|
-
"br",
|
|
92
|
-
"col",
|
|
93
|
-
"embed",
|
|
94
|
-
"hr",
|
|
95
|
-
"img",
|
|
96
|
-
"input",
|
|
97
|
-
"link",
|
|
98
|
-
"meta",
|
|
99
|
-
"param",
|
|
100
|
-
"source",
|
|
101
|
-
"track",
|
|
102
|
-
"wbr"
|
|
103
|
-
];
|
|
104
|
-
var svgTags = [
|
|
105
|
-
"circle",
|
|
106
|
-
"clipPath",
|
|
107
|
-
"defs",
|
|
108
|
-
"ellipse",
|
|
109
|
-
"filter",
|
|
110
|
-
"g",
|
|
111
|
-
"line",
|
|
112
|
-
"linearGradient",
|
|
113
|
-
"mask",
|
|
114
|
-
"path",
|
|
115
|
-
"pattern",
|
|
116
|
-
"polygon",
|
|
117
|
-
"polyline",
|
|
118
|
-
"radialGradient",
|
|
119
|
-
"rect",
|
|
120
|
-
"stop",
|
|
121
|
-
"symbol",
|
|
122
|
-
"text",
|
|
123
|
-
"use"
|
|
124
|
-
];
|
|
125
|
-
|
|
126
|
-
// src/jsx/shared.ts
|
|
127
|
-
var import_core2 = require("@babel/core");
|
|
128
|
-
function hasSiblingElement(path) {
|
|
129
|
-
const siblings = path.getAllPrevSiblings().concat(path.getAllNextSiblings());
|
|
130
|
-
const hasSibling = siblings.some(
|
|
131
|
-
(siblingPath) => siblingPath.isJSXElement() || siblingPath.isJSXExpressionContainer()
|
|
132
|
-
);
|
|
133
|
-
return hasSibling;
|
|
134
|
-
}
|
|
135
|
-
function getAttrName(attribute) {
|
|
136
|
-
if (import_core2.types.isJSXIdentifier(attribute.name)) {
|
|
137
|
-
return attribute.name.name;
|
|
138
|
-
}
|
|
139
|
-
if (import_core2.types.isJSXNamespacedName(attribute.name)) {
|
|
140
|
-
return `${attribute.name.namespace.name}:${attribute.name.name.name}`;
|
|
141
|
-
}
|
|
142
|
-
throw new Error("Unsupported attribute type");
|
|
143
|
-
}
|
|
144
|
-
function getTagName(node) {
|
|
145
|
-
const tag = node.openingElement.name;
|
|
146
|
-
return jsxElementNameToString(tag);
|
|
147
|
-
}
|
|
148
|
-
function jsxElementNameToString(node) {
|
|
149
|
-
if (import_core2.types.isJSXMemberExpression(node)) {
|
|
150
|
-
return `${jsxElementNameToString(node.object)}.${jsxElementNameToString(node.property)}`;
|
|
151
|
-
}
|
|
152
|
-
if (import_core2.types.isJSXIdentifier(node) || import_core2.types.isIdentifier(node)) {
|
|
153
|
-
return node.name;
|
|
154
|
-
}
|
|
155
|
-
return `${node.namespace.name}:${node.name.name}`;
|
|
156
|
-
}
|
|
157
|
-
function isComponent(tagName) {
|
|
158
|
-
return tagName[0] && tagName[0].toLowerCase() !== tagName[0] || tagName.includes(".") || /[^a-z]/i.test(tagName[0]);
|
|
159
|
-
}
|
|
160
|
-
function isTextChild(path) {
|
|
161
|
-
if (path.isJSXExpressionContainer()) {
|
|
162
|
-
const expression = path.get("expression");
|
|
163
|
-
if (expression.isJSXText() || expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
164
|
-
return true;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (path.isJSXText() || path.isStringLiteral() || path.isNullLiteral()) {
|
|
168
|
-
return true;
|
|
169
|
-
}
|
|
170
|
-
return false;
|
|
171
|
-
}
|
|
172
|
-
function setNodeText(path, text) {
|
|
173
|
-
if (path.isJSXText()) {
|
|
174
|
-
path.node.value = text;
|
|
175
|
-
}
|
|
176
|
-
if (path.isJSXExpressionContainer()) {
|
|
177
|
-
const expression = path.get("expression");
|
|
178
|
-
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
179
|
-
expression.replaceWith(import_core2.types.stringLiteral(text));
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// src/jsx/server.ts
|
|
185
|
-
function transformJSXService(path) {
|
|
186
|
-
const result = {
|
|
187
|
-
index: 0,
|
|
188
|
-
isLastChild: false,
|
|
189
|
-
parentIndex: 0,
|
|
190
|
-
props: {},
|
|
191
|
-
template: []
|
|
192
|
-
// 修改为数组
|
|
193
|
-
};
|
|
194
|
-
transformJSXServiceElement(path, result, true);
|
|
195
|
-
path.replaceWith(createEssorNode(path, result));
|
|
196
|
-
}
|
|
197
|
-
function createEssorNode(path, result) {
|
|
198
|
-
var _a;
|
|
199
|
-
const state = path.state;
|
|
200
|
-
let tmpl;
|
|
201
|
-
if (path.isJSXElement() && isComponent(getTagName(path.node))) {
|
|
202
|
-
tmpl = import_core3.types.identifier(getTagName(path.node));
|
|
203
|
-
} else {
|
|
204
|
-
tmpl = path.scope.generateUidIdentifier("_tmpl$");
|
|
205
|
-
const template = import_core3.types.arrayExpression(result.template.map(import_core3.types.stringLiteral));
|
|
206
|
-
const declarator = import_core3.types.variableDeclarator(tmpl, template);
|
|
207
|
-
state.tmplDeclaration.declarations.push(declarator);
|
|
208
|
-
}
|
|
209
|
-
const args = [tmpl, createProps(result.props)];
|
|
210
|
-
const key = result.props.key || ((_a = result.props[0]) == null ? void 0 : _a.key);
|
|
211
|
-
if (key) {
|
|
212
|
-
args.push(key);
|
|
213
|
-
}
|
|
214
|
-
imports.add("renderTemplate");
|
|
215
|
-
return import_core3.types.callExpression(state.renderTemplate, args);
|
|
216
|
-
}
|
|
217
|
-
function createProps(props) {
|
|
218
|
-
const result = [];
|
|
219
|
-
for (const prop in props) {
|
|
220
|
-
let value = props[prop];
|
|
221
|
-
if (prop === "key") {
|
|
222
|
-
continue;
|
|
223
|
-
}
|
|
224
|
-
if (Array.isArray(value)) {
|
|
225
|
-
value = import_core3.types.arrayExpression(value);
|
|
226
|
-
}
|
|
227
|
-
if (typeof value === "object" && value !== null && !import_core3.types.isNode(value)) {
|
|
228
|
-
value = createProps(value);
|
|
229
|
-
}
|
|
230
|
-
if (typeof value === "string") {
|
|
231
|
-
value = import_core3.types.stringLiteral(value);
|
|
232
|
-
}
|
|
233
|
-
if (typeof value === "number") {
|
|
234
|
-
value = import_core3.types.numericLiteral(value);
|
|
235
|
-
}
|
|
236
|
-
if (typeof value === "boolean") {
|
|
237
|
-
value = import_core3.types.booleanLiteral(value);
|
|
238
|
-
}
|
|
239
|
-
if (value === void 0) {
|
|
240
|
-
value = import_core3.types.tsUndefinedKeyword();
|
|
241
|
-
}
|
|
242
|
-
if (value === null) {
|
|
243
|
-
value = import_core3.types.nullLiteral();
|
|
244
|
-
}
|
|
245
|
-
if (prop === "_$spread$") {
|
|
246
|
-
result.push(import_core3.types.spreadElement(value));
|
|
247
|
-
} else {
|
|
248
|
-
result.push(import_core3.types.objectProperty(import_core3.types.stringLiteral(prop), value));
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
return import_core3.types.objectExpression(result);
|
|
252
|
-
}
|
|
253
|
-
function transformJSXServiceElement(path, result, isRoot = false) {
|
|
254
|
-
if (path.isJSXElement()) {
|
|
255
|
-
const tagName = getTagName(path.node);
|
|
256
|
-
const tagIsComponent = isComponent(tagName);
|
|
257
|
-
const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
|
|
258
|
-
const isSvg = svgTags.includes(tagName) && result.index === 1;
|
|
259
|
-
const { props, hasExpression } = getAttrProps(path);
|
|
260
|
-
if (tagIsComponent) {
|
|
261
|
-
if (isRoot) {
|
|
262
|
-
result.props = props;
|
|
263
|
-
const children = getChildren(path);
|
|
264
|
-
if (children.length > 0) {
|
|
265
|
-
const childrenGenerator = children.length === 1 ? children[0] : import_core3.types.arrayExpression(children);
|
|
266
|
-
result.props.children = childrenGenerator;
|
|
267
|
-
}
|
|
268
|
-
} else {
|
|
269
|
-
transformJSXService(path);
|
|
270
|
-
replaceChild(path.node, result);
|
|
271
|
-
}
|
|
272
|
-
} else {
|
|
273
|
-
if (isSvg) {
|
|
274
|
-
result.template.push("<svg _svg_>");
|
|
275
|
-
}
|
|
276
|
-
result.template.push(`<${tagName}`);
|
|
277
|
-
handleAttributes(props, result);
|
|
278
|
-
if (hasExpression) {
|
|
279
|
-
result.template.push(isSelfClose ? "/>" : ">");
|
|
280
|
-
result.props || (result.props = {});
|
|
281
|
-
} else {
|
|
282
|
-
result.template[result.template.length - 1] += isSelfClose ? "/>" : ">";
|
|
283
|
-
}
|
|
284
|
-
transformChildren(path, result);
|
|
285
|
-
if (!isSelfClose) {
|
|
286
|
-
result.template.push(`</${tagName}>`);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
} else {
|
|
290
|
-
result.index--;
|
|
291
|
-
transformChildren(path, result);
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
function transformChildren(path, result) {
|
|
295
|
-
const parentIndex = result.template.length;
|
|
296
|
-
path.get("children").reduce((pre, cur) => {
|
|
297
|
-
if (isValidChild(cur)) {
|
|
298
|
-
const lastChild = pre.at(-1);
|
|
299
|
-
if (lastChild && isTextChild(cur) && isTextChild(lastChild)) {
|
|
300
|
-
setNodeText(lastChild, getNodeText(lastChild) + getNodeText(cur));
|
|
301
|
-
} else {
|
|
302
|
-
pre.push(cur);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
return pre;
|
|
306
|
-
}, []).forEach((child, i, arr) => {
|
|
307
|
-
result.parentIndex = parentIndex;
|
|
308
|
-
result.isLastChild = i === arr.length - 1;
|
|
309
|
-
transformChild(child, result);
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
function transformChild(child, result) {
|
|
313
|
-
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
314
|
-
transformJSXServiceElement(child, result, false);
|
|
315
|
-
} else if (child.isJSXExpressionContainer()) {
|
|
316
|
-
const expression = child.get("expression");
|
|
317
|
-
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
318
|
-
result.template[result.template.length - 1] += String(expression.node.value);
|
|
319
|
-
} else if (expression.isExpression()) {
|
|
320
|
-
replaceChild(expression.node, result);
|
|
321
|
-
} else if (import_core3.types.isJSXEmptyExpression(expression.node)) {
|
|
322
|
-
} else {
|
|
323
|
-
throw new Error("Unsupported child type");
|
|
324
|
-
}
|
|
325
|
-
} else if (child.isJSXText()) {
|
|
326
|
-
result.template.push(String(child.node.value));
|
|
327
|
-
} else {
|
|
328
|
-
throw new Error("Unsupported child type");
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
function getNodeText(path) {
|
|
332
|
-
if (path.isJSXText()) {
|
|
333
|
-
return path.node.value;
|
|
334
|
-
}
|
|
335
|
-
if (path.isJSXExpressionContainer()) {
|
|
336
|
-
const expression = path.get("expression");
|
|
337
|
-
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
338
|
-
return String(expression.node.value);
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
return "";
|
|
342
|
-
}
|
|
343
|
-
function handleAttributes(props, result) {
|
|
344
|
-
let klass = "";
|
|
345
|
-
let style = "";
|
|
346
|
-
for (const prop in props) {
|
|
347
|
-
const value = props[prop];
|
|
348
|
-
if (prop === "class" && typeof value === "string") {
|
|
349
|
-
klass += ` ${value}`;
|
|
350
|
-
delete props[prop];
|
|
351
|
-
continue;
|
|
352
|
-
}
|
|
353
|
-
if (prop === "style" && typeof value === "string") {
|
|
354
|
-
style += `${value}${value.at(-1) === ";" ? "" : ";"}`;
|
|
355
|
-
delete props[prop];
|
|
356
|
-
continue;
|
|
357
|
-
}
|
|
358
|
-
if (value === true) {
|
|
359
|
-
result.template[result.template.length - 1] += ` ${prop}`;
|
|
360
|
-
delete props[prop];
|
|
361
|
-
}
|
|
362
|
-
if (value === false) {
|
|
363
|
-
delete props[prop];
|
|
364
|
-
}
|
|
365
|
-
if (typeof value === "string" || typeof value === "number") {
|
|
366
|
-
result.template[result.template.length - 1] += ` ${prop}="${value}"`;
|
|
367
|
-
delete props[prop];
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
if (Object.keys(props).length > 0) {
|
|
371
|
-
result.props[result.index] = props;
|
|
372
|
-
}
|
|
373
|
-
klass = klass.trim();
|
|
374
|
-
style = style.trim();
|
|
375
|
-
if (klass) {
|
|
376
|
-
result.template[result.template.length - 1] += ` class="${klass}"`;
|
|
377
|
-
}
|
|
378
|
-
if (style) {
|
|
379
|
-
result.template[result.template.length - 1] += ` style="${style}"`;
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
function replaceChild(node, result) {
|
|
383
|
-
var _a, _b, _c, _d, _e;
|
|
384
|
-
if (result.isLastChild) {
|
|
385
|
-
result.index--;
|
|
386
|
-
}
|
|
387
|
-
(_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
|
|
388
|
-
(_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
|
|
389
|
-
result.props[result.parentIndex].children.push(
|
|
390
|
-
import_core3.types.arrayExpression([
|
|
391
|
-
import_core3.types.arrowFunctionExpression([], node),
|
|
392
|
-
import_core3.types.identifier(String(result.template.length))
|
|
393
|
-
])
|
|
394
|
-
);
|
|
395
|
-
}
|
|
396
|
-
function getChildren(path) {
|
|
397
|
-
return path.get("children").filter((child) => isValidChild(child)).map((child) => {
|
|
398
|
-
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
399
|
-
transformJSXService(child);
|
|
400
|
-
} else if (child.isJSXExpressionContainer()) {
|
|
401
|
-
child.replaceWith(child.get("expression"));
|
|
402
|
-
} else if (child.isJSXText()) {
|
|
403
|
-
child.replaceWith(import_core3.types.stringLiteral(child.node.value));
|
|
404
|
-
} else {
|
|
405
|
-
throw new Error("Unsupported child type");
|
|
406
|
-
}
|
|
407
|
-
return child.node;
|
|
408
|
-
});
|
|
409
|
-
}
|
|
410
|
-
function isValidChild(path) {
|
|
411
|
-
const regex = /^\s*$/;
|
|
412
|
-
if (path.isStringLiteral() || path.isJSXText()) {
|
|
413
|
-
return !regex.test(path.node.value);
|
|
414
|
-
}
|
|
415
|
-
return Object.keys(path.node).length > 0;
|
|
416
|
-
}
|
|
417
|
-
function getAttrProps(path) {
|
|
418
|
-
const props = {};
|
|
419
|
-
let hasExpression = false;
|
|
420
|
-
path.get("openingElement").get("attributes").forEach((attribute) => {
|
|
421
|
-
if (attribute.isJSXAttribute()) {
|
|
422
|
-
const name = getAttrName(attribute.node);
|
|
423
|
-
const value = attribute.get("value");
|
|
424
|
-
if (!value.node) {
|
|
425
|
-
props[name] = true;
|
|
426
|
-
} else if (value.isStringLiteral()) {
|
|
427
|
-
props[name] = value.node.value;
|
|
428
|
-
} else {
|
|
429
|
-
if (value.isJSXExpressionContainer()) {
|
|
430
|
-
const expression = value.get("expression");
|
|
431
|
-
if (expression.isStringLiteral()) {
|
|
432
|
-
props[name] = expression.node.value;
|
|
433
|
-
} else if (expression.isNumericLiteral()) {
|
|
434
|
-
props[name] = expression.node.value;
|
|
435
|
-
} else if (expression.isJSXElement() || expression.isJSXFragment()) {
|
|
436
|
-
transformJSXService(expression);
|
|
437
|
-
props[name] = expression.node;
|
|
438
|
-
} else if (expression.isExpression()) {
|
|
439
|
-
hasExpression = true;
|
|
440
|
-
if (/^key|ref|on.+$/.test(name)) {
|
|
441
|
-
props[name] = expression.node;
|
|
442
|
-
} else if (/^bind:.+/.test(name)) {
|
|
443
|
-
const value2 = path.scope.generateUidIdentifier("value");
|
|
444
|
-
const bindName = name.slice(5).toLocaleLowerCase();
|
|
445
|
-
props[bindName] = expression.node;
|
|
446
|
-
props[`update${capitalizeFirstLetter(bindName)}`] = import_core3.types.arrowFunctionExpression(
|
|
447
|
-
[value2],
|
|
448
|
-
import_core3.types.assignmentExpression("=", expression.node, value2)
|
|
449
|
-
);
|
|
450
|
-
} else {
|
|
451
|
-
if (expression.isConditionalExpression()) {
|
|
452
|
-
props[name] = import_core3.types.arrowFunctionExpression([], expression.node);
|
|
453
|
-
} else {
|
|
454
|
-
props[name] = expression.node;
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
} else if (value.isJSXElement() || value.isJSXFragment()) {
|
|
459
|
-
transformJSXService(value);
|
|
460
|
-
props[name] = value.node;
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
} else if (attribute.isJSXSpreadAttribute()) {
|
|
464
|
-
props._$spread$ = attribute.get("argument").node;
|
|
465
|
-
hasExpression = true;
|
|
466
|
-
} else {
|
|
467
|
-
throw new Error("Unsupported attribute type");
|
|
468
|
-
}
|
|
469
|
-
});
|
|
470
|
-
return {
|
|
471
|
-
props,
|
|
472
|
-
hasExpression
|
|
473
|
-
};
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
// src/jsx/client.ts
|
|
477
|
-
var import_core4 = require("@babel/core");
|
|
478
|
-
function transformJSXClient(path) {
|
|
479
|
-
const result = {
|
|
480
|
-
index: 1,
|
|
481
|
-
isLastChild: false,
|
|
482
|
-
parentIndex: 0,
|
|
483
|
-
props: {},
|
|
484
|
-
template: ""
|
|
485
|
-
};
|
|
486
|
-
transformJSXElement(path, result, true);
|
|
487
|
-
path.replaceWith(createEssorNode2(path, result));
|
|
488
|
-
}
|
|
489
|
-
function createEssorNode2(path, result) {
|
|
490
|
-
var _a;
|
|
491
|
-
const state = path.state;
|
|
492
|
-
let tmpl;
|
|
493
|
-
if (path.isJSXElement() && isComponent(getTagName(path.node))) {
|
|
494
|
-
tmpl = import_core4.types.identifier(getTagName(path.node));
|
|
495
|
-
} else {
|
|
496
|
-
tmpl = path.scope.generateUidIdentifier("_tmpl$");
|
|
497
|
-
const template = import_core4.types.callExpression(state.template, [import_core4.types.stringLiteral(result.template)]);
|
|
498
|
-
const declarator = import_core4.types.variableDeclarator(tmpl, template);
|
|
499
|
-
state.tmplDeclaration.declarations.push(declarator);
|
|
500
|
-
imports.add("template");
|
|
501
|
-
}
|
|
502
|
-
const args = [tmpl, createProps2(result.props)];
|
|
503
|
-
const key = result.props.key || ((_a = result.props[0]) == null ? void 0 : _a.key);
|
|
504
|
-
if (key) {
|
|
505
|
-
args.push(key);
|
|
506
|
-
}
|
|
507
|
-
imports.add("h");
|
|
508
|
-
return import_core4.types.callExpression(state.h, args);
|
|
509
|
-
}
|
|
510
|
-
function createProps2(props) {
|
|
511
|
-
const toAstNode = (value) => {
|
|
512
|
-
if (Array.isArray(value)) {
|
|
513
|
-
return import_core4.types.arrayExpression(value.map(toAstNode));
|
|
514
|
-
}
|
|
515
|
-
if (value && typeof value === "object" && !import_core4.types.isNode(value)) {
|
|
516
|
-
return createProps2(value);
|
|
517
|
-
}
|
|
518
|
-
switch (typeof value) {
|
|
519
|
-
case "string":
|
|
520
|
-
return import_core4.types.stringLiteral(value);
|
|
521
|
-
case "number":
|
|
522
|
-
return import_core4.types.numericLiteral(value);
|
|
523
|
-
case "boolean":
|
|
524
|
-
return import_core4.types.booleanLiteral(value);
|
|
525
|
-
case "undefined":
|
|
526
|
-
return import_core4.types.tsUndefinedKeyword();
|
|
527
|
-
case void 0:
|
|
528
|
-
return import_core4.types.tsUndefinedKeyword();
|
|
529
|
-
case null:
|
|
530
|
-
return import_core4.types.nullLiteral();
|
|
531
|
-
default:
|
|
532
|
-
return value;
|
|
533
|
-
}
|
|
534
|
-
};
|
|
535
|
-
const result = Object.keys(props).filter((prop) => prop !== "key").map((prop) => {
|
|
536
|
-
const value = toAstNode(props[prop]);
|
|
537
|
-
return prop === "_$spread$" ? import_core4.types.spreadElement(value) : import_core4.types.objectProperty(import_core4.types.stringLiteral(prop), value);
|
|
538
|
-
});
|
|
539
|
-
return import_core4.types.objectExpression(result);
|
|
540
|
-
}
|
|
541
|
-
function transformJSXElement(path, result, isRoot = false) {
|
|
542
|
-
if (path.isJSXElement()) {
|
|
543
|
-
const tagName = getTagName(path.node);
|
|
544
|
-
const tagIsComponent = isComponent(tagName);
|
|
545
|
-
const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
|
|
546
|
-
const isSvg = svgTags.includes(tagName) && result.index === 1;
|
|
547
|
-
const props = getAttrProps2(path);
|
|
548
|
-
if (tagIsComponent) {
|
|
549
|
-
if (isRoot) {
|
|
550
|
-
result.props = props;
|
|
551
|
-
const children = getChildren2(path);
|
|
552
|
-
if (children.length > 0) {
|
|
553
|
-
const childrenGenerator = children.length === 1 ? children[0] : import_core4.types.arrayExpression(children);
|
|
554
|
-
result.props.children = childrenGenerator;
|
|
555
|
-
}
|
|
556
|
-
} else {
|
|
557
|
-
transformJSXClient(path);
|
|
558
|
-
replaceChild2(path.node, result);
|
|
559
|
-
}
|
|
560
|
-
} else {
|
|
561
|
-
if (isSvg) {
|
|
562
|
-
result.template = "<svg _svg_>";
|
|
563
|
-
}
|
|
564
|
-
result.template += `<${tagName}`;
|
|
565
|
-
handleAttributes2(props, result);
|
|
566
|
-
result.template += isSelfClose ? "/>" : ">";
|
|
567
|
-
if (!isSelfClose) {
|
|
568
|
-
transformChildren2(path, result);
|
|
569
|
-
if (hasSiblingElement(path)) {
|
|
570
|
-
result.template += `</${tagName}>`;
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
} else {
|
|
575
|
-
result.index--;
|
|
576
|
-
transformChildren2(path, result);
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
function transformChildren2(path, result) {
|
|
580
|
-
const parentIndex = result.index;
|
|
581
|
-
path.get("children").reduce((pre, cur) => {
|
|
582
|
-
if (isValidChild2(cur)) {
|
|
583
|
-
const lastChild = pre.at(-1);
|
|
584
|
-
if (lastChild && isTextChild(cur) && isTextChild(lastChild)) {
|
|
585
|
-
setNodeText(lastChild, getNodeText2(lastChild) + getNodeText2(cur));
|
|
586
|
-
} else {
|
|
587
|
-
pre.push(cur);
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
return pre;
|
|
591
|
-
}, []).forEach((child, i, arr) => {
|
|
592
|
-
result.parentIndex = parentIndex;
|
|
593
|
-
result.isLastChild = i === arr.length - 1;
|
|
594
|
-
transformChild2(child, result);
|
|
595
|
-
});
|
|
596
|
-
}
|
|
597
|
-
function transformChild2(child, result) {
|
|
598
|
-
result.index++;
|
|
599
|
-
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
600
|
-
transformJSXElement(child, result, false);
|
|
601
|
-
} else if (child.isJSXExpressionContainer()) {
|
|
602
|
-
const expression = child.get("expression");
|
|
603
|
-
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
604
|
-
result.template += String(expression.node.value);
|
|
605
|
-
} else if (expression.isExpression()) {
|
|
606
|
-
replaceChild2(expression.node, result);
|
|
607
|
-
} else if (import_core4.types.isJSXEmptyExpression(expression.node)) {
|
|
608
|
-
} else {
|
|
609
|
-
throw new Error("Unsupported child type");
|
|
610
|
-
}
|
|
611
|
-
} else if (child.isJSXText()) {
|
|
612
|
-
result.template += String(child.node.value);
|
|
613
|
-
} else {
|
|
614
|
-
throw new Error("Unsupported child type");
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
function getNodeText2(path) {
|
|
618
|
-
if (path.isJSXText()) {
|
|
619
|
-
return path.node.value;
|
|
620
|
-
}
|
|
621
|
-
if (path.isJSXExpressionContainer()) {
|
|
622
|
-
const expression = path.get("expression");
|
|
623
|
-
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
624
|
-
return String(expression.node.value);
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
return "";
|
|
628
|
-
}
|
|
629
|
-
function handleAttributes2(props, result) {
|
|
630
|
-
let klass = "";
|
|
631
|
-
let style = "";
|
|
632
|
-
for (const prop in props) {
|
|
633
|
-
let value = props[prop];
|
|
634
|
-
if (prop === "class" && typeof value === "string") {
|
|
635
|
-
klass += ` ${value}`;
|
|
636
|
-
delete props[prop];
|
|
637
|
-
continue;
|
|
638
|
-
}
|
|
639
|
-
if (prop === "style" && typeof value === "string") {
|
|
640
|
-
style += `${value}${value.at(-1) === ";" ? "" : ";"}`;
|
|
641
|
-
delete props[prop];
|
|
642
|
-
continue;
|
|
643
|
-
}
|
|
644
|
-
if (value === true) {
|
|
645
|
-
result.template += ` ${prop}`;
|
|
646
|
-
delete props[prop];
|
|
647
|
-
}
|
|
648
|
-
if (value === false) {
|
|
649
|
-
delete props[prop];
|
|
650
|
-
}
|
|
651
|
-
if (typeof value === "string" || typeof value === "number") {
|
|
652
|
-
result.template += ` ${prop}="${value}"`;
|
|
653
|
-
delete props[prop];
|
|
654
|
-
}
|
|
655
|
-
if (import_core4.types.isConditionalExpression(value)) {
|
|
656
|
-
const { test, consequent, alternate } = value;
|
|
657
|
-
value = import_core4.types.arrowFunctionExpression([], import_core4.types.conditionalExpression(test, consequent, alternate));
|
|
658
|
-
props[prop] = value;
|
|
659
|
-
}
|
|
660
|
-
if (import_core4.types.isObjectExpression(value)) {
|
|
661
|
-
let hasConditional = false;
|
|
662
|
-
value.properties.forEach((property) => {
|
|
663
|
-
if (import_core4.types.isObjectProperty(property) && import_core4.types.isConditionalExpression(property.value)) {
|
|
664
|
-
hasConditional = true;
|
|
665
|
-
}
|
|
666
|
-
});
|
|
667
|
-
if (hasConditional) {
|
|
668
|
-
value = import_core4.types.arrowFunctionExpression([], value);
|
|
669
|
-
}
|
|
670
|
-
props[prop] = value;
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
if (Object.keys(props).length > 0) {
|
|
674
|
-
result.props[result.index] = props;
|
|
675
|
-
}
|
|
676
|
-
klass = klass.trim();
|
|
677
|
-
style = style.trim();
|
|
678
|
-
if (klass) {
|
|
679
|
-
result.template += ` class="${klass}"`;
|
|
680
|
-
}
|
|
681
|
-
if (style) {
|
|
682
|
-
result.template += ` style="${style}"`;
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
function replaceChild2(node, result) {
|
|
686
|
-
var _a, _b, _c, _d, _e;
|
|
687
|
-
if (result.isLastChild) {
|
|
688
|
-
result.index--;
|
|
689
|
-
} else {
|
|
690
|
-
result.template += "<!>";
|
|
691
|
-
}
|
|
692
|
-
(_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
|
|
693
|
-
(_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
|
|
694
|
-
result.props[result.parentIndex].children.push(
|
|
695
|
-
import_core4.types.arrayExpression([
|
|
696
|
-
import_core4.types.arrowFunctionExpression([], node),
|
|
697
|
-
result.isLastChild ? import_core4.types.nullLiteral() : import_core4.types.identifier(String(result.index))
|
|
698
|
-
])
|
|
699
|
-
);
|
|
700
|
-
}
|
|
701
|
-
function getChildren2(path) {
|
|
702
|
-
return path.get("children").filter((child) => isValidChild2(child)).map((child) => {
|
|
703
|
-
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
704
|
-
transformJSXClient(child);
|
|
705
|
-
} else if (child.isJSXExpressionContainer()) {
|
|
706
|
-
child.replaceWith(child.get("expression"));
|
|
707
|
-
} else if (child.isJSXText()) {
|
|
708
|
-
child.replaceWith(import_core4.types.stringLiteral(child.node.value));
|
|
709
|
-
} else {
|
|
710
|
-
throw new Error("Unsupported child type");
|
|
711
|
-
}
|
|
712
|
-
return child.node;
|
|
713
|
-
});
|
|
714
|
-
}
|
|
715
|
-
function isValidChild2(path) {
|
|
716
|
-
const regex = /^\s*$/;
|
|
717
|
-
if (path.isStringLiteral() || path.isJSXText()) {
|
|
718
|
-
return !regex.test(path.node.value);
|
|
719
|
-
}
|
|
720
|
-
return Object.keys(path.node).length > 0;
|
|
721
|
-
}
|
|
722
|
-
function getAttrProps2(path) {
|
|
723
|
-
const props = {};
|
|
724
|
-
path.get("openingElement").get("attributes").forEach((attribute) => {
|
|
725
|
-
if (attribute.isJSXAttribute()) {
|
|
726
|
-
const name = getAttrName(attribute.node);
|
|
727
|
-
const value = attribute.get("value");
|
|
728
|
-
if (!value.node) {
|
|
729
|
-
props[name] = true;
|
|
730
|
-
} else if (value.isStringLiteral()) {
|
|
731
|
-
props[name] = value.node.value;
|
|
732
|
-
} else {
|
|
733
|
-
if (value.isJSXExpressionContainer()) {
|
|
734
|
-
const expression = value.get("expression");
|
|
735
|
-
if (expression.isStringLiteral()) {
|
|
736
|
-
props[name] = expression.node.value;
|
|
737
|
-
} else if (expression.isNumericLiteral()) {
|
|
738
|
-
props[name] = expression.node.value;
|
|
739
|
-
} else if (expression.isJSXElement() || expression.isJSXFragment()) {
|
|
740
|
-
transformJSXClient(expression);
|
|
741
|
-
props[name] = expression.node;
|
|
742
|
-
} else if (expression.isExpression()) {
|
|
743
|
-
if (/^key|ref|on.+$/.test(name)) {
|
|
744
|
-
props[name] = expression.node;
|
|
745
|
-
} else if (/^bind:.+/.test(name)) {
|
|
746
|
-
const value2 = path.scope.generateUidIdentifier("value");
|
|
747
|
-
const bindName = name.slice(5).toLocaleLowerCase();
|
|
748
|
-
props[bindName] = expression.node;
|
|
749
|
-
props[`update${capitalizeFirstLetter(bindName)}`] = import_core4.types.arrowFunctionExpression(
|
|
750
|
-
[value2],
|
|
751
|
-
import_core4.types.assignmentExpression("=", expression.node, value2)
|
|
752
|
-
);
|
|
753
|
-
} else {
|
|
754
|
-
if (expression.isConditionalExpression()) {
|
|
755
|
-
props[name] = import_core4.types.arrowFunctionExpression([], expression.node);
|
|
756
|
-
} else {
|
|
757
|
-
props[name] = expression.node;
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
}
|
|
761
|
-
} else if (value.isJSXElement() || value.isJSXFragment()) {
|
|
762
|
-
transformJSXClient(value);
|
|
763
|
-
props[name] = value.node;
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
} else if (attribute.isJSXSpreadAttribute()) {
|
|
767
|
-
props._$spread$ = attribute.get("argument").node;
|
|
768
|
-
} else {
|
|
769
|
-
throw new Error("Unsupported attribute type");
|
|
770
|
-
}
|
|
771
|
-
});
|
|
772
|
-
return props;
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
// src/jsx/index.ts
|
|
776
|
-
function transformJSX(path) {
|
|
777
|
-
const state = path.state;
|
|
778
|
-
const isSsg = state.opts.ssg;
|
|
779
|
-
return isSsg ? transformJSXService(path) : transformJSXClient(path);
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
// src/signal/symbol.ts
|
|
783
|
-
var import_core5 = require("@babel/core");
|
|
784
|
-
function replaceSymbol(path) {
|
|
785
|
-
const init = path.node.init;
|
|
786
|
-
const variableName = path.node.id.name;
|
|
787
|
-
if (import_core5.types.isObjectPattern(path.node.id) || import_core5.types.isArrayPattern(path.node.id)) {
|
|
788
|
-
return;
|
|
789
|
-
}
|
|
790
|
-
if (!startsWith(variableName, "$")) {
|
|
791
|
-
return;
|
|
792
|
-
}
|
|
793
|
-
if (init && (import_core5.types.isFunctionExpression(init) || import_core5.types.isArrowFunctionExpression(init)) && path.parent.kind === "const") {
|
|
794
|
-
const newInit = import_core5.types.callExpression(import_core5.types.identifier(path.state.useComputed.name), init ? [init] : []);
|
|
795
|
-
imports.add("useComputed");
|
|
796
|
-
path.node.init = newInit;
|
|
797
|
-
} else {
|
|
798
|
-
const newInit = import_core5.types.callExpression(import_core5.types.identifier(path.state.useSignal.name), init ? [init] : []);
|
|
799
|
-
imports.add("useSignal");
|
|
800
|
-
path.node.init = newInit;
|
|
801
|
-
}
|
|
802
|
-
}
|
|
803
|
-
function symbolIdentifier(path) {
|
|
804
|
-
const parentPath = path.parentPath;
|
|
805
|
-
if (!parentPath || import_core5.types.isVariableDeclarator(parentPath) || import_core5.types.isImportSpecifier(parentPath) || import_core5.types.isObjectProperty(parentPath) || import_core5.types.isArrayPattern(parentPath) || import_core5.types.isObjectPattern(parentPath)) {
|
|
806
|
-
return;
|
|
807
|
-
}
|
|
808
|
-
const { node } = path;
|
|
809
|
-
if (node.name.startsWith("$")) {
|
|
810
|
-
let currentPath = path;
|
|
811
|
-
while (currentPath.parentPath && !currentPath.parentPath.isProgram()) {
|
|
812
|
-
if (currentPath.parentPath.isMemberExpression() && currentPath.parentPath.node.property.name === "value") {
|
|
813
|
-
return;
|
|
814
|
-
}
|
|
815
|
-
currentPath = currentPath.parentPath;
|
|
816
|
-
}
|
|
817
|
-
const newNode = import_core5.types.memberExpression(import_core5.types.identifier(node.name), import_core5.types.identifier("value"));
|
|
818
|
-
path.replaceWith(newNode);
|
|
819
|
-
}
|
|
820
|
-
}
|
|
821
|
-
function symbolObjectPattern(path) {
|
|
822
|
-
path.node.properties.forEach((property) => {
|
|
823
|
-
if (import_core5.types.isObjectProperty(property) && import_core5.types.isIdentifier(property.key) && property.key.name.startsWith("$")) {
|
|
824
|
-
const newKey = import_core5.types.identifier(property.key.name);
|
|
825
|
-
property.key = newKey;
|
|
826
|
-
}
|
|
827
|
-
});
|
|
828
|
-
}
|
|
829
|
-
function symbolArrayPattern(path) {
|
|
830
|
-
path.node.elements.forEach((element) => {
|
|
831
|
-
if (import_core5.types.isIdentifier(element) && element.name.startsWith("$")) {
|
|
832
|
-
const newElement = import_core5.types.identifier(element.name);
|
|
833
|
-
element.name = newElement.name;
|
|
834
|
-
} else if (import_core5.types.isObjectPattern(element)) {
|
|
835
|
-
element.properties.forEach((property) => {
|
|
836
|
-
if (import_core5.types.isObjectProperty(property) && import_core5.types.isIdentifier(property.key) && property.key.name.startsWith("$")) {
|
|
837
|
-
const newKey = import_core5.types.identifier(property.key.name);
|
|
838
|
-
property.key = newKey;
|
|
839
|
-
}
|
|
840
|
-
});
|
|
841
|
-
}
|
|
842
|
-
});
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
// src/signal/import.ts
|
|
846
|
-
var import_core6 = require("@babel/core");
|
|
847
|
-
function isVariableUsedAsObject(path, variableName) {
|
|
848
|
-
const binding = path.scope.getBinding(variableName);
|
|
849
|
-
let isUsedObject = false;
|
|
850
|
-
if (!binding || !binding.referencePaths) {
|
|
851
|
-
return isUsedObject;
|
|
852
|
-
}
|
|
853
|
-
for (const referencePath of binding.referencePaths) {
|
|
854
|
-
if (import_core6.types.isMemberExpression(referencePath.parent)) {
|
|
855
|
-
isUsedObject = true;
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
return isUsedObject;
|
|
859
|
-
}
|
|
860
|
-
function replaceImportDeclaration(path) {
|
|
861
|
-
const imports2 = path.node.specifiers;
|
|
862
|
-
imports2.forEach((specifier) => {
|
|
863
|
-
const variableName = specifier.local.name;
|
|
864
|
-
if (startsWith(variableName, "$") && !isVariableUsedAsObject(path, variableName)) {
|
|
865
|
-
path.scope.rename(variableName, `${variableName}.value`);
|
|
866
|
-
specifier.local.name = `${variableName}`;
|
|
867
|
-
}
|
|
868
|
-
});
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
// src/signal/props.ts
|
|
872
|
-
var import_core7 = require("@babel/core");
|
|
873
|
-
function replaceProps(path) {
|
|
874
|
-
var _a;
|
|
875
|
-
const state = path.state;
|
|
876
|
-
const firstParam = path.node.params[0];
|
|
877
|
-
if (!firstParam || !import_core7.types.isObjectPattern(firstParam)) {
|
|
878
|
-
return;
|
|
879
|
-
}
|
|
880
|
-
const returnStatement = path.get("body").get("body").find((statement) => statement.isReturnStatement());
|
|
881
|
-
if (!returnStatement) {
|
|
882
|
-
return;
|
|
883
|
-
}
|
|
884
|
-
const returnValue = (_a = returnStatement.node) == null ? void 0 : _a.argument;
|
|
885
|
-
if (!import_core7.types.isJSXElement(returnValue)) {
|
|
886
|
-
return;
|
|
887
|
-
}
|
|
888
|
-
function replaceProperties(properties2, parentPath) {
|
|
889
|
-
properties2.forEach((property) => {
|
|
890
|
-
if (import_core7.types.isObjectProperty(property)) {
|
|
891
|
-
const keyName = property.key.name;
|
|
892
|
-
if (import_core7.types.isIdentifier(property.value)) {
|
|
893
|
-
const propertyName = property.value.name;
|
|
894
|
-
const newName = `${parentPath}${keyName}`;
|
|
895
|
-
path.scope.rename(propertyName, newName);
|
|
896
|
-
} else if (import_core7.types.isObjectPattern(property.value)) {
|
|
897
|
-
replaceProperties(property.value.properties, `${parentPath}${keyName}.`);
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
});
|
|
901
|
-
}
|
|
902
|
-
const properties = firstParam.properties;
|
|
903
|
-
replaceProperties(
|
|
904
|
-
properties.filter((property) => !import_core7.types.isRestElement(property)),
|
|
905
|
-
"__props."
|
|
906
|
-
);
|
|
907
|
-
const notRestProperties = properties.filter((property) => !import_core7.types.isRestElement(property));
|
|
908
|
-
const notRestNames = notRestProperties.map(
|
|
909
|
-
(property) => property.key.name
|
|
910
|
-
);
|
|
911
|
-
if (notRestNames.some((name) => startsWith(name, "$"))) {
|
|
912
|
-
console.warn("props name can not start with $");
|
|
913
|
-
return;
|
|
914
|
-
}
|
|
915
|
-
const restElement = properties.find((property) => import_core7.types.isRestElement(property));
|
|
916
|
-
path.node.params[0] = import_core7.types.identifier("__props");
|
|
917
|
-
if (restElement) {
|
|
918
|
-
const restName = restElement.argument.name;
|
|
919
|
-
if (notRestProperties.length === 0) {
|
|
920
|
-
path.node.params[0] = import_core7.types.identifier(restName);
|
|
921
|
-
} else {
|
|
922
|
-
const restVariableDeclaration = import_core7.types.variableDeclaration("const", [
|
|
923
|
-
import_core7.types.variableDeclarator(
|
|
924
|
-
import_core7.types.identifier(restName),
|
|
925
|
-
import_core7.types.callExpression(state.useReactive, [
|
|
926
|
-
import_core7.types.identifier("__props"),
|
|
927
|
-
import_core7.types.arrayExpression(notRestNames.map((name) => import_core7.types.stringLiteral(name)))
|
|
928
|
-
])
|
|
929
|
-
)
|
|
930
|
-
]);
|
|
931
|
-
imports.add("useReactive");
|
|
932
|
-
path.node.body.body.unshift(restVariableDeclaration);
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
|
|
937
|
-
// src/index.ts
|
|
938
|
-
function src_default() {
|
|
939
|
-
return {
|
|
940
|
-
name: "babel-plugin-essor",
|
|
941
|
-
manipulateOptions({ filename }, parserOpts) {
|
|
942
|
-
if (filename.endsWith(".ts") || filename.endsWith(".tsx")) {
|
|
943
|
-
parserOpts.plugins.push("typescript");
|
|
944
|
-
}
|
|
945
|
-
parserOpts.plugins.push("jsx");
|
|
946
|
-
},
|
|
947
|
-
visitor: {
|
|
948
|
-
Program: transformProgram,
|
|
949
|
-
JSXElement: transformJSX,
|
|
950
|
-
JSXFragment: transformJSX,
|
|
951
|
-
FunctionDeclaration: replaceProps,
|
|
952
|
-
ArrowFunctionExpression: replaceProps,
|
|
953
|
-
VariableDeclarator: replaceSymbol,
|
|
954
|
-
ImportDeclaration: replaceImportDeclaration,
|
|
955
|
-
Identifier: symbolIdentifier,
|
|
956
|
-
ObjectPattern: symbolObjectPattern,
|
|
957
|
-
ArrayPattern: symbolArrayPattern
|
|
958
|
-
}
|
|
959
|
-
};
|
|
960
|
-
}
|
|
961
|
-
//# sourceMappingURL=index.cjs.map
|