index.mjs 33 KB

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