babel-plugin-essor 0.0.10-beta.21 → 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/LICENSE +20 -20
- package/README.md +28 -28
- package/package.json +2 -2
- package/dist/index.cjs +0 -720
- 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 -700
- package/dist/index.js.map +0 -1
package/dist/index.cjs
DELETED
|
@@ -1,720 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
8
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
-
var __spreadValues = (a, b) => {
|
|
10
|
-
for (var prop in b || (b = {}))
|
|
11
|
-
if (__hasOwnProp.call(b, prop))
|
|
12
|
-
__defNormalProp(a, prop, b[prop]);
|
|
13
|
-
if (__getOwnPropSymbols)
|
|
14
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
15
|
-
if (__propIsEnum.call(b, prop))
|
|
16
|
-
__defNormalProp(a, prop, b[prop]);
|
|
17
|
-
}
|
|
18
|
-
return a;
|
|
19
|
-
};
|
|
20
|
-
var __export = (target, all) => {
|
|
21
|
-
for (var name in all)
|
|
22
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
23
|
-
};
|
|
24
|
-
var __copyProps = (to, from, except, desc) => {
|
|
25
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
26
|
-
for (let key of __getOwnPropNames(from))
|
|
27
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
28
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
29
|
-
}
|
|
30
|
-
return to;
|
|
31
|
-
};
|
|
32
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
33
|
-
|
|
34
|
-
// src/index.ts
|
|
35
|
-
var src_exports = {};
|
|
36
|
-
__export(src_exports, {
|
|
37
|
-
default: () => src_default
|
|
38
|
-
});
|
|
39
|
-
module.exports = __toCommonJS(src_exports);
|
|
40
|
-
|
|
41
|
-
// src/jsx/index.ts
|
|
42
|
-
var import_core3 = require("@babel/core");
|
|
43
|
-
|
|
44
|
-
// ../shared/dist/essor-shared.js
|
|
45
|
-
var isArray = Array.isArray;
|
|
46
|
-
function isString(val) {
|
|
47
|
-
return typeof val === "string";
|
|
48
|
-
}
|
|
49
|
-
var noop = Function.prototype;
|
|
50
|
-
function startsWith(str, searchString) {
|
|
51
|
-
if (!isString(str)) {
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
return str.indexOf(searchString) === 0;
|
|
55
|
-
}
|
|
56
|
-
var capitalizeFirstLetter = (inputString) => {
|
|
57
|
-
return inputString.charAt(0).toUpperCase() + inputString.slice(1);
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
// src/program.ts
|
|
61
|
-
var import_core = require("@babel/core");
|
|
62
|
-
var imports = /* @__PURE__ */ new Set();
|
|
63
|
-
var defaultOption = {
|
|
64
|
-
ssg: false,
|
|
65
|
-
symbol: "$",
|
|
66
|
-
props: true
|
|
67
|
-
};
|
|
68
|
-
var transformProgram = {
|
|
69
|
-
enter(path, state) {
|
|
70
|
-
imports.clear();
|
|
71
|
-
state.opts = __spreadValues(__spreadValues({}, defaultOption), state.opts);
|
|
72
|
-
path.state = {
|
|
73
|
-
h: path.scope.generateUidIdentifier("h$"),
|
|
74
|
-
renderTemplate: path.scope.generateUidIdentifier("renderTemplate$"),
|
|
75
|
-
template: path.scope.generateUidIdentifier("template$"),
|
|
76
|
-
useSignal: path.scope.generateUidIdentifier("signal$"),
|
|
77
|
-
useComputed: path.scope.generateUidIdentifier("computed$"),
|
|
78
|
-
useReactive: path.scope.generateUidIdentifier("reactive$"),
|
|
79
|
-
tmplDeclaration: import_core.types.variableDeclaration("const", []),
|
|
80
|
-
opts: state.opts
|
|
81
|
-
};
|
|
82
|
-
},
|
|
83
|
-
exit(path) {
|
|
84
|
-
const state = path.state;
|
|
85
|
-
if (state.tmplDeclaration.declarations.length > 0) {
|
|
86
|
-
const index = path.node.body.findIndex(
|
|
87
|
-
(node) => !import_core.types.isImportDeclaration(node) && !import_core.types.isExportDeclaration(node)
|
|
88
|
-
);
|
|
89
|
-
path.node.body.splice(index, 0, state.tmplDeclaration);
|
|
90
|
-
}
|
|
91
|
-
if (imports.size > 0) {
|
|
92
|
-
path.node.body.unshift(createImport(state, "essor"));
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
function createImport(state, from) {
|
|
97
|
-
const ImportSpecifier = [];
|
|
98
|
-
imports.forEach((name) => {
|
|
99
|
-
const local = import_core.types.identifier(state[name].name);
|
|
100
|
-
const imported = import_core.types.identifier(name);
|
|
101
|
-
ImportSpecifier.push(import_core.types.importSpecifier(local, imported));
|
|
102
|
-
});
|
|
103
|
-
const importSource = import_core.types.stringLiteral(from);
|
|
104
|
-
return import_core.types.importDeclaration(ImportSpecifier, importSource);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// src/shared.ts
|
|
108
|
-
var import_core2 = require("@babel/core");
|
|
109
|
-
function hasSiblingElement(path) {
|
|
110
|
-
const siblings = path.getAllPrevSiblings().concat(path.getAllNextSiblings());
|
|
111
|
-
const hasSibling = siblings.some(
|
|
112
|
-
(siblingPath) => siblingPath.isJSXElement() || siblingPath.isJSXExpressionContainer()
|
|
113
|
-
);
|
|
114
|
-
return hasSibling;
|
|
115
|
-
}
|
|
116
|
-
function getAttrName(attribute) {
|
|
117
|
-
if (import_core2.types.isJSXIdentifier(attribute.name)) {
|
|
118
|
-
return attribute.name.name;
|
|
119
|
-
}
|
|
120
|
-
if (import_core2.types.isJSXNamespacedName(attribute.name)) {
|
|
121
|
-
return `${attribute.name.namespace.name}:${attribute.name.name.name}`;
|
|
122
|
-
}
|
|
123
|
-
throw new Error("Unsupported attribute type");
|
|
124
|
-
}
|
|
125
|
-
function getTagName(node) {
|
|
126
|
-
const tag = node.openingElement.name;
|
|
127
|
-
return jsxElementNameToString(tag);
|
|
128
|
-
}
|
|
129
|
-
function jsxElementNameToString(node) {
|
|
130
|
-
if (import_core2.types.isJSXMemberExpression(node)) {
|
|
131
|
-
return `${jsxElementNameToString(node.object)}.${jsxElementNameToString(node.property)}`;
|
|
132
|
-
}
|
|
133
|
-
if (import_core2.types.isJSXIdentifier(node) || import_core2.types.isIdentifier(node)) {
|
|
134
|
-
return node.name;
|
|
135
|
-
}
|
|
136
|
-
return `${node.namespace.name}:${node.name.name}`;
|
|
137
|
-
}
|
|
138
|
-
function isComponent(tagName) {
|
|
139
|
-
return tagName[0] && tagName[0].toLowerCase() !== tagName[0] || tagName.includes(".") || /[^a-z]/i.test(tagName[0]);
|
|
140
|
-
}
|
|
141
|
-
function isTextChild(path) {
|
|
142
|
-
if (path.isJSXExpressionContainer()) {
|
|
143
|
-
const expression = path.get("expression");
|
|
144
|
-
if (expression.isJSXText() || expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
145
|
-
return true;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
if (path.isJSXText() || path.isStringLiteral() || path.isNullLiteral()) {
|
|
149
|
-
return true;
|
|
150
|
-
}
|
|
151
|
-
return false;
|
|
152
|
-
}
|
|
153
|
-
function setNodeText(path, text) {
|
|
154
|
-
if (path.isJSXText()) {
|
|
155
|
-
path.node.value = text;
|
|
156
|
-
}
|
|
157
|
-
if (path.isJSXExpressionContainer()) {
|
|
158
|
-
const expression = path.get("expression");
|
|
159
|
-
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
160
|
-
expression.replaceWith(import_core2.types.stringLiteral(text));
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
function isSymbolStart(path, name) {
|
|
165
|
-
const state = path.state;
|
|
166
|
-
const { symbol } = state.opts;
|
|
167
|
-
return startsWith(name, symbol);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// src/jsx/constants.ts
|
|
171
|
-
var selfClosingTags = [
|
|
172
|
-
"area",
|
|
173
|
-
"base",
|
|
174
|
-
"br",
|
|
175
|
-
"col",
|
|
176
|
-
"embed",
|
|
177
|
-
"hr",
|
|
178
|
-
"img",
|
|
179
|
-
"input",
|
|
180
|
-
"link",
|
|
181
|
-
"meta",
|
|
182
|
-
"param",
|
|
183
|
-
"source",
|
|
184
|
-
"track",
|
|
185
|
-
"wbr"
|
|
186
|
-
];
|
|
187
|
-
var svgTags = [
|
|
188
|
-
"circle",
|
|
189
|
-
"clipPath",
|
|
190
|
-
"defs",
|
|
191
|
-
"ellipse",
|
|
192
|
-
"filter",
|
|
193
|
-
"g",
|
|
194
|
-
"line",
|
|
195
|
-
"linearGradient",
|
|
196
|
-
"mask",
|
|
197
|
-
"path",
|
|
198
|
-
"pattern",
|
|
199
|
-
"polygon",
|
|
200
|
-
"polyline",
|
|
201
|
-
"radialGradient",
|
|
202
|
-
"rect",
|
|
203
|
-
"stop",
|
|
204
|
-
"symbol",
|
|
205
|
-
"text",
|
|
206
|
-
"use"
|
|
207
|
-
];
|
|
208
|
-
|
|
209
|
-
// src/jsx/index.ts
|
|
210
|
-
var isSsg = false;
|
|
211
|
-
function addToTemplate(result, content) {
|
|
212
|
-
if (isSsg) {
|
|
213
|
-
result.template.push(content);
|
|
214
|
-
} else {
|
|
215
|
-
result.template += content;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
function transformJSX(path) {
|
|
219
|
-
const state = path.state;
|
|
220
|
-
isSsg = state.opts.ssg;
|
|
221
|
-
const result = {
|
|
222
|
-
index: 1,
|
|
223
|
-
isLastChild: false,
|
|
224
|
-
parentIndex: 0,
|
|
225
|
-
props: {},
|
|
226
|
-
template: isSsg ? [] : ""
|
|
227
|
-
};
|
|
228
|
-
transformJSXElement(path, result, true);
|
|
229
|
-
path.replaceWith(createEssorNode(path, result));
|
|
230
|
-
}
|
|
231
|
-
function createEssorNode(path, result) {
|
|
232
|
-
var _a;
|
|
233
|
-
const state = path.state;
|
|
234
|
-
let tmpl;
|
|
235
|
-
if (path.isJSXElement() && isComponent(getTagName(path.node))) {
|
|
236
|
-
tmpl = import_core3.types.identifier(getTagName(path.node));
|
|
237
|
-
} else {
|
|
238
|
-
tmpl = path.scope.generateUidIdentifier("_tmpl$");
|
|
239
|
-
const template = isSsg ? import_core3.types.arrayExpression(result.template.map(import_core3.types.stringLiteral)) : import_core3.types.callExpression(state.template, [import_core3.types.stringLiteral(result.template)]);
|
|
240
|
-
const declarator = import_core3.types.variableDeclarator(tmpl, template);
|
|
241
|
-
state.tmplDeclaration.declarations.push(declarator);
|
|
242
|
-
if (!isSsg) {
|
|
243
|
-
imports.add("template");
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
const args = [tmpl, createProps(result.props)];
|
|
247
|
-
const key = result.props.key || ((_a = result.props[0]) == null ? void 0 : _a.key);
|
|
248
|
-
if (key) {
|
|
249
|
-
args.push(key);
|
|
250
|
-
}
|
|
251
|
-
imports.add(isSsg ? "renderTemplate" : "h");
|
|
252
|
-
return import_core3.types.callExpression(isSsg ? state.renderTemplate : state.h, args);
|
|
253
|
-
}
|
|
254
|
-
function createProps(props) {
|
|
255
|
-
const toAstNode = (value) => {
|
|
256
|
-
if (Array.isArray(value)) {
|
|
257
|
-
return import_core3.types.arrayExpression(value.map(toAstNode));
|
|
258
|
-
}
|
|
259
|
-
if (value && typeof value === "object" && !import_core3.types.isNode(value)) {
|
|
260
|
-
return createProps(value);
|
|
261
|
-
}
|
|
262
|
-
switch (typeof value) {
|
|
263
|
-
case "string":
|
|
264
|
-
return import_core3.types.stringLiteral(value);
|
|
265
|
-
case "number":
|
|
266
|
-
return import_core3.types.numericLiteral(value);
|
|
267
|
-
case "boolean":
|
|
268
|
-
return import_core3.types.booleanLiteral(value);
|
|
269
|
-
case "undefined":
|
|
270
|
-
return import_core3.types.tsUndefinedKeyword();
|
|
271
|
-
case void 0:
|
|
272
|
-
return import_core3.types.tsUndefinedKeyword();
|
|
273
|
-
case null:
|
|
274
|
-
return import_core3.types.nullLiteral();
|
|
275
|
-
default:
|
|
276
|
-
return value;
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
|
-
const result = Object.keys(props).filter((prop) => prop !== "key").map((prop) => {
|
|
280
|
-
const value = toAstNode(props[prop]);
|
|
281
|
-
return prop === "_$spread$" ? import_core3.types.spreadElement(value) : import_core3.types.objectProperty(import_core3.types.stringLiteral(prop), value);
|
|
282
|
-
});
|
|
283
|
-
return import_core3.types.objectExpression(result);
|
|
284
|
-
}
|
|
285
|
-
function transformJSXElement(path, result, isRoot = false) {
|
|
286
|
-
if (path.isJSXElement()) {
|
|
287
|
-
const tagName = getTagName(path.node);
|
|
288
|
-
const tagIsComponent = isComponent(tagName);
|
|
289
|
-
const isSelfClose = !tagIsComponent && selfClosingTags.includes(tagName);
|
|
290
|
-
const isSvg = svgTags.includes(tagName) && result.index === 1;
|
|
291
|
-
const props = getAttrProps(path);
|
|
292
|
-
if (tagIsComponent) {
|
|
293
|
-
if (isRoot) {
|
|
294
|
-
result.props = props;
|
|
295
|
-
const children = getChildren(path);
|
|
296
|
-
if (children.length > 0) {
|
|
297
|
-
const childrenGenerator = children.length === 1 ? children[0] : import_core3.types.arrayExpression(children);
|
|
298
|
-
result.props.children = childrenGenerator;
|
|
299
|
-
}
|
|
300
|
-
} else {
|
|
301
|
-
transformJSX(path);
|
|
302
|
-
replaceChild(path.node, result);
|
|
303
|
-
}
|
|
304
|
-
} else {
|
|
305
|
-
if (isSvg) {
|
|
306
|
-
result.template = isSsg ? ["<svg _svg_>"] : "<svg _svg_>";
|
|
307
|
-
}
|
|
308
|
-
addToTemplate(result, `<${tagName}`);
|
|
309
|
-
handleAttributes(props, result);
|
|
310
|
-
addToTemplate(result, isSelfClose ? "/>" : ">");
|
|
311
|
-
if (!isSelfClose) {
|
|
312
|
-
transformChildren(path, result);
|
|
313
|
-
if (hasSiblingElement(path)) {
|
|
314
|
-
addToTemplate(result, `</${tagName}>`);
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
} else {
|
|
319
|
-
result.index--;
|
|
320
|
-
transformChildren(path, result);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
function transformChildren(path, result) {
|
|
324
|
-
const parentIndex = isSsg ? result.template.length : result.index;
|
|
325
|
-
path.get("children").reduce((pre, cur) => {
|
|
326
|
-
if (isValidChild(cur)) {
|
|
327
|
-
const lastChild = pre.at(-1);
|
|
328
|
-
if (lastChild && isTextChild(cur) && isTextChild(lastChild)) {
|
|
329
|
-
setNodeText(lastChild, getNodeText(lastChild) + getNodeText(cur));
|
|
330
|
-
} else {
|
|
331
|
-
pre.push(cur);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
return pre;
|
|
335
|
-
}, []).forEach((child, i, arr) => {
|
|
336
|
-
result.parentIndex = parentIndex;
|
|
337
|
-
result.isLastChild = i === arr.length - 1;
|
|
338
|
-
transformChild(child, result);
|
|
339
|
-
});
|
|
340
|
-
}
|
|
341
|
-
function transformChild(child, result) {
|
|
342
|
-
result.index++;
|
|
343
|
-
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
344
|
-
transformJSXElement(child, result, false);
|
|
345
|
-
} else if (child.isJSXExpressionContainer()) {
|
|
346
|
-
const expression = child.get("expression");
|
|
347
|
-
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
348
|
-
addToTemplate(result, String(expression.node.value));
|
|
349
|
-
} else if (expression.isExpression()) {
|
|
350
|
-
replaceChild(expression.node, result);
|
|
351
|
-
} else if (import_core3.types.isJSXEmptyExpression(expression.node)) {
|
|
352
|
-
} else {
|
|
353
|
-
throw new Error("Unsupported child type");
|
|
354
|
-
}
|
|
355
|
-
} else if (child.isJSXText()) {
|
|
356
|
-
addToTemplate(result, String(child.node.value));
|
|
357
|
-
} else {
|
|
358
|
-
throw new Error("Unsupported child type");
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
function getNodeText(path) {
|
|
362
|
-
if (path.isJSXText()) {
|
|
363
|
-
return path.node.value;
|
|
364
|
-
}
|
|
365
|
-
if (path.isJSXExpressionContainer()) {
|
|
366
|
-
const expression = path.get("expression");
|
|
367
|
-
if (expression.isStringLiteral() || expression.isNumericLiteral()) {
|
|
368
|
-
return String(expression.node.value);
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
return "";
|
|
372
|
-
}
|
|
373
|
-
function handleAttributes(props, result) {
|
|
374
|
-
let klass = "";
|
|
375
|
-
let style = "";
|
|
376
|
-
for (const prop in props) {
|
|
377
|
-
let value = props[prop];
|
|
378
|
-
if (prop === "class" && typeof value === "string") {
|
|
379
|
-
klass += ` ${value}`;
|
|
380
|
-
delete props[prop];
|
|
381
|
-
continue;
|
|
382
|
-
}
|
|
383
|
-
if (prop === "style" && typeof value === "string") {
|
|
384
|
-
style += `${value}${value.at(-1) === ";" ? "" : ";"}`;
|
|
385
|
-
delete props[prop];
|
|
386
|
-
continue;
|
|
387
|
-
}
|
|
388
|
-
if (value === true) {
|
|
389
|
-
addToTemplate(result, ` ${prop}`);
|
|
390
|
-
delete props[prop];
|
|
391
|
-
}
|
|
392
|
-
if (value === false) {
|
|
393
|
-
delete props[prop];
|
|
394
|
-
}
|
|
395
|
-
if (typeof value === "string" || typeof value === "number") {
|
|
396
|
-
addToTemplate(result, ` ${prop}="${value}"`);
|
|
397
|
-
delete props[prop];
|
|
398
|
-
}
|
|
399
|
-
if (import_core3.types.isConditionalExpression(value)) {
|
|
400
|
-
const { test, consequent, alternate } = value;
|
|
401
|
-
value = import_core3.types.arrowFunctionExpression([], import_core3.types.conditionalExpression(test, consequent, alternate));
|
|
402
|
-
props[prop] = value;
|
|
403
|
-
}
|
|
404
|
-
if (import_core3.types.isObjectExpression(value)) {
|
|
405
|
-
let hasConditional = false;
|
|
406
|
-
value.properties.forEach((property) => {
|
|
407
|
-
if (import_core3.types.isObjectProperty(property) && import_core3.types.isConditionalExpression(property.value)) {
|
|
408
|
-
hasConditional = true;
|
|
409
|
-
}
|
|
410
|
-
});
|
|
411
|
-
if (hasConditional) {
|
|
412
|
-
value = import_core3.types.arrowFunctionExpression([], value);
|
|
413
|
-
props[prop] = value;
|
|
414
|
-
} else {
|
|
415
|
-
if (prop === "style") {
|
|
416
|
-
value.properties.forEach((property) => {
|
|
417
|
-
if (import_core3.types.isObjectProperty(property)) {
|
|
418
|
-
style += `${property.key.name}:${property.value.value};`;
|
|
419
|
-
}
|
|
420
|
-
});
|
|
421
|
-
delete props[prop];
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
if (Object.keys(props).length > 0) {
|
|
427
|
-
result.props[result.index] = props;
|
|
428
|
-
}
|
|
429
|
-
klass = klass.trim();
|
|
430
|
-
style = style.trim();
|
|
431
|
-
if (klass) {
|
|
432
|
-
addToTemplate(result, ` class="${klass}"`);
|
|
433
|
-
}
|
|
434
|
-
if (style) {
|
|
435
|
-
addToTemplate(result, ` style="${style}"`);
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
function replaceChild(node, result) {
|
|
439
|
-
var _a, _b, _c, _d, _e;
|
|
440
|
-
if (result.isLastChild) {
|
|
441
|
-
result.index--;
|
|
442
|
-
} else {
|
|
443
|
-
addToTemplate(result, "<!>");
|
|
444
|
-
}
|
|
445
|
-
(_c = (_a = result.props)[_b = result.parentIndex]) != null ? _c : _a[_b] = {};
|
|
446
|
-
(_e = (_d = result.props[result.parentIndex]).children) != null ? _e : _d.children = [];
|
|
447
|
-
result.props[result.parentIndex].children.push(
|
|
448
|
-
import_core3.types.arrayExpression([
|
|
449
|
-
import_core3.types.arrowFunctionExpression([], node),
|
|
450
|
-
result.isLastChild ? import_core3.types.nullLiteral() : import_core3.types.identifier(String(result.index))
|
|
451
|
-
])
|
|
452
|
-
);
|
|
453
|
-
}
|
|
454
|
-
function getChildren(path) {
|
|
455
|
-
return path.get("children").filter((child) => isValidChild(child)).map((child) => {
|
|
456
|
-
if (child.isJSXElement() || child.isJSXFragment()) {
|
|
457
|
-
transformJSX(child);
|
|
458
|
-
} else if (child.isJSXExpressionContainer()) {
|
|
459
|
-
child.replaceWith(child.get("expression"));
|
|
460
|
-
} else if (child.isJSXText()) {
|
|
461
|
-
child.replaceWith(import_core3.types.stringLiteral(child.node.value));
|
|
462
|
-
} else {
|
|
463
|
-
throw new Error("Unsupported child type");
|
|
464
|
-
}
|
|
465
|
-
return child.node;
|
|
466
|
-
});
|
|
467
|
-
}
|
|
468
|
-
function isValidChild(path) {
|
|
469
|
-
const regex = /^\s*$/;
|
|
470
|
-
if (path.isStringLiteral() || path.isJSXText()) {
|
|
471
|
-
return !regex.test(path.node.value);
|
|
472
|
-
}
|
|
473
|
-
return Object.keys(path.node).length > 0;
|
|
474
|
-
}
|
|
475
|
-
function getAttrProps(path) {
|
|
476
|
-
const props = {};
|
|
477
|
-
path.get("openingElement").get("attributes").forEach((attribute) => {
|
|
478
|
-
if (attribute.isJSXAttribute()) {
|
|
479
|
-
const name = getAttrName(attribute.node);
|
|
480
|
-
const value = attribute.get("value");
|
|
481
|
-
if (!value.node) {
|
|
482
|
-
props[name] = true;
|
|
483
|
-
} else if (value.isStringLiteral()) {
|
|
484
|
-
props[name] = value.node.value;
|
|
485
|
-
} else {
|
|
486
|
-
if (value.isJSXExpressionContainer()) {
|
|
487
|
-
const expression = value.get("expression");
|
|
488
|
-
if (expression.isStringLiteral()) {
|
|
489
|
-
props[name] = expression.node.value;
|
|
490
|
-
} else if (expression.isNumericLiteral()) {
|
|
491
|
-
props[name] = expression.node.value;
|
|
492
|
-
} else if (expression.isJSXElement() || expression.isJSXFragment()) {
|
|
493
|
-
transformJSX(expression);
|
|
494
|
-
props[name] = expression.node;
|
|
495
|
-
} else if (expression.isExpression()) {
|
|
496
|
-
if (/^key|ref|on.+$/.test(name)) {
|
|
497
|
-
props[name] = expression.node;
|
|
498
|
-
} else if (/^bind:.+/.test(name)) {
|
|
499
|
-
const value2 = path.scope.generateUidIdentifier("value");
|
|
500
|
-
const bindName = name.slice(5).toLocaleLowerCase();
|
|
501
|
-
props[bindName] = expression.node;
|
|
502
|
-
props[`update${capitalizeFirstLetter(bindName)}`] = import_core3.types.arrowFunctionExpression(
|
|
503
|
-
[value2],
|
|
504
|
-
import_core3.types.assignmentExpression("=", expression.node, value2)
|
|
505
|
-
);
|
|
506
|
-
} else {
|
|
507
|
-
if (expression.isConditionalExpression()) {
|
|
508
|
-
props[name] = import_core3.types.arrowFunctionExpression([], expression.node);
|
|
509
|
-
} else {
|
|
510
|
-
props[name] = expression.node;
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
} else if (value.isJSXElement() || value.isJSXFragment()) {
|
|
515
|
-
transformJSX(value);
|
|
516
|
-
props[name] = value.node;
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
} else if (attribute.isJSXSpreadAttribute()) {
|
|
520
|
-
props._$spread$ = attribute.get("argument").node;
|
|
521
|
-
} else {
|
|
522
|
-
throw new Error("Unsupported attribute type");
|
|
523
|
-
}
|
|
524
|
-
});
|
|
525
|
-
return props;
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
// src/signal/symbol.ts
|
|
529
|
-
var import_core4 = require("@babel/core");
|
|
530
|
-
function replaceSymbol(path) {
|
|
531
|
-
const init = path.node.init;
|
|
532
|
-
const variableName = path.node.id.name;
|
|
533
|
-
if (import_core4.types.isObjectPattern(path.node.id) || import_core4.types.isArrayPattern(path.node.id)) {
|
|
534
|
-
return;
|
|
535
|
-
}
|
|
536
|
-
if (!isSymbolStart(path, variableName)) {
|
|
537
|
-
return;
|
|
538
|
-
}
|
|
539
|
-
if (init && (import_core4.types.isFunctionExpression(init) || import_core4.types.isArrowFunctionExpression(init)) && path.parent.kind === "const") {
|
|
540
|
-
const newInit = import_core4.types.callExpression(import_core4.types.identifier(path.state.useComputed.name), init ? [init] : []);
|
|
541
|
-
imports.add("useComputed");
|
|
542
|
-
path.node.init = newInit;
|
|
543
|
-
} else {
|
|
544
|
-
const newInit = import_core4.types.callExpression(import_core4.types.identifier(path.state.useSignal.name), init ? [init] : []);
|
|
545
|
-
imports.add("useSignal");
|
|
546
|
-
path.node.init = newInit;
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
function symbolIdentifier(path) {
|
|
550
|
-
const parentPath = path.parentPath;
|
|
551
|
-
if (!parentPath || import_core4.types.isVariableDeclarator(parentPath) || import_core4.types.isImportSpecifier(parentPath) || import_core4.types.isObjectProperty(parentPath) || import_core4.types.isArrayPattern(parentPath) || import_core4.types.isObjectPattern(parentPath)) {
|
|
552
|
-
return;
|
|
553
|
-
}
|
|
554
|
-
const { node } = path;
|
|
555
|
-
if (isSymbolStart(path, node.name)) {
|
|
556
|
-
let currentPath = path;
|
|
557
|
-
while (currentPath.parentPath && !currentPath.parentPath.isProgram()) {
|
|
558
|
-
if (currentPath.parentPath.isMemberExpression() && currentPath.parentPath.node.property.name === "value") {
|
|
559
|
-
return;
|
|
560
|
-
}
|
|
561
|
-
currentPath = currentPath.parentPath;
|
|
562
|
-
}
|
|
563
|
-
const newNode = import_core4.types.memberExpression(import_core4.types.identifier(node.name), import_core4.types.identifier("value"));
|
|
564
|
-
path.replaceWith(newNode);
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
function symbolObjectPattern(path) {
|
|
568
|
-
path.node.properties.forEach((property) => {
|
|
569
|
-
if (import_core4.types.isObjectProperty(property) && import_core4.types.isIdentifier(property.key) && isSymbolStart(path, property.key.name)) {
|
|
570
|
-
const newKey = import_core4.types.identifier(property.key.name);
|
|
571
|
-
property.key = newKey;
|
|
572
|
-
}
|
|
573
|
-
});
|
|
574
|
-
}
|
|
575
|
-
function symbolArrayPattern(path) {
|
|
576
|
-
path.node.elements.forEach((element) => {
|
|
577
|
-
if (import_core4.types.isIdentifier(element) && element.name.startsWith("$")) {
|
|
578
|
-
const newElement = import_core4.types.identifier(element.name);
|
|
579
|
-
element.name = newElement.name;
|
|
580
|
-
} else if (import_core4.types.isObjectPattern(element)) {
|
|
581
|
-
element.properties.forEach((property) => {
|
|
582
|
-
if (import_core4.types.isObjectProperty(property) && import_core4.types.isIdentifier(property.key) && isSymbolStart(path, property.key.name)) {
|
|
583
|
-
const newKey = import_core4.types.identifier(property.key.name);
|
|
584
|
-
property.key = newKey;
|
|
585
|
-
}
|
|
586
|
-
});
|
|
587
|
-
}
|
|
588
|
-
});
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
// src/signal/import.ts
|
|
592
|
-
var import_core5 = require("@babel/core");
|
|
593
|
-
function replaceImportDeclaration(path) {
|
|
594
|
-
const imports2 = path.node.specifiers;
|
|
595
|
-
imports2.forEach((specifier) => {
|
|
596
|
-
const variableName = specifier.local.name;
|
|
597
|
-
if (startsWith(variableName, "$") && !isVariableUsedAsObject(path, variableName)) {
|
|
598
|
-
path.scope.rename(variableName, `${variableName}.value`);
|
|
599
|
-
specifier.local.name = `${variableName}`;
|
|
600
|
-
}
|
|
601
|
-
});
|
|
602
|
-
}
|
|
603
|
-
function isVariableUsedAsObject(path, variableName) {
|
|
604
|
-
const binding = path.scope.getBinding(variableName);
|
|
605
|
-
let isUsedObject = false;
|
|
606
|
-
if (!binding || !binding.referencePaths) {
|
|
607
|
-
return isUsedObject;
|
|
608
|
-
}
|
|
609
|
-
for (const referencePath of binding.referencePaths) {
|
|
610
|
-
if (import_core5.types.isMemberExpression(referencePath.parent)) {
|
|
611
|
-
const memberExprParent = referencePath.parent;
|
|
612
|
-
if (import_core5.types.isIdentifier(memberExprParent.object, { name: variableName })) {
|
|
613
|
-
const newMemberExpr = import_core5.types.memberExpression(
|
|
614
|
-
import_core5.types.memberExpression(memberExprParent.object, import_core5.types.identifier("value")),
|
|
615
|
-
memberExprParent.property
|
|
616
|
-
);
|
|
617
|
-
referencePath.parentPath.replaceWith(newMemberExpr);
|
|
618
|
-
isUsedObject = true;
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
return isUsedObject;
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
// src/signal/props.ts
|
|
626
|
-
var import_core6 = require("@babel/core");
|
|
627
|
-
function replaceProps(path) {
|
|
628
|
-
var _a;
|
|
629
|
-
const state = path.state;
|
|
630
|
-
const firstParam = path.node.params[0];
|
|
631
|
-
if (!firstParam || !import_core6.types.isObjectPattern(firstParam)) {
|
|
632
|
-
return;
|
|
633
|
-
}
|
|
634
|
-
const returnStatement = path.get("body").get("body").find((statement) => statement.isReturnStatement());
|
|
635
|
-
if (!returnStatement) {
|
|
636
|
-
return;
|
|
637
|
-
}
|
|
638
|
-
const returnValue = (_a = returnStatement.node) == null ? void 0 : _a.argument;
|
|
639
|
-
if (!import_core6.types.isJSXElement(returnValue)) {
|
|
640
|
-
return;
|
|
641
|
-
}
|
|
642
|
-
function replaceProperties(properties2, parentPath) {
|
|
643
|
-
properties2.forEach((property) => {
|
|
644
|
-
if (import_core6.types.isObjectProperty(property)) {
|
|
645
|
-
const keyName = property.key.name;
|
|
646
|
-
if (import_core6.types.isIdentifier(property.value)) {
|
|
647
|
-
const propertyName = property.value.name;
|
|
648
|
-
const newName = `${parentPath}${keyName}`;
|
|
649
|
-
path.scope.rename(propertyName, newName);
|
|
650
|
-
} else if (import_core6.types.isObjectPattern(property.value)) {
|
|
651
|
-
replaceProperties(property.value.properties, `${parentPath}${keyName}.`);
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
});
|
|
655
|
-
}
|
|
656
|
-
const properties = firstParam.properties;
|
|
657
|
-
replaceProperties(
|
|
658
|
-
properties.filter((property) => !import_core6.types.isRestElement(property)),
|
|
659
|
-
"__props."
|
|
660
|
-
);
|
|
661
|
-
const notRestProperties = properties.filter((property) => !import_core6.types.isRestElement(property));
|
|
662
|
-
const notRestNames = notRestProperties.map(
|
|
663
|
-
(property) => property.key.name
|
|
664
|
-
);
|
|
665
|
-
if (notRestNames.some((name) => startsWith(name, "$"))) {
|
|
666
|
-
console.warn("props name can not start with $");
|
|
667
|
-
return;
|
|
668
|
-
}
|
|
669
|
-
const restElement = properties.find((property) => import_core6.types.isRestElement(property));
|
|
670
|
-
path.node.params[0] = import_core6.types.identifier("__props");
|
|
671
|
-
if (restElement) {
|
|
672
|
-
const restName = restElement.argument.name;
|
|
673
|
-
if (notRestProperties.length === 0) {
|
|
674
|
-
path.node.params[0] = import_core6.types.identifier(restName);
|
|
675
|
-
} else {
|
|
676
|
-
const restVariableDeclaration = import_core6.types.variableDeclaration("const", [
|
|
677
|
-
import_core6.types.variableDeclarator(
|
|
678
|
-
import_core6.types.identifier(restName),
|
|
679
|
-
import_core6.types.callExpression(state.useReactive, [
|
|
680
|
-
import_core6.types.identifier("__props"),
|
|
681
|
-
import_core6.types.arrayExpression(notRestNames.map((name) => import_core6.types.stringLiteral(name)))
|
|
682
|
-
])
|
|
683
|
-
)
|
|
684
|
-
]);
|
|
685
|
-
imports.add("useReactive");
|
|
686
|
-
path.node.body.body.unshift(restVariableDeclaration);
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
// src/index.ts
|
|
692
|
-
function src_default() {
|
|
693
|
-
return {
|
|
694
|
-
name: "babel-plugin-essor",
|
|
695
|
-
manipulateOptions({ filename }, parserOpts) {
|
|
696
|
-
if (filename.endsWith(".ts") || filename.endsWith(".tsx")) {
|
|
697
|
-
parserOpts.plugins.push("typescript");
|
|
698
|
-
}
|
|
699
|
-
parserOpts.plugins.push("jsx");
|
|
700
|
-
},
|
|
701
|
-
visitor: {
|
|
702
|
-
Program: transformProgram,
|
|
703
|
-
FunctionDeclaration: replaceProps,
|
|
704
|
-
ArrowFunctionExpression: replaceProps,
|
|
705
|
-
VariableDeclarator: replaceSymbol,
|
|
706
|
-
ImportDeclaration: replaceImportDeclaration,
|
|
707
|
-
Identifier: symbolIdentifier,
|
|
708
|
-
ObjectPattern: symbolObjectPattern,
|
|
709
|
-
ArrayPattern: symbolArrayPattern,
|
|
710
|
-
JSXElement: transformJSX,
|
|
711
|
-
JSXFragment: transformJSX
|
|
712
|
-
}
|
|
713
|
-
};
|
|
714
|
-
}
|
|
715
|
-
/**
|
|
716
|
-
* @estjs/shared v0.0.10-beta.20
|
|
717
|
-
* (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
|
|
718
|
-
* @license MIT
|
|
719
|
-
**/
|
|
720
|
-
//# sourceMappingURL=index.cjs.map
|