index.js 34 KB

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