index.js 35 KB

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