@rolldown-plugin/solid 0.0.0
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 +22 -0
- package/README.md +184 -0
- package/dist/index.d.mts +83 -0
- package/dist/index.mjs +3443 -0
- package/package.json +56 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,3443 @@
|
|
|
1
|
+
import { parse } from "node:path";
|
|
2
|
+
import { transformAsync } from "@babel/core";
|
|
3
|
+
import { declare, declarePreset } from "@babel/helper-plugin-utils";
|
|
4
|
+
import * as t from "@babel/types";
|
|
5
|
+
import { addComment } from "@babel/types";
|
|
6
|
+
import { addNamed } from "@babel/helper-module-imports";
|
|
7
|
+
import { decode } from "html-entities";
|
|
8
|
+
import * as parse5 from "parse5";
|
|
9
|
+
import assert from "node:assert";
|
|
10
|
+
import * as template from "@babel/template";
|
|
11
|
+
import { visitors } from "@babel/traverse";
|
|
12
|
+
import { OptionValidator } from "@babel/helper-validator-option";
|
|
13
|
+
|
|
14
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/shared/utils.ts
|
|
15
|
+
const reservedNameSpaces = new Set([
|
|
16
|
+
"class",
|
|
17
|
+
"on",
|
|
18
|
+
"oncapture",
|
|
19
|
+
"style",
|
|
20
|
+
"use",
|
|
21
|
+
"prop",
|
|
22
|
+
"attr",
|
|
23
|
+
"bool"
|
|
24
|
+
]);
|
|
25
|
+
const nonSpreadNameSpaces = new Set([
|
|
26
|
+
"class",
|
|
27
|
+
"style",
|
|
28
|
+
"use",
|
|
29
|
+
"prop",
|
|
30
|
+
"attr",
|
|
31
|
+
"bool"
|
|
32
|
+
]);
|
|
33
|
+
function getConfig(path) {
|
|
34
|
+
return path.hub.file.metadata.config;
|
|
35
|
+
}
|
|
36
|
+
const getRendererConfig = (path, renderer) => {
|
|
37
|
+
const config = getConfig(path);
|
|
38
|
+
return config?.renderers?.find((r) => r.name === renderer) ?? config;
|
|
39
|
+
};
|
|
40
|
+
function registerImportMethod(path, name, moduleName) {
|
|
41
|
+
const imports = path.scope.getProgramParent().data.imports || (path.scope.getProgramParent().data.imports = /* @__PURE__ */ new Map());
|
|
42
|
+
moduleName = moduleName || getConfig(path).moduleName;
|
|
43
|
+
if (!imports.has(`${moduleName}:${name}`)) {
|
|
44
|
+
let id = addNamed(path, name, moduleName, { nameHint: `_$${name}` });
|
|
45
|
+
imports.set(`${moduleName}:${name}`, id);
|
|
46
|
+
return id;
|
|
47
|
+
} else {
|
|
48
|
+
let iden = imports.get(`${moduleName}:${name}`);
|
|
49
|
+
return t.cloneNode(iden);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function jsxElementNameToString(node) {
|
|
53
|
+
if (t.isJSXMemberExpression(node)) return `${jsxElementNameToString(node.object)}.${node.property.name}`;
|
|
54
|
+
if (t.isJSXIdentifier(node) || t.isIdentifier(node)) return node.name;
|
|
55
|
+
return `${node.namespace.name}:${node.name.name}`;
|
|
56
|
+
}
|
|
57
|
+
function getTagName(tag) {
|
|
58
|
+
const jsxName = tag.openingElement.name;
|
|
59
|
+
return jsxElementNameToString(jsxName);
|
|
60
|
+
}
|
|
61
|
+
function isComponent(tagName) {
|
|
62
|
+
return tagName[0] && tagName[0].toLowerCase() !== tagName[0] || tagName.includes(".") || /[^a-zA-Z]/.test(tagName[0]);
|
|
63
|
+
}
|
|
64
|
+
function hasStaticMarker(object, path) {
|
|
65
|
+
if (!object) return false;
|
|
66
|
+
if (object.leadingComments && object.leadingComments[0] && object.leadingComments[0].value.trim() === getConfig(path).staticMarker) return true;
|
|
67
|
+
if (object.expression) return hasStaticMarker(object.expression, path);
|
|
68
|
+
}
|
|
69
|
+
function isDynamic(path, { checkMember, checkTags, checkCallExpressions = true, native }) {
|
|
70
|
+
const config = getConfig(path);
|
|
71
|
+
if (config.generate === "ssr" && native) {
|
|
72
|
+
checkMember = false;
|
|
73
|
+
checkCallExpressions = false;
|
|
74
|
+
}
|
|
75
|
+
const expr = path.node;
|
|
76
|
+
if (t.isFunction(expr)) return false;
|
|
77
|
+
if (expr.leadingComments && expr.leadingComments[0] && expr.leadingComments[0].value.trim() === config.staticMarker) return false;
|
|
78
|
+
if (checkCallExpressions && (t.isCallExpression(expr) || t.isOptionalCallExpression(expr) || t.isTaggedTemplateExpression(expr))) return true;
|
|
79
|
+
if (checkMember && t.isMemberExpression(expr)) {
|
|
80
|
+
const object = path.get("object").node;
|
|
81
|
+
if (t.isIdentifier(object) && (!expr.computed || !isDynamic(path.get("property"), {
|
|
82
|
+
checkMember,
|
|
83
|
+
checkTags,
|
|
84
|
+
checkCallExpressions,
|
|
85
|
+
native
|
|
86
|
+
}))) {
|
|
87
|
+
const binding = path.scope.getBinding(object.name);
|
|
88
|
+
if (binding && binding.path.isImportNamespaceSpecifier()) return false;
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
if (checkMember && (t.isOptionalMemberExpression(expr) || t.isSpreadElement(expr) || t.isBinaryExpression(expr) && expr.operator === "in")) return true;
|
|
93
|
+
if (checkTags && (t.isJSXElement(expr) || t.isJSXFragment(expr) && expr.children.length)) return true;
|
|
94
|
+
let dynamic;
|
|
95
|
+
path.traverse({
|
|
96
|
+
Function(p) {
|
|
97
|
+
if (t.isObjectMethod(p.node) && p.node.computed) dynamic = isDynamic(p.get("key"), {
|
|
98
|
+
checkMember,
|
|
99
|
+
checkTags,
|
|
100
|
+
checkCallExpressions,
|
|
101
|
+
native
|
|
102
|
+
});
|
|
103
|
+
p.skip();
|
|
104
|
+
},
|
|
105
|
+
CallExpression(p) {
|
|
106
|
+
checkCallExpressions && (dynamic = true) && p.stop();
|
|
107
|
+
},
|
|
108
|
+
OptionalCallExpression(p) {
|
|
109
|
+
checkCallExpressions && (dynamic = true) && p.stop();
|
|
110
|
+
},
|
|
111
|
+
MemberExpression(p) {
|
|
112
|
+
checkMember && (dynamic = true) && p.stop();
|
|
113
|
+
},
|
|
114
|
+
OptionalMemberExpression(p) {
|
|
115
|
+
checkMember && (dynamic = true) && p.stop();
|
|
116
|
+
},
|
|
117
|
+
SpreadElement(p) {
|
|
118
|
+
checkMember && (dynamic = true) && p.stop();
|
|
119
|
+
},
|
|
120
|
+
BinaryExpression(p) {
|
|
121
|
+
checkMember && p.node.operator === "in" && (dynamic = true) && p.stop();
|
|
122
|
+
},
|
|
123
|
+
JSXElement(p) {
|
|
124
|
+
checkTags ? (dynamic = true) && p.stop() : p.skip();
|
|
125
|
+
},
|
|
126
|
+
JSXFragment(p) {
|
|
127
|
+
checkTags && p.node.children.length ? (dynamic = true) && p.stop() : p.skip();
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
return dynamic;
|
|
131
|
+
}
|
|
132
|
+
function getStaticExpression(path) {
|
|
133
|
+
const node = path.node;
|
|
134
|
+
let value, type;
|
|
135
|
+
return t.isJSXExpressionContainer(node) && t.isJSXElement(path.parent) && !isComponent(getTagName(path.parent)) && !t.isSequenceExpression(node.expression) && (value = path.get("expression").evaluate().value) !== void 0 && ((type = typeof value) === "string" || type === "number") && value;
|
|
136
|
+
}
|
|
137
|
+
function filterChildren(children) {
|
|
138
|
+
return children.filter(({ node: child }) => !(t.isJSXExpressionContainer(child) && t.isJSXEmptyExpression(child.expression)) && (!t.isJSXText(child) || !/^[\r\n]\s*$/.test(child.extra.raw)));
|
|
139
|
+
}
|
|
140
|
+
function checkLength(children) {
|
|
141
|
+
let i = 0;
|
|
142
|
+
children.forEach((path) => {
|
|
143
|
+
const child = path.node;
|
|
144
|
+
!(t.isJSXExpressionContainer(child) && t.isJSXEmptyExpression(child.expression)) && (!t.isJSXText(child) || !/^\s*$/.test(child.extra.raw) || /^ *$/.test(child.extra.raw)) && i++;
|
|
145
|
+
});
|
|
146
|
+
return i > 1;
|
|
147
|
+
}
|
|
148
|
+
function trimWhitespace(text) {
|
|
149
|
+
text = text.replace(/\r/g, "");
|
|
150
|
+
if (/\n/g.test(text)) text = text.split("\n").map((t$1, i) => i ? t$1.replace(/^\s*/g, "") : t$1).filter((s) => !/^\s*$/.test(s)).join(" ");
|
|
151
|
+
return text.replace(/\s+/g, " ");
|
|
152
|
+
}
|
|
153
|
+
function toEventName(name) {
|
|
154
|
+
return name.slice(2).toLowerCase();
|
|
155
|
+
}
|
|
156
|
+
function toPropertyName(name) {
|
|
157
|
+
return name.toLowerCase().replace(/-([a-z])/g, (_, w) => w.toUpperCase());
|
|
158
|
+
}
|
|
159
|
+
function wrappedByText(list, startIndex) {
|
|
160
|
+
let index = startIndex, wrapped;
|
|
161
|
+
while (--index >= 0) {
|
|
162
|
+
const node = list[index];
|
|
163
|
+
if (!node) continue;
|
|
164
|
+
if (node.text) {
|
|
165
|
+
wrapped = true;
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
if (node.id) return false;
|
|
169
|
+
}
|
|
170
|
+
if (!wrapped) return false;
|
|
171
|
+
index = startIndex;
|
|
172
|
+
while (++index < list.length) {
|
|
173
|
+
const node = list[index];
|
|
174
|
+
if (!node) continue;
|
|
175
|
+
if (node.text) return true;
|
|
176
|
+
if (node.id) return false;
|
|
177
|
+
}
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
function transformCondition(path, inline, deep) {
|
|
181
|
+
const config = getConfig(path);
|
|
182
|
+
const expr = path.node;
|
|
183
|
+
const memo = registerImportMethod(path, config.memoWrapper);
|
|
184
|
+
let dTest, cond, id;
|
|
185
|
+
if (t.isConditionalExpression(expr) && (isDynamic(path.get("consequent"), {
|
|
186
|
+
checkTags: true,
|
|
187
|
+
checkMember: true
|
|
188
|
+
}) || isDynamic(path.get("alternate"), {
|
|
189
|
+
checkTags: true,
|
|
190
|
+
checkMember: true
|
|
191
|
+
}))) {
|
|
192
|
+
dTest = isDynamic(path.get("test"), { checkMember: true });
|
|
193
|
+
if (dTest) {
|
|
194
|
+
cond = expr.test;
|
|
195
|
+
if (!t.isBinaryExpression(cond)) cond = t.unaryExpression("!", t.unaryExpression("!", cond, true), true);
|
|
196
|
+
id = inline ? t.callExpression(memo, [t.arrowFunctionExpression([], cond)]) : path.scope.generateUidIdentifier("_c$");
|
|
197
|
+
expr.test = t.callExpression(id, []);
|
|
198
|
+
if (t.isConditionalExpression(expr.consequent) || t.isLogicalExpression(expr.consequent)) expr.consequent = transformCondition(path.get("consequent"), true, true);
|
|
199
|
+
if (t.isConditionalExpression(expr.alternate) || t.isLogicalExpression(expr.alternate)) expr.alternate = transformCondition(path.get("alternate"), true, true);
|
|
200
|
+
}
|
|
201
|
+
} else if (t.isLogicalExpression(expr)) {
|
|
202
|
+
let nextPath = path;
|
|
203
|
+
while (nextPath.node.operator !== "&&" && t.isLogicalExpression(nextPath.node.left)) nextPath = nextPath.get("left");
|
|
204
|
+
nextPath.node.operator === "&&" && isDynamic(nextPath.get("right"), {
|
|
205
|
+
checkTags: true,
|
|
206
|
+
checkMember: true
|
|
207
|
+
}) && (dTest = isDynamic(nextPath.get("left"), { checkMember: true }));
|
|
208
|
+
if (dTest) {
|
|
209
|
+
cond = nextPath.node.left;
|
|
210
|
+
if (!t.isBinaryExpression(cond)) cond = t.unaryExpression("!", t.unaryExpression("!", cond, true), true);
|
|
211
|
+
id = inline ? t.callExpression(memo, [t.arrowFunctionExpression([], cond)]) : path.scope.generateUidIdentifier("_c$");
|
|
212
|
+
nextPath.node.left = t.callExpression(id, []);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (dTest && !inline) {
|
|
216
|
+
const statements = [t.variableDeclaration("var", [t.variableDeclarator(id, config.memoWrapper ? t.callExpression(memo, [t.arrowFunctionExpression([], cond)]) : t.arrowFunctionExpression([], cond))]), t.arrowFunctionExpression([], expr)];
|
|
217
|
+
return deep ? t.callExpression(t.arrowFunctionExpression([], t.blockStatement([statements[0], t.returnStatement(statements[1])])), []) : statements;
|
|
218
|
+
}
|
|
219
|
+
return deep ? expr : t.arrowFunctionExpression([], expr);
|
|
220
|
+
}
|
|
221
|
+
function escapeHTML(s, attr) {
|
|
222
|
+
if (typeof s !== "string") return s;
|
|
223
|
+
const delim = attr ? "\"" : "<";
|
|
224
|
+
const escDelim = attr ? """ : "<";
|
|
225
|
+
let iDelim = s.indexOf(delim);
|
|
226
|
+
let iAmp = s.indexOf("&");
|
|
227
|
+
if (iDelim < 0 && iAmp < 0) return s;
|
|
228
|
+
let left = 0, out = "";
|
|
229
|
+
while (iDelim >= 0 && iAmp >= 0) if (iDelim < iAmp) {
|
|
230
|
+
if (left < iDelim) out += s.substring(left, iDelim);
|
|
231
|
+
out += escDelim;
|
|
232
|
+
left = iDelim + 1;
|
|
233
|
+
iDelim = s.indexOf(delim, left);
|
|
234
|
+
} else {
|
|
235
|
+
if (left < iAmp) out += s.substring(left, iAmp);
|
|
236
|
+
out += "&";
|
|
237
|
+
left = iAmp + 1;
|
|
238
|
+
iAmp = s.indexOf("&", left);
|
|
239
|
+
}
|
|
240
|
+
if (iDelim >= 0) do {
|
|
241
|
+
if (left < iDelim) out += s.substring(left, iDelim);
|
|
242
|
+
out += escDelim;
|
|
243
|
+
left = iDelim + 1;
|
|
244
|
+
iDelim = s.indexOf(delim, left);
|
|
245
|
+
} while (iDelim >= 0);
|
|
246
|
+
else while (iAmp >= 0) {
|
|
247
|
+
if (left < iAmp) out += s.substring(left, iAmp);
|
|
248
|
+
out += "&";
|
|
249
|
+
left = iAmp + 1;
|
|
250
|
+
iAmp = s.indexOf("&", left);
|
|
251
|
+
}
|
|
252
|
+
return left < s.length ? out + s.substring(left) : out;
|
|
253
|
+
}
|
|
254
|
+
function convertJSXIdentifier(node) {
|
|
255
|
+
if (t.isJSXIdentifier(node)) if (t.isValidIdentifier(node.name)) node.type = "Identifier";
|
|
256
|
+
else return t.stringLiteral(node.name);
|
|
257
|
+
else if (t.isJSXMemberExpression(node)) return t.memberExpression(convertJSXIdentifier(node.object), convertJSXIdentifier(node.property));
|
|
258
|
+
else if (t.isJSXNamespacedName(node)) return t.stringLiteral(`${node.namespace.name}:${node.name.name}`);
|
|
259
|
+
return node;
|
|
260
|
+
}
|
|
261
|
+
function canNativeSpread(key, { checkNameSpaces } = {}) {
|
|
262
|
+
if (checkNameSpaces && key.includes(":") && nonSpreadNameSpaces.has(key.split(":")[0])) return false;
|
|
263
|
+
if (key === "ref") return false;
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
const chars = "etaoinshrdlucwmfygpbTAOISWCBvkxjqzPHFMDRELNGUKVYJQZX_$";
|
|
267
|
+
const base = 54;
|
|
268
|
+
function getNumberedId(num) {
|
|
269
|
+
let out = "";
|
|
270
|
+
do {
|
|
271
|
+
const digit = num % base;
|
|
272
|
+
num = Math.floor(num / base);
|
|
273
|
+
out = chars[digit] + out;
|
|
274
|
+
} while (num !== 0);
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
function escapeStringForTemplate(str) {
|
|
278
|
+
return str.replace(/[{\\`\n\t\b\f\v\r\u2028\u2029]/g, (ch) => templateEscapes.get(ch));
|
|
279
|
+
}
|
|
280
|
+
const templateEscapes = new Map([
|
|
281
|
+
["{", "\\{"],
|
|
282
|
+
["`", "\\`"],
|
|
283
|
+
["\\", "\\\\"],
|
|
284
|
+
["\n", "\\n"],
|
|
285
|
+
[" ", "\\t"],
|
|
286
|
+
["\b", "\\b"],
|
|
287
|
+
["\f", "\\f"],
|
|
288
|
+
["\v", "\\v"],
|
|
289
|
+
["\r", "\\r"],
|
|
290
|
+
["\u2028", "\\u2028"],
|
|
291
|
+
["\u2029", "\\u2029"]
|
|
292
|
+
]);
|
|
293
|
+
|
|
294
|
+
//#endregion
|
|
295
|
+
//#region src/babel-plugin-jsx-dom-expressions/dom-expressions/constants.js
|
|
296
|
+
const booleans = [
|
|
297
|
+
"allowfullscreen",
|
|
298
|
+
"async",
|
|
299
|
+
"alpha",
|
|
300
|
+
"autofocus",
|
|
301
|
+
"autoplay",
|
|
302
|
+
"checked",
|
|
303
|
+
"controls",
|
|
304
|
+
"default",
|
|
305
|
+
"disabled",
|
|
306
|
+
"formnovalidate",
|
|
307
|
+
"hidden",
|
|
308
|
+
"indeterminate",
|
|
309
|
+
"inert",
|
|
310
|
+
"ismap",
|
|
311
|
+
"loop",
|
|
312
|
+
"multiple",
|
|
313
|
+
"muted",
|
|
314
|
+
"nomodule",
|
|
315
|
+
"novalidate",
|
|
316
|
+
"open",
|
|
317
|
+
"playsinline",
|
|
318
|
+
"readonly",
|
|
319
|
+
"required",
|
|
320
|
+
"reversed",
|
|
321
|
+
"seamless",
|
|
322
|
+
"selected",
|
|
323
|
+
"adauctionheaders",
|
|
324
|
+
"browsingtopics",
|
|
325
|
+
"credentialless",
|
|
326
|
+
"defaultchecked",
|
|
327
|
+
"defaultmuted",
|
|
328
|
+
"defaultselected",
|
|
329
|
+
"defer",
|
|
330
|
+
"disablepictureinpicture",
|
|
331
|
+
"disableremoteplayback",
|
|
332
|
+
"preservespitch",
|
|
333
|
+
"shadowrootclonable",
|
|
334
|
+
"shadowrootcustomelementregistry",
|
|
335
|
+
"shadowrootdelegatesfocus",
|
|
336
|
+
"shadowrootserializable",
|
|
337
|
+
"sharedstoragewritable"
|
|
338
|
+
];
|
|
339
|
+
const BooleanAttributes = /* @__PURE__ */ new Set(booleans);
|
|
340
|
+
const Properties = /* @__PURE__ */ new Set([
|
|
341
|
+
"className",
|
|
342
|
+
"value",
|
|
343
|
+
"readOnly",
|
|
344
|
+
"noValidate",
|
|
345
|
+
"formNoValidate",
|
|
346
|
+
"isMap",
|
|
347
|
+
"noModule",
|
|
348
|
+
"playsInline",
|
|
349
|
+
"adAuctionHeaders",
|
|
350
|
+
"allowFullscreen",
|
|
351
|
+
"browsingTopics",
|
|
352
|
+
"defaultChecked",
|
|
353
|
+
"defaultMuted",
|
|
354
|
+
"defaultSelected",
|
|
355
|
+
"disablePictureInPicture",
|
|
356
|
+
"disableRemotePlayback",
|
|
357
|
+
"preservesPitch",
|
|
358
|
+
"shadowRootClonable",
|
|
359
|
+
"shadowRootCustomElementRegistry",
|
|
360
|
+
"shadowRootDelegatesFocus",
|
|
361
|
+
"shadowRootSerializable",
|
|
362
|
+
"sharedStorageWritable",
|
|
363
|
+
...booleans
|
|
364
|
+
]);
|
|
365
|
+
const ChildProperties = /* @__PURE__ */ new Set([
|
|
366
|
+
"innerHTML",
|
|
367
|
+
"textContent",
|
|
368
|
+
"innerText",
|
|
369
|
+
"children"
|
|
370
|
+
]);
|
|
371
|
+
const Aliases = /* @__PURE__ */ Object.assign(Object.create(null), {
|
|
372
|
+
className: "class",
|
|
373
|
+
htmlFor: "for"
|
|
374
|
+
});
|
|
375
|
+
const PropAliases = /* @__PURE__ */ Object.assign(Object.create(null), {
|
|
376
|
+
class: "className",
|
|
377
|
+
novalidate: {
|
|
378
|
+
$: "noValidate",
|
|
379
|
+
FORM: 1
|
|
380
|
+
},
|
|
381
|
+
formnovalidate: {
|
|
382
|
+
$: "formNoValidate",
|
|
383
|
+
BUTTON: 1,
|
|
384
|
+
INPUT: 1
|
|
385
|
+
},
|
|
386
|
+
ismap: {
|
|
387
|
+
$: "isMap",
|
|
388
|
+
IMG: 1
|
|
389
|
+
},
|
|
390
|
+
nomodule: {
|
|
391
|
+
$: "noModule",
|
|
392
|
+
SCRIPT: 1
|
|
393
|
+
},
|
|
394
|
+
playsinline: {
|
|
395
|
+
$: "playsInline",
|
|
396
|
+
VIDEO: 1
|
|
397
|
+
},
|
|
398
|
+
readonly: {
|
|
399
|
+
$: "readOnly",
|
|
400
|
+
INPUT: 1,
|
|
401
|
+
TEXTAREA: 1
|
|
402
|
+
},
|
|
403
|
+
adauctionheaders: {
|
|
404
|
+
$: "adAuctionHeaders",
|
|
405
|
+
IFRAME: 1
|
|
406
|
+
},
|
|
407
|
+
allowfullscreen: {
|
|
408
|
+
$: "allowFullscreen",
|
|
409
|
+
IFRAME: 1
|
|
410
|
+
},
|
|
411
|
+
browsingtopics: {
|
|
412
|
+
$: "browsingTopics",
|
|
413
|
+
IMG: 1
|
|
414
|
+
},
|
|
415
|
+
defaultchecked: {
|
|
416
|
+
$: "defaultChecked",
|
|
417
|
+
INPUT: 1
|
|
418
|
+
},
|
|
419
|
+
defaultmuted: {
|
|
420
|
+
$: "defaultMuted",
|
|
421
|
+
AUDIO: 1,
|
|
422
|
+
VIDEO: 1
|
|
423
|
+
},
|
|
424
|
+
defaultselected: {
|
|
425
|
+
$: "defaultSelected",
|
|
426
|
+
OPTION: 1
|
|
427
|
+
},
|
|
428
|
+
disablepictureinpicture: {
|
|
429
|
+
$: "disablePictureInPicture",
|
|
430
|
+
VIDEO: 1
|
|
431
|
+
},
|
|
432
|
+
disableremoteplayback: {
|
|
433
|
+
$: "disableRemotePlayback",
|
|
434
|
+
AUDIO: 1,
|
|
435
|
+
VIDEO: 1
|
|
436
|
+
},
|
|
437
|
+
preservespitch: {
|
|
438
|
+
$: "preservesPitch",
|
|
439
|
+
AUDIO: 1,
|
|
440
|
+
VIDEO: 1
|
|
441
|
+
},
|
|
442
|
+
shadowrootclonable: {
|
|
443
|
+
$: "shadowRootClonable",
|
|
444
|
+
TEMPLATE: 1
|
|
445
|
+
},
|
|
446
|
+
shadowrootdelegatesfocus: {
|
|
447
|
+
$: "shadowRootDelegatesFocus",
|
|
448
|
+
TEMPLATE: 1
|
|
449
|
+
},
|
|
450
|
+
shadowrootserializable: {
|
|
451
|
+
$: "shadowRootSerializable",
|
|
452
|
+
TEMPLATE: 1
|
|
453
|
+
},
|
|
454
|
+
sharedstoragewritable: {
|
|
455
|
+
$: "sharedStorageWritable",
|
|
456
|
+
IFRAME: 1,
|
|
457
|
+
IMG: 1
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
function getPropAlias(prop, tagName) {
|
|
461
|
+
const a = PropAliases[prop];
|
|
462
|
+
return typeof a === "object" ? a[tagName] ? a["$"] : void 0 : a;
|
|
463
|
+
}
|
|
464
|
+
const DelegatedEvents = /* @__PURE__ */ new Set([
|
|
465
|
+
"beforeinput",
|
|
466
|
+
"click",
|
|
467
|
+
"dblclick",
|
|
468
|
+
"contextmenu",
|
|
469
|
+
"focusin",
|
|
470
|
+
"focusout",
|
|
471
|
+
"input",
|
|
472
|
+
"keydown",
|
|
473
|
+
"keyup",
|
|
474
|
+
"mousedown",
|
|
475
|
+
"mousemove",
|
|
476
|
+
"mouseout",
|
|
477
|
+
"mouseover",
|
|
478
|
+
"mouseup",
|
|
479
|
+
"pointerdown",
|
|
480
|
+
"pointermove",
|
|
481
|
+
"pointerout",
|
|
482
|
+
"pointerover",
|
|
483
|
+
"pointerup",
|
|
484
|
+
"touchend",
|
|
485
|
+
"touchmove",
|
|
486
|
+
"touchstart"
|
|
487
|
+
]);
|
|
488
|
+
const SVGElements = /* @__PURE__ */ new Set([
|
|
489
|
+
"altGlyph",
|
|
490
|
+
"altGlyphDef",
|
|
491
|
+
"altGlyphItem",
|
|
492
|
+
"animate",
|
|
493
|
+
"animateColor",
|
|
494
|
+
"animateMotion",
|
|
495
|
+
"animateTransform",
|
|
496
|
+
"circle",
|
|
497
|
+
"clipPath",
|
|
498
|
+
"color-profile",
|
|
499
|
+
"cursor",
|
|
500
|
+
"defs",
|
|
501
|
+
"desc",
|
|
502
|
+
"ellipse",
|
|
503
|
+
"feBlend",
|
|
504
|
+
"feColorMatrix",
|
|
505
|
+
"feComponentTransfer",
|
|
506
|
+
"feComposite",
|
|
507
|
+
"feConvolveMatrix",
|
|
508
|
+
"feDiffuseLighting",
|
|
509
|
+
"feDisplacementMap",
|
|
510
|
+
"feDistantLight",
|
|
511
|
+
"feDropShadow",
|
|
512
|
+
"feFlood",
|
|
513
|
+
"feFuncA",
|
|
514
|
+
"feFuncB",
|
|
515
|
+
"feFuncG",
|
|
516
|
+
"feFuncR",
|
|
517
|
+
"feGaussianBlur",
|
|
518
|
+
"feImage",
|
|
519
|
+
"feMerge",
|
|
520
|
+
"feMergeNode",
|
|
521
|
+
"feMorphology",
|
|
522
|
+
"feOffset",
|
|
523
|
+
"fePointLight",
|
|
524
|
+
"feSpecularLighting",
|
|
525
|
+
"feSpotLight",
|
|
526
|
+
"feTile",
|
|
527
|
+
"feTurbulence",
|
|
528
|
+
"filter",
|
|
529
|
+
"font",
|
|
530
|
+
"font-face",
|
|
531
|
+
"font-face-format",
|
|
532
|
+
"font-face-name",
|
|
533
|
+
"font-face-src",
|
|
534
|
+
"font-face-uri",
|
|
535
|
+
"foreignObject",
|
|
536
|
+
"g",
|
|
537
|
+
"glyph",
|
|
538
|
+
"glyphRef",
|
|
539
|
+
"hkern",
|
|
540
|
+
"image",
|
|
541
|
+
"line",
|
|
542
|
+
"linearGradient",
|
|
543
|
+
"marker",
|
|
544
|
+
"mask",
|
|
545
|
+
"metadata",
|
|
546
|
+
"missing-glyph",
|
|
547
|
+
"mpath",
|
|
548
|
+
"path",
|
|
549
|
+
"pattern",
|
|
550
|
+
"polygon",
|
|
551
|
+
"polyline",
|
|
552
|
+
"radialGradient",
|
|
553
|
+
"rect",
|
|
554
|
+
"set",
|
|
555
|
+
"stop",
|
|
556
|
+
"svg",
|
|
557
|
+
"switch",
|
|
558
|
+
"symbol",
|
|
559
|
+
"text",
|
|
560
|
+
"textPath",
|
|
561
|
+
"tref",
|
|
562
|
+
"tspan",
|
|
563
|
+
"use",
|
|
564
|
+
"view",
|
|
565
|
+
"vkern"
|
|
566
|
+
]);
|
|
567
|
+
const SVGNamespace = {
|
|
568
|
+
xlink: "http://www.w3.org/1999/xlink",
|
|
569
|
+
xml: "http://www.w3.org/XML/1998/namespace"
|
|
570
|
+
};
|
|
571
|
+
|
|
572
|
+
//#endregion
|
|
573
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/VoidElements.ts
|
|
574
|
+
const voidElements = [
|
|
575
|
+
"area",
|
|
576
|
+
"base",
|
|
577
|
+
"br",
|
|
578
|
+
"col",
|
|
579
|
+
"embed",
|
|
580
|
+
"hr",
|
|
581
|
+
"img",
|
|
582
|
+
"input",
|
|
583
|
+
"keygen",
|
|
584
|
+
"link",
|
|
585
|
+
"menuitem",
|
|
586
|
+
"meta",
|
|
587
|
+
"param",
|
|
588
|
+
"source",
|
|
589
|
+
"track",
|
|
590
|
+
"wbr"
|
|
591
|
+
];
|
|
592
|
+
var VoidElements_default = voidElements;
|
|
593
|
+
|
|
594
|
+
//#endregion
|
|
595
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/ssr/template.ts
|
|
596
|
+
function createTemplate$2(path, result) {
|
|
597
|
+
if (!result.template) return result.exprs[0];
|
|
598
|
+
let template$1, id;
|
|
599
|
+
if (!Array.isArray(result.template)) template$1 = t.stringLiteral(result.template);
|
|
600
|
+
else if (result.template.length === 1) template$1 = t.stringLiteral(result.template[0]);
|
|
601
|
+
else {
|
|
602
|
+
const strings = result.template.map((tmpl) => t.stringLiteral(tmpl));
|
|
603
|
+
template$1 = t.arrayExpression(strings);
|
|
604
|
+
}
|
|
605
|
+
const templates = path.scope.getProgramParent().data.templates || (path.scope.getProgramParent().data.templates = []);
|
|
606
|
+
const found = templates.find((tmp) => {
|
|
607
|
+
if (t.isArrayExpression(tmp.template) && t.isArrayExpression(template$1)) return tmp.template.elements.every((el, i) => template$1.elements[i] && el.value === template$1.elements[i].value);
|
|
608
|
+
return tmp.template.value === template$1.value;
|
|
609
|
+
});
|
|
610
|
+
if (!found) {
|
|
611
|
+
id = path.scope.generateUidIdentifier("tmpl$");
|
|
612
|
+
templates.push({
|
|
613
|
+
id,
|
|
614
|
+
template: template$1,
|
|
615
|
+
templateWithClosingTags: template$1,
|
|
616
|
+
renderer: "ssr"
|
|
617
|
+
});
|
|
618
|
+
} else id = found.id;
|
|
619
|
+
if (result.wontEscape) {
|
|
620
|
+
if (!Array.isArray(result.template) || result.template.length === 1) return id;
|
|
621
|
+
else if (Array.isArray(result.template) && result.template.length === 2 && result.templateValues[0].type === "CallExpression" && result.templateValues[0].callee.name === "_$ssrHydrationKey") return t.binaryExpression("+", t.binaryExpression("+", t.memberExpression(id, t.numericLiteral(0), true), result.templateValues[0]), t.memberExpression(id, t.numericLiteral(1), true));
|
|
622
|
+
}
|
|
623
|
+
return t.callExpression(registerImportMethod(path, "ssr"), Array.isArray(result.template) && result.template.length > 1 ? [id, ...result.templateValues] : [id]);
|
|
624
|
+
}
|
|
625
|
+
function appendTemplates$1(path, templates) {
|
|
626
|
+
const declarators = templates.map((template$1) => {
|
|
627
|
+
return t.variableDeclarator(template$1.id, template$1.template);
|
|
628
|
+
});
|
|
629
|
+
path.node.body.unshift(t.variableDeclaration("var", declarators));
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
//#endregion
|
|
633
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/ssr/element.ts
|
|
634
|
+
function appendToTemplate(template$1, value) {
|
|
635
|
+
let array;
|
|
636
|
+
if (Array.isArray(value)) [value, ...array] = value;
|
|
637
|
+
template$1[template$1.length - 1] += value;
|
|
638
|
+
if (array && array.length) template$1.push.apply(template$1, array);
|
|
639
|
+
}
|
|
640
|
+
function transformElement$3(path, info) {
|
|
641
|
+
const config = getConfig(path);
|
|
642
|
+
const tagName = getTagName(path.node);
|
|
643
|
+
if (tagName === "script" || tagName === "style") path.doNotEscape = true;
|
|
644
|
+
if (path.node.openingElement.attributes.some((a) => t.isJSXSpreadAttribute(a))) return createElement(path, {
|
|
645
|
+
...info,
|
|
646
|
+
...config
|
|
647
|
+
});
|
|
648
|
+
const voidTag = VoidElements_default.indexOf(tagName) > -1, results = {
|
|
649
|
+
template: [`<${tagName}`],
|
|
650
|
+
templateValues: [],
|
|
651
|
+
declarations: [],
|
|
652
|
+
exprs: [],
|
|
653
|
+
dynamics: [],
|
|
654
|
+
tagName,
|
|
655
|
+
wontEscape: path.node.wontEscape,
|
|
656
|
+
renderer: "ssr"
|
|
657
|
+
};
|
|
658
|
+
if (info.topLevel && config.hydratable) {
|
|
659
|
+
if (tagName === "head") {
|
|
660
|
+
registerImportMethod(path, "NoHydration");
|
|
661
|
+
registerImportMethod(path, "createComponent");
|
|
662
|
+
const child = transformElement$3(path, {
|
|
663
|
+
...info,
|
|
664
|
+
topLevel: false
|
|
665
|
+
});
|
|
666
|
+
results.template = "";
|
|
667
|
+
results.templateWithClosingTags = "";
|
|
668
|
+
results.exprs.push(t.callExpression(t.identifier("_$createComponent"), [t.identifier("_$NoHydration"), t.objectExpression([t.objectMethod("get", t.identifier("children"), [], t.blockStatement([t.returnStatement(createTemplate$2(path, child))]))])]));
|
|
669
|
+
return results;
|
|
670
|
+
}
|
|
671
|
+
results.template.push("");
|
|
672
|
+
results.templateValues.push(t.callExpression(registerImportMethod(path, "ssrHydrationKey"), []));
|
|
673
|
+
}
|
|
674
|
+
transformAttributes$2(path, results, {
|
|
675
|
+
...config,
|
|
676
|
+
...info
|
|
677
|
+
});
|
|
678
|
+
appendToTemplate(results.template, ">");
|
|
679
|
+
if (!voidTag) {
|
|
680
|
+
transformChildren$2(path, results, {
|
|
681
|
+
...config,
|
|
682
|
+
...info
|
|
683
|
+
});
|
|
684
|
+
appendToTemplate(results.template, `</${tagName}>`);
|
|
685
|
+
}
|
|
686
|
+
return results;
|
|
687
|
+
}
|
|
688
|
+
function toAttribute(key, isSVG) {
|
|
689
|
+
key = Aliases[key] || key;
|
|
690
|
+
!isSVG && (key = key.toLowerCase());
|
|
691
|
+
return key;
|
|
692
|
+
}
|
|
693
|
+
function setAttr$2(attribute, results, name, value, isSVG) {
|
|
694
|
+
let parts;
|
|
695
|
+
if ((parts = name.split(":")) && parts[1] && reservedNameSpaces.has(parts[0])) {
|
|
696
|
+
name = parts[1];
|
|
697
|
+
parts[0];
|
|
698
|
+
}
|
|
699
|
+
name = toAttribute(name, isSVG);
|
|
700
|
+
const attr = t.callExpression(registerImportMethod(attribute, "ssrAttribute"), [
|
|
701
|
+
t.stringLiteral(name),
|
|
702
|
+
value,
|
|
703
|
+
t.booleanLiteral(false)
|
|
704
|
+
]);
|
|
705
|
+
if (results.template[results.template.length - 1].length) {
|
|
706
|
+
results.template.push("");
|
|
707
|
+
results.templateValues.push(attr);
|
|
708
|
+
} else {
|
|
709
|
+
const last = results.templateValues.length - 1;
|
|
710
|
+
results.templateValues[last] = t.binaryExpression("+", results.templateValues[last], attr);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
function escapeExpression(path, expression, attr, escapeLiterals) {
|
|
714
|
+
if (t.isStringLiteral(expression) || t.isNumericLiteral(expression) || t.isTemplateLiteral(expression) && expression.expressions.length === 0) {
|
|
715
|
+
if (escapeLiterals) {
|
|
716
|
+
if (t.isStringLiteral(expression)) return t.stringLiteral(escapeHTML(expression.value, attr));
|
|
717
|
+
else if (t.isTemplateLiteral(expression)) return t.stringLiteral(escapeHTML(expression.quasis[0].value.raw, attr));
|
|
718
|
+
}
|
|
719
|
+
return expression;
|
|
720
|
+
} else if (t.isFunction(expression)) {
|
|
721
|
+
if (t.isBlockStatement(expression.body)) expression.body.body = expression.body.body.map((e) => {
|
|
722
|
+
if (t.isReturnStatement(e)) e.argument = escapeExpression(path, e.argument, attr, escapeLiterals);
|
|
723
|
+
return e;
|
|
724
|
+
});
|
|
725
|
+
else expression.body = escapeExpression(path, expression.body, attr, escapeLiterals);
|
|
726
|
+
return expression;
|
|
727
|
+
} else if (t.isTemplateLiteral(expression)) {
|
|
728
|
+
expression.expressions = expression.expressions.map((e) => escapeExpression(path, e, attr, escapeLiterals));
|
|
729
|
+
return expression;
|
|
730
|
+
} else if (t.isUnaryExpression(expression)) return expression;
|
|
731
|
+
else if (t.isBinaryExpression(expression)) {
|
|
732
|
+
expression.left = escapeExpression(path, expression.left, attr, escapeLiterals);
|
|
733
|
+
expression.right = escapeExpression(path, expression.right, attr, escapeLiterals);
|
|
734
|
+
return expression;
|
|
735
|
+
} else if (t.isConditionalExpression(expression)) {
|
|
736
|
+
expression.consequent = escapeExpression(path, expression.consequent, attr, escapeLiterals);
|
|
737
|
+
expression.alternate = escapeExpression(path, expression.alternate, attr, escapeLiterals);
|
|
738
|
+
return expression;
|
|
739
|
+
} else if (t.isLogicalExpression(expression)) {
|
|
740
|
+
expression.right = escapeExpression(path, expression.right, attr, escapeLiterals);
|
|
741
|
+
if (expression.operator !== "&&") expression.left = escapeExpression(path, expression.left, attr, escapeLiterals);
|
|
742
|
+
return expression;
|
|
743
|
+
} else if (t.isCallExpression(expression) && t.isFunction(expression.callee)) {
|
|
744
|
+
if (t.isBlockStatement(expression.callee.body)) expression.callee.body.body = expression.callee.body.body.map((e) => {
|
|
745
|
+
if (t.isReturnStatement(e)) e.argument = escapeExpression(path, e.argument, attr, escapeLiterals);
|
|
746
|
+
return e;
|
|
747
|
+
});
|
|
748
|
+
else expression.callee.body = escapeExpression(path, expression.callee.body, attr, escapeLiterals);
|
|
749
|
+
return expression;
|
|
750
|
+
} else if (t.isJSXElement(expression) && !isComponent(getTagName(expression))) {
|
|
751
|
+
expression.wontEscape = true;
|
|
752
|
+
return expression;
|
|
753
|
+
}
|
|
754
|
+
return t.callExpression(registerImportMethod(path, "escape"), [expression].concat(attr ? [t.booleanLiteral(true)] : []));
|
|
755
|
+
}
|
|
756
|
+
function transformToObject(attrName, attributes, selectedAttributes) {
|
|
757
|
+
const properties = [];
|
|
758
|
+
const existingAttribute = attributes.find((a) => a.node.name.name === attrName);
|
|
759
|
+
for (let i = 0; i < selectedAttributes.length; i++) {
|
|
760
|
+
const attr = selectedAttributes[i].node;
|
|
761
|
+
const computed = !t.isValidIdentifier(attr.name.name.name);
|
|
762
|
+
if (!computed) attr.name.name.type = "Identifier";
|
|
763
|
+
properties.push(t.objectProperty(computed ? t.stringLiteral(attr.name.name.name) : attr.name.name, t.isJSXExpressionContainer(attr.value) ? attr.value.expression : attr.value));
|
|
764
|
+
(existingAttribute || i) && attributes.splice(selectedAttributes[i].key, 1);
|
|
765
|
+
}
|
|
766
|
+
if (existingAttribute && t.isJSXExpressionContainer(existingAttribute.node.value) && t.isObjectExpression(existingAttribute.node.value.expression)) existingAttribute.node.value.expression.properties.push(...properties);
|
|
767
|
+
else selectedAttributes[0].node = t.jsxAttribute(t.jsxIdentifier(attrName), t.jsxExpressionContainer(t.objectExpression(properties)));
|
|
768
|
+
}
|
|
769
|
+
function normalizeAttributes(path) {
|
|
770
|
+
const attributes = path.get("openingElement").get("attributes"), styleAttributes = attributes.filter((a) => t.isJSXNamespacedName(a.node.name) && a.node.name.namespace.name === "style"), classNamespaceAttributes = attributes.filter((a) => t.isJSXNamespacedName(a.node.name) && a.node.name.namespace.name === "class");
|
|
771
|
+
if (classNamespaceAttributes.length) transformToObject("classList", attributes, classNamespaceAttributes);
|
|
772
|
+
const classAttributes = attributes.filter((a) => a.node.name && (a.node.name.name === "class" || a.node.name.name === "className" || a.node.name.name === "classList"));
|
|
773
|
+
if (classAttributes.length > 1) {
|
|
774
|
+
const first = classAttributes[0].node, values = [], quasis = [t.templateElement({ raw: "" })];
|
|
775
|
+
for (let i = 0; i < classAttributes.length; i++) {
|
|
776
|
+
const attr = classAttributes[i].node, isLast = i === classAttributes.length - 1;
|
|
777
|
+
if (!t.isJSXExpressionContainer(attr.value)) {
|
|
778
|
+
const prev = quasis.pop();
|
|
779
|
+
quasis.push(t.templateElement({ raw: (prev ? prev.value.raw : "") + `${attr.value.value}` + (isLast ? "" : " ") }));
|
|
780
|
+
} else {
|
|
781
|
+
let expr = attr.value.expression;
|
|
782
|
+
if (attr.name.name === "classList") {
|
|
783
|
+
if (t.isObjectExpression(expr) && !expr.properties.some((p) => t.isSpreadElement(p))) {
|
|
784
|
+
transformClasslistObject(path, expr, values, quasis);
|
|
785
|
+
if (!isLast) quasis[quasis.length - 1].value.raw += " ";
|
|
786
|
+
i && attributes.splice(attributes.indexOf(classAttributes[i]), 1);
|
|
787
|
+
continue;
|
|
788
|
+
}
|
|
789
|
+
expr = t.callExpression(registerImportMethod(path, "ssrClassList"), [expr]);
|
|
790
|
+
}
|
|
791
|
+
values.push(t.logicalExpression("||", expr, t.stringLiteral("")));
|
|
792
|
+
quasis.push(t.templateElement({ raw: isLast ? "" : " " }));
|
|
793
|
+
}
|
|
794
|
+
i && attributes.splice(attributes.indexOf(classAttributes[i]), 1);
|
|
795
|
+
}
|
|
796
|
+
first.name = t.jsxIdentifier("class");
|
|
797
|
+
first.value = t.jsxExpressionContainer(t.templateLiteral(quasis, values));
|
|
798
|
+
}
|
|
799
|
+
if (styleAttributes.length) transformToObject("style", attributes, styleAttributes);
|
|
800
|
+
return attributes;
|
|
801
|
+
}
|
|
802
|
+
function transformAttributes$2(path, results, info) {
|
|
803
|
+
const tagName = getTagName(path.node), isSVG = SVGElements.has(tagName), hasChildren = path.node.children.length > 0, attributes = normalizeAttributes(path);
|
|
804
|
+
let children;
|
|
805
|
+
attributes.forEach((attribute) => {
|
|
806
|
+
const node = attribute.node;
|
|
807
|
+
let value = node.value, key = t.isJSXNamespacedName(node.name) ? `${node.name.namespace.name}:${node.name.name.name}` : node.name.name, reservedNameSpace = t.isJSXNamespacedName(node.name) && reservedNameSpaces.has(node.name.namespace.name);
|
|
808
|
+
if ((t.isJSXNamespacedName(node.name) && reservedNameSpace || ChildProperties.has(key)) && !t.isJSXExpressionContainer(value)) node.value = value = t.jsxExpressionContainer(value || t.jsxEmptyExpression());
|
|
809
|
+
if (t.isJSXExpressionContainer(value) && (reservedNameSpace || ChildProperties.has(key) || !(t.isStringLiteral(value.expression) || t.isNumericLiteral(value.expression) || t.isBooleanLiteral(value.expression)))) if (key === "ref" || key.startsWith("use:") || key.startsWith("prop:") || key.startsWith("on")) return;
|
|
810
|
+
else if (ChildProperties.has(key)) {
|
|
811
|
+
if (info.hydratable && key === "textContent" && value && value.expression) value.expression = t.logicalExpression("||", value.expression, t.stringLiteral(" "));
|
|
812
|
+
if (key === "innerHTML") path.doNotEscape = true;
|
|
813
|
+
children = value;
|
|
814
|
+
} else {
|
|
815
|
+
let doEscape = true;
|
|
816
|
+
if (key.startsWith("attr:")) key = key.replace("attr:", "");
|
|
817
|
+
if (BooleanAttributes.has(key)) {
|
|
818
|
+
results.template.push("");
|
|
819
|
+
const fn = t.callExpression(registerImportMethod(attribute, "ssrAttribute"), [
|
|
820
|
+
t.stringLiteral(key),
|
|
821
|
+
value.expression,
|
|
822
|
+
t.booleanLiteral(true)
|
|
823
|
+
]);
|
|
824
|
+
results.templateValues.push(fn);
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
if (key === "style") {
|
|
828
|
+
if (t.isJSXExpressionContainer(value) && t.isObjectExpression(value.expression) && !value.expression.properties.some((p) => t.isSpreadElement(p))) {
|
|
829
|
+
const props = value.expression.properties.map((p, i) => t.callExpression(registerImportMethod(path, "ssrStyleProperty"), [t.stringLiteral((i ? ";" : "") + (t.isIdentifier(p.key) ? p.key.name : p.key.value) + ":"), escapeExpression(path, p.value, true, true)]));
|
|
830
|
+
let res = props[0];
|
|
831
|
+
for (let i = 1; i < props.length; i++) res = t.binaryExpression("+", res, props[i]);
|
|
832
|
+
value.expression = res;
|
|
833
|
+
} else value.expression = t.callExpression(registerImportMethod(path, "ssrStyle"), [value.expression]);
|
|
834
|
+
doEscape = false;
|
|
835
|
+
}
|
|
836
|
+
if (key === "classList") {
|
|
837
|
+
if (t.isObjectExpression(value.expression) && !value.expression.properties.some((p) => t.isSpreadElement(p))) {
|
|
838
|
+
const values = [], quasis = [t.templateElement({ raw: "" })];
|
|
839
|
+
transformClasslistObject(path, value.expression, values, quasis);
|
|
840
|
+
if (!values.length) value.expression = t.stringLiteral(quasis[0].value.raw);
|
|
841
|
+
else if (values.length === 1 && !quasis[0].value.raw && !quasis[1].value.raw) value.expression = values[0];
|
|
842
|
+
else value.expression = t.templateLiteral(quasis, values);
|
|
843
|
+
} else value.expression = t.callExpression(registerImportMethod(path, "ssrClassList"), [value.expression]);
|
|
844
|
+
key = "class";
|
|
845
|
+
doEscape = false;
|
|
846
|
+
}
|
|
847
|
+
if (doEscape) value.expression = escapeExpression(path, value.expression, true);
|
|
848
|
+
if (!doEscape || t.isLiteral(value.expression)) {
|
|
849
|
+
key = toAttribute(key, isSVG);
|
|
850
|
+
appendToTemplate(results.template, ` ${key}="`);
|
|
851
|
+
results.template.push(`"`);
|
|
852
|
+
results.templateValues.push(value.expression);
|
|
853
|
+
} else setAttr$2(attribute, results, key, value.expression, isSVG);
|
|
854
|
+
}
|
|
855
|
+
else {
|
|
856
|
+
if (key === "$ServerOnly") return;
|
|
857
|
+
if (t.isJSXExpressionContainer(value)) value = value.expression;
|
|
858
|
+
key = toAttribute(key, isSVG);
|
|
859
|
+
const isBoolean = BooleanAttributes.has(key);
|
|
860
|
+
if (isBoolean && value && value.value !== "" && !value.value) return;
|
|
861
|
+
appendToTemplate(results.template, ` ${key}`);
|
|
862
|
+
if (!value) return;
|
|
863
|
+
let text = isBoolean ? "" : value.value;
|
|
864
|
+
if (key === "style" || key === "class") {
|
|
865
|
+
text = trimWhitespace(String(text));
|
|
866
|
+
if (key === "style") text = text.replace(/; /g, ";").replace(/: /g, ":");
|
|
867
|
+
}
|
|
868
|
+
appendToTemplate(results.template, String(text) === "" ? `` : `="${escapeHTML(text, true)}"`);
|
|
869
|
+
}
|
|
870
|
+
});
|
|
871
|
+
if (!hasChildren && children) path.node.children.push(children);
|
|
872
|
+
}
|
|
873
|
+
function transformClasslistObject(path, expr, values, quasis) {
|
|
874
|
+
expr.properties.forEach((prop, i) => {
|
|
875
|
+
const isLast = expr.properties.length - 1 === i;
|
|
876
|
+
let key = prop.key;
|
|
877
|
+
if (t.isIdentifier(prop.key) && !prop.computed) key = t.stringLiteral(key.name);
|
|
878
|
+
else if (prop.computed) key = t.callExpression(registerImportMethod(path, "escape"), [prop.key, t.booleanLiteral(true)]);
|
|
879
|
+
else key = t.stringLiteral(escapeHTML(prop.key.value));
|
|
880
|
+
if (t.isBooleanLiteral(prop.value)) {
|
|
881
|
+
if (prop.value.value === true) if (!prop.computed) {
|
|
882
|
+
const prev = quasis.pop();
|
|
883
|
+
quasis.push(t.templateElement({ raw: (prev ? prev.value.raw : "") + (i ? " " : "") + `${key.value}` + (isLast ? "" : " ") }));
|
|
884
|
+
} else {
|
|
885
|
+
values.push(key);
|
|
886
|
+
quasis.push(t.templateElement({ raw: isLast ? "" : " " }));
|
|
887
|
+
}
|
|
888
|
+
} else {
|
|
889
|
+
values.push(t.conditionalExpression(prop.value, key, t.stringLiteral("")));
|
|
890
|
+
quasis.push(t.templateElement({ raw: isLast ? "" : " " }));
|
|
891
|
+
}
|
|
892
|
+
});
|
|
893
|
+
}
|
|
894
|
+
function transformChildren$2(path, results, { hydratable }) {
|
|
895
|
+
const doNotEscape = path.doNotEscape;
|
|
896
|
+
const filteredChildren = filterChildren(path.get("children"));
|
|
897
|
+
const multi = checkLength(filteredChildren), markers = hydratable && multi;
|
|
898
|
+
filteredChildren.forEach((node) => {
|
|
899
|
+
if (t.isJSXElement(node.node) && getTagName(node.node) === "head") {
|
|
900
|
+
const child$1 = transformNode(node, {
|
|
901
|
+
doNotEscape,
|
|
902
|
+
hydratable: false
|
|
903
|
+
});
|
|
904
|
+
registerImportMethod(path, "NoHydration");
|
|
905
|
+
registerImportMethod(path, "createComponent");
|
|
906
|
+
results.template.push("");
|
|
907
|
+
results.templateValues.push(t.callExpression(t.identifier("_$createComponent"), [t.identifier("_$NoHydration"), t.objectExpression([t.objectMethod("get", t.identifier("children"), [], t.blockStatement([t.returnStatement(createTemplate$2(path, child$1))]))])]));
|
|
908
|
+
return;
|
|
909
|
+
}
|
|
910
|
+
const child = transformNode(node, { doNotEscape });
|
|
911
|
+
if (!child) return;
|
|
912
|
+
appendToTemplate(results.template, child.template);
|
|
913
|
+
results.templateValues.push.apply(results.templateValues, child.templateValues || []);
|
|
914
|
+
if (child.exprs.length) {
|
|
915
|
+
if (!doNotEscape && !child.spreadElement) child.exprs[0] = escapeExpression(path, child.exprs[0]);
|
|
916
|
+
if (markers && !child.spreadElement) {
|
|
917
|
+
appendToTemplate(results.template, `<!--$-->`);
|
|
918
|
+
results.template.push("");
|
|
919
|
+
results.templateValues.push(child.exprs[0]);
|
|
920
|
+
appendToTemplate(results.template, `<!--/-->`);
|
|
921
|
+
} else {
|
|
922
|
+
results.template.push("");
|
|
923
|
+
results.templateValues.push(child.exprs[0]);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
});
|
|
927
|
+
}
|
|
928
|
+
function createElement(path, { topLevel, hydratable }) {
|
|
929
|
+
const tagName = getTagName(path.node), config = getConfig(path), attributes = normalizeAttributes(path), doNotEscape = path.doNotEscape;
|
|
930
|
+
const filteredChildren = filterChildren(path.get("children")), multi = checkLength(filteredChildren), markers = hydratable && multi, childNodes = filteredChildren.reduce((memo, path$1) => {
|
|
931
|
+
if (t.isJSXText(path$1.node)) {
|
|
932
|
+
const v$1 = decode(trimWhitespace(path$1.node.extra.raw));
|
|
933
|
+
if (v$1.length) memo.push(t.stringLiteral(v$1));
|
|
934
|
+
} else {
|
|
935
|
+
const child = transformNode(path$1);
|
|
936
|
+
if (markers && child.exprs.length && !child.spreadElement) memo.push(t.stringLiteral("<!--$-->"));
|
|
937
|
+
if (child.exprs.length && !doNotEscape && !child.spreadElement) child.exprs[0] = escapeExpression(path$1, child.exprs[0]);
|
|
938
|
+
memo.push(getCreateTemplate(config, path$1, child)(path$1, child, true));
|
|
939
|
+
if (markers && child.exprs.length && !child.spreadElement) memo.push(t.stringLiteral("<!--/-->"));
|
|
940
|
+
}
|
|
941
|
+
return memo;
|
|
942
|
+
}, []);
|
|
943
|
+
let props;
|
|
944
|
+
if (attributes.length === 1) props = [attributes[0].node.argument];
|
|
945
|
+
else {
|
|
946
|
+
props = [];
|
|
947
|
+
let runningObject = [], dynamicSpread = false, hasChildren = path.node.children.length > 0;
|
|
948
|
+
attributes.forEach((attribute) => {
|
|
949
|
+
const node = attribute.node;
|
|
950
|
+
if (t.isJSXSpreadAttribute(node)) {
|
|
951
|
+
if (runningObject.length) {
|
|
952
|
+
props.push(t.objectExpression(runningObject));
|
|
953
|
+
runningObject = [];
|
|
954
|
+
}
|
|
955
|
+
props.push(isDynamic(attribute.get("argument"), { checkMember: true }) && (dynamicSpread = true) ? t.isCallExpression(node.argument) && !node.argument.arguments.length && !t.isCallExpression(node.argument.callee) && !t.isMemberExpression(node.argument.callee) ? node.argument.callee : t.arrowFunctionExpression([], node.argument) : node.argument);
|
|
956
|
+
} else {
|
|
957
|
+
const value = node.value || t.booleanLiteral(true), id = convertJSXIdentifier(node.name), key = t.isJSXNamespacedName(node.name) ? `${node.name.namespace.name}:${node.name.name.name}` : node.name.name;
|
|
958
|
+
if (hasChildren && key === "children") return;
|
|
959
|
+
if (key === "ref" || key.startsWith("use:") || key.startsWith("prop:") || key.startsWith("on")) return;
|
|
960
|
+
if (t.isJSXExpressionContainer(value)) if (isDynamic(attribute.get("value").get("expression"), {
|
|
961
|
+
checkMember: true,
|
|
962
|
+
checkTags: true
|
|
963
|
+
})) {
|
|
964
|
+
let expr = t.arrowFunctionExpression([], value.expression);
|
|
965
|
+
runningObject.push(t.objectMethod("get", id, [], t.blockStatement([t.returnStatement(expr.body)]), !t.isValidIdentifier(key)));
|
|
966
|
+
} else runningObject.push(t.objectProperty(id, value.expression));
|
|
967
|
+
else runningObject.push(t.objectProperty(id, value));
|
|
968
|
+
}
|
|
969
|
+
});
|
|
970
|
+
if (runningObject.length || !props.length) props.push(t.objectExpression(runningObject));
|
|
971
|
+
if (props.length > 1 || dynamicSpread) props = [t.callExpression(registerImportMethod(path, "mergeProps"), props)];
|
|
972
|
+
}
|
|
973
|
+
return {
|
|
974
|
+
exprs: [t.callExpression(registerImportMethod(path, "ssrElement"), [
|
|
975
|
+
t.stringLiteral(tagName),
|
|
976
|
+
props[0],
|
|
977
|
+
childNodes.length ? hydratable ? t.arrowFunctionExpression([], childNodes.length === 1 ? childNodes[0] : t.arrayExpression(childNodes)) : childNodes.length === 1 ? childNodes[0] : t.arrayExpression(childNodes) : t.identifier("undefined"),
|
|
978
|
+
t.booleanLiteral(Boolean(topLevel && config.hydratable))
|
|
979
|
+
])],
|
|
980
|
+
template: "",
|
|
981
|
+
spreadElement: true
|
|
982
|
+
};
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
//#endregion
|
|
986
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/universal/element.ts
|
|
987
|
+
function transformElement$2(path, info) {
|
|
988
|
+
let tagName = getTagName(path.node), results = {
|
|
989
|
+
id: path.scope.generateUidIdentifier("el$"),
|
|
990
|
+
declarations: [],
|
|
991
|
+
exprs: [],
|
|
992
|
+
dynamics: [],
|
|
993
|
+
postExprs: [],
|
|
994
|
+
tagName,
|
|
995
|
+
renderer: "universal"
|
|
996
|
+
};
|
|
997
|
+
results.declarations.push(t.variableDeclarator(results.id, t.callExpression(registerImportMethod(path, "createElement", getRendererConfig(path, "universal").moduleName), [t.stringLiteral(tagName)])));
|
|
998
|
+
transformAttributes$1(path, results);
|
|
999
|
+
transformChildren$1(path, results);
|
|
1000
|
+
return results;
|
|
1001
|
+
}
|
|
1002
|
+
function transformAttributes$1(path, results) {
|
|
1003
|
+
let children, spreadExpr;
|
|
1004
|
+
let attributes = path.get("openingElement").get("attributes");
|
|
1005
|
+
const elem = results.id, hasChildren = path.node.children.length > 0, config = getConfig(path);
|
|
1006
|
+
if (attributes.some((attribute) => t.isJSXSpreadAttribute(attribute.node))) {
|
|
1007
|
+
[attributes, spreadExpr] = processSpreads$1(path, attributes, {
|
|
1008
|
+
elem,
|
|
1009
|
+
hasChildren,
|
|
1010
|
+
wrapConditionals: config.wrapConditionals
|
|
1011
|
+
});
|
|
1012
|
+
path.get("openingElement").set("attributes", attributes.map((a) => a.node));
|
|
1013
|
+
}
|
|
1014
|
+
path.get("openingElement").get("attributes").forEach((attribute) => {
|
|
1015
|
+
const node = attribute.node;
|
|
1016
|
+
let value = node.value, key = t.isJSXNamespacedName(node.name) ? `${node.name.namespace.name}:${node.name.name.name}` : node.name.name, reservedNameSpace = t.isJSXNamespacedName(node.name) && node.name.namespace.name === "use";
|
|
1017
|
+
if (t.isJSXNamespacedName(node.name) && reservedNameSpace && !t.isJSXExpressionContainer(value)) node.value = value = t.jsxExpressionContainer(value || t.jsxEmptyExpression());
|
|
1018
|
+
if (t.isJSXExpressionContainer(value)) if (key === "ref") {
|
|
1019
|
+
while (t.isTSNonNullExpression(value.expression) || t.isTSAsExpression(value.expression)) value.expression = value.expression.expression;
|
|
1020
|
+
let binding, isConstant = t.isIdentifier(value.expression) && (binding = path.scope.getBinding(value.expression.name)) && (binding.kind === "const" || binding.kind === "module");
|
|
1021
|
+
if (!isConstant && t.isLVal(value.expression)) {
|
|
1022
|
+
const refIdentifier = path.scope.generateUidIdentifier("_ref$");
|
|
1023
|
+
results.exprs.unshift(t.variableDeclaration("var", [t.variableDeclarator(refIdentifier, value.expression)]), t.expressionStatement(t.conditionalExpression(t.binaryExpression("===", t.unaryExpression("typeof", refIdentifier), t.stringLiteral("function")), t.callExpression(registerImportMethod(path, "use", getRendererConfig(path, "universal").moduleName), [refIdentifier, elem]), t.assignmentExpression("=", value.expression, elem))));
|
|
1024
|
+
} else if (isConstant || t.isFunction(value.expression)) results.exprs.unshift(t.expressionStatement(t.callExpression(registerImportMethod(path, "use", getRendererConfig(path, "universal").moduleName), [value.expression, elem])));
|
|
1025
|
+
else {
|
|
1026
|
+
const refIdentifier = path.scope.generateUidIdentifier("_ref$");
|
|
1027
|
+
results.exprs.unshift(t.variableDeclaration("var", [t.variableDeclarator(refIdentifier, value.expression)]), t.expressionStatement(t.logicalExpression("&&", t.binaryExpression("===", t.unaryExpression("typeof", refIdentifier), t.stringLiteral("function")), t.callExpression(registerImportMethod(path, "use", getRendererConfig(path, "universal").moduleName), [refIdentifier, elem]))));
|
|
1028
|
+
}
|
|
1029
|
+
} else if (key.startsWith("use:")) {
|
|
1030
|
+
node.name.name.type = "Identifier";
|
|
1031
|
+
results.exprs.unshift(t.expressionStatement(t.callExpression(registerImportMethod(path, "use", getRendererConfig(path, "universal").moduleName), [
|
|
1032
|
+
node.name.name,
|
|
1033
|
+
elem,
|
|
1034
|
+
t.arrowFunctionExpression([], t.isJSXEmptyExpression(value.expression) ? t.booleanLiteral(true) : value.expression)
|
|
1035
|
+
])));
|
|
1036
|
+
} else if (key === "children") children = value;
|
|
1037
|
+
else if (config.effectWrapper && isDynamic(attribute.get("value").get("expression"), { checkMember: true })) results.dynamics.push({
|
|
1038
|
+
elem,
|
|
1039
|
+
key,
|
|
1040
|
+
value: value.expression
|
|
1041
|
+
});
|
|
1042
|
+
else results.exprs.push(t.expressionStatement(setAttr$1(attribute, elem, key, value.expression)));
|
|
1043
|
+
else results.exprs.push(t.expressionStatement(setAttr$1(attribute, elem, key, value)));
|
|
1044
|
+
});
|
|
1045
|
+
if (spreadExpr) results.exprs.push(spreadExpr);
|
|
1046
|
+
if (!hasChildren && children) path.node.children.push(children);
|
|
1047
|
+
}
|
|
1048
|
+
function setAttr$1(path, elem, name, value, { prevId } = {}) {
|
|
1049
|
+
if (!value) value = t.booleanLiteral(true);
|
|
1050
|
+
return t.callExpression(registerImportMethod(path, "setProp", getRendererConfig(path, "universal").moduleName), prevId ? [
|
|
1051
|
+
elem,
|
|
1052
|
+
t.stringLiteral(name),
|
|
1053
|
+
value,
|
|
1054
|
+
prevId
|
|
1055
|
+
] : [
|
|
1056
|
+
elem,
|
|
1057
|
+
t.stringLiteral(name),
|
|
1058
|
+
value
|
|
1059
|
+
]);
|
|
1060
|
+
}
|
|
1061
|
+
function transformChildren$1(path, results) {
|
|
1062
|
+
const filteredChildren = filterChildren(path.get("children")), multi = checkLength(filteredChildren), childNodes = filteredChildren.map(transformNode).reduce((memo, child) => {
|
|
1063
|
+
if (!child) return memo;
|
|
1064
|
+
const i = memo.length;
|
|
1065
|
+
if (child.text && i && memo[i - 1].text) {
|
|
1066
|
+
memo[i - 1].template += child.template;
|
|
1067
|
+
memo[i - 1].templateWithClosingTags += child.templateWithClosingTags || child.template;
|
|
1068
|
+
} else memo.push(child);
|
|
1069
|
+
return memo;
|
|
1070
|
+
}, []);
|
|
1071
|
+
const appends = [];
|
|
1072
|
+
childNodes.forEach((child, index) => {
|
|
1073
|
+
if (!child) return;
|
|
1074
|
+
if (child.tagName && child.renderer !== "universal") throw new Error(`<${child.tagName}> is not supported in <${getTagName(path.node)}>.
|
|
1075
|
+
Wrap the usage with a component that would render this element, eg. Canvas`);
|
|
1076
|
+
if (child.id) {
|
|
1077
|
+
let insertNode = registerImportMethod(path, "insertNode", getRendererConfig(path, "universal").moduleName);
|
|
1078
|
+
let insert = child.id;
|
|
1079
|
+
if (child.text) {
|
|
1080
|
+
let createTextNode = registerImportMethod(path, "createTextNode", getRendererConfig(path, "universal").moduleName);
|
|
1081
|
+
if (multi) results.declarations.push(t.variableDeclarator(child.id, t.callExpression(createTextNode, [t.templateLiteral([t.templateElement({ raw: escapeStringForTemplate(child.template) })], [])])));
|
|
1082
|
+
else insert = t.callExpression(createTextNode, [t.templateLiteral([t.templateElement({ raw: escapeStringForTemplate(child.template) })], [])]);
|
|
1083
|
+
}
|
|
1084
|
+
appends.push(t.expressionStatement(t.callExpression(insertNode, [results.id, insert])));
|
|
1085
|
+
results.declarations.push(...child.declarations);
|
|
1086
|
+
results.exprs.push(...child.exprs);
|
|
1087
|
+
results.dynamics.push(...child.dynamics);
|
|
1088
|
+
} else if (child.exprs.length) {
|
|
1089
|
+
let insert = registerImportMethod(path, "insert", getRendererConfig(path, "universal").moduleName);
|
|
1090
|
+
if (multi) results.exprs.push(t.expressionStatement(t.callExpression(insert, [
|
|
1091
|
+
results.id,
|
|
1092
|
+
child.exprs[0],
|
|
1093
|
+
nextChild$1(childNodes, index) || t.nullLiteral()
|
|
1094
|
+
])));
|
|
1095
|
+
else results.exprs.push(t.expressionStatement(t.callExpression(insert, [results.id, child.exprs[0]])));
|
|
1096
|
+
}
|
|
1097
|
+
});
|
|
1098
|
+
results.exprs.unshift(...appends);
|
|
1099
|
+
}
|
|
1100
|
+
function nextChild$1(children, index) {
|
|
1101
|
+
return children[index + 1] && (children[index + 1].id || nextChild$1(children, index + 1));
|
|
1102
|
+
}
|
|
1103
|
+
function processSpreads$1(path, attributes, { elem, hasChildren, wrapConditionals }) {
|
|
1104
|
+
const filteredAttributes = [];
|
|
1105
|
+
const spreadArgs = [];
|
|
1106
|
+
let runningObject = [];
|
|
1107
|
+
let dynamicSpread = false;
|
|
1108
|
+
let firstSpread = false;
|
|
1109
|
+
attributes.forEach((attribute) => {
|
|
1110
|
+
const node = attribute.node;
|
|
1111
|
+
const key = !t.isJSXSpreadAttribute(node) && (t.isJSXNamespacedName(node.name) ? `${node.name.namespace.name}:${node.name.name.name}` : node.name.name);
|
|
1112
|
+
if (t.isJSXSpreadAttribute(node)) {
|
|
1113
|
+
firstSpread = true;
|
|
1114
|
+
if (runningObject.length) {
|
|
1115
|
+
spreadArgs.push(t.objectExpression(runningObject));
|
|
1116
|
+
runningObject = [];
|
|
1117
|
+
}
|
|
1118
|
+
spreadArgs.push(isDynamic(attribute.get("argument"), { checkMember: true }) && (dynamicSpread = true) ? t.isCallExpression(node.argument) && !node.argument.arguments.length && !t.isCallExpression(node.argument.callee) && !t.isMemberExpression(node.argument.callee) ? node.argument.callee : t.arrowFunctionExpression([], node.argument) : node.argument);
|
|
1119
|
+
} else if ((firstSpread || t.isJSXExpressionContainer(node.value) && isDynamic(attribute.get("value").get("expression"), { checkMember: true })) && canNativeSpread(key, { checkNameSpaces: true })) {
|
|
1120
|
+
const isContainer = t.isJSXExpressionContainer(node.value);
|
|
1121
|
+
if (isContainer && isDynamic(attribute.get("value").get("expression"), { checkMember: true })) {
|
|
1122
|
+
const id = convertJSXIdentifier(node.name);
|
|
1123
|
+
let expr = wrapConditionals && (t.isLogicalExpression(node.value.expression) || t.isConditionalExpression(node.value.expression)) ? transformCondition(attribute.get("value").get("expression"), true) : t.arrowFunctionExpression([], node.value.expression);
|
|
1124
|
+
runningObject.push(t.objectMethod("get", id, [], t.blockStatement([t.returnStatement(expr.body)]), !t.isValidIdentifier(key)));
|
|
1125
|
+
} else runningObject.push(t.objectProperty(t.stringLiteral(key), isContainer ? node.value.expression : node.value || t.booleanLiteral(true)));
|
|
1126
|
+
} else filteredAttributes.push(attribute);
|
|
1127
|
+
});
|
|
1128
|
+
if (runningObject.length) spreadArgs.push(t.objectExpression(runningObject));
|
|
1129
|
+
const props = spreadArgs.length === 1 && !dynamicSpread ? spreadArgs[0] : t.callExpression(registerImportMethod(path, "mergeProps"), spreadArgs);
|
|
1130
|
+
return [filteredAttributes, t.expressionStatement(t.callExpression(registerImportMethod(path, "spread", getRendererConfig(path, "universal").moduleName), [
|
|
1131
|
+
elem,
|
|
1132
|
+
props,
|
|
1133
|
+
t.booleanLiteral(hasChildren)
|
|
1134
|
+
]))];
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
//#endregion
|
|
1138
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/universal/template.ts
|
|
1139
|
+
function createTemplate$1(path, result, wrap) {
|
|
1140
|
+
const config = getConfig(path);
|
|
1141
|
+
if (result.id) {
|
|
1142
|
+
result.decl = t.variableDeclaration("var", result.declarations);
|
|
1143
|
+
if (!(result.exprs.length || result.dynamics.length || result.postExprs.length) && result.decl.declarations.length === 1) return result.decl.declarations[0].init;
|
|
1144
|
+
else return t.callExpression(t.arrowFunctionExpression([], t.blockStatement([
|
|
1145
|
+
result.decl,
|
|
1146
|
+
...result.exprs.concat(wrapDynamics$1(path, result.dynamics) || [], result.postExprs || []),
|
|
1147
|
+
t.returnStatement(result.id)
|
|
1148
|
+
])), []);
|
|
1149
|
+
}
|
|
1150
|
+
if (wrap && result.dynamic && config.memoWrapper) return t.callExpression(registerImportMethod(path, config.memoWrapper), [result.exprs[0]]);
|
|
1151
|
+
return result.exprs[0];
|
|
1152
|
+
}
|
|
1153
|
+
function wrapDynamics$1(path, dynamics) {
|
|
1154
|
+
if (!dynamics.length) return;
|
|
1155
|
+
const effectWrapperId = registerImportMethod(path, getConfig(path).effectWrapper);
|
|
1156
|
+
if (dynamics.length === 1) {
|
|
1157
|
+
const prevValue = t.identifier("_$p");
|
|
1158
|
+
return t.expressionStatement(t.callExpression(effectWrapperId, [t.arrowFunctionExpression([prevValue], setAttr$1(path, dynamics[0].elem, dynamics[0].key, dynamics[0].value, {
|
|
1159
|
+
dynamic: true,
|
|
1160
|
+
prevId: prevValue
|
|
1161
|
+
}))]));
|
|
1162
|
+
}
|
|
1163
|
+
const prevId = t.identifier("_p$");
|
|
1164
|
+
/** @type {t.VariableDeclarator[]} */
|
|
1165
|
+
const declarations = [];
|
|
1166
|
+
/** @type {t.ExpressionStatement[]} */
|
|
1167
|
+
const statements = [];
|
|
1168
|
+
/** @type {t.Identifier[]} */
|
|
1169
|
+
const properties = [];
|
|
1170
|
+
dynamics.forEach(({ elem, key, value }, index) => {
|
|
1171
|
+
const varIdent = path.scope.generateUidIdentifier("v$");
|
|
1172
|
+
const propIdent = t.identifier(getNumberedId(index));
|
|
1173
|
+
const propMember = t.memberExpression(prevId, propIdent);
|
|
1174
|
+
properties.push(propIdent);
|
|
1175
|
+
declarations.push(t.variableDeclarator(varIdent, value));
|
|
1176
|
+
statements.push(t.expressionStatement(t.logicalExpression("&&", t.binaryExpression("!==", varIdent, propMember), t.assignmentExpression("=", propMember, setAttr$1(path, elem, key, varIdent, {
|
|
1177
|
+
dynamic: true,
|
|
1178
|
+
prevId: propMember
|
|
1179
|
+
})))));
|
|
1180
|
+
});
|
|
1181
|
+
return t.expressionStatement(t.callExpression(effectWrapperId, [t.arrowFunctionExpression([prevId], t.blockStatement([
|
|
1182
|
+
t.variableDeclaration("var", declarations),
|
|
1183
|
+
...statements,
|
|
1184
|
+
t.returnStatement(prevId)
|
|
1185
|
+
])), t.objectExpression(properties.map((id) => t.objectProperty(id, t.identifier("undefined"))))]));
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
//#endregion
|
|
1189
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/shared/component.ts
|
|
1190
|
+
function convertComponentIdentifier(node) {
|
|
1191
|
+
if (t.isJSXIdentifier(node)) {
|
|
1192
|
+
if (node.name === "this") return t.thisExpression();
|
|
1193
|
+
if (t.isValidIdentifier(node.name)) node.type = "Identifier";
|
|
1194
|
+
else return t.stringLiteral(node.name);
|
|
1195
|
+
} else if (t.isJSXMemberExpression(node)) {
|
|
1196
|
+
const prop = convertComponentIdentifier(node.property);
|
|
1197
|
+
const computed = t.isStringLiteral(prop);
|
|
1198
|
+
return t.memberExpression(convertComponentIdentifier(node.object), prop, computed);
|
|
1199
|
+
}
|
|
1200
|
+
return node;
|
|
1201
|
+
}
|
|
1202
|
+
function transformComponent(path) {
|
|
1203
|
+
let exprs = [], config = getConfig(path), tagId = convertComponentIdentifier(path.node.openingElement.name), props = [], runningObject = [], dynamicSpread = false, hasChildren = path.node.children.length > 0;
|
|
1204
|
+
if (config.builtIns.indexOf(tagId.name) > -1 && !path.scope.hasBinding(tagId.name)) tagId.name = registerImportMethod(path, tagId.name).name;
|
|
1205
|
+
path.get("openingElement").get("attributes").forEach((attribute) => {
|
|
1206
|
+
const node = attribute.node;
|
|
1207
|
+
if (t.isJSXSpreadAttribute(node)) {
|
|
1208
|
+
if (runningObject.length) {
|
|
1209
|
+
props.push(t.objectExpression(runningObject));
|
|
1210
|
+
runningObject = [];
|
|
1211
|
+
}
|
|
1212
|
+
props.push(isDynamic(attribute.get("argument"), { checkMember: true }) && (dynamicSpread = true) ? t.isCallExpression(node.argument) && !node.argument.arguments.length && !t.isCallExpression(node.argument.callee) && !t.isMemberExpression(node.argument.callee) ? node.argument.callee : t.arrowFunctionExpression([], node.argument) : node.argument);
|
|
1213
|
+
} else {
|
|
1214
|
+
const value = (t.isStringLiteral(node.value) ? t.stringLiteral(node.value.value) : node.value) || t.booleanLiteral(true), id = convertJSXIdentifier(node.name), key = id.name;
|
|
1215
|
+
if (hasChildren && key === "children") return;
|
|
1216
|
+
if (t.isJSXExpressionContainer(value)) if (key === "ref") {
|
|
1217
|
+
if (config.generate === "ssr") return;
|
|
1218
|
+
while (t.isTSNonNullExpression(value.expression) || t.isTSAsExpression(value.expression) || t.isTSSatisfiesExpression(value.expression)) value.expression = value.expression.expression;
|
|
1219
|
+
let binding, isConstant = t.isIdentifier(value.expression) && (binding = path.scope.getBinding(value.expression.name)) && (binding.kind === "const" || binding.kind === "module");
|
|
1220
|
+
if (!isConstant && t.isLVal(value.expression)) {
|
|
1221
|
+
const refIdentifier = path.scope.generateUidIdentifier("_ref$");
|
|
1222
|
+
runningObject.push(t.objectMethod("method", t.identifier("ref"), [t.identifier("r$")], t.blockStatement([t.variableDeclaration("var", [t.variableDeclarator(refIdentifier, value.expression)]), t.expressionStatement(t.conditionalExpression(t.binaryExpression("===", t.unaryExpression("typeof", refIdentifier), t.stringLiteral("function")), t.callExpression(refIdentifier, [t.identifier("r$")]), t.assignmentExpression("=", value.expression, t.identifier("r$"))))])));
|
|
1223
|
+
} else if (!isConstant && t.isOptionalMemberExpression(value.expression)) {
|
|
1224
|
+
const refIdentifier = path.scope.generateUidIdentifier("_ref$");
|
|
1225
|
+
runningObject.push(t.objectMethod("method", t.identifier("ref"), [t.identifier("r$")], t.blockStatement([t.variableDeclaration("var", [t.variableDeclarator(refIdentifier, value.expression)]), t.expressionStatement(t.conditionalExpression(t.binaryExpression("===", t.unaryExpression("typeof", refIdentifier), t.stringLiteral("function")), t.callExpression(refIdentifier, [t.identifier("r$")]), t.logicalExpression("&&", t.unaryExpression("!", t.unaryExpression("!", t.identifier(value.expression.object.name))), t.assignmentExpression("=", t.memberExpression(t.identifier(value.expression.object.name), t.identifier(value.expression.property.name)), t.identifier("r$")))))])));
|
|
1226
|
+
} else if (isConstant || t.isFunction(value.expression)) runningObject.push(t.objectProperty(t.identifier("ref"), value.expression));
|
|
1227
|
+
else if (t.isCallExpression(value.expression)) {
|
|
1228
|
+
const refIdentifier = path.scope.generateUidIdentifier("_ref$");
|
|
1229
|
+
runningObject.push(t.objectMethod("method", t.identifier("ref"), [t.identifier("r$")], t.blockStatement([t.variableDeclaration("var", [t.variableDeclarator(refIdentifier, value.expression)]), t.expressionStatement(t.logicalExpression("&&", t.binaryExpression("===", t.unaryExpression("typeof", refIdentifier), t.stringLiteral("function")), t.callExpression(refIdentifier, [t.identifier("r$")])))])));
|
|
1230
|
+
}
|
|
1231
|
+
} else if (isDynamic(attribute.get("value").get("expression"), {
|
|
1232
|
+
checkMember: true,
|
|
1233
|
+
checkTags: true
|
|
1234
|
+
})) if (config.wrapConditionals && config.generate !== "ssr" && (t.isLogicalExpression(value.expression) || t.isConditionalExpression(value.expression))) {
|
|
1235
|
+
const expr = transformCondition(attribute.get("value").get("expression"), true);
|
|
1236
|
+
runningObject.push(t.objectMethod("get", id, [], t.blockStatement([t.returnStatement(expr.body)]), !t.isValidIdentifier(key)));
|
|
1237
|
+
} else if (t.isCallExpression(value.expression) && t.isArrowFunctionExpression(value.expression.callee) && value.expression.callee.params.length === 0) {
|
|
1238
|
+
const callee = value.expression.callee;
|
|
1239
|
+
const body = t.isBlockStatement(callee.body) ? callee.body : t.blockStatement([t.returnStatement(callee.body)]);
|
|
1240
|
+
runningObject.push(t.objectMethod("get", id, [], body, !t.isValidIdentifier(key)));
|
|
1241
|
+
} else runningObject.push(t.objectMethod("get", id, [], t.blockStatement([t.returnStatement(value.expression)]), !t.isValidIdentifier(key)));
|
|
1242
|
+
else runningObject.push(t.objectProperty(id, value.expression));
|
|
1243
|
+
else runningObject.push(t.objectProperty(id, value));
|
|
1244
|
+
}
|
|
1245
|
+
});
|
|
1246
|
+
const childResult = transformComponentChildren(path.get("children"), config);
|
|
1247
|
+
if (childResult && childResult[0]) if (childResult[1]) {
|
|
1248
|
+
const body = t.isCallExpression(childResult[0]) && t.isFunction(childResult[0].arguments[0]) ? childResult[0].arguments[0].body : childResult[0].body ? childResult[0].body : childResult[0];
|
|
1249
|
+
runningObject.push(t.objectMethod("get", t.identifier("children"), [], t.isExpression(body) ? t.blockStatement([t.returnStatement(body)]) : body));
|
|
1250
|
+
} else runningObject.push(t.objectProperty(t.identifier("children"), childResult[0]));
|
|
1251
|
+
if (runningObject.length || !props.length) props.push(t.objectExpression(runningObject));
|
|
1252
|
+
if (props.length > 1 || dynamicSpread) props = [t.callExpression(registerImportMethod(path, "mergeProps"), props)];
|
|
1253
|
+
const componentArgs = [tagId, props[0]];
|
|
1254
|
+
exprs.push(t.callExpression(registerImportMethod(path, "createComponent"), componentArgs));
|
|
1255
|
+
if (exprs.length > 1) {
|
|
1256
|
+
const ret = exprs.pop();
|
|
1257
|
+
exprs = [t.callExpression(t.arrowFunctionExpression([], t.blockStatement([...exprs, t.returnStatement(ret)])), [])];
|
|
1258
|
+
}
|
|
1259
|
+
return {
|
|
1260
|
+
exprs,
|
|
1261
|
+
template: "",
|
|
1262
|
+
component: true
|
|
1263
|
+
};
|
|
1264
|
+
}
|
|
1265
|
+
function transformComponentChildren(children, config) {
|
|
1266
|
+
const filteredChildren = filterChildren(children);
|
|
1267
|
+
if (!filteredChildren.length) return;
|
|
1268
|
+
let dynamic = false;
|
|
1269
|
+
let pathNodes = [];
|
|
1270
|
+
let transformedChildren = filteredChildren.reduce((memo, path) => {
|
|
1271
|
+
if (t.isJSXText(path.node)) {
|
|
1272
|
+
const v$1 = decode(trimWhitespace(path.node.extra.raw));
|
|
1273
|
+
if (v$1.length) {
|
|
1274
|
+
pathNodes.push(path.node);
|
|
1275
|
+
memo.push(t.stringLiteral(v$1));
|
|
1276
|
+
}
|
|
1277
|
+
} else {
|
|
1278
|
+
const child = transformNode(path, {
|
|
1279
|
+
topLevel: true,
|
|
1280
|
+
componentChild: true,
|
|
1281
|
+
lastElement: true
|
|
1282
|
+
});
|
|
1283
|
+
dynamic = dynamic || child.dynamic;
|
|
1284
|
+
if (config.generate === "ssr" && filteredChildren.length > 1 && child.dynamic && t.isFunction(child.exprs[0])) child.exprs[0] = child.exprs[0].body;
|
|
1285
|
+
pathNodes.push(path.node);
|
|
1286
|
+
memo.push(getCreateTemplate(config, path, child)(path, child, filteredChildren.length > 1));
|
|
1287
|
+
}
|
|
1288
|
+
return memo;
|
|
1289
|
+
}, []);
|
|
1290
|
+
if (transformedChildren.length === 1) {
|
|
1291
|
+
transformedChildren = transformedChildren[0];
|
|
1292
|
+
if (!t.isJSXExpressionContainer(pathNodes[0]) && !t.isJSXSpreadChild(pathNodes[0]) && !t.isJSXText(pathNodes[0])) {
|
|
1293
|
+
transformedChildren = t.isCallExpression(transformedChildren) && !transformedChildren.arguments.length && !t.isIdentifier(transformedChildren.callee) ? transformedChildren.callee : t.arrowFunctionExpression([], transformedChildren);
|
|
1294
|
+
dynamic = true;
|
|
1295
|
+
}
|
|
1296
|
+
} else {
|
|
1297
|
+
transformedChildren = t.arrowFunctionExpression([], t.arrayExpression(transformedChildren));
|
|
1298
|
+
dynamic = true;
|
|
1299
|
+
}
|
|
1300
|
+
return [transformedChildren, dynamic];
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
//#endregion
|
|
1304
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/shared/fragment.ts
|
|
1305
|
+
function transformFragmentChildren(children, results, config) {
|
|
1306
|
+
const childNodes = filterChildren(children).reduce((memo, path) => {
|
|
1307
|
+
if (t.isJSXText(path.node)) {
|
|
1308
|
+
const v$1 = decode(trimWhitespace(path.node.extra.raw));
|
|
1309
|
+
if (v$1.length) memo.push(t.stringLiteral(v$1));
|
|
1310
|
+
} else {
|
|
1311
|
+
const child = transformNode(path, {
|
|
1312
|
+
topLevel: true,
|
|
1313
|
+
fragmentChild: true,
|
|
1314
|
+
lastElement: true
|
|
1315
|
+
});
|
|
1316
|
+
memo.push(getCreateTemplate(config, path, child)(path, child, true));
|
|
1317
|
+
}
|
|
1318
|
+
return memo;
|
|
1319
|
+
}, []);
|
|
1320
|
+
results.exprs.push(childNodes.length === 1 ? childNodes[0] : t.arrayExpression(childNodes));
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
//#endregion
|
|
1324
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/shared/transform.ts
|
|
1325
|
+
function transformJSX(path, state) {
|
|
1326
|
+
if (state.skip) return;
|
|
1327
|
+
const config = getConfig(path);
|
|
1328
|
+
const replace = transformThis(path);
|
|
1329
|
+
const result = transformNode(path, t.isJSXFragment(path.node) ? {} : {
|
|
1330
|
+
topLevel: true,
|
|
1331
|
+
lastElement: true
|
|
1332
|
+
});
|
|
1333
|
+
const template$1 = getCreateTemplate(config, path, result);
|
|
1334
|
+
path.replaceWith(replace(template$1(path, result, false)));
|
|
1335
|
+
path.traverse({ enter(path$1) {
|
|
1336
|
+
if (path$1.node.leadingComments && path$1.node.leadingComments[0] && path$1.node.leadingComments[0].value.trim() === config.staticMarker) path$1.node.leadingComments.shift();
|
|
1337
|
+
} });
|
|
1338
|
+
}
|
|
1339
|
+
function getTargetFunctionParent(path, parent) {
|
|
1340
|
+
let current = path.scope.getFunctionParent();
|
|
1341
|
+
while (current !== parent && current.path.isArrowFunctionExpression()) current = current.path.parentPath.scope.getFunctionParent();
|
|
1342
|
+
return current;
|
|
1343
|
+
}
|
|
1344
|
+
function transformThis(path) {
|
|
1345
|
+
const parent = path.scope.getFunctionParent();
|
|
1346
|
+
let thisId;
|
|
1347
|
+
path.traverse({
|
|
1348
|
+
ThisExpression(path$1) {
|
|
1349
|
+
if (getTargetFunctionParent(path$1, parent) === parent) {
|
|
1350
|
+
thisId || (thisId = path$1.scope.generateUidIdentifier("self$"));
|
|
1351
|
+
path$1.replaceWith(thisId);
|
|
1352
|
+
}
|
|
1353
|
+
},
|
|
1354
|
+
JSXElement(path$1) {
|
|
1355
|
+
let source = path$1.get("openingElement").get("name");
|
|
1356
|
+
while (source.isJSXMemberExpression()) source = source.get("object");
|
|
1357
|
+
if (source.isJSXIdentifier() && source.node.name === "this") {
|
|
1358
|
+
if (getTargetFunctionParent(path$1, parent) === parent) {
|
|
1359
|
+
thisId || (thisId = path$1.scope.generateUidIdentifier("self$"));
|
|
1360
|
+
source.replaceWith(t.jsxIdentifier(thisId.name));
|
|
1361
|
+
if (path$1.node.closingElement) path$1.node.closingElement.name = path$1.node.openingElement.name;
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
});
|
|
1366
|
+
return (node) => {
|
|
1367
|
+
if (thisId) if (!parent || parent.block.type === "ClassMethod") {
|
|
1368
|
+
const decl = t.variableDeclaration("const", [t.variableDeclarator(thisId, t.thisExpression())]);
|
|
1369
|
+
if (parent) path.getStatementParent().insertBefore(decl);
|
|
1370
|
+
else return t.callExpression(t.arrowFunctionExpression([], t.blockStatement([decl, t.returnStatement(node)])), []);
|
|
1371
|
+
} else parent.push({
|
|
1372
|
+
id: thisId,
|
|
1373
|
+
init: t.thisExpression(),
|
|
1374
|
+
kind: "const"
|
|
1375
|
+
});
|
|
1376
|
+
return node;
|
|
1377
|
+
};
|
|
1378
|
+
}
|
|
1379
|
+
function transformNode(path, info = {}) {
|
|
1380
|
+
const config = getConfig(path);
|
|
1381
|
+
const node = path.node;
|
|
1382
|
+
let staticValue;
|
|
1383
|
+
if (t.isJSXElement(node)) return transformElement$1(config, path, info);
|
|
1384
|
+
else if (t.isJSXFragment(node)) {
|
|
1385
|
+
let results = {
|
|
1386
|
+
template: "",
|
|
1387
|
+
declarations: [],
|
|
1388
|
+
exprs: [],
|
|
1389
|
+
dynamics: []
|
|
1390
|
+
};
|
|
1391
|
+
transformFragmentChildren(path.get("children"), results, config);
|
|
1392
|
+
return results;
|
|
1393
|
+
} else if (t.isJSXText(node) || (staticValue = getStaticExpression(path)) !== false) {
|
|
1394
|
+
const text = staticValue !== void 0 ? info.doNotEscape ? staticValue.toString() : escapeHTML(staticValue.toString()) : trimWhitespace(node.extra.raw);
|
|
1395
|
+
if (!text.length) return null;
|
|
1396
|
+
const results = {
|
|
1397
|
+
template: text,
|
|
1398
|
+
declarations: [],
|
|
1399
|
+
exprs: [],
|
|
1400
|
+
dynamics: [],
|
|
1401
|
+
postExprs: [],
|
|
1402
|
+
text: true
|
|
1403
|
+
};
|
|
1404
|
+
if (!info.skipId && config.generate !== "ssr") results.id = path.scope.generateUidIdentifier("el$");
|
|
1405
|
+
return results;
|
|
1406
|
+
} else if (t.isJSXExpressionContainer(node)) {
|
|
1407
|
+
if (t.isJSXEmptyExpression(node.expression)) return null;
|
|
1408
|
+
if (!isDynamic(path.get("expression"), {
|
|
1409
|
+
checkMember: true,
|
|
1410
|
+
checkTags: !!info.componentChild,
|
|
1411
|
+
native: !info.componentChild
|
|
1412
|
+
})) return {
|
|
1413
|
+
exprs: [node.expression],
|
|
1414
|
+
template: ""
|
|
1415
|
+
};
|
|
1416
|
+
const expr = config.wrapConditionals && config.generate !== "ssr" && (t.isLogicalExpression(node.expression) || t.isConditionalExpression(node.expression)) ? transformCondition(path.get("expression"), info.componentChild || info.fragmentChild) : !info.componentChild && (config.generate !== "ssr" || info.fragmentChild) && t.isCallExpression(node.expression) && !t.isCallExpression(node.expression.callee) && !t.isMemberExpression(node.expression.callee) && node.expression.arguments.length === 0 ? node.expression.callee : t.arrowFunctionExpression([], node.expression);
|
|
1417
|
+
return {
|
|
1418
|
+
exprs: expr.length > 1 ? [t.callExpression(t.arrowFunctionExpression([], t.blockStatement([expr[0], t.returnStatement(expr[1])])), [])] : [expr],
|
|
1419
|
+
template: "",
|
|
1420
|
+
dynamic: true
|
|
1421
|
+
};
|
|
1422
|
+
} else if (t.isJSXSpreadChild(node)) {
|
|
1423
|
+
if (!isDynamic(path.get("expression"), {
|
|
1424
|
+
checkMember: true,
|
|
1425
|
+
native: !info.componentChild
|
|
1426
|
+
})) return {
|
|
1427
|
+
exprs: [node.expression],
|
|
1428
|
+
template: ""
|
|
1429
|
+
};
|
|
1430
|
+
return {
|
|
1431
|
+
exprs: [t.arrowFunctionExpression([], node.expression)],
|
|
1432
|
+
template: "",
|
|
1433
|
+
dynamic: true
|
|
1434
|
+
};
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
function getCreateTemplate(config, path, result) {
|
|
1438
|
+
if (result.tagName && result.renderer === "dom" || config.generate === "dom") return createTemplate;
|
|
1439
|
+
if (result.renderer === "ssr" || config.generate === "ssr") return createTemplate$2;
|
|
1440
|
+
return createTemplate$1;
|
|
1441
|
+
}
|
|
1442
|
+
function transformElement$1(config, path, info = {}) {
|
|
1443
|
+
const node = path.node;
|
|
1444
|
+
let tagName = getTagName(node);
|
|
1445
|
+
if (isComponent(tagName)) return transformComponent(path);
|
|
1446
|
+
if ((config.renderers ?? []).find((renderer) => renderer.elements.includes(tagName))?.name === "dom" || getConfig(path).generate === "dom") return transformElement(path, info);
|
|
1447
|
+
if (getConfig(path).generate === "ssr") return transformElement$3(path, info);
|
|
1448
|
+
return transformElement$2(path, info);
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
//#endregion
|
|
1452
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/dom/constants.ts
|
|
1453
|
+
const InlineElements = [
|
|
1454
|
+
"a",
|
|
1455
|
+
"abbr",
|
|
1456
|
+
"acronym",
|
|
1457
|
+
"b",
|
|
1458
|
+
"bdi",
|
|
1459
|
+
"bdo",
|
|
1460
|
+
"big",
|
|
1461
|
+
"br",
|
|
1462
|
+
"button",
|
|
1463
|
+
"canvas",
|
|
1464
|
+
"cite",
|
|
1465
|
+
"code",
|
|
1466
|
+
"data",
|
|
1467
|
+
"datalist",
|
|
1468
|
+
"del",
|
|
1469
|
+
"dfn",
|
|
1470
|
+
"em",
|
|
1471
|
+
"embed",
|
|
1472
|
+
"i",
|
|
1473
|
+
"iframe",
|
|
1474
|
+
"img",
|
|
1475
|
+
"input",
|
|
1476
|
+
"ins",
|
|
1477
|
+
"kbd",
|
|
1478
|
+
"label",
|
|
1479
|
+
"map",
|
|
1480
|
+
"mark",
|
|
1481
|
+
"meter",
|
|
1482
|
+
"noscript",
|
|
1483
|
+
"object",
|
|
1484
|
+
"output",
|
|
1485
|
+
"picture",
|
|
1486
|
+
"progress",
|
|
1487
|
+
"q",
|
|
1488
|
+
"ruby",
|
|
1489
|
+
"s",
|
|
1490
|
+
"samp",
|
|
1491
|
+
"script",
|
|
1492
|
+
"select",
|
|
1493
|
+
"slot",
|
|
1494
|
+
"small",
|
|
1495
|
+
"span",
|
|
1496
|
+
"strong",
|
|
1497
|
+
"sub",
|
|
1498
|
+
"sup",
|
|
1499
|
+
"svg",
|
|
1500
|
+
"template",
|
|
1501
|
+
"textarea",
|
|
1502
|
+
"time",
|
|
1503
|
+
"u",
|
|
1504
|
+
"tt",
|
|
1505
|
+
"var",
|
|
1506
|
+
"video"
|
|
1507
|
+
];
|
|
1508
|
+
const BlockElements = [
|
|
1509
|
+
"address",
|
|
1510
|
+
"article",
|
|
1511
|
+
"aside",
|
|
1512
|
+
"blockquote",
|
|
1513
|
+
"dd",
|
|
1514
|
+
"details",
|
|
1515
|
+
"dialog",
|
|
1516
|
+
"div",
|
|
1517
|
+
"dl",
|
|
1518
|
+
"dt",
|
|
1519
|
+
"fieldset",
|
|
1520
|
+
"figcaption",
|
|
1521
|
+
"figure",
|
|
1522
|
+
"footer",
|
|
1523
|
+
"form",
|
|
1524
|
+
"h1",
|
|
1525
|
+
"h2",
|
|
1526
|
+
"h3",
|
|
1527
|
+
"h4",
|
|
1528
|
+
"h5",
|
|
1529
|
+
"h6",
|
|
1530
|
+
"header",
|
|
1531
|
+
"hgroup",
|
|
1532
|
+
"hr",
|
|
1533
|
+
"li",
|
|
1534
|
+
"main",
|
|
1535
|
+
"menu",
|
|
1536
|
+
"nav",
|
|
1537
|
+
"ol",
|
|
1538
|
+
"p",
|
|
1539
|
+
"pre",
|
|
1540
|
+
"section",
|
|
1541
|
+
"table",
|
|
1542
|
+
"ul"
|
|
1543
|
+
];
|
|
1544
|
+
|
|
1545
|
+
//#endregion
|
|
1546
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/dom/element.ts
|
|
1547
|
+
const alwaysClose = [
|
|
1548
|
+
"title",
|
|
1549
|
+
"style",
|
|
1550
|
+
"a",
|
|
1551
|
+
"strong",
|
|
1552
|
+
"small",
|
|
1553
|
+
"b",
|
|
1554
|
+
"u",
|
|
1555
|
+
"i",
|
|
1556
|
+
"em",
|
|
1557
|
+
"s",
|
|
1558
|
+
"code",
|
|
1559
|
+
"object",
|
|
1560
|
+
"table",
|
|
1561
|
+
"button",
|
|
1562
|
+
"textarea",
|
|
1563
|
+
"select",
|
|
1564
|
+
"iframe",
|
|
1565
|
+
"script",
|
|
1566
|
+
"noscript",
|
|
1567
|
+
"template",
|
|
1568
|
+
"fieldset"
|
|
1569
|
+
];
|
|
1570
|
+
function transformElement(path, info) {
|
|
1571
|
+
let tagName = getTagName(path.node), config = getConfig(path), wrapSVG = info.topLevel && tagName != "svg" && SVGElements.has(tagName), voidTag = VoidElements_default.indexOf(tagName) > -1, isCustomElement = tagName.indexOf("-") > -1 || path.get("openingElement").get("attributes").some((a) => a.node?.name?.name === "is" || a.name?.name === "is"), isImportNode = (tagName === "img" || tagName === "iframe") && path.get("openingElement").get("attributes").some((a) => a.node.name?.name === "loading"), results = {
|
|
1572
|
+
template: `<${tagName}`,
|
|
1573
|
+
templateWithClosingTags: `<${tagName}`,
|
|
1574
|
+
declarations: [],
|
|
1575
|
+
exprs: [],
|
|
1576
|
+
dynamics: [],
|
|
1577
|
+
postExprs: [],
|
|
1578
|
+
isSVG: wrapSVG,
|
|
1579
|
+
hasCustomElement: isCustomElement,
|
|
1580
|
+
isImportNode,
|
|
1581
|
+
tagName,
|
|
1582
|
+
renderer: "dom",
|
|
1583
|
+
skipTemplate: false
|
|
1584
|
+
};
|
|
1585
|
+
path.get("openingElement").get("attributes").some((a) => {
|
|
1586
|
+
if (a.node.name?.name === "data-hk") {
|
|
1587
|
+
a.remove();
|
|
1588
|
+
let filename = "";
|
|
1589
|
+
try {
|
|
1590
|
+
filename = path.scope.getProgramParent().path.hub.file.opts.filename;
|
|
1591
|
+
} catch (e) {}
|
|
1592
|
+
console.log("\n" + path.buildCodeFrameError(`"data-hk" attribute found in template, which could potentially cause hydration miss-matches. Usually happens when copying and pasting Solid SSRed code into JSX. Please remove the attribute from the JSX. \n\n${filename}\n`).toString());
|
|
1593
|
+
}
|
|
1594
|
+
});
|
|
1595
|
+
if (config.hydratable && (tagName === "html" || tagName === "head" || tagName === "body")) {
|
|
1596
|
+
results.skipTemplate = true;
|
|
1597
|
+
if (tagName === "head" && info.topLevel) {
|
|
1598
|
+
const createComponent = registerImportMethod(path, "createComponent", getRendererConfig(path, "dom").moduleName);
|
|
1599
|
+
const NoHydration = registerImportMethod(path, "NoHydration", getRendererConfig(path, "dom").moduleName);
|
|
1600
|
+
results.exprs.push(t.expressionStatement(t.callExpression(createComponent, [NoHydration, t.objectExpression([])])));
|
|
1601
|
+
return results;
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
if (wrapSVG) {
|
|
1605
|
+
results.template = "<svg>" + results.template;
|
|
1606
|
+
results.templateWithClosingTags = "<svg>" + results.templateWithClosingTags;
|
|
1607
|
+
}
|
|
1608
|
+
if (!info.skipId) results.id = path.scope.generateUidIdentifier("el$");
|
|
1609
|
+
transformAttributes(path, results);
|
|
1610
|
+
if (config.contextToCustomElements && (tagName === "slot" || isCustomElement)) contextToCustomElement(path, results);
|
|
1611
|
+
results.template += ">";
|
|
1612
|
+
results.templateWithClosingTags += ">";
|
|
1613
|
+
if (!voidTag) {
|
|
1614
|
+
const toBeClosed = !info.lastElement || !config.omitLastClosingTag || info.toBeClosed && (!config.omitNestedClosingTags || info.toBeClosed.has(tagName));
|
|
1615
|
+
if (toBeClosed) {
|
|
1616
|
+
results.toBeClosed = new Set(info.toBeClosed || alwaysClose);
|
|
1617
|
+
results.toBeClosed.add(tagName);
|
|
1618
|
+
if (InlineElements.includes(tagName)) BlockElements.forEach((i) => results.toBeClosed.add(i));
|
|
1619
|
+
} else results.toBeClosed = info.toBeClosed;
|
|
1620
|
+
if (tagName !== "noscript") transformChildren(path, results, config);
|
|
1621
|
+
if (toBeClosed) results.template += `</${tagName}>`;
|
|
1622
|
+
results.templateWithClosingTags += `</${tagName}>`;
|
|
1623
|
+
}
|
|
1624
|
+
if (info.topLevel && config.hydratable && results.hasHydratableEvent) {
|
|
1625
|
+
let runHydrationEvents = registerImportMethod(path, "runHydrationEvents", getRendererConfig(path, "dom").moduleName);
|
|
1626
|
+
results.postExprs.push(t.expressionStatement(t.callExpression(runHydrationEvents, [])));
|
|
1627
|
+
}
|
|
1628
|
+
if (wrapSVG) {
|
|
1629
|
+
results.template += "</svg>";
|
|
1630
|
+
results.templateWithClosingTags += "</svg>";
|
|
1631
|
+
}
|
|
1632
|
+
return results;
|
|
1633
|
+
}
|
|
1634
|
+
function setAttr(path, elem, name, value, { isSVG, dynamic, prevId, isCE, tagName }) {
|
|
1635
|
+
const config = getConfig(path);
|
|
1636
|
+
let parts, namespace;
|
|
1637
|
+
if ((parts = name.split(":")) && parts[1] && reservedNameSpaces.has(parts[0])) {
|
|
1638
|
+
name = parts[1];
|
|
1639
|
+
namespace = parts[0];
|
|
1640
|
+
}
|
|
1641
|
+
if (namespace === "style") {
|
|
1642
|
+
const setStyleProperty = registerImportMethod(path, "setStyleProperty", getRendererConfig(path, "dom").moduleName);
|
|
1643
|
+
return t.callExpression(setStyleProperty, [
|
|
1644
|
+
elem,
|
|
1645
|
+
t.stringLiteral(name),
|
|
1646
|
+
t.isAssignmentExpression(value) && t.isIdentifier(value.left) ? value.right : value
|
|
1647
|
+
]);
|
|
1648
|
+
}
|
|
1649
|
+
if (namespace === "class") return t.callExpression(t.memberExpression(t.memberExpression(elem, t.identifier("classList")), t.identifier("toggle")), [t.stringLiteral(name), dynamic ? value : t.unaryExpression("!", t.unaryExpression("!", value))]);
|
|
1650
|
+
if (name === "style") return t.callExpression(registerImportMethod(path, "style", getRendererConfig(path, "dom").moduleName), prevId ? [
|
|
1651
|
+
elem,
|
|
1652
|
+
value,
|
|
1653
|
+
prevId
|
|
1654
|
+
] : [elem, value]);
|
|
1655
|
+
if (!isSVG && name === "class") return t.callExpression(registerImportMethod(path, "className", getRendererConfig(path, "dom").moduleName), [elem, value]);
|
|
1656
|
+
if (name === "classList") return t.callExpression(registerImportMethod(path, "classList", getRendererConfig(path, "dom").moduleName), prevId ? [
|
|
1657
|
+
elem,
|
|
1658
|
+
value,
|
|
1659
|
+
prevId
|
|
1660
|
+
] : [elem, value]);
|
|
1661
|
+
if (dynamic && name === "textContent") {
|
|
1662
|
+
if (config.hydratable) return t.callExpression(registerImportMethod(path, "setProperty"), [
|
|
1663
|
+
elem,
|
|
1664
|
+
t.stringLiteral("data"),
|
|
1665
|
+
value
|
|
1666
|
+
]);
|
|
1667
|
+
return t.assignmentExpression("=", t.memberExpression(elem, t.identifier("data")), value);
|
|
1668
|
+
}
|
|
1669
|
+
if (namespace === "bool") return t.callExpression(registerImportMethod(path, "setBoolAttribute", getRendererConfig(path, "dom").moduleName), [
|
|
1670
|
+
elem,
|
|
1671
|
+
t.stringLiteral(name),
|
|
1672
|
+
value
|
|
1673
|
+
]);
|
|
1674
|
+
const isChildProp = ChildProperties.has(name);
|
|
1675
|
+
const isProp = Properties.has(name);
|
|
1676
|
+
const alias = getPropAlias(name, tagName.toUpperCase());
|
|
1677
|
+
if (namespace !== "attr" && (isChildProp || !isSVG && isProp || isCE || namespace === "prop")) {
|
|
1678
|
+
if (isCE && !isChildProp && !isProp && namespace !== "prop") name = toPropertyName(name);
|
|
1679
|
+
if (config.hydratable && namespace !== "prop") return t.callExpression(registerImportMethod(path, "setProperty"), [
|
|
1680
|
+
elem,
|
|
1681
|
+
t.stringLiteral(alias || name),
|
|
1682
|
+
value
|
|
1683
|
+
]);
|
|
1684
|
+
return t.assignmentExpression("=", t.memberExpression(elem, t.identifier(alias || name)), value);
|
|
1685
|
+
}
|
|
1686
|
+
let isNameSpaced = name.indexOf(":") > -1;
|
|
1687
|
+
name = Aliases[name] || name;
|
|
1688
|
+
!isSVG && (name = name.toLowerCase());
|
|
1689
|
+
const ns = isNameSpaced && SVGNamespace[name.split(":")[0]];
|
|
1690
|
+
if (ns) return t.callExpression(registerImportMethod(path, "setAttributeNS", getRendererConfig(path, "dom").moduleName), [
|
|
1691
|
+
elem,
|
|
1692
|
+
t.stringLiteral(ns),
|
|
1693
|
+
t.stringLiteral(name),
|
|
1694
|
+
value
|
|
1695
|
+
]);
|
|
1696
|
+
else return t.callExpression(registerImportMethod(path, "setAttribute", getRendererConfig(path, "dom").moduleName), [
|
|
1697
|
+
elem,
|
|
1698
|
+
t.stringLiteral(name),
|
|
1699
|
+
value
|
|
1700
|
+
]);
|
|
1701
|
+
}
|
|
1702
|
+
function detectResolvableEventHandler(attribute, handler) {
|
|
1703
|
+
while (t.isIdentifier(handler)) {
|
|
1704
|
+
const lookup = attribute.scope.getBinding(handler.name);
|
|
1705
|
+
if (lookup) if (t.isVariableDeclarator(lookup.path.node)) handler = lookup.path.node.init;
|
|
1706
|
+
else if (t.isFunctionDeclaration(lookup.path.node)) return true;
|
|
1707
|
+
else return false;
|
|
1708
|
+
else return false;
|
|
1709
|
+
}
|
|
1710
|
+
return t.isFunction(handler);
|
|
1711
|
+
}
|
|
1712
|
+
function transformAttributes(path, results) {
|
|
1713
|
+
let elem = results.id, hasHydratableEvent = false, children, spreadExpr, attributes = path.get("openingElement").get("attributes");
|
|
1714
|
+
const tagName = getTagName(path.node), isSVG = SVGElements.has(tagName), isCE = tagName.includes("-") || attributes.some((a) => a.node.name?.name === "is"), hasChildren = path.node.children.length > 0, config = getConfig(path);
|
|
1715
|
+
if (attributes.some((attribute) => t.isJSXSpreadAttribute(attribute.node))) {
|
|
1716
|
+
[attributes, spreadExpr] = processSpreads(path, attributes, {
|
|
1717
|
+
elem,
|
|
1718
|
+
isSVG,
|
|
1719
|
+
hasChildren,
|
|
1720
|
+
wrapConditionals: config.wrapConditionals
|
|
1721
|
+
});
|
|
1722
|
+
path.get("openingElement").set("attributes", attributes.map((a) => a.node));
|
|
1723
|
+
hasHydratableEvent = true;
|
|
1724
|
+
}
|
|
1725
|
+
/**
|
|
1726
|
+
* Inline styles
|
|
1727
|
+
*
|
|
1728
|
+
* 1. When string
|
|
1729
|
+
* 2. When is an object, the key is a string, and value is string/numeric
|
|
1730
|
+
* 3. Remove properties from object when value is undefined/null
|
|
1731
|
+
* 4. When `value.evaluate().confident`
|
|
1732
|
+
*
|
|
1733
|
+
* Also, when `key` is computed value is also `value.evaluate().confident`
|
|
1734
|
+
*/
|
|
1735
|
+
attributes = path.get("openingElement").get("attributes");
|
|
1736
|
+
const styleAttributes = attributes.filter((a) => a.node.name && a.node.name.name === "style");
|
|
1737
|
+
if (styleAttributes.length > 0) {
|
|
1738
|
+
let inlinedStyle = "";
|
|
1739
|
+
for (let i = 0; i < styleAttributes.length; i++) {
|
|
1740
|
+
const attr = styleAttributes[i];
|
|
1741
|
+
let value = attr.node.value;
|
|
1742
|
+
const node = attr.get("value");
|
|
1743
|
+
if (t.isJSXExpressionContainer(value)) value = value.expression;
|
|
1744
|
+
if (t.isStringLiteral(value)) {
|
|
1745
|
+
inlinedStyle += `${value.value.replace(/;$/, "")};`;
|
|
1746
|
+
attr.remove();
|
|
1747
|
+
} else if (t.isObjectExpression(value)) {
|
|
1748
|
+
const properties = value.properties;
|
|
1749
|
+
const propertiesNode = node.get("expression").get("properties");
|
|
1750
|
+
const toRemoveProperty = [];
|
|
1751
|
+
for (let i$1 = 0; i$1 < properties.length; i$1++) {
|
|
1752
|
+
const property = properties[i$1];
|
|
1753
|
+
if (property.computed) {
|
|
1754
|
+
const r = propertiesNode[i$1].get("value").evaluate();
|
|
1755
|
+
if (r.confident && (typeof r.value === "string" || typeof r.value === "number")) property.value = t.inherits(t.stringLiteral(`${r.value}`), property.value);
|
|
1756
|
+
continue;
|
|
1757
|
+
}
|
|
1758
|
+
if (t.isObjectProperty(property)) {
|
|
1759
|
+
const key = t.isIdentifier(property.key) ? property.key.name : property.key.value;
|
|
1760
|
+
if (t.isStringLiteral(property.value) || t.isNumericLiteral(property.value)) {
|
|
1761
|
+
inlinedStyle += `${key}:${property.value.value};`;
|
|
1762
|
+
toRemoveProperty.push(property);
|
|
1763
|
+
} else if (t.isIdentifier(property.value) && property.value.name === "undefined" || t.isNullLiteral(property.value)) toRemoveProperty.push(property);
|
|
1764
|
+
else {
|
|
1765
|
+
const r = propertiesNode[i$1].get("value").evaluate();
|
|
1766
|
+
if (r.confident && (typeof r.value === "string" || typeof r.value === "number")) {
|
|
1767
|
+
inlinedStyle += `${key}:${r.value};`;
|
|
1768
|
+
toRemoveProperty.push(property);
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
for (const remove of toRemoveProperty) value.properties.splice(value.properties.indexOf(remove), 1);
|
|
1774
|
+
if (value.properties.length === 0) attr.remove();
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
if (inlinedStyle !== "") {
|
|
1778
|
+
const styleAttribute$1 = t.jsxAttribute(t.jsxIdentifier("style"), t.stringLiteral(inlinedStyle.replace(/;$/, "")));
|
|
1779
|
+
path.get("openingElement").node.attributes.push(styleAttribute$1);
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
const styleAttribute = path.get("openingElement").get("attributes").find((a) => a.node.name && a.node.name.name === "style" && t.isJSXExpressionContainer(a.node.value) && t.isObjectExpression(a.node.value.expression) && !a.node.value.expression.properties.some((p) => t.isSpreadElement(p)));
|
|
1783
|
+
if (styleAttribute) {
|
|
1784
|
+
let i = 0, leading = styleAttribute.node.value.expression.leadingComments;
|
|
1785
|
+
styleAttribute.node.value.expression.properties.slice().forEach((p, index) => {
|
|
1786
|
+
if (!p.computed) {
|
|
1787
|
+
if (leading) p.value.leadingComments = leading;
|
|
1788
|
+
path.get("openingElement").node.attributes.splice(styleAttribute.key + ++i, 0, t.jsxAttribute(t.jsxNamespacedName(t.jsxIdentifier("style"), t.jsxIdentifier(t.isIdentifier(p.key) ? p.key.name : p.key.value)), t.jsxExpressionContainer(p.value)));
|
|
1789
|
+
styleAttribute.node.value.expression.properties.splice(index - i - 1, 1);
|
|
1790
|
+
}
|
|
1791
|
+
});
|
|
1792
|
+
if (!styleAttribute.node.value.expression.properties.length) path.get("openingElement").node.attributes.splice(styleAttribute.key, 1);
|
|
1793
|
+
}
|
|
1794
|
+
attributes = path.get("openingElement").get("attributes");
|
|
1795
|
+
const classListAttribute = attributes.find((a) => a.node.name && a.node.name.name === "classList" && t.isJSXExpressionContainer(a.node.value) && t.isObjectExpression(a.node.value.expression) && !a.node.value.expression.properties.some((p) => t.isSpreadElement(p) || p.computed || t.isStringLiteral(p.key) && (p.key.value.includes(" ") || p.key.value.includes(":"))));
|
|
1796
|
+
if (classListAttribute) {
|
|
1797
|
+
let i = 0, leading = classListAttribute.node.value.expression.leadingComments, classListProperties = classListAttribute.get("value").get("expression").get("properties");
|
|
1798
|
+
classListProperties.slice().forEach((propPath, index) => {
|
|
1799
|
+
const p = propPath.node;
|
|
1800
|
+
const { confident, value: computed } = propPath.get("value").evaluate();
|
|
1801
|
+
if (leading) p.value.leadingComments = leading;
|
|
1802
|
+
if (!confident) path.get("openingElement").node.attributes.splice(classListAttribute.key + ++i, 0, t.jsxAttribute(t.jsxNamespacedName(t.jsxIdentifier("class"), t.jsxIdentifier(t.isIdentifier(p.key) ? p.key.name : p.key.value)), t.jsxExpressionContainer(p.value)));
|
|
1803
|
+
else if (computed) path.get("openingElement").node.attributes.splice(classListAttribute.key + ++i, 0, t.jsxAttribute(t.jsxIdentifier("class"), t.stringLiteral(t.isIdentifier(p.key) ? p.key.name : p.key.value)));
|
|
1804
|
+
classListProperties.splice(index - i - 1, 1);
|
|
1805
|
+
});
|
|
1806
|
+
if (!classListProperties.length) path.get("openingElement").node.attributes.splice(classListAttribute.key, 1);
|
|
1807
|
+
}
|
|
1808
|
+
attributes = path.get("openingElement").get("attributes");
|
|
1809
|
+
const classAttributes = attributes.filter((a) => a.node.name && (a.node.name.name === "class" || a.node.name.name === "className"));
|
|
1810
|
+
if (classAttributes.length > 1) {
|
|
1811
|
+
const first = classAttributes[0].node, values = [], quasis = [t.templateElement({ raw: "" })];
|
|
1812
|
+
for (let i = 0; i < classAttributes.length; i++) {
|
|
1813
|
+
const attr = classAttributes[i].node, isLast = i === classAttributes.length - 1;
|
|
1814
|
+
if (!t.isJSXExpressionContainer(attr.value)) {
|
|
1815
|
+
const prev = quasis.pop();
|
|
1816
|
+
quasis.push(t.templateElement({ raw: (prev ? prev.value.raw : "") + `${attr.value.value}` + (isLast ? "" : " ") }));
|
|
1817
|
+
} else {
|
|
1818
|
+
values.push(t.logicalExpression("||", attr.value.expression, t.stringLiteral("")));
|
|
1819
|
+
quasis.push(t.templateElement({ raw: isLast ? "" : " " }));
|
|
1820
|
+
}
|
|
1821
|
+
i && attributes.splice(attributes.indexOf(classAttributes[i]), 1);
|
|
1822
|
+
}
|
|
1823
|
+
if (values.length) first.value = t.jsxExpressionContainer(t.templateLiteral(quasis, values));
|
|
1824
|
+
else first.value = t.stringLiteral(quasis[0].value.raw);
|
|
1825
|
+
}
|
|
1826
|
+
path.get("openingElement").set("attributes", attributes.map((a) => a.node));
|
|
1827
|
+
let needsSpacing = true;
|
|
1828
|
+
function inlineAttributeOnTemplate(isSVG$1, key, results$1, value) {
|
|
1829
|
+
!isSVG$1 && (key = key.toLowerCase());
|
|
1830
|
+
results$1.template += `${needsSpacing ? " " : ""}${key}`;
|
|
1831
|
+
if (!value) {
|
|
1832
|
+
needsSpacing = true;
|
|
1833
|
+
return;
|
|
1834
|
+
}
|
|
1835
|
+
let text = value.value;
|
|
1836
|
+
if (typeof text === "number") text = String(text);
|
|
1837
|
+
let needsQuoting = !config.omitQuotes;
|
|
1838
|
+
if (key === "style" || key === "class") {
|
|
1839
|
+
text = trimWhitespace(text);
|
|
1840
|
+
if (key === "style") text = text.replace(/; /g, ";").replace(/: /g, ":");
|
|
1841
|
+
}
|
|
1842
|
+
if (!text.length) {
|
|
1843
|
+
needsSpacing = true;
|
|
1844
|
+
results$1.template += ``;
|
|
1845
|
+
return;
|
|
1846
|
+
}
|
|
1847
|
+
for (let i = 0, len = text.length; i < len; i++) {
|
|
1848
|
+
let char = text[i];
|
|
1849
|
+
if (char === "'" || char === "\"" || char === " " || char === " " || char === "\n" || char === "\r" || char === "`" || char === "=" || char === "<" || char === ">") needsQuoting = true;
|
|
1850
|
+
}
|
|
1851
|
+
if (needsQuoting) {
|
|
1852
|
+
needsSpacing = false;
|
|
1853
|
+
results$1.template += `="${escapeHTML(text, true)}"`;
|
|
1854
|
+
} else {
|
|
1855
|
+
needsSpacing = true;
|
|
1856
|
+
results$1.template += `=${escapeHTML(text, true)}`;
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
path.get("openingElement").get("attributes").forEach((attribute) => {
|
|
1860
|
+
const node = attribute.node;
|
|
1861
|
+
let value = node.value, key = t.isJSXNamespacedName(node.name) ? `${node.name.namespace.name}:${node.name.name.name}` : node.name.name, reservedNameSpace = t.isJSXNamespacedName(node.name) && reservedNameSpaces.has(node.name.namespace.name);
|
|
1862
|
+
if (t.isJSXExpressionContainer(value) && !key.startsWith("use:")) {
|
|
1863
|
+
const evaluated = attribute.get("value").get("expression").evaluate().value;
|
|
1864
|
+
let type;
|
|
1865
|
+
if (evaluated !== void 0 && ((type = typeof evaluated) === "string" || type === "number")) if (type === "number" && (Properties.has(key) || key.startsWith("prop:"))) value = t.jsxExpressionContainer(t.numericLiteral(evaluated));
|
|
1866
|
+
else value = t.stringLiteral(String(evaluated));
|
|
1867
|
+
}
|
|
1868
|
+
if (t.isJSXNamespacedName(node.name) && reservedNameSpace && !t.isJSXExpressionContainer(value)) node.value = value = t.jsxExpressionContainer(value || t.jsxEmptyExpression());
|
|
1869
|
+
if (t.isJSXExpressionContainer(value) && (reservedNameSpace || !(t.isStringLiteral(value.expression) || t.isNumericLiteral(value.expression)))) if (key === "ref") {
|
|
1870
|
+
while (t.isTSNonNullExpression(value.expression) || t.isTSAsExpression(value.expression)) value.expression = value.expression.expression;
|
|
1871
|
+
let binding, isConstant = t.isIdentifier(value.expression) && (binding = path.scope.getBinding(value.expression.name)) && (binding.kind === "const" || binding.kind === "module");
|
|
1872
|
+
if (!isConstant && t.isLVal(value.expression)) {
|
|
1873
|
+
const refIdentifier = path.scope.generateUidIdentifier("_ref$");
|
|
1874
|
+
results.exprs.unshift(t.variableDeclaration("var", [t.variableDeclarator(refIdentifier, value.expression)]), t.expressionStatement(t.conditionalExpression(t.binaryExpression("===", t.unaryExpression("typeof", refIdentifier), t.stringLiteral("function")), t.callExpression(registerImportMethod(path, "use", getRendererConfig(path, "dom").moduleName), [refIdentifier, elem]), t.assignmentExpression("=", value.expression, elem))));
|
|
1875
|
+
} else if (isConstant || t.isFunction(value.expression)) results.exprs.unshift(t.expressionStatement(t.callExpression(registerImportMethod(path, "use", getRendererConfig(path, "dom").moduleName), [value.expression, elem])));
|
|
1876
|
+
else {
|
|
1877
|
+
const refIdentifier = path.scope.generateUidIdentifier("_ref$");
|
|
1878
|
+
results.exprs.unshift(t.variableDeclaration("var", [t.variableDeclarator(refIdentifier, value.expression)]), t.expressionStatement(t.logicalExpression("&&", t.binaryExpression("===", t.unaryExpression("typeof", refIdentifier), t.stringLiteral("function")), t.callExpression(registerImportMethod(path, "use", getRendererConfig(path, "dom").moduleName), [refIdentifier, elem]))));
|
|
1879
|
+
}
|
|
1880
|
+
} else if (key.startsWith("use:")) {
|
|
1881
|
+
node.name.name.type = "Identifier";
|
|
1882
|
+
results.exprs.unshift(t.expressionStatement(t.callExpression(registerImportMethod(path, "use", getRendererConfig(path, "dom").moduleName), [
|
|
1883
|
+
node.name.name,
|
|
1884
|
+
elem,
|
|
1885
|
+
t.arrowFunctionExpression([], t.isJSXEmptyExpression(value.expression) ? t.booleanLiteral(true) : value.expression)
|
|
1886
|
+
])));
|
|
1887
|
+
} else if (key === "children") children = value;
|
|
1888
|
+
else if (key.startsWith("on")) {
|
|
1889
|
+
const ev = toEventName(key);
|
|
1890
|
+
if (key.startsWith("on:")) {
|
|
1891
|
+
const args = [
|
|
1892
|
+
elem,
|
|
1893
|
+
t.stringLiteral(key.split(":")[1]),
|
|
1894
|
+
value.expression
|
|
1895
|
+
];
|
|
1896
|
+
results.exprs.unshift(t.expressionStatement(t.callExpression(registerImportMethod(path, "addEventListener", getRendererConfig(path, "dom").moduleName), args)));
|
|
1897
|
+
} else if (key.startsWith("oncapture:")) {
|
|
1898
|
+
const args = [
|
|
1899
|
+
t.stringLiteral(key.split(":")[1]),
|
|
1900
|
+
value.expression,
|
|
1901
|
+
t.booleanLiteral(true)
|
|
1902
|
+
];
|
|
1903
|
+
results.exprs.push(t.expressionStatement(t.callExpression(t.memberExpression(elem, t.identifier("addEventListener")), args)));
|
|
1904
|
+
} else if (config.delegateEvents && (DelegatedEvents.has(ev) || config.delegatedEvents.indexOf(ev) !== -1)) {
|
|
1905
|
+
hasHydratableEvent = true;
|
|
1906
|
+
(attribute.scope.getProgramParent().data.events || (attribute.scope.getProgramParent().data.events = /* @__PURE__ */ new Set())).add(ev);
|
|
1907
|
+
let handler = value.expression;
|
|
1908
|
+
const resolveable = detectResolvableEventHandler(attribute, handler);
|
|
1909
|
+
if (t.isArrayExpression(handler)) {
|
|
1910
|
+
if (handler.elements.length > 1) results.exprs.unshift(t.expressionStatement(t.assignmentExpression("=", t.memberExpression(elem, t.identifier(`$$${ev}Data`)), handler.elements[1])));
|
|
1911
|
+
handler = handler.elements[0];
|
|
1912
|
+
results.exprs.unshift(t.expressionStatement(t.assignmentExpression("=", t.memberExpression(elem, t.identifier(`$$${ev}`)), handler)));
|
|
1913
|
+
} else if (t.isFunction(handler) || resolveable) results.exprs.unshift(t.expressionStatement(t.assignmentExpression("=", t.memberExpression(elem, t.identifier(`$$${ev}`)), handler)));
|
|
1914
|
+
else results.exprs.unshift(t.expressionStatement(t.callExpression(registerImportMethod(path, "addEventListener", getRendererConfig(path, "dom").moduleName), [
|
|
1915
|
+
elem,
|
|
1916
|
+
t.stringLiteral(ev),
|
|
1917
|
+
handler,
|
|
1918
|
+
t.booleanLiteral(true)
|
|
1919
|
+
])));
|
|
1920
|
+
} else {
|
|
1921
|
+
let handler = value.expression;
|
|
1922
|
+
const resolveable = detectResolvableEventHandler(attribute, handler);
|
|
1923
|
+
if (t.isArrayExpression(handler)) {
|
|
1924
|
+
if (handler.elements.length > 1) handler = t.arrowFunctionExpression([t.identifier("e")], t.callExpression(handler.elements[0], [handler.elements[1], t.identifier("e")]));
|
|
1925
|
+
else handler = handler.elements[0];
|
|
1926
|
+
results.exprs.unshift(t.expressionStatement(t.callExpression(t.memberExpression(elem, t.identifier("addEventListener")), [t.stringLiteral(ev), handler])));
|
|
1927
|
+
} else if (t.isFunction(handler) || resolveable) results.exprs.unshift(t.expressionStatement(t.callExpression(t.memberExpression(elem, t.identifier("addEventListener")), [t.stringLiteral(ev), handler])));
|
|
1928
|
+
else results.exprs.unshift(t.expressionStatement(t.callExpression(registerImportMethod(path, "addEventListener", getRendererConfig(path, "dom").moduleName), [
|
|
1929
|
+
elem,
|
|
1930
|
+
t.stringLiteral(ev),
|
|
1931
|
+
handler
|
|
1932
|
+
])));
|
|
1933
|
+
}
|
|
1934
|
+
} else if (config.effectWrapper && (isDynamic(attribute.get("value").get("expression"), { checkMember: true }) || (key === "classList" || key === "style") && !attribute.get("value").get("expression").evaluate().confident && !hasStaticMarker(value, path))) {
|
|
1935
|
+
let nextElem = elem;
|
|
1936
|
+
if (key === "value" || key === "checked") {
|
|
1937
|
+
const effectWrapperId = registerImportMethod(path, config.effectWrapper);
|
|
1938
|
+
results.postExprs.push(t.expressionStatement(t.callExpression(effectWrapperId, [t.arrowFunctionExpression([], setAttr(path, elem, key, value.expression, {
|
|
1939
|
+
tagName,
|
|
1940
|
+
isSVG,
|
|
1941
|
+
isCE
|
|
1942
|
+
}))])));
|
|
1943
|
+
return;
|
|
1944
|
+
}
|
|
1945
|
+
if (key === "textContent") {
|
|
1946
|
+
nextElem = attribute.scope.generateUidIdentifier("el$");
|
|
1947
|
+
children = t.jsxText(" ");
|
|
1948
|
+
children.extra = {
|
|
1949
|
+
raw: " ",
|
|
1950
|
+
rawValue: " "
|
|
1951
|
+
};
|
|
1952
|
+
results.declarations.push(t.variableDeclarator(nextElem, t.memberExpression(elem, t.identifier("firstChild"))));
|
|
1953
|
+
}
|
|
1954
|
+
results.dynamics.push({
|
|
1955
|
+
elem: nextElem,
|
|
1956
|
+
key,
|
|
1957
|
+
value: value.expression,
|
|
1958
|
+
isSVG,
|
|
1959
|
+
isCE,
|
|
1960
|
+
tagName
|
|
1961
|
+
});
|
|
1962
|
+
} else if (key.slice(0, 5) === "attr:") {
|
|
1963
|
+
if (t.isJSXExpressionContainer(value)) value = value.expression;
|
|
1964
|
+
if (t.isStringLiteral(value) || t.isNumericLiteral(value)) inlineAttributeOnTemplate(isSVG, key.slice(5), results, value);
|
|
1965
|
+
else results.exprs.push(t.expressionStatement(setAttr(attribute, elem, key, value, {
|
|
1966
|
+
isSVG,
|
|
1967
|
+
isCE,
|
|
1968
|
+
tagName
|
|
1969
|
+
})));
|
|
1970
|
+
} else if (key.slice(0, 5) === "bool:") {
|
|
1971
|
+
let content = value;
|
|
1972
|
+
if (t.isJSXExpressionContainer(content)) content = content.expression;
|
|
1973
|
+
function addBoolAttribute() {
|
|
1974
|
+
results.template += `${needsSpacing ? " " : ""}${key.slice(5)}`;
|
|
1975
|
+
needsSpacing = true;
|
|
1976
|
+
}
|
|
1977
|
+
switch (content.type) {
|
|
1978
|
+
case "StringLiteral":
|
|
1979
|
+
if (content.value.length && content.value !== "0") addBoolAttribute();
|
|
1980
|
+
return;
|
|
1981
|
+
case "NullLiteral": return;
|
|
1982
|
+
case "BooleanLiteral":
|
|
1983
|
+
if (content.value) addBoolAttribute();
|
|
1984
|
+
return;
|
|
1985
|
+
case "Identifier":
|
|
1986
|
+
if (content.name === "undefined") return;
|
|
1987
|
+
break;
|
|
1988
|
+
}
|
|
1989
|
+
results.exprs.push(t.expressionStatement(setAttr(attribute, elem, key, t.isJSXExpressionContainer(value) ? value.expression : value, {
|
|
1990
|
+
isSVG,
|
|
1991
|
+
isCE,
|
|
1992
|
+
tagName
|
|
1993
|
+
})));
|
|
1994
|
+
} else results.exprs.push(t.expressionStatement(setAttr(attribute, elem, key, value.expression, {
|
|
1995
|
+
isSVG,
|
|
1996
|
+
isCE,
|
|
1997
|
+
tagName
|
|
1998
|
+
})));
|
|
1999
|
+
else {
|
|
2000
|
+
if (config.hydratable && key === "$ServerOnly") {
|
|
2001
|
+
results.skipTemplate = true;
|
|
2002
|
+
return;
|
|
2003
|
+
}
|
|
2004
|
+
if (t.isJSXExpressionContainer(value)) value = value.expression;
|
|
2005
|
+
key = Aliases[key] || key;
|
|
2006
|
+
if (value && ChildProperties.has(key)) results.exprs.push(t.expressionStatement(setAttr(attribute, elem, key, value, {
|
|
2007
|
+
isSVG,
|
|
2008
|
+
isCE,
|
|
2009
|
+
tagName
|
|
2010
|
+
})));
|
|
2011
|
+
else inlineAttributeOnTemplate(isSVG, key, results, value);
|
|
2012
|
+
}
|
|
2013
|
+
});
|
|
2014
|
+
if (!hasChildren && children) path.node.children.push(children);
|
|
2015
|
+
if (spreadExpr) results.exprs.push(spreadExpr);
|
|
2016
|
+
results.hasHydratableEvent = results.hasHydratableEvent || hasHydratableEvent;
|
|
2017
|
+
}
|
|
2018
|
+
function findLastElement(children, hydratable) {
|
|
2019
|
+
let lastElement = -1, tagName;
|
|
2020
|
+
for (let i = children.length - 1; i >= 0; i--) {
|
|
2021
|
+
const node = children[i].node;
|
|
2022
|
+
if (hydratable || t.isJSXText(node) || getStaticExpression(children[i]) !== false || t.isJSXElement(node) && (tagName = getTagName(node)) && !isComponent(tagName)) {
|
|
2023
|
+
lastElement = i;
|
|
2024
|
+
break;
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2027
|
+
return lastElement;
|
|
2028
|
+
}
|
|
2029
|
+
function transformChildren(path, results, config) {
|
|
2030
|
+
let tempPath = results.id && results.id.name, tagName = getTagName(path.node), nextPlaceholder, childPostExprs = [], i = 0;
|
|
2031
|
+
const filteredChildren = filterChildren(path.get("children")), lastElement = findLastElement(filteredChildren, config.hydratable), childNodes = filteredChildren.reduce((memo, child, index) => {
|
|
2032
|
+
if (child.isJSXFragment()) throw new Error(`Fragments can only be used top level in JSX. Not used under a <${tagName}>.`);
|
|
2033
|
+
const transformed = transformNode(child, {
|
|
2034
|
+
toBeClosed: results.toBeClosed,
|
|
2035
|
+
lastElement: index === lastElement,
|
|
2036
|
+
skipId: !results.id || !detectExpressions(filteredChildren, index, config)
|
|
2037
|
+
});
|
|
2038
|
+
if (!transformed) return memo;
|
|
2039
|
+
const i$1 = memo.length;
|
|
2040
|
+
if (transformed.text && i$1 && memo[i$1 - 1].text) {
|
|
2041
|
+
memo[i$1 - 1].template += transformed.template;
|
|
2042
|
+
memo[i$1 - 1].templateWithClosingTags += transformed.templateWithClosingTags || transformed.template;
|
|
2043
|
+
} else memo.push(transformed);
|
|
2044
|
+
return memo;
|
|
2045
|
+
}, []);
|
|
2046
|
+
childNodes.forEach((child, index) => {
|
|
2047
|
+
if (!child) return;
|
|
2048
|
+
if (child.tagName && child.renderer !== "dom") throw new Error(`<${child.tagName}> is not supported in <${tagName}>.
|
|
2049
|
+
Wrap the usage with a component that would render this element, eg. Canvas`);
|
|
2050
|
+
results.template += child.template;
|
|
2051
|
+
results.templateWithClosingTags += child.templateWithClosingTags || child.template;
|
|
2052
|
+
results.isImportNode = results.isImportNode || child.isImportNode;
|
|
2053
|
+
if (child.id) {
|
|
2054
|
+
if (child.tagName === "head") {
|
|
2055
|
+
if (config.hydratable) {
|
|
2056
|
+
const createComponent = registerImportMethod(path, "createComponent", getRendererConfig(path, "dom").moduleName);
|
|
2057
|
+
const NoHydration = registerImportMethod(path, "NoHydration", getRendererConfig(path, "dom").moduleName);
|
|
2058
|
+
results.exprs.push(t.expressionStatement(t.callExpression(createComponent, [NoHydration, t.objectExpression([])])));
|
|
2059
|
+
}
|
|
2060
|
+
return;
|
|
2061
|
+
}
|
|
2062
|
+
let getNextMatch;
|
|
2063
|
+
if (config.hydratable && tagName === "html") getNextMatch = registerImportMethod(path, "getNextMatch", getRendererConfig(path, "dom").moduleName);
|
|
2064
|
+
const walk = t.memberExpression(t.identifier(tempPath), t.identifier(i === 0 ? "firstChild" : "nextSibling"));
|
|
2065
|
+
results.declarations.push(t.variableDeclarator(child.id, config.hydratable && tagName === "html" ? t.callExpression(getNextMatch, [walk, t.stringLiteral(child.tagName)]) : walk));
|
|
2066
|
+
results.declarations.push(...child.declarations);
|
|
2067
|
+
results.exprs.push(...child.exprs);
|
|
2068
|
+
results.dynamics.push(...child.dynamics);
|
|
2069
|
+
childPostExprs.push(...child.postExprs);
|
|
2070
|
+
results.hasHydratableEvent = results.hasHydratableEvent || child.hasHydratableEvent;
|
|
2071
|
+
results.hasCustomElement = results.hasCustomElement || child.hasCustomElement;
|
|
2072
|
+
results.isImportNode = results.isImportNode || child.isImportNode;
|
|
2073
|
+
tempPath = child.id.name;
|
|
2074
|
+
nextPlaceholder = null;
|
|
2075
|
+
i++;
|
|
2076
|
+
} else if (child.exprs.length) {
|
|
2077
|
+
let insert = registerImportMethod(path, "insert", getRendererConfig(path, "dom").moduleName);
|
|
2078
|
+
const multi = checkLength(filteredChildren), markers = config.hydratable && multi;
|
|
2079
|
+
if (markers || wrappedByText(childNodes, index)) {
|
|
2080
|
+
let exprId, contentId;
|
|
2081
|
+
if (markers) tempPath = createPlaceholder(path, results, tempPath, i++, "$")[0].name;
|
|
2082
|
+
if (nextPlaceholder) exprId = nextPlaceholder;
|
|
2083
|
+
else [exprId, contentId] = createPlaceholder(path, results, tempPath, i++, markers ? "/" : "");
|
|
2084
|
+
if (!markers) nextPlaceholder = exprId;
|
|
2085
|
+
results.exprs.push(t.expressionStatement(t.callExpression(insert, contentId ? [
|
|
2086
|
+
results.id,
|
|
2087
|
+
child.exprs[0],
|
|
2088
|
+
exprId,
|
|
2089
|
+
contentId
|
|
2090
|
+
] : [
|
|
2091
|
+
results.id,
|
|
2092
|
+
child.exprs[0],
|
|
2093
|
+
exprId
|
|
2094
|
+
])));
|
|
2095
|
+
tempPath = exprId.name;
|
|
2096
|
+
} else if (multi) results.exprs.push(t.expressionStatement(t.callExpression(insert, [
|
|
2097
|
+
results.id,
|
|
2098
|
+
child.exprs[0],
|
|
2099
|
+
nextChild(childNodes, index) || t.nullLiteral()
|
|
2100
|
+
])));
|
|
2101
|
+
else results.exprs.push(t.expressionStatement(t.callExpression(insert, [results.id, child.exprs[0]])));
|
|
2102
|
+
} else nextPlaceholder = null;
|
|
2103
|
+
});
|
|
2104
|
+
results.postExprs.unshift(...childPostExprs);
|
|
2105
|
+
}
|
|
2106
|
+
function createPlaceholder(path, results, tempPath, i, char) {
|
|
2107
|
+
const exprId = path.scope.generateUidIdentifier("el$"), config = getConfig(path);
|
|
2108
|
+
let contentId;
|
|
2109
|
+
results.template += `<!${char}>`;
|
|
2110
|
+
results.templateWithClosingTags += `<!${char}>`;
|
|
2111
|
+
if (config.hydratable && char === "/") {
|
|
2112
|
+
contentId = path.scope.generateUidIdentifier("co$");
|
|
2113
|
+
results.declarations.push(t.variableDeclarator(t.arrayPattern([exprId, contentId]), t.callExpression(registerImportMethod(path, "getNextMarker", getRendererConfig(path, "dom").moduleName), [t.memberExpression(t.identifier(tempPath), t.identifier("nextSibling"))])));
|
|
2114
|
+
} else results.declarations.push(t.variableDeclarator(exprId, t.memberExpression(t.identifier(tempPath), t.identifier(i === 0 ? "firstChild" : "nextSibling"))));
|
|
2115
|
+
return [exprId, contentId];
|
|
2116
|
+
}
|
|
2117
|
+
function nextChild(children, index) {
|
|
2118
|
+
return children[index + 1] && (children[index + 1].id || nextChild(children, index + 1));
|
|
2119
|
+
}
|
|
2120
|
+
function detectExpressions(children, index, config) {
|
|
2121
|
+
if (children[index - 1]) {
|
|
2122
|
+
const node = children[index - 1].node;
|
|
2123
|
+
if (t.isJSXExpressionContainer(node) && !t.isJSXEmptyExpression(node.expression) && getStaticExpression(children[index - 1]) === false) return true;
|
|
2124
|
+
let tagName;
|
|
2125
|
+
if (t.isJSXElement(node) && (tagName = getTagName(node)) && isComponent(tagName)) return true;
|
|
2126
|
+
}
|
|
2127
|
+
for (let i = index; i < children.length; i++) {
|
|
2128
|
+
const child = children[i].node;
|
|
2129
|
+
if (t.isJSXExpressionContainer(child)) {
|
|
2130
|
+
if (!t.isJSXEmptyExpression(child.expression) && getStaticExpression(children[i]) === false) return true;
|
|
2131
|
+
} else if (t.isJSXElement(child)) {
|
|
2132
|
+
const tagName = getTagName(child);
|
|
2133
|
+
if (isComponent(tagName)) return true;
|
|
2134
|
+
if (config.contextToCustomElements && (tagName === "slot" || tagName.indexOf("-") > -1 || child.openingElement.attributes.some((a) => a.name?.name === "is"))) return true;
|
|
2135
|
+
if (child.openingElement.attributes.some((attr) => t.isJSXSpreadAttribute(attr) || [
|
|
2136
|
+
"textContent",
|
|
2137
|
+
"innerHTML",
|
|
2138
|
+
"innerText"
|
|
2139
|
+
].includes(attr.name.name) || attr.name.namespace && (attr.name.namespace.name === "use" || attr.name.namespace.name === "prop") || t.isJSXExpressionContainer(attr.value) && !(t.isStringLiteral(attr.value.expression) || t.isNumericLiteral(attr.value.expression)))) return true;
|
|
2140
|
+
const nextChildren = filterChildren(children[i].get("children"));
|
|
2141
|
+
if (nextChildren.length) {
|
|
2142
|
+
if (detectExpressions(nextChildren, 0, config)) return true;
|
|
2143
|
+
}
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
}
|
|
2147
|
+
function contextToCustomElement(path, results) {
|
|
2148
|
+
results.exprs.push(t.expressionStatement(t.assignmentExpression("=", t.memberExpression(results.id, t.identifier("_$owner")), t.callExpression(registerImportMethod(path, "getOwner", getRendererConfig(path, "dom").moduleName), []))));
|
|
2149
|
+
}
|
|
2150
|
+
function processSpreads(path, attributes, { elem, isSVG, hasChildren, wrapConditionals }) {
|
|
2151
|
+
const config = getConfig(path);
|
|
2152
|
+
const filteredAttributes = [];
|
|
2153
|
+
const spreadArgs = [];
|
|
2154
|
+
let runningObject = [];
|
|
2155
|
+
let dynamicSpread = false;
|
|
2156
|
+
let firstSpread = false;
|
|
2157
|
+
attributes.forEach((attribute) => {
|
|
2158
|
+
const node = attribute.node;
|
|
2159
|
+
const key = !t.isJSXSpreadAttribute(node) && (t.isJSXNamespacedName(node.name) ? `${node.name.namespace.name}:${node.name.name.name}` : node.name.name);
|
|
2160
|
+
if (t.isJSXSpreadAttribute(node)) {
|
|
2161
|
+
const isStatic = node.innerComments && node.innerComments[0] && node.innerComments[0].value.trim() === config.staticMarker;
|
|
2162
|
+
firstSpread = true;
|
|
2163
|
+
if (runningObject.length) {
|
|
2164
|
+
spreadArgs.push(t.objectExpression(runningObject));
|
|
2165
|
+
runningObject = [];
|
|
2166
|
+
}
|
|
2167
|
+
const s = isDynamic(attribute.get("argument"), { checkMember: true }) && (dynamicSpread = true) ? t.isCallExpression(node.argument) && !node.argument.arguments.length && !t.isCallExpression(node.argument.callee) && !t.isMemberExpression(node.argument.callee) ? node.argument.callee : t.arrowFunctionExpression([], node.argument) : node.argument;
|
|
2168
|
+
spreadArgs.push(isStatic ? t.objectExpression([t.spreadElement(s)]) : s);
|
|
2169
|
+
} else if ((firstSpread || t.isJSXExpressionContainer(node.value) && isDynamic(attribute.get("value").get("expression"), { checkMember: true })) && canNativeSpread(key, { checkNameSpaces: true })) {
|
|
2170
|
+
const isContainer = t.isJSXExpressionContainer(node.value);
|
|
2171
|
+
if (isContainer && isDynamic(attribute.get("value").get("expression"), { checkMember: true })) {
|
|
2172
|
+
const id = convertJSXIdentifier(node.name);
|
|
2173
|
+
let expr = wrapConditionals && (t.isLogicalExpression(node.value.expression) || t.isConditionalExpression(node.value.expression)) ? transformCondition(attribute.get("value").get("expression"), true) : t.arrowFunctionExpression([], node.value.expression);
|
|
2174
|
+
runningObject.push(t.objectMethod("get", id, [], t.blockStatement([t.returnStatement(expr.body)]), !t.isValidIdentifier(key)));
|
|
2175
|
+
} else runningObject.push(t.objectProperty(t.stringLiteral(key), isContainer ? node.value.expression : node.value || (Properties.has(key) ? t.booleanLiteral(true) : t.stringLiteral(""))));
|
|
2176
|
+
} else filteredAttributes.push(attribute);
|
|
2177
|
+
});
|
|
2178
|
+
if (runningObject.length) spreadArgs.push(t.objectExpression(runningObject));
|
|
2179
|
+
const props = spreadArgs.length === 1 && !dynamicSpread ? spreadArgs[0] : t.callExpression(registerImportMethod(path, "mergeProps"), spreadArgs);
|
|
2180
|
+
return [filteredAttributes, t.expressionStatement(t.callExpression(registerImportMethod(path, "spread", getRendererConfig(path, "dom").moduleName), [
|
|
2181
|
+
elem,
|
|
2182
|
+
props,
|
|
2183
|
+
t.booleanLiteral(isSVG),
|
|
2184
|
+
t.booleanLiteral(hasChildren)
|
|
2185
|
+
]))];
|
|
2186
|
+
}
|
|
2187
|
+
|
|
2188
|
+
//#endregion
|
|
2189
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/dom/template.ts
|
|
2190
|
+
function createTemplate(path, result, wrap) {
|
|
2191
|
+
const config = getConfig(path);
|
|
2192
|
+
if (result.id) {
|
|
2193
|
+
registerTemplate(path, result);
|
|
2194
|
+
if (!(result.exprs.length || result.dynamics.length || result.postExprs.length) && result.decl.declarations.length === 1) return result.decl.declarations[0].init;
|
|
2195
|
+
else return t.callExpression(t.arrowFunctionExpression([], t.blockStatement([
|
|
2196
|
+
result.decl,
|
|
2197
|
+
...result.exprs.concat(wrapDynamics(path, result.dynamics) || [], result.postExprs || []),
|
|
2198
|
+
t.returnStatement(result.id)
|
|
2199
|
+
])), []);
|
|
2200
|
+
}
|
|
2201
|
+
if (wrap && result.dynamic && config.memoWrapper) return t.callExpression(registerImportMethod(path, config.memoWrapper), [result.exprs[0]]);
|
|
2202
|
+
return result.exprs[0];
|
|
2203
|
+
}
|
|
2204
|
+
function appendTemplates(path, templates) {
|
|
2205
|
+
const declarators = templates.map((template$1) => {
|
|
2206
|
+
const tmpl = {
|
|
2207
|
+
cooked: template$1.template,
|
|
2208
|
+
raw: escapeStringForTemplate(template$1.template)
|
|
2209
|
+
};
|
|
2210
|
+
const shouldUseImportNode = template$1.isCE || template$1.isImportNode;
|
|
2211
|
+
const isMathML = /^<(math|annotation|annotation-xml|maction|math|merror|mfrac|mi|mmultiscripts|mn|mo|mover|mpadded|mphantom|mprescripts|mroot|mrow|ms|mspace|msqrt|mstyle|msub|msubsup|msup|mtable|mtd|mtext|mtr|munder|munderover|semantics|menclose|mfenced)(\s|>)/.test(template$1.template);
|
|
2212
|
+
return t.variableDeclarator(template$1.id, t.addComment(t.callExpression(registerImportMethod(path, "template", getRendererConfig(path, "dom").moduleName), [t.templateLiteral([t.templateElement(tmpl, true)], [])].concat(template$1.isSVG || shouldUseImportNode || isMathML ? [
|
|
2213
|
+
t.booleanLiteral(!!shouldUseImportNode),
|
|
2214
|
+
t.booleanLiteral(template$1.isSVG),
|
|
2215
|
+
t.booleanLiteral(isMathML)
|
|
2216
|
+
] : [])), "leading", "#__PURE__"));
|
|
2217
|
+
});
|
|
2218
|
+
path.node.body.unshift(t.variableDeclaration("var", declarators));
|
|
2219
|
+
}
|
|
2220
|
+
function registerTemplate(path, results) {
|
|
2221
|
+
const { hydratable } = getConfig(path);
|
|
2222
|
+
let decl;
|
|
2223
|
+
if (results.template.length) {
|
|
2224
|
+
let templateDef, templateId;
|
|
2225
|
+
if (!results.skipTemplate) {
|
|
2226
|
+
const templates = path.scope.getProgramParent().data.templates || (path.scope.getProgramParent().data.templates = []);
|
|
2227
|
+
if (templateDef = templates.find((t$1) => t$1.template === results.template)) templateId = templateDef.id;
|
|
2228
|
+
else {
|
|
2229
|
+
templateId = path.scope.generateUidIdentifier("tmpl$");
|
|
2230
|
+
templates.push({
|
|
2231
|
+
id: templateId,
|
|
2232
|
+
template: results.template,
|
|
2233
|
+
templateWithClosingTags: results.templateWithClosingTags,
|
|
2234
|
+
isSVG: results.isSVG,
|
|
2235
|
+
isCE: results.hasCustomElement,
|
|
2236
|
+
isImportNode: results.isImportNode,
|
|
2237
|
+
renderer: "dom"
|
|
2238
|
+
});
|
|
2239
|
+
}
|
|
2240
|
+
}
|
|
2241
|
+
decl = t.variableDeclarator(results.id, hydratable ? t.callExpression(registerImportMethod(path, "getNextElement", getRendererConfig(path, "dom").moduleName), templateId ? [templateId] : []) : t.callExpression(templateId, []));
|
|
2242
|
+
}
|
|
2243
|
+
results.declarations.unshift(decl);
|
|
2244
|
+
results.decl = t.variableDeclaration("var", results.declarations);
|
|
2245
|
+
}
|
|
2246
|
+
function wrapDynamics(path, dynamics) {
|
|
2247
|
+
if (!dynamics.length) return;
|
|
2248
|
+
const effectWrapperId = registerImportMethod(path, getConfig(path).effectWrapper);
|
|
2249
|
+
if (dynamics.length === 1) {
|
|
2250
|
+
let dynamicStyle;
|
|
2251
|
+
const prevValue = dynamics[0].key === "classList" || dynamics[0].key === "style" || (dynamicStyle = dynamics[0].key.startsWith("style:")) ? t.identifier("_$p") : void 0;
|
|
2252
|
+
if (dynamicStyle) dynamics[0].value = t.assignmentExpression("=", prevValue, dynamics[0].value);
|
|
2253
|
+
else if (dynamics[0].key.startsWith("class:") && !t.isBooleanLiteral(dynamics[0].value) && !t.isUnaryExpression(dynamics[0].value)) dynamics[0].value = t.unaryExpression("!", t.unaryExpression("!", dynamics[0].value));
|
|
2254
|
+
return t.expressionStatement(t.callExpression(effectWrapperId, [t.arrowFunctionExpression(prevValue ? [prevValue] : [], setAttr(path, dynamics[0].elem, dynamics[0].key, dynamics[0].value, {
|
|
2255
|
+
isSVG: dynamics[0].isSVG,
|
|
2256
|
+
isCE: dynamics[0].isCE,
|
|
2257
|
+
tagName: dynamics[0].tagName,
|
|
2258
|
+
dynamic: true,
|
|
2259
|
+
prevId: prevValue
|
|
2260
|
+
}))]));
|
|
2261
|
+
}
|
|
2262
|
+
const prevId = t.identifier("_p$");
|
|
2263
|
+
/** @type {t.VariableDeclarator[]} */
|
|
2264
|
+
const declarations = [];
|
|
2265
|
+
/** @type {t.ExpressionStatement[]} */
|
|
2266
|
+
const statements = [];
|
|
2267
|
+
/** @type {t.Identifier[]} */
|
|
2268
|
+
const properties = [];
|
|
2269
|
+
dynamics.forEach(({ elem, key, value, isSVG, isCE, tagName }, index) => {
|
|
2270
|
+
const varIdent = path.scope.generateUidIdentifier("v$");
|
|
2271
|
+
const propIdent = t.identifier(getNumberedId(index));
|
|
2272
|
+
const propMember = t.memberExpression(prevId, propIdent);
|
|
2273
|
+
if (key.startsWith("class:") && !t.isBooleanLiteral(value) && !t.isUnaryExpression(value)) value = t.unaryExpression("!", t.unaryExpression("!", value));
|
|
2274
|
+
properties.push(propIdent);
|
|
2275
|
+
declarations.push(t.variableDeclarator(varIdent, value));
|
|
2276
|
+
if (key === "classList" || key === "style") statements.push(t.expressionStatement(t.assignmentExpression("=", propMember, setAttr(path, elem, key, varIdent, {
|
|
2277
|
+
isSVG,
|
|
2278
|
+
isCE,
|
|
2279
|
+
tagName,
|
|
2280
|
+
dynamic: true,
|
|
2281
|
+
prevId: propMember
|
|
2282
|
+
}))));
|
|
2283
|
+
else {
|
|
2284
|
+
const prev = key.startsWith("style:") ? varIdent : void 0;
|
|
2285
|
+
statements.push(t.expressionStatement(t.logicalExpression("&&", t.binaryExpression("!==", varIdent, propMember), setAttr(path, elem, key, t.assignmentExpression("=", propMember, varIdent), {
|
|
2286
|
+
isSVG,
|
|
2287
|
+
isCE,
|
|
2288
|
+
tagName,
|
|
2289
|
+
dynamic: true,
|
|
2290
|
+
prevId: prev
|
|
2291
|
+
}))));
|
|
2292
|
+
}
|
|
2293
|
+
});
|
|
2294
|
+
return t.expressionStatement(t.callExpression(effectWrapperId, [t.arrowFunctionExpression([prevId], t.blockStatement([
|
|
2295
|
+
t.variableDeclaration("var", declarations),
|
|
2296
|
+
...statements,
|
|
2297
|
+
t.returnStatement(prevId)
|
|
2298
|
+
])), t.objectExpression(properties.map((id) => t.objectProperty(id, t.identifier("undefined"))))]));
|
|
2299
|
+
}
|
|
2300
|
+
|
|
2301
|
+
//#endregion
|
|
2302
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/shared/validate.ts
|
|
2303
|
+
/** `bodyElement` will be used as a `context` (The place where we run `innerHTML`) */
|
|
2304
|
+
const bodyElement = parse5.parse(`<!DOCTYPE html><html><head></head><body></body></html>`).childNodes[1].childNodes[1];
|
|
2305
|
+
function innerHTML(htmlFragment) {
|
|
2306
|
+
/** `htmlFragment` will be parsed as if it was set to the `bodyElement`'s `innerHTML` property. */
|
|
2307
|
+
const parsedFragment = parse5.parseFragment(bodyElement, htmlFragment);
|
|
2308
|
+
/** `serialize` returns back a string from the parsed nodes */
|
|
2309
|
+
return parse5.serialize(parsedFragment);
|
|
2310
|
+
}
|
|
2311
|
+
/**
|
|
2312
|
+
* Returns an object with information when the markup is invalid
|
|
2313
|
+
*
|
|
2314
|
+
* @param {string} html - The html string to validate
|
|
2315
|
+
* @returns {{
|
|
2316
|
+
* html: string; // html stripped of attributives and content
|
|
2317
|
+
* browser: string; // what the browser returned from evaluating `html`
|
|
2318
|
+
* } | null}
|
|
2319
|
+
*/
|
|
2320
|
+
function isInvalidMarkup(html) {
|
|
2321
|
+
html = html.replaceAll("<!>", "<!---->").replaceAll("<!$>", "<!--$-->").replaceAll("<!/>", "<!--/-->").replace(/^[^<]+/, "#text").replace(/[^>]+$/, "#text").replace(/>[^<]+</gi, ">#text<").replace(/<([^>]+)>/gi, "<$1>");
|
|
2322
|
+
if (/^<(td|th)>/.test(html)) html = `<table><tbody><tr>${html}</tr></tbody></table>`;
|
|
2323
|
+
if (/^<tr>/.test(html)) html = `<table><tbody>${html}</tbody></table>`;
|
|
2324
|
+
if (/^<col>/.test(html)) html = `<table><colgroup>${html}</colgroup></table>`;
|
|
2325
|
+
if (/^<(thead|tbody|tfoot|colgroup|caption)>/.test(html)) html = `<table>${html}</table>`;
|
|
2326
|
+
switch (html) {
|
|
2327
|
+
case "<table></table>":
|
|
2328
|
+
case "<table><thead></thead></table>":
|
|
2329
|
+
case "<table><tbody></tbody></table>":
|
|
2330
|
+
case "<table><thead></thead><tbody></tbody></table>": return;
|
|
2331
|
+
}
|
|
2332
|
+
/** Parse HTML. `browser` is a string with the supposed resulting html of a real `innerHTML` call */
|
|
2333
|
+
const browser = innerHTML(html);
|
|
2334
|
+
if (html.toLowerCase() !== browser.toLowerCase()) return {
|
|
2335
|
+
html,
|
|
2336
|
+
browser
|
|
2337
|
+
};
|
|
2338
|
+
}
|
|
2339
|
+
|
|
2340
|
+
//#endregion
|
|
2341
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/shared/postprocess.ts
|
|
2342
|
+
var postprocess_default = (path, state) => {
|
|
2343
|
+
if (state.skip) return;
|
|
2344
|
+
if (path.scope.data.events) path.node.body.push(t.expressionStatement(t.callExpression(registerImportMethod(path, "delegateEvents", getRendererConfig(path, "dom").moduleName), [t.arrayExpression(Array.from(path.scope.data.events).map((e) => t.stringLiteral(e)))])));
|
|
2345
|
+
if (path.scope.data.templates?.length) {
|
|
2346
|
+
if (path.hub.file.metadata.config.validate) for (const template$1 of path.scope.data.templates) {
|
|
2347
|
+
const html = template$1.templateWithClosingTags;
|
|
2348
|
+
if (typeof html === "string") {
|
|
2349
|
+
const result = isInvalidMarkup(html);
|
|
2350
|
+
if (result) {
|
|
2351
|
+
console.warn("\nThe HTML provided is malformed and will yield unexpected output when evaluated by a browser.\n");
|
|
2352
|
+
console.warn("User HTML:\n", result.html);
|
|
2353
|
+
console.warn("Browser HTML:\n", result.browser);
|
|
2354
|
+
console.warn("Original HTML:\n", html);
|
|
2355
|
+
}
|
|
2356
|
+
}
|
|
2357
|
+
}
|
|
2358
|
+
let domTemplates = path.scope.data.templates.filter((temp) => temp.renderer === "dom");
|
|
2359
|
+
let ssrTemplates = path.scope.data.templates.filter((temp) => temp.renderer === "ssr");
|
|
2360
|
+
domTemplates.length > 0 && appendTemplates(path, domTemplates);
|
|
2361
|
+
ssrTemplates.length > 0 && appendTemplates$1(path, ssrTemplates);
|
|
2362
|
+
}
|
|
2363
|
+
};
|
|
2364
|
+
|
|
2365
|
+
//#endregion
|
|
2366
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/config.ts
|
|
2367
|
+
var config_default = {
|
|
2368
|
+
moduleName: "dom",
|
|
2369
|
+
generate: "dom",
|
|
2370
|
+
hydratable: false,
|
|
2371
|
+
delegateEvents: true,
|
|
2372
|
+
delegatedEvents: [],
|
|
2373
|
+
builtIns: [],
|
|
2374
|
+
requireImportSource: false,
|
|
2375
|
+
wrapConditionals: true,
|
|
2376
|
+
omitNestedClosingTags: false,
|
|
2377
|
+
omitLastClosingTag: true,
|
|
2378
|
+
omitQuotes: true,
|
|
2379
|
+
contextToCustomElements: false,
|
|
2380
|
+
staticMarker: "@once",
|
|
2381
|
+
effectWrapper: "effect",
|
|
2382
|
+
memoWrapper: "memo",
|
|
2383
|
+
validate: true
|
|
2384
|
+
};
|
|
2385
|
+
|
|
2386
|
+
//#endregion
|
|
2387
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/shared/preprocess.ts
|
|
2388
|
+
var preprocess_default = (path, state) => {
|
|
2389
|
+
const lib = (path.hub.file.metadata.config = Object.assign({}, config_default, state.opts)).requireImportSource;
|
|
2390
|
+
if (lib) {
|
|
2391
|
+
const comments = path.hub.file.ast.comments;
|
|
2392
|
+
let process = false;
|
|
2393
|
+
for (let i = 0; i < comments.length; i++) {
|
|
2394
|
+
const pieces = comments[i].value.split("@jsxImportSource");
|
|
2395
|
+
if (pieces.length === 2 && pieces[1].trim() === lib) {
|
|
2396
|
+
process = true;
|
|
2397
|
+
break;
|
|
2398
|
+
}
|
|
2399
|
+
}
|
|
2400
|
+
if (!process) {
|
|
2401
|
+
state.skip = true;
|
|
2402
|
+
return;
|
|
2403
|
+
}
|
|
2404
|
+
}
|
|
2405
|
+
};
|
|
2406
|
+
|
|
2407
|
+
//#endregion
|
|
2408
|
+
//#region src/babel-plugin-jsx-dom-expressions/babel-plugin-jsx-dom-expressions/index.ts
|
|
2409
|
+
const syntaxJSX = declare(() => {
|
|
2410
|
+
return {
|
|
2411
|
+
name: "syntax-jsx",
|
|
2412
|
+
manipulateOptions(opts, parserOpts) {
|
|
2413
|
+
if (parserOpts.plugins.some((p) => (Array.isArray(p) ? p[0] : p) === "typescript")) return;
|
|
2414
|
+
parserOpts.plugins.push("jsx");
|
|
2415
|
+
}
|
|
2416
|
+
};
|
|
2417
|
+
});
|
|
2418
|
+
const jsxTransform = () => {
|
|
2419
|
+
return {
|
|
2420
|
+
name: "JSX DOM Expressions",
|
|
2421
|
+
inherits: syntaxJSX,
|
|
2422
|
+
visitor: {
|
|
2423
|
+
JSXElement: transformJSX,
|
|
2424
|
+
JSXFragment: transformJSX,
|
|
2425
|
+
Program: {
|
|
2426
|
+
enter: preprocess_default,
|
|
2427
|
+
exit: postprocess_default
|
|
2428
|
+
}
|
|
2429
|
+
}
|
|
2430
|
+
};
|
|
2431
|
+
};
|
|
2432
|
+
var babel_plugin_jsx_dom_expressions_default = jsxTransform;
|
|
2433
|
+
|
|
2434
|
+
//#endregion
|
|
2435
|
+
//#region src/plugin-transform-typescript/enum.ts
|
|
2436
|
+
function isTransparentExprWrapper(node) {
|
|
2437
|
+
return t.isTSAsExpression(node) || t.isTSSatisfiesExpression(node) || t.isTSTypeAssertion(node) || t.isTSNonNullExpression(node) || t.isTypeCastExpression(node) || t.isParenthesizedExpression(node);
|
|
2438
|
+
}
|
|
2439
|
+
function skipTransparentExprWrapperNodes(node) {
|
|
2440
|
+
while (isTransparentExprWrapper(node)) node = node.expression;
|
|
2441
|
+
return node;
|
|
2442
|
+
}
|
|
2443
|
+
const PURE_ANNOTATION = "#__PURE__";
|
|
2444
|
+
const isPureAnnotated = ({ leadingComments }) => !!leadingComments && leadingComments.some((comment) => /[@#]__PURE__/.test(comment.value));
|
|
2445
|
+
function annotateAsPure(pathOrNode) {
|
|
2446
|
+
const node = pathOrNode.node || pathOrNode;
|
|
2447
|
+
if (isPureAnnotated(node)) return;
|
|
2448
|
+
addComment(node, "leading", PURE_ANNOTATION);
|
|
2449
|
+
}
|
|
2450
|
+
const ENUMS = /* @__PURE__ */ new WeakMap();
|
|
2451
|
+
const buildEnumWrapper = template.expression(`
|
|
2452
|
+
(function (ID) {
|
|
2453
|
+
ASSIGNMENTS;
|
|
2454
|
+
return ID;
|
|
2455
|
+
})(INIT)
|
|
2456
|
+
`);
|
|
2457
|
+
function transpileEnum(path, t$1) {
|
|
2458
|
+
const { node, parentPath } = path;
|
|
2459
|
+
if (node.declare) {
|
|
2460
|
+
path.remove();
|
|
2461
|
+
return;
|
|
2462
|
+
}
|
|
2463
|
+
const name = node.id.name;
|
|
2464
|
+
const { fill, data, isPure } = enumFill(path, t$1, node.id);
|
|
2465
|
+
switch (parentPath.type) {
|
|
2466
|
+
case "BlockStatement":
|
|
2467
|
+
case "ExportNamedDeclaration":
|
|
2468
|
+
case "Program": {
|
|
2469
|
+
const isGlobal = t$1.isProgram(path.parent);
|
|
2470
|
+
const isSeen = seen(parentPath);
|
|
2471
|
+
let init = t$1.objectExpression([]);
|
|
2472
|
+
if (isSeen || isGlobal) init = t$1.logicalExpression("||", t$1.cloneNode(fill.ID), init);
|
|
2473
|
+
const enumIIFE = buildEnumWrapper({
|
|
2474
|
+
...fill,
|
|
2475
|
+
INIT: init
|
|
2476
|
+
});
|
|
2477
|
+
if (isPure) annotateAsPure(enumIIFE);
|
|
2478
|
+
if (isSeen) (parentPath.isExportDeclaration() ? parentPath : path).replaceWith(t$1.expressionStatement(t$1.assignmentExpression("=", t$1.cloneNode(node.id), enumIIFE)));
|
|
2479
|
+
else path.scope.registerDeclaration(path.replaceWith(t$1.variableDeclaration(isGlobal ? "var" : "let", [t$1.variableDeclarator(node.id, enumIIFE)]))[0]);
|
|
2480
|
+
ENUMS.set(path.scope.getBindingIdentifier(name), data);
|
|
2481
|
+
break;
|
|
2482
|
+
}
|
|
2483
|
+
default: throw new Error(`Unexpected enum parent '${path.parent.type}`);
|
|
2484
|
+
}
|
|
2485
|
+
function seen(parentPath$1) {
|
|
2486
|
+
if (parentPath$1.isExportDeclaration()) return seen(parentPath$1.parentPath);
|
|
2487
|
+
if (parentPath$1.getData(name)) return true;
|
|
2488
|
+
else {
|
|
2489
|
+
parentPath$1.setData(name, true);
|
|
2490
|
+
return false;
|
|
2491
|
+
}
|
|
2492
|
+
}
|
|
2493
|
+
}
|
|
2494
|
+
const buildStringAssignment = template.statement(`
|
|
2495
|
+
ENUM["NAME"] = VALUE;
|
|
2496
|
+
`);
|
|
2497
|
+
const buildNumericAssignment = template.statement(`
|
|
2498
|
+
ENUM[ENUM["NAME"] = VALUE] = "NAME";
|
|
2499
|
+
`);
|
|
2500
|
+
const buildEnumMember = (isString, options) => (isString ? buildStringAssignment : buildNumericAssignment)(options);
|
|
2501
|
+
/**
|
|
2502
|
+
* Generates the statement that fills in the variable declared by the enum.
|
|
2503
|
+
* `(function (E) { ... assignments ... })(E || (E = {}));`
|
|
2504
|
+
*/
|
|
2505
|
+
function enumFill(path, t$1, id) {
|
|
2506
|
+
const { enumValues, data, isPure } = translateEnumValues(path, t$1);
|
|
2507
|
+
const enumMembers = path.get("members");
|
|
2508
|
+
const assignments = [];
|
|
2509
|
+
for (let i = 0; i < enumMembers.length; i++) {
|
|
2510
|
+
const [memberName, memberValue] = enumValues[i];
|
|
2511
|
+
assignments.push(t$1.inheritsComments(buildEnumMember(isSyntacticallyString(memberValue), {
|
|
2512
|
+
ENUM: t$1.cloneNode(id),
|
|
2513
|
+
NAME: memberName,
|
|
2514
|
+
VALUE: memberValue
|
|
2515
|
+
}), enumMembers[i].node));
|
|
2516
|
+
}
|
|
2517
|
+
return {
|
|
2518
|
+
fill: {
|
|
2519
|
+
ID: t$1.cloneNode(id),
|
|
2520
|
+
ASSIGNMENTS: assignments
|
|
2521
|
+
},
|
|
2522
|
+
data,
|
|
2523
|
+
isPure
|
|
2524
|
+
};
|
|
2525
|
+
}
|
|
2526
|
+
function isSyntacticallyString(expr) {
|
|
2527
|
+
expr = skipTransparentExprWrapperNodes(expr);
|
|
2528
|
+
switch (expr.type) {
|
|
2529
|
+
case "BinaryExpression": {
|
|
2530
|
+
const left = expr.left;
|
|
2531
|
+
const right = expr.right;
|
|
2532
|
+
return expr.operator === "+" && (isSyntacticallyString(left) || isSyntacticallyString(right));
|
|
2533
|
+
}
|
|
2534
|
+
case "TemplateLiteral":
|
|
2535
|
+
case "StringLiteral": return true;
|
|
2536
|
+
}
|
|
2537
|
+
return false;
|
|
2538
|
+
}
|
|
2539
|
+
function ReferencedIdentifier(expr, state) {
|
|
2540
|
+
const { seen, path, t: t$1 } = state;
|
|
2541
|
+
const name = expr.node.name;
|
|
2542
|
+
if (seen.has(name)) {
|
|
2543
|
+
for (let curScope = expr.scope; curScope !== path.scope; curScope = curScope.parent) if (curScope.hasOwnBinding(name)) return;
|
|
2544
|
+
expr.replaceWith(t$1.memberExpression(t$1.cloneNode(path.node.id), t$1.cloneNode(expr.node)));
|
|
2545
|
+
expr.skip();
|
|
2546
|
+
}
|
|
2547
|
+
}
|
|
2548
|
+
const enumSelfReferenceVisitor = { ReferencedIdentifier };
|
|
2549
|
+
function translateEnumValues(path, t$1) {
|
|
2550
|
+
const bindingIdentifier = path.scope.getBindingIdentifier(path.node.id.name);
|
|
2551
|
+
const seen = ENUMS.get(bindingIdentifier) ?? /* @__PURE__ */ new Map();
|
|
2552
|
+
let constValue = -1;
|
|
2553
|
+
let lastName;
|
|
2554
|
+
let isPure = true;
|
|
2555
|
+
const enumValues = path.get("members").map((memberPath) => {
|
|
2556
|
+
const member = memberPath.node;
|
|
2557
|
+
const name = t$1.isIdentifier(member.id) ? member.id.name : member.id.value;
|
|
2558
|
+
const initializerPath = memberPath.get("initializer");
|
|
2559
|
+
const initializer = member.initializer;
|
|
2560
|
+
let value;
|
|
2561
|
+
if (initializer) {
|
|
2562
|
+
constValue = computeConstantValue(initializerPath, seen);
|
|
2563
|
+
if (constValue !== void 0) {
|
|
2564
|
+
seen.set(name, constValue);
|
|
2565
|
+
assert(typeof constValue === "number" || typeof constValue === "string");
|
|
2566
|
+
if (constValue === Infinity || Number.isNaN(constValue)) value = t$1.identifier(String(constValue));
|
|
2567
|
+
else if (constValue === -Infinity) value = t$1.unaryExpression("-", t$1.identifier("Infinity"));
|
|
2568
|
+
else value = t$1.valueToNode(constValue);
|
|
2569
|
+
} else {
|
|
2570
|
+
isPure &&= initializerPath.isPure();
|
|
2571
|
+
if (initializerPath.isReferencedIdentifier()) ReferencedIdentifier(initializerPath, {
|
|
2572
|
+
t: t$1,
|
|
2573
|
+
seen,
|
|
2574
|
+
path
|
|
2575
|
+
});
|
|
2576
|
+
else initializerPath.traverse(enumSelfReferenceVisitor, {
|
|
2577
|
+
t: t$1,
|
|
2578
|
+
seen,
|
|
2579
|
+
path
|
|
2580
|
+
});
|
|
2581
|
+
value = initializerPath.node;
|
|
2582
|
+
seen.set(name, void 0);
|
|
2583
|
+
}
|
|
2584
|
+
} else if (typeof constValue === "number") {
|
|
2585
|
+
constValue += 1;
|
|
2586
|
+
value = t$1.numericLiteral(constValue);
|
|
2587
|
+
seen.set(name, constValue);
|
|
2588
|
+
} else if (typeof constValue === "string") throw path.buildCodeFrameError("Enum member must have initializer.");
|
|
2589
|
+
else {
|
|
2590
|
+
const lastRef = t$1.memberExpression(t$1.cloneNode(path.node.id), t$1.stringLiteral(lastName), true);
|
|
2591
|
+
value = t$1.binaryExpression("+", t$1.numericLiteral(1), lastRef);
|
|
2592
|
+
seen.set(name, void 0);
|
|
2593
|
+
}
|
|
2594
|
+
lastName = name;
|
|
2595
|
+
return [name, value];
|
|
2596
|
+
});
|
|
2597
|
+
return {
|
|
2598
|
+
isPure,
|
|
2599
|
+
data: seen,
|
|
2600
|
+
enumValues
|
|
2601
|
+
};
|
|
2602
|
+
}
|
|
2603
|
+
function computeConstantValue(path, prevMembers, seen = /* @__PURE__ */ new Set()) {
|
|
2604
|
+
return evaluate(path);
|
|
2605
|
+
function evaluate(path$1) {
|
|
2606
|
+
const expr = path$1.node;
|
|
2607
|
+
switch (expr.type) {
|
|
2608
|
+
case "MemberExpression": return evaluateRef(path$1, prevMembers, seen);
|
|
2609
|
+
case "StringLiteral": return expr.value;
|
|
2610
|
+
case "UnaryExpression": return evalUnaryExpression(path$1);
|
|
2611
|
+
case "BinaryExpression": return evalBinaryExpression(path$1);
|
|
2612
|
+
case "NumericLiteral": return expr.value;
|
|
2613
|
+
case "ParenthesizedExpression": return evaluate(path$1.get("expression"));
|
|
2614
|
+
case "Identifier": return evaluateRef(path$1, prevMembers, seen);
|
|
2615
|
+
case "TemplateLiteral": {
|
|
2616
|
+
if (expr.quasis.length === 1) return expr.quasis[0].value.cooked;
|
|
2617
|
+
const paths = path$1.get("expressions");
|
|
2618
|
+
const quasis = expr.quasis;
|
|
2619
|
+
let str = "";
|
|
2620
|
+
for (let i = 0; i < quasis.length; i++) {
|
|
2621
|
+
str += quasis[i].value.cooked;
|
|
2622
|
+
if (i + 1 < quasis.length) {
|
|
2623
|
+
const value = evaluateRef(paths[i], prevMembers, seen);
|
|
2624
|
+
if (value === void 0) return void 0;
|
|
2625
|
+
str += value;
|
|
2626
|
+
}
|
|
2627
|
+
}
|
|
2628
|
+
return str;
|
|
2629
|
+
}
|
|
2630
|
+
default: return;
|
|
2631
|
+
}
|
|
2632
|
+
}
|
|
2633
|
+
function evaluateRef(path$1, prevMembers$1, seen$1) {
|
|
2634
|
+
if (path$1.isMemberExpression()) {
|
|
2635
|
+
const expr = path$1.node;
|
|
2636
|
+
const obj = expr.object;
|
|
2637
|
+
const prop = expr.property;
|
|
2638
|
+
if (!t.isIdentifier(obj) || (expr.computed ? !t.isStringLiteral(prop) : !t.isIdentifier(prop))) return;
|
|
2639
|
+
const bindingIdentifier = path$1.scope.getBindingIdentifier(obj.name);
|
|
2640
|
+
const data = ENUMS.get(bindingIdentifier);
|
|
2641
|
+
if (!data) return;
|
|
2642
|
+
return data.get(prop.computed ? prop.value : prop.name);
|
|
2643
|
+
} else if (path$1.isIdentifier()) {
|
|
2644
|
+
const name = path$1.node.name;
|
|
2645
|
+
if (["Infinity", "NaN"].includes(name)) return Number(name);
|
|
2646
|
+
let value = prevMembers$1?.get(name);
|
|
2647
|
+
if (value !== void 0) return value;
|
|
2648
|
+
if (prevMembers$1?.has(name)) return;
|
|
2649
|
+
if (seen$1.has(path$1.node)) return;
|
|
2650
|
+
seen$1.add(path$1.node);
|
|
2651
|
+
value = computeConstantValue(path$1.resolve(), prevMembers$1, seen$1);
|
|
2652
|
+
return value;
|
|
2653
|
+
}
|
|
2654
|
+
}
|
|
2655
|
+
function evalUnaryExpression(path$1) {
|
|
2656
|
+
const value = evaluate(path$1.get("argument"));
|
|
2657
|
+
if (value === void 0) return;
|
|
2658
|
+
switch (path$1.node.operator) {
|
|
2659
|
+
case "+": return value;
|
|
2660
|
+
case "-": return -value;
|
|
2661
|
+
case "~": return ~value;
|
|
2662
|
+
default: return;
|
|
2663
|
+
}
|
|
2664
|
+
}
|
|
2665
|
+
function evalBinaryExpression(path$1) {
|
|
2666
|
+
const left = evaluate(path$1.get("left"));
|
|
2667
|
+
if (left === void 0) return;
|
|
2668
|
+
const right = evaluate(path$1.get("right"));
|
|
2669
|
+
if (right === void 0) return;
|
|
2670
|
+
switch (path$1.node.operator) {
|
|
2671
|
+
case "|": return left | right;
|
|
2672
|
+
case "&": return left & right;
|
|
2673
|
+
case ">>": return left >> right;
|
|
2674
|
+
case ">>>": return left >>> right;
|
|
2675
|
+
case "<<": return left << right;
|
|
2676
|
+
case "^": return left ^ right;
|
|
2677
|
+
case "*": return left * right;
|
|
2678
|
+
case "/": return left / right;
|
|
2679
|
+
case "+": return left + right;
|
|
2680
|
+
case "-": return left - right;
|
|
2681
|
+
case "%": return left % right;
|
|
2682
|
+
case "**": return left ** right;
|
|
2683
|
+
default: return;
|
|
2684
|
+
}
|
|
2685
|
+
}
|
|
2686
|
+
}
|
|
2687
|
+
|
|
2688
|
+
//#endregion
|
|
2689
|
+
//#region src/plugin-transform-typescript/const-enum.ts
|
|
2690
|
+
const EXPORTED_CONST_ENUMS_IN_NAMESPACE = /* @__PURE__ */ new WeakSet();
|
|
2691
|
+
function transpileConstEnum(path, t$1) {
|
|
2692
|
+
const { name } = path.node.id;
|
|
2693
|
+
const parentIsExport = path.parentPath.isExportNamedDeclaration();
|
|
2694
|
+
let isExported = parentIsExport;
|
|
2695
|
+
if (!isExported && t$1.isProgram(path.parent)) isExported = path.parent.body.some((stmt) => t$1.isExportNamedDeclaration(stmt) && stmt.exportKind !== "type" && !stmt.source && stmt.specifiers.some((spec) => t$1.isExportSpecifier(spec) && spec.exportKind !== "type" && spec.local.name === name));
|
|
2696
|
+
const { enumValues: entries } = translateEnumValues(path, t$1);
|
|
2697
|
+
if (isExported || EXPORTED_CONST_ENUMS_IN_NAMESPACE.has(path.node)) {
|
|
2698
|
+
const obj = t$1.objectExpression(entries.map(([name$1, value]) => t$1.objectProperty(t$1.isValidIdentifier(name$1) ? t$1.identifier(name$1) : t$1.stringLiteral(name$1), value)));
|
|
2699
|
+
if (path.scope.hasOwnBinding(name)) (parentIsExport ? path.parentPath : path).replaceWith(t$1.expressionStatement(t$1.callExpression(t$1.memberExpression(t$1.identifier("Object"), t$1.identifier("assign")), [path.node.id, obj])));
|
|
2700
|
+
else {
|
|
2701
|
+
path.replaceWith(t$1.variableDeclaration("var", [t$1.variableDeclarator(path.node.id, obj)]));
|
|
2702
|
+
path.scope.registerDeclaration(path);
|
|
2703
|
+
}
|
|
2704
|
+
return;
|
|
2705
|
+
}
|
|
2706
|
+
const entriesMap = new Map(entries);
|
|
2707
|
+
path.scope.path.traverse({
|
|
2708
|
+
Scope(path$1) {
|
|
2709
|
+
if (path$1.scope.hasOwnBinding(name)) path$1.skip();
|
|
2710
|
+
},
|
|
2711
|
+
MemberExpression(path$1) {
|
|
2712
|
+
if (!t$1.isIdentifier(path$1.node.object, { name })) return;
|
|
2713
|
+
let key;
|
|
2714
|
+
if (path$1.node.computed) if (t$1.isStringLiteral(path$1.node.property)) key = path$1.node.property.value;
|
|
2715
|
+
else return;
|
|
2716
|
+
else if (t$1.isIdentifier(path$1.node.property)) key = path$1.node.property.name;
|
|
2717
|
+
else return;
|
|
2718
|
+
if (!entriesMap.has(key)) return;
|
|
2719
|
+
path$1.replaceWith(t$1.cloneNode(entriesMap.get(key)));
|
|
2720
|
+
}
|
|
2721
|
+
});
|
|
2722
|
+
path.remove();
|
|
2723
|
+
}
|
|
2724
|
+
|
|
2725
|
+
//#endregion
|
|
2726
|
+
//#region src/plugin-transform-typescript/create-class-features-plugin.ts
|
|
2727
|
+
const findBareSupers = visitors.environmentVisitor({ Super(path) {
|
|
2728
|
+
const { node, parentPath } = path;
|
|
2729
|
+
if (parentPath.isCallExpression({ callee: node })) this.push(parentPath);
|
|
2730
|
+
} });
|
|
2731
|
+
const referenceVisitor = {
|
|
2732
|
+
"TSTypeAnnotation|TypeAnnotation"(path) {
|
|
2733
|
+
path.skip();
|
|
2734
|
+
},
|
|
2735
|
+
ReferencedIdentifier(path, { scope }) {
|
|
2736
|
+
if (scope.hasOwnBinding(path.node.name)) {
|
|
2737
|
+
scope.rename(path.node.name);
|
|
2738
|
+
path.skip();
|
|
2739
|
+
}
|
|
2740
|
+
}
|
|
2741
|
+
};
|
|
2742
|
+
function injectInitialization(path, constructor, nodes, renamer, lastReturnsThis) {
|
|
2743
|
+
if (!nodes.length) return;
|
|
2744
|
+
const isDerived = !!path.node.superClass;
|
|
2745
|
+
if (!constructor) {
|
|
2746
|
+
const newConstructor = t.classMethod("constructor", t.identifier("constructor"), [], t.blockStatement([]));
|
|
2747
|
+
if (isDerived) {
|
|
2748
|
+
newConstructor.params = [t.restElement(t.identifier("args"))];
|
|
2749
|
+
newConstructor.body.body.push(template.statement.ast`super(...args)`);
|
|
2750
|
+
}
|
|
2751
|
+
[constructor] = path.get("body").unshiftContainer("body", newConstructor);
|
|
2752
|
+
}
|
|
2753
|
+
if (renamer) renamer(referenceVisitor, { scope: constructor.scope });
|
|
2754
|
+
if (isDerived) {
|
|
2755
|
+
const bareSupers = [];
|
|
2756
|
+
constructor.traverse(findBareSupers, bareSupers);
|
|
2757
|
+
let isFirst = true;
|
|
2758
|
+
for (const bareSuper of bareSupers) {
|
|
2759
|
+
if (isFirst) isFirst = false;
|
|
2760
|
+
else nodes = nodes.map((n) => t.cloneNode(n));
|
|
2761
|
+
if (!bareSuper.parentPath.isExpressionStatement()) {
|
|
2762
|
+
const allNodes = [bareSuper.node, ...nodes.map((n) => t.toExpression(n))];
|
|
2763
|
+
if (!lastReturnsThis) allNodes.push(t.thisExpression());
|
|
2764
|
+
bareSuper.replaceWith(t.sequenceExpression(allNodes));
|
|
2765
|
+
} else bareSuper.insertAfter(nodes);
|
|
2766
|
+
}
|
|
2767
|
+
} else constructor.get("body").unshiftContainer("body", nodes);
|
|
2768
|
+
}
|
|
2769
|
+
|
|
2770
|
+
//#endregion
|
|
2771
|
+
//#region src/plugin-transform-typescript/global-types.ts
|
|
2772
|
+
const GLOBAL_TYPES = /* @__PURE__ */ new WeakMap();
|
|
2773
|
+
function isGlobalType({ scope }, name) {
|
|
2774
|
+
if (scope.hasBinding(name)) return false;
|
|
2775
|
+
if (GLOBAL_TYPES.get(scope).has(name)) return true;
|
|
2776
|
+
console.warn(`The exported identifier "${name}" is not declared in Babel's scope tracker\nas a JavaScript value binding, and "@babel/plugin-transform-typescript"\nnever encountered it as a TypeScript type declaration.\nIt will be treated as a JavaScript value.\n\nThis problem is likely caused by another plugin injecting\n"${name}" without registering it in the scope tracker. If you are the author\n of that plugin, please use "scope.registerDeclaration(declarationPath)".`);
|
|
2777
|
+
return false;
|
|
2778
|
+
}
|
|
2779
|
+
function registerGlobalType(programScope, name) {
|
|
2780
|
+
GLOBAL_TYPES.get(programScope).add(name);
|
|
2781
|
+
}
|
|
2782
|
+
|
|
2783
|
+
//#endregion
|
|
2784
|
+
//#region src/plugin-transform-typescript/namespace.ts
|
|
2785
|
+
function getFirstIdentifier(node) {
|
|
2786
|
+
if (t.isIdentifier(node)) return node;
|
|
2787
|
+
return getFirstIdentifier(node.left);
|
|
2788
|
+
}
|
|
2789
|
+
function transpileNamespace(path, allowNamespaces) {
|
|
2790
|
+
if (path.node.declare || path.node.id.type === "StringLiteral") {
|
|
2791
|
+
path.remove();
|
|
2792
|
+
return;
|
|
2793
|
+
}
|
|
2794
|
+
if (!allowNamespaces) throw path.get("id").buildCodeFrameError("Namespace not marked type-only declare. Non-declarative namespaces are only supported experimentally in Babel. To enable and review caveats see: https://babeljs.io/docs/en/babel-plugin-transform-typescript");
|
|
2795
|
+
const name = getFirstIdentifier(path.node.id).name;
|
|
2796
|
+
const value = handleNested(path, path.node);
|
|
2797
|
+
if (value === null) {
|
|
2798
|
+
registerGlobalType(path.findParent((p) => p.isProgram()).scope, name);
|
|
2799
|
+
path.remove();
|
|
2800
|
+
} else if (path.scope.hasOwnBinding(name)) path.replaceWith(value);
|
|
2801
|
+
else path.scope.registerDeclaration(path.replaceWithMultiple([getDeclaration(name), value])[0]);
|
|
2802
|
+
}
|
|
2803
|
+
function getDeclaration(name) {
|
|
2804
|
+
return t.variableDeclaration("let", [t.variableDeclarator(t.identifier(name))]);
|
|
2805
|
+
}
|
|
2806
|
+
function getMemberExpression(name, itemName) {
|
|
2807
|
+
return t.memberExpression(t.identifier(name), t.identifier(itemName));
|
|
2808
|
+
}
|
|
2809
|
+
/**
|
|
2810
|
+
* Convert export const foo = 1 to Namespace.foo = 1;
|
|
2811
|
+
*
|
|
2812
|
+
* @param {t.VariableDeclaration} node given variable declaration, e.g. `const foo = 1`
|
|
2813
|
+
* @param {string} name the generated unique namespace member name
|
|
2814
|
+
* @param {*} hub An instance implements HubInterface defined in `@babel/traverse` that can throw a code frame error
|
|
2815
|
+
*/
|
|
2816
|
+
function handleVariableDeclaration(node, name, hub) {
|
|
2817
|
+
if (node.kind !== "const") throw hub.file.buildCodeFrameError(node, "Namespaces exporting non-const are not supported by Babel. Change to const or see: https://babeljs.io/docs/en/babel-plugin-transform-typescript");
|
|
2818
|
+
const { declarations } = node;
|
|
2819
|
+
if (declarations.every((declarator) => t.isIdentifier(declarator.id))) {
|
|
2820
|
+
for (const declarator of declarations) declarator.init = t.assignmentExpression("=", getMemberExpression(name, declarator.id.name), declarator.init);
|
|
2821
|
+
return [node];
|
|
2822
|
+
}
|
|
2823
|
+
const bindingIdentifiers = t.getBindingIdentifiers(node);
|
|
2824
|
+
const assignments = [];
|
|
2825
|
+
for (const idName in bindingIdentifiers) assignments.push(t.assignmentExpression("=", getMemberExpression(name, idName), t.cloneNode(bindingIdentifiers[idName])));
|
|
2826
|
+
return [node, t.expressionStatement(t.sequenceExpression(assignments))];
|
|
2827
|
+
}
|
|
2828
|
+
function buildNestedAmbientModuleError(path, node) {
|
|
2829
|
+
return path.hub.buildError(node, "Ambient modules cannot be nested in other modules or namespaces.", Error);
|
|
2830
|
+
}
|
|
2831
|
+
function handleNested(path, node, parentExport) {
|
|
2832
|
+
const names = /* @__PURE__ */ new Set();
|
|
2833
|
+
const realName = node.id;
|
|
2834
|
+
const name = path.scope.generateUid(realName.name);
|
|
2835
|
+
const body = node.body;
|
|
2836
|
+
let namespaceTopLevel;
|
|
2837
|
+
namespaceTopLevel = t.isTSModuleBlock(body) ? body.body : [t.exportNamedDeclaration(body)];
|
|
2838
|
+
let isEmpty = true;
|
|
2839
|
+
for (let i = 0; i < namespaceTopLevel.length; i++) {
|
|
2840
|
+
const subNode = namespaceTopLevel[i];
|
|
2841
|
+
switch (subNode.type) {
|
|
2842
|
+
case "TSModuleDeclaration": {
|
|
2843
|
+
if (!t.isIdentifier(subNode.id)) throw buildNestedAmbientModuleError(path, subNode);
|
|
2844
|
+
const transformed = handleNested(path, subNode);
|
|
2845
|
+
if (transformed !== null) {
|
|
2846
|
+
isEmpty = false;
|
|
2847
|
+
const moduleName = subNode.id.name;
|
|
2848
|
+
if (names.has(moduleName)) namespaceTopLevel[i] = transformed;
|
|
2849
|
+
else {
|
|
2850
|
+
names.add(moduleName);
|
|
2851
|
+
namespaceTopLevel.splice(i++, 1, getDeclaration(moduleName), transformed);
|
|
2852
|
+
}
|
|
2853
|
+
}
|
|
2854
|
+
continue;
|
|
2855
|
+
}
|
|
2856
|
+
case "TSEnumDeclaration":
|
|
2857
|
+
case "FunctionDeclaration":
|
|
2858
|
+
case "ClassDeclaration":
|
|
2859
|
+
isEmpty = false;
|
|
2860
|
+
names.add(subNode.id.name);
|
|
2861
|
+
continue;
|
|
2862
|
+
case "VariableDeclaration":
|
|
2863
|
+
isEmpty = false;
|
|
2864
|
+
for (const name$1 in t.getBindingIdentifiers(subNode)) names.add(name$1);
|
|
2865
|
+
continue;
|
|
2866
|
+
default:
|
|
2867
|
+
isEmpty &&= t.isTypeScript(subNode);
|
|
2868
|
+
continue;
|
|
2869
|
+
case "ExportNamedDeclaration":
|
|
2870
|
+
}
|
|
2871
|
+
if ("declare" in subNode.declaration && subNode.declaration.declare) continue;
|
|
2872
|
+
switch (subNode.declaration.type) {
|
|
2873
|
+
case "TSEnumDeclaration": EXPORTED_CONST_ENUMS_IN_NAMESPACE.add(subNode.declaration);
|
|
2874
|
+
case "FunctionDeclaration":
|
|
2875
|
+
case "ClassDeclaration": {
|
|
2876
|
+
isEmpty = false;
|
|
2877
|
+
const itemName = subNode.declaration.id.name;
|
|
2878
|
+
names.add(itemName);
|
|
2879
|
+
namespaceTopLevel.splice(i++, 1, subNode.declaration, t.expressionStatement(t.assignmentExpression("=", getMemberExpression(name, itemName), t.identifier(itemName))));
|
|
2880
|
+
break;
|
|
2881
|
+
}
|
|
2882
|
+
case "VariableDeclaration": {
|
|
2883
|
+
isEmpty = false;
|
|
2884
|
+
const nodes = handleVariableDeclaration(subNode.declaration, name, path.hub);
|
|
2885
|
+
namespaceTopLevel.splice(i, nodes.length, ...nodes);
|
|
2886
|
+
i += nodes.length - 1;
|
|
2887
|
+
break;
|
|
2888
|
+
}
|
|
2889
|
+
case "TSModuleDeclaration": {
|
|
2890
|
+
if (!t.isIdentifier(subNode.declaration.id)) throw buildNestedAmbientModuleError(path, subNode.declaration);
|
|
2891
|
+
const transformed = handleNested(path, subNode.declaration, t.identifier(name));
|
|
2892
|
+
if (transformed !== null) {
|
|
2893
|
+
isEmpty = false;
|
|
2894
|
+
const moduleName = subNode.declaration.id.name;
|
|
2895
|
+
if (names.has(moduleName)) namespaceTopLevel[i] = transformed;
|
|
2896
|
+
else {
|
|
2897
|
+
names.add(moduleName);
|
|
2898
|
+
namespaceTopLevel.splice(i++, 1, getDeclaration(moduleName), transformed);
|
|
2899
|
+
}
|
|
2900
|
+
} else {
|
|
2901
|
+
namespaceTopLevel.splice(i, 1);
|
|
2902
|
+
i--;
|
|
2903
|
+
}
|
|
2904
|
+
}
|
|
2905
|
+
}
|
|
2906
|
+
}
|
|
2907
|
+
if (isEmpty) return null;
|
|
2908
|
+
let fallthroughValue = t.objectExpression([]);
|
|
2909
|
+
if (parentExport) {
|
|
2910
|
+
const memberExpr = t.memberExpression(parentExport, realName);
|
|
2911
|
+
fallthroughValue = template.expression.ast`
|
|
2912
|
+
${t.cloneNode(memberExpr)} ||
|
|
2913
|
+
(${t.cloneNode(memberExpr)} = ${fallthroughValue})
|
|
2914
|
+
`;
|
|
2915
|
+
}
|
|
2916
|
+
return template.statement.ast`
|
|
2917
|
+
(function (${t.identifier(name)}) {
|
|
2918
|
+
${namespaceTopLevel}
|
|
2919
|
+
})(${realName} || (${t.cloneNode(realName)} = ${fallthroughValue}));
|
|
2920
|
+
`;
|
|
2921
|
+
}
|
|
2922
|
+
|
|
2923
|
+
//#endregion
|
|
2924
|
+
//#region src/plugin-transform-typescript/plugin-syntax-typescript.ts
|
|
2925
|
+
var removePlugin = function(plugins, name) {
|
|
2926
|
+
const indices = [];
|
|
2927
|
+
plugins.forEach((plugin, i) => {
|
|
2928
|
+
if ((Array.isArray(plugin) ? plugin[0] : plugin) === name) indices.unshift(i);
|
|
2929
|
+
});
|
|
2930
|
+
for (const i of indices) plugins.splice(i, 1);
|
|
2931
|
+
};
|
|
2932
|
+
const syntaxTypeScript = declare((api, opts) => {
|
|
2933
|
+
const { disallowAmbiguousJSXLike, dts } = opts;
|
|
2934
|
+
var { isTSX } = opts;
|
|
2935
|
+
return {
|
|
2936
|
+
name: "syntax-typescript",
|
|
2937
|
+
manipulateOptions(opts$1, parserOpts) {
|
|
2938
|
+
{
|
|
2939
|
+
const { plugins } = parserOpts;
|
|
2940
|
+
removePlugin(plugins, "flow");
|
|
2941
|
+
removePlugin(plugins, "jsx");
|
|
2942
|
+
plugins.push("objectRestSpread", "classProperties");
|
|
2943
|
+
if (isTSX) plugins.push("jsx");
|
|
2944
|
+
}
|
|
2945
|
+
parserOpts.plugins.push(["typescript", {
|
|
2946
|
+
disallowAmbiguousJSXLike,
|
|
2947
|
+
dts
|
|
2948
|
+
}]);
|
|
2949
|
+
}
|
|
2950
|
+
};
|
|
2951
|
+
});
|
|
2952
|
+
var plugin_syntax_typescript_default = syntaxTypeScript;
|
|
2953
|
+
|
|
2954
|
+
//#endregion
|
|
2955
|
+
//#region src/plugin-transform-typescript/index.ts
|
|
2956
|
+
function isInType(path) {
|
|
2957
|
+
switch (path.parent.type) {
|
|
2958
|
+
case "TSTypeReference":
|
|
2959
|
+
case "TSExpressionWithTypeArguments":
|
|
2960
|
+
case "TSExpressionWithTypeArguments":
|
|
2961
|
+
case "TSTypeQuery": return true;
|
|
2962
|
+
case "TSQualifiedName": return path.parentPath.findParent((path$1) => path$1.type !== "TSQualifiedName").type !== "TSImportEqualsDeclaration";
|
|
2963
|
+
case "ExportSpecifier": return path.parent.exportKind === "type" || path.parentPath.parent.exportKind === "type";
|
|
2964
|
+
default: return false;
|
|
2965
|
+
}
|
|
2966
|
+
}
|
|
2967
|
+
const NEEDS_EXPLICIT_ESM = /* @__PURE__ */ new WeakMap();
|
|
2968
|
+
const PARSED_PARAMS = /* @__PURE__ */ new WeakSet();
|
|
2969
|
+
function safeRemove(path) {
|
|
2970
|
+
const ids = path.getBindingIdentifiers();
|
|
2971
|
+
for (const name of Object.keys(ids)) {
|
|
2972
|
+
const binding = path.scope.getBinding(name);
|
|
2973
|
+
if (binding && binding.identifier === ids[name]) binding.scope.removeBinding(name);
|
|
2974
|
+
}
|
|
2975
|
+
path.opts.noScope = true;
|
|
2976
|
+
path.remove();
|
|
2977
|
+
path.opts.noScope = false;
|
|
2978
|
+
}
|
|
2979
|
+
function assertCjsTransformEnabled(path, pass, wrong, suggestion, extra = "") {
|
|
2980
|
+
if (pass.file.get("@babel/plugin-transform-modules-*") !== "commonjs") throw path.buildCodeFrameError(`\`${wrong}\` is only supported when compiling modules to CommonJS.\nPlease consider using \`${suggestion}\`${extra}, or add @babel/plugin-transform-modules-commonjs to your Babel config.`);
|
|
2981
|
+
}
|
|
2982
|
+
const pluginTransformTypescript = declare((api, opts) => {
|
|
2983
|
+
const { types: t$1, template: template$1 } = api;
|
|
2984
|
+
const JSX_PRAGMA_REGEX = /\*?\s*@jsx((?:Frag)?)\s+(\S+)/;
|
|
2985
|
+
const { allowNamespaces = true, jsxPragma = "React.createElement", jsxPragmaFrag = "React.Fragment", onlyRemoveTypeImports = false, optimizeConstEnums = false } = opts;
|
|
2986
|
+
var { allowDeclareFields = false } = opts;
|
|
2987
|
+
const classMemberVisitors = {
|
|
2988
|
+
field(path) {
|
|
2989
|
+
const { node } = path;
|
|
2990
|
+
if (!allowDeclareFields && node.declare) throw path.buildCodeFrameError("The 'declare' modifier is only allowed when the 'allowDeclareFields' option of @babel/plugin-transform-typescript or @babel/preset-typescript is enabled.");
|
|
2991
|
+
if (node.declare) {
|
|
2992
|
+
if (node.value) throw path.buildCodeFrameError(`Fields with the 'declare' modifier cannot be initialized here, but only in the constructor`);
|
|
2993
|
+
if (!node.decorators) path.remove();
|
|
2994
|
+
} else if (node.definite) {
|
|
2995
|
+
if (node.value) throw path.buildCodeFrameError(`Definitely assigned fields cannot be initialized here, but only in the constructor`);
|
|
2996
|
+
if (!allowDeclareFields && !node.decorators && !t$1.isClassPrivateProperty(node)) path.remove();
|
|
2997
|
+
} else if (node.abstract) path.remove();
|
|
2998
|
+
else if (!allowDeclareFields && !node.value && !node.decorators && !t$1.isClassPrivateProperty(node)) path.remove();
|
|
2999
|
+
if (node.accessibility) node.accessibility = null;
|
|
3000
|
+
if (node.abstract) node.abstract = null;
|
|
3001
|
+
if (node.readonly) node.readonly = null;
|
|
3002
|
+
if (node.optional) node.optional = null;
|
|
3003
|
+
if (node.typeAnnotation) node.typeAnnotation = null;
|
|
3004
|
+
if (node.definite) node.definite = null;
|
|
3005
|
+
if (node.declare) node.declare = null;
|
|
3006
|
+
if (node.override) node.override = null;
|
|
3007
|
+
},
|
|
3008
|
+
method({ node }) {
|
|
3009
|
+
if (node.accessibility) node.accessibility = null;
|
|
3010
|
+
if (node.abstract) node.abstract = null;
|
|
3011
|
+
if (node.optional) node.optional = null;
|
|
3012
|
+
if (node.override) node.override = null;
|
|
3013
|
+
},
|
|
3014
|
+
constructor(path, classPath) {
|
|
3015
|
+
if (path.node.accessibility) path.node.accessibility = null;
|
|
3016
|
+
const assigns = [];
|
|
3017
|
+
const { scope } = path;
|
|
3018
|
+
for (const paramPath of path.get("params")) {
|
|
3019
|
+
const param = paramPath.node;
|
|
3020
|
+
if (param.type === "TSParameterProperty") {
|
|
3021
|
+
const parameter = param.parameter;
|
|
3022
|
+
if (PARSED_PARAMS.has(parameter)) continue;
|
|
3023
|
+
PARSED_PARAMS.add(parameter);
|
|
3024
|
+
let id;
|
|
3025
|
+
if (t$1.isIdentifier(parameter)) id = parameter;
|
|
3026
|
+
else if (t$1.isAssignmentPattern(parameter) && t$1.isIdentifier(parameter.left)) id = parameter.left;
|
|
3027
|
+
else throw paramPath.buildCodeFrameError("Parameter properties can not be destructuring patterns.");
|
|
3028
|
+
assigns.push(template$1.statement.ast`
|
|
3029
|
+
this.${t$1.cloneNode(id)} = ${t$1.cloneNode(id)}
|
|
3030
|
+
`);
|
|
3031
|
+
paramPath.replaceWith(paramPath.get("parameter"));
|
|
3032
|
+
scope.registerBinding("param", paramPath);
|
|
3033
|
+
}
|
|
3034
|
+
}
|
|
3035
|
+
injectInitialization(classPath, path, assigns);
|
|
3036
|
+
}
|
|
3037
|
+
};
|
|
3038
|
+
return {
|
|
3039
|
+
name: "transform-typescript",
|
|
3040
|
+
inherits: plugin_syntax_typescript_default,
|
|
3041
|
+
visitor: {
|
|
3042
|
+
Pattern: visitPattern,
|
|
3043
|
+
Identifier: visitPattern,
|
|
3044
|
+
RestElement: visitPattern,
|
|
3045
|
+
Program: {
|
|
3046
|
+
enter(path, state) {
|
|
3047
|
+
const { file } = state;
|
|
3048
|
+
let fileJsxPragma = null;
|
|
3049
|
+
let fileJsxPragmaFrag = null;
|
|
3050
|
+
const programScope = path.scope;
|
|
3051
|
+
if (!GLOBAL_TYPES.has(programScope)) GLOBAL_TYPES.set(programScope, /* @__PURE__ */ new Set());
|
|
3052
|
+
if (file.ast.comments) for (const comment of file.ast.comments) {
|
|
3053
|
+
const jsxMatches = JSX_PRAGMA_REGEX.exec(comment.value);
|
|
3054
|
+
if (jsxMatches) if (jsxMatches[1]) fileJsxPragmaFrag = jsxMatches[2];
|
|
3055
|
+
else fileJsxPragma = jsxMatches[2];
|
|
3056
|
+
}
|
|
3057
|
+
let pragmaImportName = fileJsxPragma || jsxPragma;
|
|
3058
|
+
if (pragmaImportName) [pragmaImportName] = pragmaImportName.split(".");
|
|
3059
|
+
let pragmaFragImportName = fileJsxPragmaFrag || jsxPragmaFrag;
|
|
3060
|
+
if (pragmaFragImportName) [pragmaFragImportName] = pragmaFragImportName.split(".");
|
|
3061
|
+
for (let stmt of path.get("body")) {
|
|
3062
|
+
if (stmt.isImportDeclaration()) {
|
|
3063
|
+
if (!NEEDS_EXPLICIT_ESM.has(state.file.ast.program)) NEEDS_EXPLICIT_ESM.set(state.file.ast.program, true);
|
|
3064
|
+
if (stmt.node.importKind === "type") {
|
|
3065
|
+
for (const specifier of stmt.node.specifiers) registerGlobalType(programScope, specifier.local.name);
|
|
3066
|
+
stmt.remove();
|
|
3067
|
+
continue;
|
|
3068
|
+
}
|
|
3069
|
+
const importsToRemove = /* @__PURE__ */ new Set();
|
|
3070
|
+
const specifiersLength = stmt.node.specifiers.length;
|
|
3071
|
+
const isAllSpecifiersElided = () => specifiersLength > 0 && specifiersLength === importsToRemove.size;
|
|
3072
|
+
for (const specifier of stmt.node.specifiers) if (specifier.type === "ImportSpecifier" && specifier.importKind === "type") {
|
|
3073
|
+
registerGlobalType(programScope, specifier.local.name);
|
|
3074
|
+
const binding = stmt.scope.getBinding(specifier.local.name);
|
|
3075
|
+
if (binding) importsToRemove.add(binding.path);
|
|
3076
|
+
}
|
|
3077
|
+
if (onlyRemoveTypeImports) NEEDS_EXPLICIT_ESM.set(path.node, false);
|
|
3078
|
+
else {
|
|
3079
|
+
if (stmt.node.specifiers.length === 0) {
|
|
3080
|
+
NEEDS_EXPLICIT_ESM.set(path.node, false);
|
|
3081
|
+
continue;
|
|
3082
|
+
}
|
|
3083
|
+
for (const specifier of stmt.node.specifiers) {
|
|
3084
|
+
const binding = stmt.scope.getBinding(specifier.local.name);
|
|
3085
|
+
if (binding && !importsToRemove.has(binding.path)) if (isImportTypeOnly({
|
|
3086
|
+
binding,
|
|
3087
|
+
programPath: path,
|
|
3088
|
+
pragmaImportName,
|
|
3089
|
+
pragmaFragImportName
|
|
3090
|
+
})) importsToRemove.add(binding.path);
|
|
3091
|
+
else NEEDS_EXPLICIT_ESM.set(path.node, false);
|
|
3092
|
+
}
|
|
3093
|
+
}
|
|
3094
|
+
if (isAllSpecifiersElided() && !onlyRemoveTypeImports) stmt.remove();
|
|
3095
|
+
else for (const importPath of importsToRemove) importPath.remove();
|
|
3096
|
+
continue;
|
|
3097
|
+
}
|
|
3098
|
+
if (!onlyRemoveTypeImports && stmt.isTSImportEqualsDeclaration()) {
|
|
3099
|
+
const { id } = stmt.node;
|
|
3100
|
+
const binding = stmt.scope.getBinding(id.name);
|
|
3101
|
+
if (binding && !stmt.node.isExport && isImportTypeOnly({
|
|
3102
|
+
binding,
|
|
3103
|
+
programPath: path,
|
|
3104
|
+
pragmaImportName,
|
|
3105
|
+
pragmaFragImportName
|
|
3106
|
+
})) {
|
|
3107
|
+
stmt.remove();
|
|
3108
|
+
continue;
|
|
3109
|
+
}
|
|
3110
|
+
}
|
|
3111
|
+
if (stmt.isExportDeclaration()) stmt = stmt.get("declaration");
|
|
3112
|
+
if (stmt.isVariableDeclaration({ declare: true })) for (const name of Object.keys(stmt.getBindingIdentifiers())) registerGlobalType(programScope, name);
|
|
3113
|
+
else if (stmt.isTSTypeAliasDeclaration() || stmt.isTSDeclareFunction() && stmt.get("id").isIdentifier() || stmt.isTSInterfaceDeclaration() || stmt.isClassDeclaration({ declare: true }) || stmt.isTSEnumDeclaration({ declare: true }) || stmt.isTSModuleDeclaration({ declare: true }) && stmt.get("id").isIdentifier()) registerGlobalType(programScope, stmt.node.id.name);
|
|
3114
|
+
}
|
|
3115
|
+
},
|
|
3116
|
+
exit(path) {
|
|
3117
|
+
if (path.node.sourceType === "module" && NEEDS_EXPLICIT_ESM.get(path.node)) path.pushContainer("body", t$1.exportNamedDeclaration());
|
|
3118
|
+
}
|
|
3119
|
+
},
|
|
3120
|
+
ExportNamedDeclaration(path, state) {
|
|
3121
|
+
if (!NEEDS_EXPLICIT_ESM.has(state.file.ast.program)) NEEDS_EXPLICIT_ESM.set(state.file.ast.program, true);
|
|
3122
|
+
if (path.node.exportKind === "type") {
|
|
3123
|
+
path.remove();
|
|
3124
|
+
return;
|
|
3125
|
+
}
|
|
3126
|
+
if (path.node.source && path.node.specifiers.length > 0 && path.node.specifiers.every((specifier) => specifier.type === "ExportSpecifier" && specifier.exportKind === "type")) {
|
|
3127
|
+
path.remove();
|
|
3128
|
+
return;
|
|
3129
|
+
}
|
|
3130
|
+
if (!path.node.source && path.node.specifiers.length > 0 && path.node.specifiers.every((specifier) => t$1.isExportSpecifier(specifier) && isGlobalType(path, specifier.local.name))) {
|
|
3131
|
+
path.remove();
|
|
3132
|
+
return;
|
|
3133
|
+
}
|
|
3134
|
+
if (t$1.isTSModuleDeclaration(path.node.declaration)) {
|
|
3135
|
+
const namespace = path.node.declaration;
|
|
3136
|
+
if (!t$1.isStringLiteral(namespace.id)) {
|
|
3137
|
+
const id = getFirstIdentifier(namespace.id);
|
|
3138
|
+
if (path.scope.hasOwnBinding(id.name)) path.replaceWith(namespace);
|
|
3139
|
+
else {
|
|
3140
|
+
const [newExport] = path.replaceWithMultiple([t$1.exportNamedDeclaration(t$1.variableDeclaration("let", [t$1.variableDeclarator(t$1.cloneNode(id))])), namespace]);
|
|
3141
|
+
path.scope.registerDeclaration(newExport);
|
|
3142
|
+
}
|
|
3143
|
+
}
|
|
3144
|
+
}
|
|
3145
|
+
NEEDS_EXPLICIT_ESM.set(state.file.ast.program, false);
|
|
3146
|
+
},
|
|
3147
|
+
ExportAllDeclaration(path) {
|
|
3148
|
+
if (path.node.exportKind === "type") path.remove();
|
|
3149
|
+
},
|
|
3150
|
+
ExportSpecifier(path) {
|
|
3151
|
+
if (!path.parent.source && isGlobalType(path, path.node.local.name) || path.node.exportKind === "type") path.remove();
|
|
3152
|
+
},
|
|
3153
|
+
ExportDefaultDeclaration(path, state) {
|
|
3154
|
+
if (!NEEDS_EXPLICIT_ESM.has(state.file.ast.program)) NEEDS_EXPLICIT_ESM.set(state.file.ast.program, true);
|
|
3155
|
+
if (t$1.isIdentifier(path.node.declaration) && isGlobalType(path, path.node.declaration.name)) {
|
|
3156
|
+
path.remove();
|
|
3157
|
+
return;
|
|
3158
|
+
}
|
|
3159
|
+
NEEDS_EXPLICIT_ESM.set(state.file.ast.program, false);
|
|
3160
|
+
},
|
|
3161
|
+
TSDeclareFunction(path) {
|
|
3162
|
+
safeRemove(path);
|
|
3163
|
+
},
|
|
3164
|
+
TSDeclareMethod(path) {
|
|
3165
|
+
safeRemove(path);
|
|
3166
|
+
},
|
|
3167
|
+
VariableDeclaration(path) {
|
|
3168
|
+
if (path.node.declare) safeRemove(path);
|
|
3169
|
+
},
|
|
3170
|
+
VariableDeclarator({ node }) {
|
|
3171
|
+
if (node.definite) node.definite = null;
|
|
3172
|
+
},
|
|
3173
|
+
TSIndexSignature(path) {
|
|
3174
|
+
path.remove();
|
|
3175
|
+
},
|
|
3176
|
+
ClassDeclaration(path) {
|
|
3177
|
+
const { node } = path;
|
|
3178
|
+
if (node.declare) safeRemove(path);
|
|
3179
|
+
},
|
|
3180
|
+
Class(path) {
|
|
3181
|
+
const { node } = path;
|
|
3182
|
+
if (node.typeParameters) node.typeParameters = null;
|
|
3183
|
+
if (node.superTypeParameters) node.superTypeParameters = null;
|
|
3184
|
+
if (node.implements) node.implements = null;
|
|
3185
|
+
if (node.abstract) node.abstract = null;
|
|
3186
|
+
path.get("body.body").forEach((child) => {
|
|
3187
|
+
if (child.isClassMethod() || child.isClassPrivateMethod()) if (child.node.kind === "constructor") classMemberVisitors.constructor(child, path);
|
|
3188
|
+
else classMemberVisitors.method(child);
|
|
3189
|
+
else if (child.isClassProperty() || child.isClassPrivateProperty() || child.isClassAccessorProperty()) classMemberVisitors.field(child);
|
|
3190
|
+
});
|
|
3191
|
+
},
|
|
3192
|
+
Function(path) {
|
|
3193
|
+
const { node } = path;
|
|
3194
|
+
if (node.typeParameters) node.typeParameters = null;
|
|
3195
|
+
if (node.returnType) node.returnType = null;
|
|
3196
|
+
const params = node.params;
|
|
3197
|
+
if (params.length > 0 && t$1.isIdentifier(params[0], { name: "this" })) params.shift();
|
|
3198
|
+
},
|
|
3199
|
+
TSModuleDeclaration(path) {
|
|
3200
|
+
transpileNamespace(path, allowNamespaces);
|
|
3201
|
+
},
|
|
3202
|
+
TSInterfaceDeclaration(path) {
|
|
3203
|
+
path.remove();
|
|
3204
|
+
},
|
|
3205
|
+
TSTypeAliasDeclaration(path) {
|
|
3206
|
+
path.remove();
|
|
3207
|
+
},
|
|
3208
|
+
TSEnumDeclaration(path) {
|
|
3209
|
+
if (optimizeConstEnums && path.node.const) transpileConstEnum(path, t$1);
|
|
3210
|
+
else transpileEnum(path, t$1);
|
|
3211
|
+
},
|
|
3212
|
+
TSImportEqualsDeclaration(path, pass) {
|
|
3213
|
+
const { id, moduleReference } = path.node;
|
|
3214
|
+
let init;
|
|
3215
|
+
let varKind;
|
|
3216
|
+
if (t$1.isTSExternalModuleReference(moduleReference)) {
|
|
3217
|
+
assertCjsTransformEnabled(path, pass, `import ${id.name} = require(...);`, `import ${id.name} from '...';`, " alongside Typescript's --allowSyntheticDefaultImports option");
|
|
3218
|
+
init = t$1.callExpression(t$1.identifier("require"), [moduleReference.expression]);
|
|
3219
|
+
varKind = "const";
|
|
3220
|
+
} else {
|
|
3221
|
+
init = entityNameToExpr(moduleReference);
|
|
3222
|
+
varKind = "var";
|
|
3223
|
+
}
|
|
3224
|
+
const newNode = t$1.variableDeclaration(varKind, [t$1.variableDeclarator(id, init)]);
|
|
3225
|
+
path.replaceWith(path.node.isExport ? t$1.exportNamedDeclaration(newNode) : newNode);
|
|
3226
|
+
path.scope.registerDeclaration(path);
|
|
3227
|
+
},
|
|
3228
|
+
TSExportAssignment(path, pass) {
|
|
3229
|
+
assertCjsTransformEnabled(path, pass, `export = <value>;`, `export default <value>;`);
|
|
3230
|
+
path.replaceWith(template$1.statement.ast`module.exports = ${path.node.expression}`);
|
|
3231
|
+
},
|
|
3232
|
+
TSTypeAssertion(path) {
|
|
3233
|
+
path.replaceWith(path.node.expression);
|
|
3234
|
+
},
|
|
3235
|
+
[`TSAsExpression${t$1.tsSatisfiesExpression ? "|TSSatisfiesExpression" : ""}`](path) {
|
|
3236
|
+
let { node } = path;
|
|
3237
|
+
do
|
|
3238
|
+
node = node.expression;
|
|
3239
|
+
while (t$1.isTSAsExpression(node) || t$1.isTSSatisfiesExpression?.(node));
|
|
3240
|
+
path.replaceWith(node);
|
|
3241
|
+
},
|
|
3242
|
+
[api.types.tsInstantiationExpression ? "TSNonNullExpression|TSInstantiationExpression" : "TSNonNullExpression"](path) {
|
|
3243
|
+
path.replaceWith(path.node.expression);
|
|
3244
|
+
},
|
|
3245
|
+
CallExpression(path) {
|
|
3246
|
+
path.node.typeParameters = null;
|
|
3247
|
+
},
|
|
3248
|
+
OptionalCallExpression(path) {
|
|
3249
|
+
path.node.typeParameters = null;
|
|
3250
|
+
},
|
|
3251
|
+
NewExpression(path) {
|
|
3252
|
+
path.node.typeParameters = null;
|
|
3253
|
+
},
|
|
3254
|
+
JSXOpeningElement(path) {
|
|
3255
|
+
path.node.typeParameters = null;
|
|
3256
|
+
},
|
|
3257
|
+
TaggedTemplateExpression(path) {
|
|
3258
|
+
path.node.typeParameters = null;
|
|
3259
|
+
}
|
|
3260
|
+
}
|
|
3261
|
+
};
|
|
3262
|
+
function entityNameToExpr(node) {
|
|
3263
|
+
if (t$1.isTSQualifiedName(node)) return t$1.memberExpression(entityNameToExpr(node.left), node.right);
|
|
3264
|
+
return node;
|
|
3265
|
+
}
|
|
3266
|
+
function visitPattern({ node }) {
|
|
3267
|
+
if (node.typeAnnotation) node.typeAnnotation = null;
|
|
3268
|
+
if (t$1.isIdentifier(node) && node.optional) node.optional = null;
|
|
3269
|
+
}
|
|
3270
|
+
function isImportTypeOnly({ binding, programPath, pragmaImportName, pragmaFragImportName }) {
|
|
3271
|
+
for (const path of binding.referencePaths) if (!isInType(path)) return false;
|
|
3272
|
+
if (binding.identifier.name !== pragmaImportName && binding.identifier.name !== pragmaFragImportName) return true;
|
|
3273
|
+
let sourceFileHasJsx = false;
|
|
3274
|
+
programPath.traverse({ "JSXElement|JSXFragment"(path) {
|
|
3275
|
+
sourceFileHasJsx = true;
|
|
3276
|
+
path.stop();
|
|
3277
|
+
} });
|
|
3278
|
+
return !sourceFileHasJsx;
|
|
3279
|
+
}
|
|
3280
|
+
});
|
|
3281
|
+
var plugin_transform_typescript_default = pluginTransformTypescript;
|
|
3282
|
+
|
|
3283
|
+
//#endregion
|
|
3284
|
+
//#region src/preset-typescript/normalize-options.ts
|
|
3285
|
+
const v = new OptionValidator("preset-typescript");
|
|
3286
|
+
function normalizeOptions(options = {}) {
|
|
3287
|
+
const { allowNamespaces = true, jsxPragma, onlyRemoveTypeImports } = options;
|
|
3288
|
+
const TopLevelOptions = {
|
|
3289
|
+
ignoreExtensions: "ignoreExtensions",
|
|
3290
|
+
allowNamespaces: "allowNamespaces",
|
|
3291
|
+
disallowAmbiguousJSXLike: "disallowAmbiguousJSXLike",
|
|
3292
|
+
jsxPragma: "jsxPragma",
|
|
3293
|
+
jsxPragmaFrag: "jsxPragmaFrag",
|
|
3294
|
+
onlyRemoveTypeImports: "onlyRemoveTypeImports",
|
|
3295
|
+
optimizeConstEnums: "optimizeConstEnums",
|
|
3296
|
+
rewriteImportExtensions: "rewriteImportExtensions",
|
|
3297
|
+
allExtensions: "allExtensions",
|
|
3298
|
+
isTSX: "isTSX"
|
|
3299
|
+
};
|
|
3300
|
+
const jsxPragmaFrag = v.validateStringOption(TopLevelOptions.jsxPragmaFrag, options.jsxPragmaFrag, "React.Fragment");
|
|
3301
|
+
const allExtensions = v.validateBooleanOption(TopLevelOptions.allExtensions, options.allExtensions, false);
|
|
3302
|
+
const isTSX = v.validateBooleanOption(TopLevelOptions.isTSX, options.isTSX, false);
|
|
3303
|
+
if (isTSX) v.invariant(allExtensions, "isTSX:true requires allExtensions:true");
|
|
3304
|
+
const ignoreExtensions = v.validateBooleanOption(TopLevelOptions.ignoreExtensions, options.ignoreExtensions, false);
|
|
3305
|
+
const disallowAmbiguousJSXLike = v.validateBooleanOption(TopLevelOptions.disallowAmbiguousJSXLike, options.disallowAmbiguousJSXLike, false);
|
|
3306
|
+
if (disallowAmbiguousJSXLike) v.invariant(allExtensions, "disallowAmbiguousJSXLike:true requires allExtensions:true");
|
|
3307
|
+
return {
|
|
3308
|
+
ignoreExtensions,
|
|
3309
|
+
allowNamespaces,
|
|
3310
|
+
disallowAmbiguousJSXLike,
|
|
3311
|
+
jsxPragma,
|
|
3312
|
+
jsxPragmaFrag,
|
|
3313
|
+
onlyRemoveTypeImports,
|
|
3314
|
+
optimizeConstEnums: v.validateBooleanOption(TopLevelOptions.optimizeConstEnums, options.optimizeConstEnums, false),
|
|
3315
|
+
rewriteImportExtensions: v.validateBooleanOption(TopLevelOptions.rewriteImportExtensions, options.rewriteImportExtensions, false),
|
|
3316
|
+
allExtensions,
|
|
3317
|
+
isTSX
|
|
3318
|
+
};
|
|
3319
|
+
}
|
|
3320
|
+
|
|
3321
|
+
//#endregion
|
|
3322
|
+
//#region src/preset-typescript/plugin-rewrite-ts-imports.ts
|
|
3323
|
+
const pluginRewriteTSImports = declare(({ types: t$1, template: template$1 }) => {
|
|
3324
|
+
function maybeReplace(source, path, state) {
|
|
3325
|
+
if (!source) return;
|
|
3326
|
+
if (t$1.isStringLiteral(source)) {
|
|
3327
|
+
if (/^\.\.?\//.test(source.value)) source.value = source.value.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+)?)\.([cm]?)ts$/i, (m, tsx, d, ext, cm) => tsx ? ".js" : d && (!ext || !cm) ? m : d + ext + "." + cm.toLowerCase() + "js");
|
|
3328
|
+
return;
|
|
3329
|
+
}
|
|
3330
|
+
if (state.availableHelper("tsRewriteRelativeImportExtensions")) path.replaceWith(t$1.callExpression(state.addHelper("tsRewriteRelativeImportExtensions"), [source]));
|
|
3331
|
+
else path.replaceWith(template$1.expression.ast`(${source} + "").replace(/([\\/].*\.[mc]?)tsx?$/, "$1js")`);
|
|
3332
|
+
}
|
|
3333
|
+
return {
|
|
3334
|
+
name: "preset-typescript/plugin-rewrite-ts-imports",
|
|
3335
|
+
visitor: {
|
|
3336
|
+
"ImportDeclaration|ExportAllDeclaration|ExportNamedDeclaration"(path, state) {
|
|
3337
|
+
const node = path.node;
|
|
3338
|
+
if ((t$1.isImportDeclaration(node) ? node.importKind : node.exportKind) === "value") maybeReplace(node.source, path.get("source"), state);
|
|
3339
|
+
},
|
|
3340
|
+
CallExpression(path, state) {
|
|
3341
|
+
if (t$1.isImport(path.node.callee)) maybeReplace(path.node.arguments[0], path.get("arguments.0"), state);
|
|
3342
|
+
},
|
|
3343
|
+
ImportExpression(path, state) {
|
|
3344
|
+
maybeReplace(path.node.source, path.get("source"), state);
|
|
3345
|
+
}
|
|
3346
|
+
}
|
|
3347
|
+
};
|
|
3348
|
+
});
|
|
3349
|
+
var plugin_rewrite_ts_imports_default = pluginRewriteTSImports;
|
|
3350
|
+
|
|
3351
|
+
//#endregion
|
|
3352
|
+
//#region src/preset-typescript/index.ts
|
|
3353
|
+
const presetTypescript = declarePreset((api, opts) => {
|
|
3354
|
+
const { allExtensions, ignoreExtensions, allowNamespaces, disallowAmbiguousJSXLike, isTSX, jsxPragma, jsxPragmaFrag, onlyRemoveTypeImports, optimizeConstEnums, rewriteImportExtensions } = normalizeOptions(opts);
|
|
3355
|
+
const pluginOptions = (disallowAmbiguousJSXLike$1) => ({
|
|
3356
|
+
allowDeclareFields: opts.allowDeclareFields,
|
|
3357
|
+
allowNamespaces,
|
|
3358
|
+
disallowAmbiguousJSXLike: disallowAmbiguousJSXLike$1,
|
|
3359
|
+
jsxPragma,
|
|
3360
|
+
jsxPragmaFrag,
|
|
3361
|
+
onlyRemoveTypeImports,
|
|
3362
|
+
optimizeConstEnums
|
|
3363
|
+
});
|
|
3364
|
+
const getPlugins = (isTSX$1, disallowAmbiguousJSXLike$1) => {
|
|
3365
|
+
return [[plugin_transform_typescript_default, {
|
|
3366
|
+
isTSX: isTSX$1,
|
|
3367
|
+
...pluginOptions(disallowAmbiguousJSXLike$1)
|
|
3368
|
+
}]];
|
|
3369
|
+
};
|
|
3370
|
+
return {
|
|
3371
|
+
plugins: rewriteImportExtensions ? [plugin_rewrite_ts_imports_default] : [],
|
|
3372
|
+
overrides: allExtensions || ignoreExtensions ? [{ plugins: getPlugins(isTSX, disallowAmbiguousJSXLike) }] : [
|
|
3373
|
+
{
|
|
3374
|
+
test: /\.ts$/,
|
|
3375
|
+
plugins: getPlugins(false, false)
|
|
3376
|
+
},
|
|
3377
|
+
{
|
|
3378
|
+
test: /\.mts$/,
|
|
3379
|
+
sourceType: "module",
|
|
3380
|
+
plugins: getPlugins(false, true)
|
|
3381
|
+
},
|
|
3382
|
+
{
|
|
3383
|
+
test: /\.cts$/,
|
|
3384
|
+
sourceType: "unambiguous",
|
|
3385
|
+
plugins: [[plugin_transform_typescript_default, pluginOptions(true)]]
|
|
3386
|
+
},
|
|
3387
|
+
{
|
|
3388
|
+
test: /\.tsx$/,
|
|
3389
|
+
plugins: getPlugins(true, false)
|
|
3390
|
+
}
|
|
3391
|
+
]
|
|
3392
|
+
};
|
|
3393
|
+
});
|
|
3394
|
+
var preset_typescript_default = presetTypescript;
|
|
3395
|
+
|
|
3396
|
+
//#endregion
|
|
3397
|
+
//#region src/index.ts
|
|
3398
|
+
function solidPreset(_context, options = {}) {
|
|
3399
|
+
return { plugins: [[babel_plugin_jsx_dom_expressions_default, Object.assign({
|
|
3400
|
+
moduleName: "solid-js/web",
|
|
3401
|
+
builtIns: [
|
|
3402
|
+
"For",
|
|
3403
|
+
"Show",
|
|
3404
|
+
"Switch",
|
|
3405
|
+
"Match",
|
|
3406
|
+
"Suspense",
|
|
3407
|
+
"SuspenseList",
|
|
3408
|
+
"Portal",
|
|
3409
|
+
"Index",
|
|
3410
|
+
"Dynamic",
|
|
3411
|
+
"ErrorBoundary"
|
|
3412
|
+
],
|
|
3413
|
+
contextToCustomElements: true,
|
|
3414
|
+
wrapConditionals: true,
|
|
3415
|
+
generate: "dom"
|
|
3416
|
+
}, options)]] };
|
|
3417
|
+
}
|
|
3418
|
+
const rolldownPluginSolid = (options) => {
|
|
3419
|
+
if (options?.solid?.generate === "universal") {
|
|
3420
|
+
if (!options?.solid?.moduleName) throw new Error("Universal mode requires a 'moduleName' option pointing to your custom renderer.\n\nPlease provide a moduleName that exports the required universal renderer functions:\n- createElement\n- createTextNode\n- insertNode\n- setProp\n- insert\n- spread\n- mergeProps\n- effect\n- memo\n- use\n\nExample configuration:\n{ solid: { generate: \"universal\", moduleName: \"my-custom-renderer\" } }\n\nYour custom renderer should be created using 'createRenderer' from 'solid-js/universal'.");
|
|
3421
|
+
}
|
|
3422
|
+
return {
|
|
3423
|
+
name: "rolldown-plugin-solid",
|
|
3424
|
+
transform: {
|
|
3425
|
+
filter: { id: /\.(t|j)sx$/ },
|
|
3426
|
+
async handler(code, id) {
|
|
3427
|
+
const { name, ext } = parse(id);
|
|
3428
|
+
const filename = name + ext;
|
|
3429
|
+
const result = await transformAsync(code, {
|
|
3430
|
+
presets: [[solidPreset, options?.solid ?? {}], [preset_typescript_default, options?.typescript ?? {}]],
|
|
3431
|
+
filename,
|
|
3432
|
+
sourceMaps: "inline"
|
|
3433
|
+
});
|
|
3434
|
+
if (result?.code === void 0 || result.code === null) throw new Error("No result was provided from Babel");
|
|
3435
|
+
return result.code;
|
|
3436
|
+
}
|
|
3437
|
+
}
|
|
3438
|
+
};
|
|
3439
|
+
};
|
|
3440
|
+
var src_default = rolldownPluginSolid;
|
|
3441
|
+
|
|
3442
|
+
//#endregion
|
|
3443
|
+
export { src_default as default };
|