You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

984 lines
33 KiB

3 months ago
  1. var __defProp = Object.defineProperty;
  2. var __defProps = Object.defineProperties;
  3. var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
  4. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  7. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  8. var __spreadValues = (a, b) => {
  9. for (var prop in b || (b = {}))
  10. if (__hasOwnProp.call(b, prop))
  11. __defNormalProp(a, prop, b[prop]);
  12. if (__getOwnPropSymbols)
  13. for (var prop of __getOwnPropSymbols(b)) {
  14. if (__propIsEnum.call(b, prop))
  15. __defNormalProp(a, prop, b[prop]);
  16. }
  17. return a;
  18. };
  19. var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
  20. // src/index.ts
  21. import * as t5 from "@babel/types";
  22. import _template from "@babel/template";
  23. import _syntaxJsx from "@babel/plugin-syntax-jsx";
  24. import { addNamed, addNamespace, isModule } from "@babel/helper-module-imports";
  25. import ResolveType from "@vue/babel-plugin-resolve-type";
  26. import { declare } from "@babel/helper-plugin-utils";
  27. // src/transform-vue-jsx.ts
  28. import * as t3 from "@babel/types";
  29. import { addDefault } from "@babel/helper-module-imports";
  30. // src/utils.ts
  31. import * as t from "@babel/types";
  32. import htmlTags from "html-tags";
  33. import svgTags from "svg-tags";
  34. // src/slotFlags.ts
  35. var SlotFlags = /* @__PURE__ */ ((SlotFlags2) => {
  36. SlotFlags2[SlotFlags2["STABLE"] = 1] = "STABLE";
  37. SlotFlags2[SlotFlags2["DYNAMIC"] = 2] = "DYNAMIC";
  38. SlotFlags2[SlotFlags2["FORWARDED"] = 3] = "FORWARDED";
  39. return SlotFlags2;
  40. })(SlotFlags || {});
  41. var slotFlags_default = SlotFlags;
  42. // src/utils.ts
  43. var FRAGMENT = "Fragment";
  44. var KEEP_ALIVE = "KeepAlive";
  45. var createIdentifier = (state, name) => state.get(name)();
  46. var isDirective = (src) => src.startsWith("v-") || src.startsWith("v") && src.length >= 2 && src[1] >= "A" && src[1] <= "Z";
  47. var shouldTransformedToSlots = (tag) => !(tag.match(RegExp(`^_?${FRAGMENT}\\d*$`)) || tag === KEEP_ALIVE);
  48. var checkIsComponent = (path, state) => {
  49. var _a, _b;
  50. const namePath = path.get("name");
  51. if (namePath.isJSXMemberExpression()) {
  52. return shouldTransformedToSlots(namePath.node.property.name);
  53. }
  54. const tag = namePath.node.name;
  55. return !((_b = (_a = state.opts).isCustomElement) == null ? void 0 : _b.call(_a, tag)) && shouldTransformedToSlots(tag) && !htmlTags.includes(tag) && !svgTags.includes(tag);
  56. };
  57. var transformJSXMemberExpression = (path) => {
  58. const objectPath = path.node.object;
  59. const propertyPath = path.node.property;
  60. const transformedObject = t.isJSXMemberExpression(objectPath) ? transformJSXMemberExpression(
  61. path.get("object")
  62. ) : t.isJSXIdentifier(objectPath) ? t.identifier(objectPath.name) : t.nullLiteral();
  63. const transformedProperty = t.identifier(propertyPath.name);
  64. return t.memberExpression(transformedObject, transformedProperty);
  65. };
  66. var getTag = (path, state) => {
  67. var _a, _b;
  68. const namePath = path.get("openingElement").get("name");
  69. if (namePath.isJSXIdentifier()) {
  70. const { name } = namePath.node;
  71. if (!htmlTags.includes(name) && !svgTags.includes(name)) {
  72. return name === FRAGMENT ? createIdentifier(state, FRAGMENT) : path.scope.hasBinding(name) ? t.identifier(name) : ((_b = (_a = state.opts).isCustomElement) == null ? void 0 : _b.call(_a, name)) ? t.stringLiteral(name) : t.callExpression(createIdentifier(state, "resolveComponent"), [
  73. t.stringLiteral(name)
  74. ]);
  75. }
  76. return t.stringLiteral(name);
  77. }
  78. if (namePath.isJSXMemberExpression()) {
  79. return transformJSXMemberExpression(namePath);
  80. }
  81. throw new Error(`getTag: ${namePath.type} is not supported`);
  82. };
  83. var getJSXAttributeName = (path) => {
  84. const nameNode = path.node.name;
  85. if (t.isJSXIdentifier(nameNode)) {
  86. return nameNode.name;
  87. }
  88. return `${nameNode.namespace.name}:${nameNode.name.name}`;
  89. };
  90. var transformJSXText = (path) => {
  91. const str = transformText(path.node.value);
  92. return str !== "" ? t.stringLiteral(str) : null;
  93. };
  94. var transformText = (text) => {
  95. const lines = text.split(/\r\n|\n|\r/);
  96. let lastNonEmptyLine = 0;
  97. for (let i = 0; i < lines.length; i++) {
  98. if (lines[i].match(/[^ \t]/)) {
  99. lastNonEmptyLine = i;
  100. }
  101. }
  102. let str = "";
  103. for (let i = 0; i < lines.length; i++) {
  104. const line = lines[i];
  105. const isFirstLine = i === 0;
  106. const isLastLine = i === lines.length - 1;
  107. const isLastNonEmptyLine = i === lastNonEmptyLine;
  108. let trimmedLine = line.replace(/\t/g, " ");
  109. if (!isFirstLine) {
  110. trimmedLine = trimmedLine.replace(/^[ ]+/, "");
  111. }
  112. if (!isLastLine) {
  113. trimmedLine = trimmedLine.replace(/[ ]+$/, "");
  114. }
  115. if (trimmedLine) {
  116. if (!isLastNonEmptyLine) {
  117. trimmedLine += " ";
  118. }
  119. str += trimmedLine;
  120. }
  121. }
  122. return str;
  123. };
  124. var transformJSXExpressionContainer = (path) => path.get("expression").node;
  125. var transformJSXSpreadChild = (path) => t.spreadElement(path.get("expression").node);
  126. var walksScope = (path, name, slotFlag) => {
  127. if (path.scope.hasBinding(name) && path.parentPath) {
  128. if (t.isJSXElement(path.parentPath.node)) {
  129. path.parentPath.setData("slotFlag", slotFlag);
  130. }
  131. walksScope(path.parentPath, name, slotFlag);
  132. }
  133. };
  134. var buildIIFE = (path, children) => {
  135. const { parentPath } = path;
  136. if (parentPath.isAssignmentExpression()) {
  137. const { left } = parentPath.node;
  138. if (t.isIdentifier(left)) {
  139. return children.map((child) => {
  140. if (t.isIdentifier(child) && child.name === left.name) {
  141. const insertName = path.scope.generateUidIdentifier(child.name);
  142. parentPath.insertBefore(
  143. t.variableDeclaration("const", [
  144. t.variableDeclarator(
  145. insertName,
  146. t.callExpression(
  147. t.functionExpression(
  148. null,
  149. [],
  150. t.blockStatement([t.returnStatement(child)])
  151. ),
  152. []
  153. )
  154. )
  155. ])
  156. );
  157. return insertName;
  158. }
  159. return child;
  160. });
  161. }
  162. }
  163. return children;
  164. };
  165. var onRE = /^on[^a-z]/;
  166. var isOn = (key) => onRE.test(key);
  167. var mergeAsArray = (existing, incoming) => {
  168. if (t.isArrayExpression(existing.value)) {
  169. existing.value.elements.push(incoming.value);
  170. } else {
  171. existing.value = t.arrayExpression([
  172. existing.value,
  173. incoming.value
  174. ]);
  175. }
  176. };
  177. var dedupeProperties = (properties = [], mergeProps) => {
  178. if (!mergeProps) {
  179. return properties;
  180. }
  181. const knownProps = /* @__PURE__ */ new Map();
  182. const deduped = [];
  183. properties.forEach((prop) => {
  184. if (t.isStringLiteral(prop.key)) {
  185. const { value: name } = prop.key;
  186. const existing = knownProps.get(name);
  187. if (existing) {
  188. if (name === "style" || name === "class" || name.startsWith("on")) {
  189. mergeAsArray(existing, prop);
  190. }
  191. } else {
  192. knownProps.set(name, prop);
  193. deduped.push(prop);
  194. }
  195. } else {
  196. deduped.push(prop);
  197. }
  198. });
  199. return deduped;
  200. };
  201. var isConstant = (node) => {
  202. if (t.isIdentifier(node)) {
  203. return node.name === "undefined";
  204. }
  205. if (t.isArrayExpression(node)) {
  206. const { elements } = node;
  207. return elements.every((element) => element && isConstant(element));
  208. }
  209. if (t.isObjectExpression(node)) {
  210. return node.properties.every(
  211. (property) => isConstant(property.value)
  212. );
  213. }
  214. if (t.isLiteral(node)) {
  215. return true;
  216. }
  217. return false;
  218. };
  219. var transformJSXSpreadAttribute = (nodePath, path, mergeProps, args) => {
  220. const argument = path.get("argument");
  221. const properties = t.isObjectExpression(argument.node) ? argument.node.properties : void 0;
  222. if (!properties) {
  223. if (argument.isIdentifier()) {
  224. walksScope(
  225. nodePath,
  226. argument.node.name,
  227. slotFlags_default.DYNAMIC
  228. );
  229. }
  230. args.push(mergeProps ? argument.node : t.spreadElement(argument.node));
  231. } else if (mergeProps) {
  232. args.push(t.objectExpression(properties));
  233. } else {
  234. args.push(...properties);
  235. }
  236. };
  237. // src/parseDirectives.ts
  238. import * as t2 from "@babel/types";
  239. var getType = (path) => {
  240. const typePath = path.get("attributes").find((attribute) => {
  241. if (!attribute.isJSXAttribute()) {
  242. return false;
  243. }
  244. return attribute.get("name").isJSXIdentifier() && attribute.get("name").node.name === "type";
  245. });
  246. return typePath ? typePath.get("value").node : null;
  247. };
  248. var parseModifiers = (value) => t2.isArrayExpression(value) ? value.elements.map((el) => t2.isStringLiteral(el) ? el.value : "").filter(Boolean) : [];
  249. var parseDirectives = (params) => {
  250. var _a, _b;
  251. const { path, value, state, tag, isComponent } = params;
  252. const args = [];
  253. const vals = [];
  254. const modifiersSet = [];
  255. let directiveName;
  256. let directiveArgument;
  257. let directiveModifiers;
  258. if ("namespace" in path.node.name) {
  259. [directiveName, directiveArgument] = params.name.split(":");
  260. directiveName = path.node.name.namespace.name;
  261. directiveArgument = path.node.name.name.name;
  262. directiveModifiers = directiveArgument.split("_").slice(1);
  263. } else {
  264. const underscoreModifiers = params.name.split("_");
  265. directiveName = underscoreModifiers.shift() || "";
  266. directiveModifiers = underscoreModifiers;
  267. }
  268. directiveName = directiveName.replace(/^v/, "").replace(/^-/, "").replace(/^\S/, (s) => s.toLowerCase());
  269. if (directiveArgument) {
  270. args.push(t2.stringLiteral(directiveArgument.split("_")[0]));
  271. }
  272. const isVModels = directiveName === "models";
  273. const isVModel = directiveName === "model";
  274. if (isVModel && !path.get("value").isJSXExpressionContainer()) {
  275. throw new Error("You have to use JSX Expression inside your v-model");
  276. }
  277. if (isVModels && !isComponent) {
  278. throw new Error("v-models can only use in custom components");
  279. }
  280. const shouldResolve = !["html", "text", "model", "slots", "models"].includes(directiveName) || isVModel && !isComponent;
  281. let modifiers = directiveModifiers;
  282. if (t2.isArrayExpression(value)) {
  283. const elementsList = isVModels ? value.elements : [value];
  284. elementsList.forEach((element) => {
  285. if (isVModels && !t2.isArrayExpression(element)) {
  286. throw new Error("You should pass a Two-dimensional Arrays to v-models");
  287. }
  288. const { elements } = element;
  289. const [first, second, third] = elements;
  290. if (second && !t2.isArrayExpression(second) && !t2.isSpreadElement(second)) {
  291. args.push(second);
  292. modifiers = parseModifiers(third);
  293. } else if (t2.isArrayExpression(second)) {
  294. if (!shouldResolve) {
  295. args.push(t2.nullLiteral());
  296. }
  297. modifiers = parseModifiers(second);
  298. } else if (!shouldResolve) {
  299. args.push(t2.nullLiteral());
  300. }
  301. modifiersSet.push(new Set(modifiers));
  302. vals.push(first);
  303. });
  304. } else if (isVModel && !shouldResolve) {
  305. args.push(t2.nullLiteral());
  306. modifiersSet.push(new Set(directiveModifiers));
  307. } else {
  308. modifiersSet.push(new Set(directiveModifiers));
  309. }
  310. return {
  311. directiveName,
  312. modifiers: modifiersSet,
  313. values: vals.length ? vals : [value],
  314. args,
  315. directive: shouldResolve ? [
  316. resolveDirective(path, state, tag, directiveName),
  317. vals[0] || value,
  318. ((_a = modifiersSet[0]) == null ? void 0 : _a.size) ? args[0] || t2.unaryExpression("void", t2.numericLiteral(0), true) : args[0],
  319. !!((_b = modifiersSet[0]) == null ? void 0 : _b.size) && t2.objectExpression(
  320. [...modifiersSet[0]].map(
  321. (modifier) => t2.objectProperty(t2.identifier(modifier), t2.booleanLiteral(true))
  322. )
  323. )
  324. ].filter(Boolean) : void 0
  325. };
  326. };
  327. var resolveDirective = (path, state, tag, directiveName) => {
  328. if (directiveName === "show") {
  329. return createIdentifier(state, "vShow");
  330. }
  331. if (directiveName === "model") {
  332. let modelToUse;
  333. const type = getType(path.parentPath);
  334. switch (tag.value) {
  335. case "select":
  336. modelToUse = createIdentifier(state, "vModelSelect");
  337. break;
  338. case "textarea":
  339. modelToUse = createIdentifier(state, "vModelText");
  340. break;
  341. default:
  342. if (t2.isStringLiteral(type) || !type) {
  343. switch (type == null ? void 0 : type.value) {
  344. case "checkbox":
  345. modelToUse = createIdentifier(state, "vModelCheckbox");
  346. break;
  347. case "radio":
  348. modelToUse = createIdentifier(state, "vModelRadio");
  349. break;
  350. default:
  351. modelToUse = createIdentifier(state, "vModelText");
  352. }
  353. } else {
  354. modelToUse = createIdentifier(state, "vModelDynamic");
  355. }
  356. }
  357. return modelToUse;
  358. }
  359. return t2.callExpression(createIdentifier(state, "resolveDirective"), [
  360. t2.stringLiteral(directiveName)
  361. ]);
  362. };
  363. var parseDirectives_default = parseDirectives;
  364. // src/transform-vue-jsx.ts
  365. var xlinkRE = /^xlink([A-Z])/;
  366. var getJSXAttributeValue = (path, state) => {
  367. const valuePath = path.get("value");
  368. if (valuePath.isJSXElement()) {
  369. return transformJSXElement(valuePath, state);
  370. }
  371. if (valuePath.isStringLiteral()) {
  372. return t3.stringLiteral(transformText(valuePath.node.value));
  373. }
  374. if (valuePath.isJSXExpressionContainer()) {
  375. return transformJSXExpressionContainer(valuePath);
  376. }
  377. return null;
  378. };
  379. var buildProps = (path, state) => {
  380. const tag = getTag(path, state);
  381. const isComponent = checkIsComponent(path.get("openingElement"), state);
  382. const props = path.get("openingElement").get("attributes");
  383. const directives = [];
  384. const dynamicPropNames = /* @__PURE__ */ new Set();
  385. let slots = null;
  386. let patchFlag = 0;
  387. if (props.length === 0) {
  388. return {
  389. tag,
  390. isComponent,
  391. slots,
  392. props: t3.nullLiteral(),
  393. directives,
  394. patchFlag,
  395. dynamicPropNames
  396. };
  397. }
  398. let properties = [];
  399. let hasRef = false;
  400. let hasClassBinding = false;
  401. let hasStyleBinding = false;
  402. let hasHydrationEventBinding = false;
  403. let hasDynamicKeys = false;
  404. const mergeArgs = [];
  405. const { mergeProps = true } = state.opts;
  406. props.forEach((prop) => {
  407. if (prop.isJSXAttribute()) {
  408. let name = getJSXAttributeName(prop);
  409. const attributeValue = getJSXAttributeValue(prop, state);
  410. if (!isConstant(attributeValue) || name === "ref") {
  411. if (!isComponent && isOn(name) && // omit the flag for click handlers becaues hydration gives click
  412. // dedicated fast path.
  413. name.toLowerCase() !== "onclick" && // omit v-model handlers
  414. name !== "onUpdate:modelValue") {
  415. hasHydrationEventBinding = true;
  416. }
  417. if (name === "ref") {
  418. hasRef = true;
  419. } else if (name === "class" && !isComponent) {
  420. hasClassBinding = true;
  421. } else if (name === "style" && !isComponent) {
  422. hasStyleBinding = true;
  423. } else if (name !== "key" && !isDirective(name) && name !== "on") {
  424. dynamicPropNames.add(name);
  425. }
  426. }
  427. if (state.opts.transformOn && (name === "on" || name === "nativeOn")) {
  428. if (!state.get("transformOn")) {
  429. state.set(
  430. "transformOn",
  431. addDefault(path, "@vue/babel-helper-vue-transform-on", {
  432. nameHint: "_transformOn"
  433. })
  434. );
  435. }
  436. mergeArgs.push(
  437. t3.callExpression(state.get("transformOn"), [
  438. attributeValue || t3.booleanLiteral(true)
  439. ])
  440. );
  441. return;
  442. }
  443. if (isDirective(name)) {
  444. const { directive, modifiers, values, args, directiveName } = parseDirectives_default({
  445. tag,
  446. isComponent,
  447. name,
  448. path: prop,
  449. state,
  450. value: attributeValue
  451. });
  452. if (directiveName === "slots") {
  453. slots = attributeValue;
  454. return;
  455. }
  456. if (directive) {
  457. directives.push(t3.arrayExpression(directive));
  458. } else if (directiveName === "html") {
  459. properties.push(
  460. t3.objectProperty(t3.stringLiteral("innerHTML"), values[0])
  461. );
  462. dynamicPropNames.add("innerHTML");
  463. } else if (directiveName === "text") {
  464. properties.push(
  465. t3.objectProperty(t3.stringLiteral("textContent"), values[0])
  466. );
  467. dynamicPropNames.add("textContent");
  468. }
  469. if (["models", "model"].includes(directiveName)) {
  470. values.forEach((value, index) => {
  471. var _a;
  472. const propName = args[index];
  473. const isDynamic = propName && !t3.isStringLiteral(propName) && !t3.isNullLiteral(propName);
  474. if (!directive) {
  475. properties.push(
  476. t3.objectProperty(
  477. t3.isNullLiteral(propName) ? t3.stringLiteral("modelValue") : propName,
  478. value,
  479. isDynamic
  480. )
  481. );
  482. if (!isDynamic) {
  483. dynamicPropNames.add(
  484. (propName == null ? void 0 : propName.value) || "modelValue"
  485. );
  486. }
  487. if ((_a = modifiers[index]) == null ? void 0 : _a.size) {
  488. properties.push(
  489. t3.objectProperty(
  490. isDynamic ? t3.binaryExpression(
  491. "+",
  492. propName,
  493. t3.stringLiteral("Modifiers")
  494. ) : t3.stringLiteral(
  495. `${(propName == null ? void 0 : propName.value) || "model"}Modifiers`
  496. ),
  497. t3.objectExpression(
  498. [...modifiers[index]].map(
  499. (modifier) => t3.objectProperty(
  500. t3.stringLiteral(modifier),
  501. t3.booleanLiteral(true)
  502. )
  503. )
  504. ),
  505. isDynamic
  506. )
  507. );
  508. }
  509. }
  510. const updateName = isDynamic ? t3.binaryExpression("+", t3.stringLiteral("onUpdate:"), propName) : t3.stringLiteral(
  511. `onUpdate:${(propName == null ? void 0 : propName.value) || "modelValue"}`
  512. );
  513. properties.push(
  514. t3.objectProperty(
  515. updateName,
  516. t3.arrowFunctionExpression(
  517. [t3.identifier("$event")],
  518. t3.assignmentExpression(
  519. "=",
  520. value,
  521. t3.identifier("$event")
  522. )
  523. ),
  524. isDynamic
  525. )
  526. );
  527. if (!isDynamic) {
  528. dynamicPropNames.add(updateName.value);
  529. } else {
  530. hasDynamicKeys = true;
  531. }
  532. });
  533. }
  534. } else {
  535. if (name.match(xlinkRE)) {
  536. name = name.replace(
  537. xlinkRE,
  538. (_, firstCharacter) => `xlink:${firstCharacter.toLowerCase()}`
  539. );
  540. }
  541. properties.push(
  542. t3.objectProperty(
  543. t3.stringLiteral(name),
  544. attributeValue || t3.booleanLiteral(true)
  545. )
  546. );
  547. }
  548. } else {
  549. if (properties.length && mergeProps) {
  550. mergeArgs.push(
  551. t3.objectExpression(dedupeProperties(properties, mergeProps))
  552. );
  553. properties = [];
  554. }
  555. hasDynamicKeys = true;
  556. transformJSXSpreadAttribute(
  557. path,
  558. prop,
  559. mergeProps,
  560. mergeProps ? mergeArgs : properties
  561. );
  562. }
  563. });
  564. if (hasDynamicKeys) {
  565. patchFlag |= 16 /* FULL_PROPS */;
  566. } else {
  567. if (hasClassBinding) {
  568. patchFlag |= 2 /* CLASS */;
  569. }
  570. if (hasStyleBinding) {
  571. patchFlag |= 4 /* STYLE */;
  572. }
  573. if (dynamicPropNames.size) {
  574. patchFlag |= 8 /* PROPS */;
  575. }
  576. if (hasHydrationEventBinding) {
  577. patchFlag |= 32 /* HYDRATE_EVENTS */;
  578. }
  579. }
  580. if ((patchFlag === 0 || patchFlag === 32 /* HYDRATE_EVENTS */) && (hasRef || directives.length > 0)) {
  581. patchFlag |= 512 /* NEED_PATCH */;
  582. }
  583. let propsExpression = t3.nullLiteral();
  584. if (mergeArgs.length) {
  585. if (properties.length) {
  586. mergeArgs.push(
  587. t3.objectExpression(dedupeProperties(properties, mergeProps))
  588. );
  589. }
  590. if (mergeArgs.length > 1) {
  591. propsExpression = t3.callExpression(
  592. createIdentifier(state, "mergeProps"),
  593. mergeArgs
  594. );
  595. } else {
  596. propsExpression = mergeArgs[0];
  597. }
  598. } else if (properties.length) {
  599. if (properties.length === 1 && t3.isSpreadElement(properties[0])) {
  600. propsExpression = properties[0].argument;
  601. } else {
  602. propsExpression = t3.objectExpression(
  603. dedupeProperties(properties, mergeProps)
  604. );
  605. }
  606. }
  607. return {
  608. tag,
  609. props: propsExpression,
  610. isComponent,
  611. slots,
  612. directives,
  613. patchFlag,
  614. dynamicPropNames
  615. };
  616. };
  617. var getChildren = (paths, state) => paths.map((path) => {
  618. if (path.isJSXText()) {
  619. const transformedText = transformJSXText(path);
  620. if (transformedText) {
  621. return t3.callExpression(createIdentifier(state, "createTextVNode"), [
  622. transformedText
  623. ]);
  624. }
  625. return transformedText;
  626. }
  627. if (path.isJSXExpressionContainer()) {
  628. const expression = transformJSXExpressionContainer(path);
  629. if (t3.isIdentifier(expression)) {
  630. const { name } = expression;
  631. const { referencePaths = [] } = path.scope.getBinding(name) || {};
  632. referencePaths.forEach((referencePath) => {
  633. walksScope(referencePath, name, slotFlags_default.DYNAMIC);
  634. });
  635. }
  636. return expression;
  637. }
  638. if (path.isJSXSpreadChild()) {
  639. return transformJSXSpreadChild(path);
  640. }
  641. if (path.isCallExpression()) {
  642. return path.node;
  643. }
  644. if (path.isJSXElement()) {
  645. return transformJSXElement(path, state);
  646. }
  647. throw new Error(`getChildren: ${path.type} is not supported`);
  648. }).filter(
  649. (value) => value != null && !t3.isJSXEmptyExpression(value)
  650. );
  651. var transformJSXElement = (path, state) => {
  652. const children = getChildren(path.get("children"), state);
  653. const {
  654. tag,
  655. props,
  656. isComponent,
  657. directives,
  658. patchFlag,
  659. dynamicPropNames,
  660. slots
  661. } = buildProps(path, state);
  662. const { optimize = false } = state.opts;
  663. const slotFlag = path.getData("slotFlag") || slotFlags_default.STABLE;
  664. let VNodeChild;
  665. if (children.length > 1 || slots) {
  666. VNodeChild = isComponent ? children.length ? t3.objectExpression(
  667. [
  668. !!children.length && t3.objectProperty(
  669. t3.identifier("default"),
  670. t3.arrowFunctionExpression(
  671. [],
  672. t3.arrayExpression(buildIIFE(path, children))
  673. )
  674. ),
  675. ...slots ? t3.isObjectExpression(slots) ? slots.properties : [t3.spreadElement(slots)] : [],
  676. optimize && t3.objectProperty(t3.identifier("_"), t3.numericLiteral(slotFlag))
  677. ].filter(Boolean)
  678. ) : slots : t3.arrayExpression(children);
  679. } else if (children.length === 1) {
  680. const { enableObjectSlots = true } = state.opts;
  681. const child = children[0];
  682. const objectExpression4 = t3.objectExpression(
  683. [
  684. t3.objectProperty(
  685. t3.identifier("default"),
  686. t3.arrowFunctionExpression(
  687. [],
  688. t3.arrayExpression(buildIIFE(path, [child]))
  689. )
  690. ),
  691. optimize && t3.objectProperty(
  692. t3.identifier("_"),
  693. t3.numericLiteral(slotFlag)
  694. )
  695. ].filter(Boolean)
  696. );
  697. if (t3.isIdentifier(child) && isComponent) {
  698. VNodeChild = enableObjectSlots ? t3.conditionalExpression(
  699. t3.callExpression(
  700. state.get("@vue/babel-plugin-jsx/runtimeIsSlot")(),
  701. [child]
  702. ),
  703. child,
  704. objectExpression4
  705. ) : objectExpression4;
  706. } else if (t3.isCallExpression(child) && child.loc && isComponent) {
  707. if (enableObjectSlots) {
  708. const { scope } = path;
  709. const slotId = scope.generateUidIdentifier("slot");
  710. if (scope) {
  711. scope.push({
  712. id: slotId,
  713. kind: "let"
  714. });
  715. }
  716. const alternate = t3.objectExpression(
  717. [
  718. t3.objectProperty(
  719. t3.identifier("default"),
  720. t3.arrowFunctionExpression(
  721. [],
  722. t3.arrayExpression(buildIIFE(path, [slotId]))
  723. )
  724. ),
  725. optimize && t3.objectProperty(
  726. t3.identifier("_"),
  727. t3.numericLiteral(slotFlag)
  728. )
  729. ].filter(Boolean)
  730. );
  731. const assignment = t3.assignmentExpression("=", slotId, child);
  732. const condition = t3.callExpression(
  733. state.get("@vue/babel-plugin-jsx/runtimeIsSlot")(),
  734. [assignment]
  735. );
  736. VNodeChild = t3.conditionalExpression(condition, slotId, alternate);
  737. } else {
  738. VNodeChild = objectExpression4;
  739. }
  740. } else if (t3.isFunctionExpression(child) || t3.isArrowFunctionExpression(child)) {
  741. VNodeChild = t3.objectExpression([
  742. t3.objectProperty(t3.identifier("default"), child)
  743. ]);
  744. } else if (t3.isObjectExpression(child)) {
  745. VNodeChild = t3.objectExpression(
  746. [
  747. ...child.properties,
  748. optimize && t3.objectProperty(t3.identifier("_"), t3.numericLiteral(slotFlag))
  749. ].filter(Boolean)
  750. );
  751. } else {
  752. VNodeChild = isComponent ? t3.objectExpression([
  753. t3.objectProperty(
  754. t3.identifier("default"),
  755. t3.arrowFunctionExpression([], t3.arrayExpression([child]))
  756. )
  757. ]) : t3.arrayExpression([child]);
  758. }
  759. }
  760. const createVNode = t3.callExpression(
  761. createIdentifier(state, "createVNode"),
  762. [
  763. tag,
  764. props,
  765. VNodeChild || t3.nullLiteral(),
  766. !!patchFlag && optimize && t3.numericLiteral(patchFlag),
  767. !!dynamicPropNames.size && optimize && t3.arrayExpression(
  768. [...dynamicPropNames.keys()].map((name) => t3.stringLiteral(name))
  769. )
  770. ].filter(Boolean)
  771. );
  772. if (!directives.length) {
  773. return createVNode;
  774. }
  775. return t3.callExpression(createIdentifier(state, "withDirectives"), [
  776. createVNode,
  777. t3.arrayExpression(directives)
  778. ]);
  779. };
  780. var visitor = {
  781. JSXElement: {
  782. exit(path, state) {
  783. path.replaceWith(transformJSXElement(path, state));
  784. }
  785. }
  786. };
  787. var transform_vue_jsx_default = visitor;
  788. // src/sugar-fragment.ts
  789. import * as t4 from "@babel/types";
  790. var transformFragment = (path, Fragment) => {
  791. const children = path.get("children") || [];
  792. return t4.jsxElement(
  793. t4.jsxOpeningElement(Fragment, []),
  794. t4.jsxClosingElement(Fragment),
  795. children.map(({ node }) => node),
  796. false
  797. );
  798. };
  799. var visitor2 = {
  800. JSXFragment: {
  801. enter(path, state) {
  802. const fragmentCallee = createIdentifier(state, FRAGMENT);
  803. path.replaceWith(
  804. transformFragment(
  805. path,
  806. t4.isIdentifier(fragmentCallee) ? t4.jsxIdentifier(fragmentCallee.name) : t4.jsxMemberExpression(
  807. t4.jsxIdentifier(fragmentCallee.object.name),
  808. t4.jsxIdentifier(fragmentCallee.property.name)
  809. )
  810. )
  811. );
  812. }
  813. }
  814. };
  815. var sugar_fragment_default = visitor2;
  816. // src/index.ts
  817. var hasJSX = (parentPath) => {
  818. let fileHasJSX = false;
  819. parentPath.traverse({
  820. JSXElement(path) {
  821. fileHasJSX = true;
  822. path.stop();
  823. },
  824. JSXFragment(path) {
  825. fileHasJSX = true;
  826. path.stop();
  827. }
  828. });
  829. return fileHasJSX;
  830. };
  831. var JSX_ANNOTATION_REGEX = /\*?\s*@jsx\s+([^\s]+)/;
  832. // @__NO_SIDE_EFFECTS__
  833. function interopDefault(m) {
  834. return m.default || m;
  835. }
  836. var syntaxJsx = /* @__PURE__ */ interopDefault(_syntaxJsx);
  837. var template = /* @__PURE__ */ interopDefault(_template);
  838. var src_default = declare(
  839. (api, opt, dirname) => {
  840. const { types } = api;
  841. let resolveType;
  842. if (opt.resolveType) {
  843. if (typeof opt.resolveType === "boolean") opt.resolveType = {};
  844. resolveType = ResolveType(api, opt.resolveType, dirname);
  845. }
  846. return __spreadProps(__spreadValues({}, resolveType || {}), {
  847. name: "babel-plugin-jsx",
  848. inherits: /* @__PURE__ */ interopDefault(syntaxJsx),
  849. visitor: __spreadProps(__spreadValues(__spreadValues(__spreadValues({}, resolveType == null ? void 0 : resolveType.visitor), transform_vue_jsx_default), sugar_fragment_default), {
  850. Program: {
  851. enter(path, state) {
  852. if (hasJSX(path)) {
  853. const importNames = [
  854. "createVNode",
  855. "Fragment",
  856. "resolveComponent",
  857. "withDirectives",
  858. "vShow",
  859. "vModelSelect",
  860. "vModelText",
  861. "vModelCheckbox",
  862. "vModelRadio",
  863. "vModelText",
  864. "vModelDynamic",
  865. "resolveDirective",
  866. "mergeProps",
  867. "createTextVNode",
  868. "isVNode"
  869. ];
  870. if (isModule(path)) {
  871. const importMap = {};
  872. importNames.forEach((name) => {
  873. state.set(name, () => {
  874. if (importMap[name]) {
  875. return types.cloneNode(importMap[name]);
  876. }
  877. const identifier5 = addNamed(path, name, "vue", {
  878. ensureLiveReference: true
  879. });
  880. importMap[name] = identifier5;
  881. return identifier5;
  882. });
  883. });
  884. const { enableObjectSlots = true } = state.opts;
  885. if (enableObjectSlots) {
  886. state.set("@vue/babel-plugin-jsx/runtimeIsSlot", () => {
  887. if (importMap.runtimeIsSlot) {
  888. return importMap.runtimeIsSlot;
  889. }
  890. const { name: isVNodeName } = state.get(
  891. "isVNode"
  892. )();
  893. const isSlot = path.scope.generateUidIdentifier("isSlot");
  894. const ast = template.ast`
  895. function ${isSlot.name}(s) {
  896. return typeof s === 'function' || (Object.prototype.toString.call(s) === '[object Object]' && !${isVNodeName}(s));
  897. }
  898. `;
  899. const lastImport = path.get("body").filter((p) => p.isImportDeclaration()).pop();
  900. if (lastImport) {
  901. lastImport.insertAfter(ast);
  902. }
  903. importMap.runtimeIsSlot = isSlot;
  904. return isSlot;
  905. });
  906. }
  907. } else {
  908. let sourceName;
  909. importNames.forEach((name) => {
  910. state.set(name, () => {
  911. if (!sourceName) {
  912. sourceName = addNamespace(path, "vue", {
  913. ensureLiveReference: true
  914. });
  915. }
  916. return t5.memberExpression(sourceName, t5.identifier(name));
  917. });
  918. });
  919. const helpers = {};
  920. const { enableObjectSlots = true } = state.opts;
  921. if (enableObjectSlots) {
  922. state.set("@vue/babel-plugin-jsx/runtimeIsSlot", () => {
  923. if (helpers.runtimeIsSlot) {
  924. return helpers.runtimeIsSlot;
  925. }
  926. const isSlot = path.scope.generateUidIdentifier("isSlot");
  927. const { object: objectName } = state.get(
  928. "isVNode"
  929. )();
  930. const ast = template.ast`
  931. function ${isSlot.name}(s) {
  932. return typeof s === 'function' || (Object.prototype.toString.call(s) === '[object Object]' && !${objectName.name}.isVNode(s));
  933. }
  934. `;
  935. const nodePaths = path.get("body");
  936. const lastImport = nodePaths.filter(
  937. (p) => p.isVariableDeclaration() && p.node.declarations.some(
  938. (d) => {
  939. var _a;
  940. return ((_a = d.id) == null ? void 0 : _a.name) === sourceName.name;
  941. }
  942. )
  943. ).pop();
  944. if (lastImport) {
  945. lastImport.insertAfter(ast);
  946. }
  947. return isSlot;
  948. });
  949. }
  950. }
  951. const {
  952. opts: { pragma = "" },
  953. file
  954. } = state;
  955. if (pragma) {
  956. state.set("createVNode", () => t5.identifier(pragma));
  957. }
  958. if (file.ast.comments) {
  959. for (const comment of file.ast.comments) {
  960. const jsxMatches = JSX_ANNOTATION_REGEX.exec(comment.value);
  961. if (jsxMatches) {
  962. state.set("createVNode", () => t5.identifier(jsxMatches[1]));
  963. }
  964. }
  965. }
  966. }
  967. }
  968. }
  969. })
  970. });
  971. }
  972. );
  973. export {
  974. src_default as default
  975. };