index.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. //#region rolldown:runtime
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __commonJS = (cb, mod) => function() {
  9. return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
  13. key = keys[i];
  14. if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
  15. get: ((k) => from[k]).bind(null, key),
  16. enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
  17. });
  18. }
  19. return to;
  20. };
  21. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
  22. value: mod,
  23. enumerable: true
  24. }) : target, mod));
  25. //#endregion
  26. const __babel_types = __toESM(require("@babel/types"));
  27. const __babel_template = __toESM(require("@babel/template"));
  28. const __babel_plugin_syntax_jsx = __toESM(require("@babel/plugin-syntax-jsx"));
  29. const __babel_helper_module_imports = __toESM(require("@babel/helper-module-imports"));
  30. const __vue_babel_plugin_resolve_type = __toESM(require("@vue/babel-plugin-resolve-type"));
  31. const __babel_helper_plugin_utils = __toESM(require("@babel/helper-plugin-utils"));
  32. const __vue_shared = __toESM(require("@vue/shared"));
  33. //#region src/slotFlags.ts
  34. var SlotFlags = /* @__PURE__ */ function(SlotFlags$1) {
  35. /**
  36. * Stable slots that only reference slot props or context state. The slot
  37. * can fully capture its own dependencies so when passed down the parent won't
  38. * need to force the child to update.
  39. */
  40. SlotFlags$1[SlotFlags$1["STABLE"] = 1] = "STABLE";
  41. /**
  42. * Slots that reference scope variables (v-for or an outer slot prop), or
  43. * has conditional structure (v-if, v-for). The parent will need to force
  44. * the child to update because the slot does not fully capture its dependencies.
  45. */
  46. SlotFlags$1[SlotFlags$1["DYNAMIC"] = 2] = "DYNAMIC";
  47. /**
  48. * `<slot/>` being forwarded into a child component. Whether the parent needs
  49. * to update the child is dependent on what kind of slots the parent itself
  50. * received. This has to be refined at runtime, when the child's vnode
  51. * is being created (in `normalizeChildren`)
  52. */
  53. SlotFlags$1[SlotFlags$1["FORWARDED"] = 3] = "FORWARDED";
  54. return SlotFlags$1;
  55. }(SlotFlags || {});
  56. var slotFlags_default = SlotFlags;
  57. //#endregion
  58. //#region src/utils.ts
  59. const FRAGMENT = "Fragment";
  60. const KEEP_ALIVE = "KeepAlive";
  61. /**
  62. * create Identifier
  63. * @param path NodePath
  64. * @param state
  65. * @param name string
  66. * @returns MemberExpression
  67. */
  68. const createIdentifier = (state, name) => state.get(name)();
  69. /**
  70. * Checks if string is describing a directive
  71. * @param src string
  72. */
  73. const isDirective = (src) => src.startsWith("v-") || src.startsWith("v") && src.length >= 2 && src[1] >= "A" && src[1] <= "Z";
  74. /**
  75. * Should transformed to slots
  76. * @param tag string
  77. * @returns boolean
  78. */
  79. const shouldTransformedToSlots = (tag) => !(tag.match(RegExp(`^_?${FRAGMENT}\\d*$`)) || tag === KEEP_ALIVE);
  80. /**
  81. * Check if a Node is a component
  82. *
  83. * @param t
  84. * @param path JSXOpeningElement
  85. * @returns boolean
  86. */
  87. const checkIsComponent = (path, state) => {
  88. var _state$opts$isCustomE, _state$opts;
  89. const namePath = path.get("name");
  90. if (namePath.isJSXMemberExpression()) return shouldTransformedToSlots(namePath.node.property.name);
  91. const tag = namePath.node.name;
  92. return !((_state$opts$isCustomE = (_state$opts = state.opts).isCustomElement) === null || _state$opts$isCustomE === void 0 ? void 0 : _state$opts$isCustomE.call(_state$opts, tag)) && shouldTransformedToSlots(tag) && !(0, __vue_shared.isHTMLTag)(tag) && !(0, __vue_shared.isSVGTag)(tag);
  93. };
  94. /**
  95. * Transform JSXMemberExpression to MemberExpression
  96. * @param path JSXMemberExpression
  97. * @returns MemberExpression
  98. */
  99. const transformJSXMemberExpression = (path) => {
  100. const objectPath = path.node.object;
  101. const propertyPath = path.node.property;
  102. const transformedObject = __babel_types.isJSXMemberExpression(objectPath) ? transformJSXMemberExpression(path.get("object")) : __babel_types.isJSXIdentifier(objectPath) ? __babel_types.identifier(objectPath.name) : __babel_types.nullLiteral();
  103. const transformedProperty = __babel_types.identifier(propertyPath.name);
  104. return __babel_types.memberExpression(transformedObject, transformedProperty);
  105. };
  106. /**
  107. * Get tag (first attribute for h) from JSXOpeningElement
  108. * @param path JSXElement
  109. * @param state State
  110. * @returns Identifier | StringLiteral | MemberExpression | CallExpression
  111. */
  112. const getTag = (path, state) => {
  113. const namePath = path.get("openingElement").get("name");
  114. if (namePath.isJSXIdentifier()) {
  115. const { name } = namePath.node;
  116. if (!(0, __vue_shared.isHTMLTag)(name) && !(0, __vue_shared.isSVGTag)(name)) {
  117. var _state$opts$isCustomE2, _state$opts2;
  118. return name === FRAGMENT ? createIdentifier(state, FRAGMENT) : path.scope.hasBinding(name) ? __babel_types.identifier(name) : ((_state$opts$isCustomE2 = (_state$opts2 = state.opts).isCustomElement) === null || _state$opts$isCustomE2 === void 0 ? void 0 : _state$opts$isCustomE2.call(_state$opts2, name)) ? __babel_types.stringLiteral(name) : __babel_types.callExpression(createIdentifier(state, "resolveComponent"), [__babel_types.stringLiteral(name)]);
  119. }
  120. return __babel_types.stringLiteral(name);
  121. }
  122. if (namePath.isJSXMemberExpression()) return transformJSXMemberExpression(namePath);
  123. throw new Error(`getTag: ${namePath.type} is not supported`);
  124. };
  125. const getJSXAttributeName = (path) => {
  126. const nameNode = path.node.name;
  127. if (__babel_types.isJSXIdentifier(nameNode)) return nameNode.name;
  128. return `${nameNode.namespace.name}:${nameNode.name.name}`;
  129. };
  130. /**
  131. * Transform JSXText to StringLiteral
  132. * @param path JSXText
  133. * @returns StringLiteral | null
  134. */
  135. const transformJSXText = (path) => {
  136. const str = transformText(path.node.value);
  137. return str !== "" ? __babel_types.stringLiteral(str) : null;
  138. };
  139. const transformText = (text) => {
  140. const lines = text.split(/\r\n|\n|\r/);
  141. let lastNonEmptyLine = 0;
  142. for (let i = 0; i < lines.length; i++) if (lines[i].match(/[^ \t]/)) lastNonEmptyLine = i;
  143. let str = "";
  144. for (let i = 0; i < lines.length; i++) {
  145. const line = lines[i];
  146. const isFirstLine = i === 0;
  147. const isLastLine = i === lines.length - 1;
  148. const isLastNonEmptyLine = i === lastNonEmptyLine;
  149. let trimmedLine = line.replace(/\t/g, " ");
  150. if (!isFirstLine) trimmedLine = trimmedLine.replace(/^[ ]+/, "");
  151. if (!isLastLine) trimmedLine = trimmedLine.replace(/[ ]+$/, "");
  152. if (trimmedLine) {
  153. if (!isLastNonEmptyLine) trimmedLine += " ";
  154. str += trimmedLine;
  155. }
  156. }
  157. return str;
  158. };
  159. /**
  160. * Transform JSXExpressionContainer to Expression
  161. * @param path JSXExpressionContainer
  162. * @returns Expression
  163. */
  164. const transformJSXExpressionContainer = (path) => path.get("expression").node;
  165. /**
  166. * Transform JSXSpreadChild
  167. * @param path JSXSpreadChild
  168. * @returns SpreadElement
  169. */
  170. const transformJSXSpreadChild = (path) => __babel_types.spreadElement(path.get("expression").node);
  171. const walksScope = (path, name, slotFlag) => {
  172. if (path.scope.hasBinding(name) && path.parentPath) {
  173. if (__babel_types.isJSXElement(path.parentPath.node)) path.parentPath.setData("slotFlag", slotFlag);
  174. walksScope(path.parentPath, name, slotFlag);
  175. }
  176. };
  177. const buildIIFE = (path, children) => {
  178. const { parentPath } = path;
  179. if (parentPath.isAssignmentExpression()) {
  180. const { left } = parentPath.node;
  181. if (__babel_types.isIdentifier(left)) return children.map((child) => {
  182. if (__babel_types.isIdentifier(child) && child.name === left.name) {
  183. const insertName = path.scope.generateUidIdentifier(child.name);
  184. parentPath.insertBefore(__babel_types.variableDeclaration("const", [__babel_types.variableDeclarator(insertName, __babel_types.callExpression(__babel_types.functionExpression(null, [], __babel_types.blockStatement([__babel_types.returnStatement(child)])), []))]));
  185. return insertName;
  186. }
  187. return child;
  188. });
  189. }
  190. return children;
  191. };
  192. const onRE = /^on[^a-z]/;
  193. const isOn = (key) => onRE.test(key);
  194. const mergeAsArray = (existing, incoming) => {
  195. if (__babel_types.isArrayExpression(existing.value)) existing.value.elements.push(incoming.value);
  196. else existing.value = __babel_types.arrayExpression([existing.value, incoming.value]);
  197. };
  198. const dedupeProperties = (properties = [], mergeProps) => {
  199. if (!mergeProps) return properties;
  200. const knownProps = /* @__PURE__ */ new Map();
  201. const deduped = [];
  202. properties.forEach((prop) => {
  203. if (__babel_types.isStringLiteral(prop.key)) {
  204. const { value: name } = prop.key;
  205. const existing = knownProps.get(name);
  206. if (existing) {
  207. if (name === "style" || name === "class" || name.startsWith("on")) mergeAsArray(existing, prop);
  208. } else {
  209. knownProps.set(name, prop);
  210. deduped.push(prop);
  211. }
  212. } else deduped.push(prop);
  213. });
  214. return deduped;
  215. };
  216. /**
  217. * Check if an attribute value is constant
  218. * @param node
  219. * @returns boolean
  220. */
  221. const isConstant = (node) => {
  222. if (__babel_types.isIdentifier(node)) return node.name === "undefined";
  223. if (__babel_types.isArrayExpression(node)) {
  224. const { elements } = node;
  225. return elements.every((element) => element && isConstant(element));
  226. }
  227. if (__babel_types.isObjectExpression(node)) return node.properties.every((property) => isConstant(property.value));
  228. if (__babel_types.isTemplateLiteral(node) ? !node.expressions.length : __babel_types.isLiteral(node)) return true;
  229. return false;
  230. };
  231. const transformJSXSpreadAttribute = (nodePath, path, mergeProps, args) => {
  232. const argument = path.get("argument");
  233. const properties = __babel_types.isObjectExpression(argument.node) ? argument.node.properties : void 0;
  234. if (!properties) {
  235. if (argument.isIdentifier()) walksScope(nodePath, argument.node.name, slotFlags_default.DYNAMIC);
  236. args.push(mergeProps ? argument.node : __babel_types.spreadElement(argument.node));
  237. } else if (mergeProps) args.push(__babel_types.objectExpression(properties));
  238. else args.push(...properties);
  239. };
  240. //#endregion
  241. //#region src/patchFlags.ts
  242. let PatchFlags = /* @__PURE__ */ function(PatchFlags$1) {
  243. PatchFlags$1[PatchFlags$1["TEXT"] = 1] = "TEXT";
  244. PatchFlags$1[PatchFlags$1["CLASS"] = 2] = "CLASS";
  245. PatchFlags$1[PatchFlags$1["STYLE"] = 4] = "STYLE";
  246. PatchFlags$1[PatchFlags$1["PROPS"] = 8] = "PROPS";
  247. PatchFlags$1[PatchFlags$1["FULL_PROPS"] = 16] = "FULL_PROPS";
  248. PatchFlags$1[PatchFlags$1["HYDRATE_EVENTS"] = 32] = "HYDRATE_EVENTS";
  249. PatchFlags$1[PatchFlags$1["STABLE_FRAGMENT"] = 64] = "STABLE_FRAGMENT";
  250. PatchFlags$1[PatchFlags$1["KEYED_FRAGMENT"] = 128] = "KEYED_FRAGMENT";
  251. PatchFlags$1[PatchFlags$1["UNKEYED_FRAGMENT"] = 256] = "UNKEYED_FRAGMENT";
  252. PatchFlags$1[PatchFlags$1["NEED_PATCH"] = 512] = "NEED_PATCH";
  253. PatchFlags$1[PatchFlags$1["DYNAMIC_SLOTS"] = 1024] = "DYNAMIC_SLOTS";
  254. PatchFlags$1[PatchFlags$1["HOISTED"] = -1] = "HOISTED";
  255. PatchFlags$1[PatchFlags$1["BAIL"] = -2] = "BAIL";
  256. return PatchFlags$1;
  257. }({});
  258. const PatchFlagNames = {
  259. [PatchFlags.TEXT]: "TEXT",
  260. [PatchFlags.CLASS]: "CLASS",
  261. [PatchFlags.STYLE]: "STYLE",
  262. [PatchFlags.PROPS]: "PROPS",
  263. [PatchFlags.FULL_PROPS]: "FULL_PROPS",
  264. [PatchFlags.HYDRATE_EVENTS]: "HYDRATE_EVENTS",
  265. [PatchFlags.STABLE_FRAGMENT]: "STABLE_FRAGMENT",
  266. [PatchFlags.KEYED_FRAGMENT]: "KEYED_FRAGMENT",
  267. [PatchFlags.UNKEYED_FRAGMENT]: "UNKEYED_FRAGMENT",
  268. [PatchFlags.DYNAMIC_SLOTS]: "DYNAMIC_SLOTS",
  269. [PatchFlags.NEED_PATCH]: "NEED_PATCH",
  270. [PatchFlags.HOISTED]: "HOISTED",
  271. [PatchFlags.BAIL]: "BAIL"
  272. };
  273. //#endregion
  274. //#region src/parseDirectives.ts
  275. /**
  276. * Get JSX element type
  277. *
  278. * @param path Path<JSXOpeningElement>
  279. */
  280. const getType = (path) => {
  281. const typePath = path.get("attributes").find((attribute) => {
  282. if (!attribute.isJSXAttribute()) return false;
  283. return attribute.get("name").isJSXIdentifier() && attribute.get("name").node.name === "type";
  284. });
  285. return typePath ? typePath.get("value").node : null;
  286. };
  287. const parseModifiers = (value) => __babel_types.isArrayExpression(value) ? value.elements.map((el) => __babel_types.isStringLiteral(el) ? el.value : "").filter(Boolean) : [];
  288. const parseDirectives = (params) => {
  289. var _modifiersSet$, _modifiersSet$2;
  290. const { path, value, state, tag, isComponent } = params;
  291. const args = [];
  292. const vals = [];
  293. const modifiersSet = [];
  294. let directiveName;
  295. let directiveArgument;
  296. let directiveModifiers;
  297. if ("namespace" in path.node.name) {
  298. [directiveName, directiveArgument] = params.name.split(":");
  299. directiveName = path.node.name.namespace.name;
  300. directiveArgument = path.node.name.name.name;
  301. directiveModifiers = directiveArgument.split("_").slice(1);
  302. } else {
  303. const underscoreModifiers = params.name.split("_");
  304. directiveName = underscoreModifiers.shift() || "";
  305. directiveModifiers = underscoreModifiers;
  306. }
  307. directiveName = directiveName.replace(/^v/, "").replace(/^-/, "").replace(/^\S/, (s) => s.toLowerCase());
  308. if (directiveArgument) args.push(__babel_types.stringLiteral(directiveArgument.split("_")[0]));
  309. const isVModels = directiveName === "models";
  310. const isVModel = directiveName === "model";
  311. if (isVModel && !path.get("value").isJSXExpressionContainer()) throw new Error("You have to use JSX Expression inside your v-model");
  312. if (isVModels && !isComponent) throw new Error("v-models can only use in custom components");
  313. const shouldResolve = ![
  314. "html",
  315. "text",
  316. "model",
  317. "slots",
  318. "models"
  319. ].includes(directiveName) || isVModel && !isComponent;
  320. let modifiers = directiveModifiers;
  321. if (__babel_types.isArrayExpression(value)) {
  322. const elementsList = isVModels ? value.elements : [value];
  323. elementsList.forEach((element) => {
  324. if (isVModels && !__babel_types.isArrayExpression(element)) throw new Error("You should pass a Two-dimensional Arrays to v-models");
  325. const { elements } = element;
  326. const [first, second, third] = elements;
  327. if (second && !__babel_types.isArrayExpression(second) && !__babel_types.isSpreadElement(second)) {
  328. args.push(second);
  329. modifiers = parseModifiers(third);
  330. } else if (__babel_types.isArrayExpression(second)) {
  331. if (!shouldResolve) args.push(__babel_types.nullLiteral());
  332. modifiers = parseModifiers(second);
  333. } else if (!shouldResolve) args.push(__babel_types.nullLiteral());
  334. modifiersSet.push(new Set(modifiers));
  335. vals.push(first);
  336. });
  337. } else if (isVModel && !shouldResolve) {
  338. args.push(__babel_types.nullLiteral());
  339. modifiersSet.push(new Set(directiveModifiers));
  340. } else modifiersSet.push(new Set(directiveModifiers));
  341. return {
  342. directiveName,
  343. modifiers: modifiersSet,
  344. values: vals.length ? vals : [value],
  345. args,
  346. directive: shouldResolve ? [
  347. resolveDirective(path, state, tag, directiveName),
  348. vals[0] || value,
  349. ((_modifiersSet$ = modifiersSet[0]) === null || _modifiersSet$ === void 0 ? void 0 : _modifiersSet$.size) ? args[0] || __babel_types.unaryExpression("void", __babel_types.numericLiteral(0), true) : args[0],
  350. !!((_modifiersSet$2 = modifiersSet[0]) === null || _modifiersSet$2 === void 0 ? void 0 : _modifiersSet$2.size) && __babel_types.objectExpression([...modifiersSet[0]].map((modifier) => __babel_types.objectProperty(__babel_types.identifier(modifier), __babel_types.booleanLiteral(true))))
  351. ].filter(Boolean) : void 0
  352. };
  353. };
  354. const resolveDirective = (path, state, tag, directiveName) => {
  355. if (directiveName === "show") return createIdentifier(state, "vShow");
  356. if (directiveName === "model") {
  357. let modelToUse;
  358. const type = getType(path.parentPath);
  359. switch (tag.value) {
  360. case "select":
  361. modelToUse = createIdentifier(state, "vModelSelect");
  362. break;
  363. case "textarea":
  364. modelToUse = createIdentifier(state, "vModelText");
  365. break;
  366. default: if (__babel_types.isStringLiteral(type) || !type) switch (type === null || type === void 0 ? void 0 : type.value) {
  367. case "checkbox":
  368. modelToUse = createIdentifier(state, "vModelCheckbox");
  369. break;
  370. case "radio":
  371. modelToUse = createIdentifier(state, "vModelRadio");
  372. break;
  373. default: modelToUse = createIdentifier(state, "vModelText");
  374. }
  375. else modelToUse = createIdentifier(state, "vModelDynamic");
  376. }
  377. return modelToUse;
  378. }
  379. const referenceName = "v" + directiveName[0].toUpperCase() + directiveName.slice(1);
  380. if (path.scope.references[referenceName]) return __babel_types.identifier(referenceName);
  381. return __babel_types.callExpression(createIdentifier(state, "resolveDirective"), [__babel_types.stringLiteral(directiveName)]);
  382. };
  383. var parseDirectives_default = parseDirectives;
  384. //#endregion
  385. //#region src/transform-vue-jsx.ts
  386. const xlinkRE = new RegExp("^xlink([A-Z])", "");
  387. const getJSXAttributeValue = (path, state) => {
  388. const valuePath = path.get("value");
  389. if (valuePath.isJSXElement()) return transformJSXElement(valuePath, state);
  390. if (valuePath.isStringLiteral()) return __babel_types.stringLiteral(transformText(valuePath.node.value));
  391. if (valuePath.isJSXExpressionContainer()) return transformJSXExpressionContainer(valuePath);
  392. return null;
  393. };
  394. const buildProps = (path, state) => {
  395. const tag = getTag(path, state);
  396. const isComponent = checkIsComponent(path.get("openingElement"), state);
  397. const props = path.get("openingElement").get("attributes");
  398. const directives = [];
  399. const dynamicPropNames = /* @__PURE__ */ new Set();
  400. let slots = null;
  401. let patchFlag = 0;
  402. if (props.length === 0) return {
  403. tag,
  404. isComponent,
  405. slots,
  406. props: __babel_types.nullLiteral(),
  407. directives,
  408. patchFlag,
  409. dynamicPropNames
  410. };
  411. let properties = [];
  412. let hasRef = false;
  413. let hasClassBinding = false;
  414. let hasStyleBinding = false;
  415. let hasHydrationEventBinding = false;
  416. let hasDynamicKeys = false;
  417. const mergeArgs = [];
  418. const { mergeProps = true } = state.opts;
  419. props.forEach((prop) => {
  420. if (prop.isJSXAttribute()) {
  421. let name = getJSXAttributeName(prop);
  422. const attributeValue = getJSXAttributeValue(prop, state);
  423. if (!isConstant(attributeValue) || name === "ref") {
  424. if (!isComponent && isOn(name) && name.toLowerCase() !== "onclick" && name !== "onUpdate:modelValue") hasHydrationEventBinding = true;
  425. if (name === "ref") hasRef = true;
  426. else if (name === "class" && !isComponent) hasClassBinding = true;
  427. else if (name === "style" && !isComponent) hasStyleBinding = true;
  428. else if (name !== "key" && !isDirective(name) && name !== "on") dynamicPropNames.add(name);
  429. }
  430. if (state.opts.transformOn && (name === "on" || name === "nativeOn")) {
  431. if (!state.get("transformOn")) state.set("transformOn", (0, __babel_helper_module_imports.addDefault)(path, "@vue/babel-helper-vue-transform-on", { nameHint: "_transformOn" }));
  432. mergeArgs.push(__babel_types.callExpression(state.get("transformOn"), [attributeValue || __babel_types.booleanLiteral(true)]));
  433. return;
  434. }
  435. if (isDirective(name)) {
  436. const { directive, modifiers, values, args, directiveName } = parseDirectives_default({
  437. tag,
  438. isComponent,
  439. name,
  440. path: prop,
  441. state,
  442. value: attributeValue
  443. });
  444. if (directiveName === "slots") {
  445. slots = attributeValue;
  446. return;
  447. }
  448. if (directive) directives.push(__babel_types.arrayExpression(directive));
  449. else if (directiveName === "html") {
  450. properties.push(__babel_types.objectProperty(__babel_types.stringLiteral("innerHTML"), values[0]));
  451. dynamicPropNames.add("innerHTML");
  452. } else if (directiveName === "text") {
  453. properties.push(__babel_types.objectProperty(__babel_types.stringLiteral("textContent"), values[0]));
  454. dynamicPropNames.add("textContent");
  455. }
  456. if (["models", "model"].includes(directiveName)) values.forEach((value, index) => {
  457. const propName = args[index];
  458. const isDynamic = propName && !__babel_types.isStringLiteral(propName) && !__babel_types.isNullLiteral(propName);
  459. if (!directive) {
  460. var _modifiers$index;
  461. properties.push(__babel_types.objectProperty(__babel_types.isNullLiteral(propName) ? __babel_types.stringLiteral("modelValue") : propName, value, isDynamic));
  462. if (!isDynamic) dynamicPropNames.add((propName === null || propName === void 0 ? void 0 : propName.value) || "modelValue");
  463. if ((_modifiers$index = modifiers[index]) === null || _modifiers$index === void 0 ? void 0 : _modifiers$index.size) properties.push(__babel_types.objectProperty(isDynamic ? __babel_types.binaryExpression("+", propName, __babel_types.stringLiteral("Modifiers")) : __babel_types.stringLiteral(`${(propName === null || propName === void 0 ? void 0 : propName.value) || "model"}Modifiers`), __babel_types.objectExpression([...modifiers[index]].map((modifier) => __babel_types.objectProperty(__babel_types.stringLiteral(modifier), __babel_types.booleanLiteral(true)))), isDynamic));
  464. }
  465. const updateName = isDynamic ? __babel_types.binaryExpression("+", __babel_types.stringLiteral("onUpdate:"), propName) : __babel_types.stringLiteral(`onUpdate:${(propName === null || propName === void 0 ? void 0 : propName.value) || "modelValue"}`);
  466. properties.push(__babel_types.objectProperty(updateName, __babel_types.arrowFunctionExpression([__babel_types.identifier("$event")], __babel_types.assignmentExpression("=", value, __babel_types.identifier("$event"))), isDynamic));
  467. if (!isDynamic) dynamicPropNames.add(updateName.value);
  468. else hasDynamicKeys = true;
  469. });
  470. } else {
  471. if (name.match(xlinkRE)) name = name.replace(xlinkRE, (_, firstCharacter) => `xlink:${firstCharacter.toLowerCase()}`);
  472. properties.push(__babel_types.objectProperty(__babel_types.stringLiteral(name), attributeValue || __babel_types.booleanLiteral(true)));
  473. }
  474. } else {
  475. if (properties.length && mergeProps) {
  476. mergeArgs.push(__babel_types.objectExpression(dedupeProperties(properties, mergeProps)));
  477. properties = [];
  478. }
  479. hasDynamicKeys = true;
  480. transformJSXSpreadAttribute(path, prop, mergeProps, mergeProps ? mergeArgs : properties);
  481. }
  482. });
  483. if (hasDynamicKeys) patchFlag |= PatchFlags.FULL_PROPS;
  484. else {
  485. if (hasClassBinding) patchFlag |= PatchFlags.CLASS;
  486. if (hasStyleBinding) patchFlag |= PatchFlags.STYLE;
  487. if (dynamicPropNames.size) patchFlag |= PatchFlags.PROPS;
  488. if (hasHydrationEventBinding) patchFlag |= PatchFlags.HYDRATE_EVENTS;
  489. }
  490. if ((patchFlag === 0 || patchFlag === PatchFlags.HYDRATE_EVENTS) && (hasRef || directives.length > 0)) patchFlag |= PatchFlags.NEED_PATCH;
  491. let propsExpression = __babel_types.nullLiteral();
  492. if (mergeArgs.length) {
  493. if (properties.length) mergeArgs.push(__babel_types.objectExpression(dedupeProperties(properties, mergeProps)));
  494. if (mergeArgs.length > 1) propsExpression = __babel_types.callExpression(createIdentifier(state, "mergeProps"), mergeArgs);
  495. else propsExpression = mergeArgs[0];
  496. } else if (properties.length) if (properties.length === 1 && __babel_types.isSpreadElement(properties[0])) propsExpression = properties[0].argument;
  497. else propsExpression = __babel_types.objectExpression(dedupeProperties(properties, mergeProps));
  498. return {
  499. tag,
  500. props: propsExpression,
  501. isComponent,
  502. slots,
  503. directives,
  504. patchFlag,
  505. dynamicPropNames
  506. };
  507. };
  508. /**
  509. * Get children from Array of JSX children
  510. * @param paths Array<JSXText | JSXExpressionContainer | JSXElement | JSXFragment>
  511. * @returns Array<Expression | SpreadElement>
  512. */
  513. const getChildren = (paths, state) => paths.map((path) => {
  514. if (path.isJSXText()) {
  515. const transformedText = transformJSXText(path);
  516. if (transformedText) return __babel_types.callExpression(createIdentifier(state, "createTextVNode"), [transformedText]);
  517. return transformedText;
  518. }
  519. if (path.isJSXExpressionContainer()) {
  520. const expression = transformJSXExpressionContainer(path);
  521. if (__babel_types.isIdentifier(expression)) {
  522. const { name } = expression;
  523. const { referencePaths = [] } = path.scope.getBinding(name) || {};
  524. referencePaths.forEach((referencePath) => {
  525. walksScope(referencePath, name, slotFlags_default.DYNAMIC);
  526. });
  527. }
  528. return expression;
  529. }
  530. if (path.isJSXSpreadChild()) return transformJSXSpreadChild(path);
  531. if (path.isCallExpression()) return path.node;
  532. if (path.isJSXElement()) return transformJSXElement(path, state);
  533. throw new Error(`getChildren: ${path.type} is not supported`);
  534. }).filter(((value) => value != null && !__babel_types.isJSXEmptyExpression(value)));
  535. const transformJSXElement = (path, state) => {
  536. var _path$getData;
  537. const children = getChildren(path.get("children"), state);
  538. const { tag, props, isComponent, directives, patchFlag, dynamicPropNames, slots } = buildProps(path, state);
  539. const { optimize = false } = state.opts;
  540. if (directives.length && directives.some((d) => {
  541. var _d$elements;
  542. return ((_d$elements = d.elements) === null || _d$elements === void 0 || (_d$elements = _d$elements[0]) === null || _d$elements === void 0 ? void 0 : _d$elements.type) === "CallExpression" && d.elements[0].callee.type === "Identifier" && d.elements[0].callee.name === "_resolveDirective";
  543. })) {
  544. var _currentPath$parentPa;
  545. let currentPath = path;
  546. while ((_currentPath$parentPa = currentPath.parentPath) === null || _currentPath$parentPa === void 0 ? void 0 : _currentPath$parentPa.isJSXElement()) {
  547. currentPath = currentPath.parentPath;
  548. currentPath.setData("slotFlag", 0);
  549. }
  550. }
  551. const slotFlag = (_path$getData = path.getData("slotFlag")) !== null && _path$getData !== void 0 ? _path$getData : slotFlags_default.STABLE;
  552. const optimizeSlots = optimize && slotFlag !== 0;
  553. let VNodeChild;
  554. if (children.length > 1 || slots) VNodeChild = isComponent ? children.length ? __babel_types.objectExpression([
  555. !!children.length && __babel_types.objectProperty(__babel_types.identifier("default"), __babel_types.arrowFunctionExpression([], __babel_types.arrayExpression(buildIIFE(path, children)))),
  556. ...slots ? __babel_types.isObjectExpression(slots) ? slots.properties : [__babel_types.spreadElement(slots)] : [],
  557. optimizeSlots && __babel_types.objectProperty(__babel_types.identifier("_"), __babel_types.numericLiteral(slotFlag))
  558. ].filter(Boolean)) : slots : __babel_types.arrayExpression(children);
  559. else if (children.length === 1) {
  560. const { enableObjectSlots = true } = state.opts;
  561. const child = children[0];
  562. const objectExpression = __babel_types.objectExpression([__babel_types.objectProperty(__babel_types.identifier("default"), __babel_types.arrowFunctionExpression([], __babel_types.arrayExpression(buildIIFE(path, [child])))), optimizeSlots && __babel_types.objectProperty(__babel_types.identifier("_"), __babel_types.numericLiteral(slotFlag))].filter(Boolean));
  563. if (__babel_types.isIdentifier(child) && isComponent) VNodeChild = enableObjectSlots ? __babel_types.conditionalExpression(__babel_types.callExpression(state.get("@vue/babel-plugin-jsx/runtimeIsSlot")(), [child]), child, objectExpression) : objectExpression;
  564. else if (__babel_types.isCallExpression(child) && child.loc && isComponent) if (enableObjectSlots) {
  565. const { scope } = path;
  566. const slotId = scope.generateUidIdentifier("slot");
  567. if (scope) scope.push({
  568. id: slotId,
  569. kind: "let"
  570. });
  571. const alternate = __babel_types.objectExpression([__babel_types.objectProperty(__babel_types.identifier("default"), __babel_types.arrowFunctionExpression([], __babel_types.arrayExpression(buildIIFE(path, [slotId])))), optimizeSlots && __babel_types.objectProperty(__babel_types.identifier("_"), __babel_types.numericLiteral(slotFlag))].filter(Boolean));
  572. const assignment = __babel_types.assignmentExpression("=", slotId, child);
  573. const condition = __babel_types.callExpression(state.get("@vue/babel-plugin-jsx/runtimeIsSlot")(), [assignment]);
  574. VNodeChild = __babel_types.conditionalExpression(condition, slotId, alternate);
  575. } else VNodeChild = objectExpression;
  576. else if (__babel_types.isFunctionExpression(child) || __babel_types.isArrowFunctionExpression(child)) VNodeChild = __babel_types.objectExpression([__babel_types.objectProperty(__babel_types.identifier("default"), child)]);
  577. else if (__babel_types.isObjectExpression(child)) VNodeChild = __babel_types.objectExpression([...child.properties, optimizeSlots && __babel_types.objectProperty(__babel_types.identifier("_"), __babel_types.numericLiteral(slotFlag))].filter(Boolean));
  578. else VNodeChild = isComponent ? __babel_types.objectExpression([__babel_types.objectProperty(__babel_types.identifier("default"), __babel_types.arrowFunctionExpression([], __babel_types.arrayExpression([child])))]) : __babel_types.arrayExpression([child]);
  579. }
  580. const createVNode = __babel_types.callExpression(createIdentifier(state, "createVNode"), [
  581. tag,
  582. props,
  583. VNodeChild || __babel_types.nullLiteral(),
  584. !!patchFlag && optimize && __babel_types.numericLiteral(patchFlag),
  585. !!dynamicPropNames.size && optimize && __babel_types.arrayExpression([...dynamicPropNames.keys()].map((name) => __babel_types.stringLiteral(name)))
  586. ].filter(Boolean));
  587. if (!directives.length) return createVNode;
  588. return __babel_types.callExpression(createIdentifier(state, "withDirectives"), [createVNode, __babel_types.arrayExpression(directives)]);
  589. };
  590. const visitor$1 = { JSXElement: { exit(path, state) {
  591. path.replaceWith(transformJSXElement(path, state));
  592. } } };
  593. var transform_vue_jsx_default = visitor$1;
  594. //#endregion
  595. //#region src/sugar-fragment.ts
  596. const transformFragment = (path, Fragment) => {
  597. const children = path.get("children") || [];
  598. return __babel_types.jsxElement(__babel_types.jsxOpeningElement(Fragment, []), __babel_types.jsxClosingElement(Fragment), children.map(({ node }) => node), false);
  599. };
  600. const visitor = { JSXFragment: { enter(path, state) {
  601. const fragmentCallee = createIdentifier(state, FRAGMENT);
  602. path.replaceWith(transformFragment(path, __babel_types.isIdentifier(fragmentCallee) ? __babel_types.jsxIdentifier(fragmentCallee.name) : __babel_types.jsxMemberExpression(__babel_types.jsxIdentifier(fragmentCallee.object.name), __babel_types.jsxIdentifier(fragmentCallee.property.name))));
  603. } } };
  604. var sugar_fragment_default = visitor;
  605. //#endregion
  606. //#region ../../node_modules/.pnpm/@oxc-project+runtime@0.80.0/node_modules/@oxc-project/runtime/src/helpers/typeof.js
  607. var require_typeof = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.80.0/node_modules/@oxc-project/runtime/src/helpers/typeof.js": ((exports, module) => {
  608. function _typeof$2(o) {
  609. "@babel/helpers - typeof";
  610. return module.exports = _typeof$2 = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o$1) {
  611. return typeof o$1;
  612. } : function(o$1) {
  613. return o$1 && "function" == typeof Symbol && o$1.constructor === Symbol && o$1 !== Symbol.prototype ? "symbol" : typeof o$1;
  614. }, module.exports.__esModule = true, module.exports["default"] = module.exports, _typeof$2(o);
  615. }
  616. module.exports = _typeof$2, module.exports.__esModule = true, module.exports["default"] = module.exports;
  617. }) });
  618. //#endregion
  619. //#region ../../node_modules/.pnpm/@oxc-project+runtime@0.80.0/node_modules/@oxc-project/runtime/src/helpers/toPrimitive.js
  620. var require_toPrimitive = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.80.0/node_modules/@oxc-project/runtime/src/helpers/toPrimitive.js": ((exports, module) => {
  621. var _typeof$1 = require_typeof()["default"];
  622. function toPrimitive$1(t, r) {
  623. if ("object" != _typeof$1(t) || !t) return t;
  624. var e = t[Symbol.toPrimitive];
  625. if (void 0 !== e) {
  626. var i = e.call(t, r || "default");
  627. if ("object" != _typeof$1(i)) return i;
  628. throw new TypeError("@@toPrimitive must return a primitive value.");
  629. }
  630. return ("string" === r ? String : Number)(t);
  631. }
  632. module.exports = toPrimitive$1, module.exports.__esModule = true, module.exports["default"] = module.exports;
  633. }) });
  634. //#endregion
  635. //#region ../../node_modules/.pnpm/@oxc-project+runtime@0.80.0/node_modules/@oxc-project/runtime/src/helpers/toPropertyKey.js
  636. var require_toPropertyKey = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.80.0/node_modules/@oxc-project/runtime/src/helpers/toPropertyKey.js": ((exports, module) => {
  637. var _typeof = require_typeof()["default"];
  638. var toPrimitive = require_toPrimitive();
  639. function toPropertyKey$1(t) {
  640. var i = toPrimitive(t, "string");
  641. return "symbol" == _typeof(i) ? i : i + "";
  642. }
  643. module.exports = toPropertyKey$1, module.exports.__esModule = true, module.exports["default"] = module.exports;
  644. }) });
  645. //#endregion
  646. //#region ../../node_modules/.pnpm/@oxc-project+runtime@0.80.0/node_modules/@oxc-project/runtime/src/helpers/defineProperty.js
  647. var require_defineProperty = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.80.0/node_modules/@oxc-project/runtime/src/helpers/defineProperty.js": ((exports, module) => {
  648. var toPropertyKey = require_toPropertyKey();
  649. function _defineProperty(e, r, t) {
  650. return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
  651. value: t,
  652. enumerable: !0,
  653. configurable: !0,
  654. writable: !0
  655. }) : e[r] = t, e;
  656. }
  657. module.exports = _defineProperty, module.exports.__esModule = true, module.exports["default"] = module.exports;
  658. }) });
  659. //#endregion
  660. //#region ../../node_modules/.pnpm/@oxc-project+runtime@0.80.0/node_modules/@oxc-project/runtime/src/helpers/objectSpread2.js
  661. var require_objectSpread2 = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.80.0/node_modules/@oxc-project/runtime/src/helpers/objectSpread2.js": ((exports, module) => {
  662. var defineProperty = require_defineProperty();
  663. function ownKeys(e, r) {
  664. var t = Object.keys(e);
  665. if (Object.getOwnPropertySymbols) {
  666. var o = Object.getOwnPropertySymbols(e);
  667. r && (o = o.filter(function(r$1) {
  668. return Object.getOwnPropertyDescriptor(e, r$1).enumerable;
  669. })), t.push.apply(t, o);
  670. }
  671. return t;
  672. }
  673. function _objectSpread2(e) {
  674. for (var r = 1; r < arguments.length; r++) {
  675. var t = null != arguments[r] ? arguments[r] : {};
  676. r % 2 ? ownKeys(Object(t), !0).forEach(function(r$1) {
  677. defineProperty(e, r$1, t[r$1]);
  678. }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r$1) {
  679. Object.defineProperty(e, r$1, Object.getOwnPropertyDescriptor(t, r$1));
  680. });
  681. }
  682. return e;
  683. }
  684. module.exports = _objectSpread2, module.exports.__esModule = true, module.exports["default"] = module.exports;
  685. }) });
  686. //#endregion
  687. //#region src/index.ts
  688. var import_objectSpread2 = /* @__PURE__ */ __toESM(require_objectSpread2());
  689. const hasJSX = (parentPath) => {
  690. let fileHasJSX = false;
  691. parentPath.traverse({
  692. JSXElement(path) {
  693. fileHasJSX = true;
  694. path.stop();
  695. },
  696. JSXFragment(path) {
  697. fileHasJSX = true;
  698. path.stop();
  699. }
  700. });
  701. return fileHasJSX;
  702. };
  703. const JSX_ANNOTATION_REGEX = new RegExp("\\*?\\s*@jsx\\s+([^\\s]+)", "");
  704. /* @__NO_SIDE_EFFECTS__ */
  705. function interopDefault(m) {
  706. return m.default || m;
  707. }
  708. const syntaxJsx = /* @__PURE__ */ interopDefault(__babel_plugin_syntax_jsx.default);
  709. const template = /* @__PURE__ */ interopDefault(__babel_template.default);
  710. const plugin = (0, __babel_helper_plugin_utils.declare)((api, opt, dirname) => {
  711. const { types } = api;
  712. let resolveType;
  713. if (opt.resolveType) {
  714. if (typeof opt.resolveType === "boolean") opt.resolveType = {};
  715. resolveType = (0, __vue_babel_plugin_resolve_type.default)(api, opt.resolveType, dirname);
  716. }
  717. return (0, import_objectSpread2.default)((0, import_objectSpread2.default)({}, resolveType || {}), {}, {
  718. name: "babel-plugin-jsx",
  719. inherits: /* @__PURE__ */ interopDefault(syntaxJsx),
  720. visitor: (0, import_objectSpread2.default)((0, import_objectSpread2.default)((0, import_objectSpread2.default)((0, import_objectSpread2.default)({}, resolveType === null || resolveType === void 0 ? void 0 : resolveType.visitor), transform_vue_jsx_default), sugar_fragment_default), {}, { Program: { enter(path, state) {
  721. if (hasJSX(path)) {
  722. const importNames = [
  723. "createVNode",
  724. "Fragment",
  725. "resolveComponent",
  726. "withDirectives",
  727. "vShow",
  728. "vModelSelect",
  729. "vModelText",
  730. "vModelCheckbox",
  731. "vModelRadio",
  732. "vModelText",
  733. "vModelDynamic",
  734. "resolveDirective",
  735. "mergeProps",
  736. "createTextVNode",
  737. "isVNode"
  738. ];
  739. if ((0, __babel_helper_module_imports.isModule)(path)) {
  740. const importMap = {};
  741. importNames.forEach((name) => {
  742. state.set(name, () => {
  743. if (importMap[name]) return types.cloneNode(importMap[name]);
  744. const identifier = (0, __babel_helper_module_imports.addNamed)(path, name, "vue", { ensureLiveReference: true });
  745. importMap[name] = identifier;
  746. return identifier;
  747. });
  748. });
  749. const { enableObjectSlots = true } = state.opts;
  750. if (enableObjectSlots) state.set("@vue/babel-plugin-jsx/runtimeIsSlot", () => {
  751. if (importMap.runtimeIsSlot) return importMap.runtimeIsSlot;
  752. const { name: isVNodeName } = state.get("isVNode")();
  753. const isSlot = path.scope.generateUidIdentifier("isSlot");
  754. const ast = template.ast`
  755. function ${isSlot.name}(s) {
  756. return typeof s === 'function' || (Object.prototype.toString.call(s) === '[object Object]' && !${isVNodeName}(s));
  757. }
  758. `;
  759. const lastImport = path.get("body").filter((p) => p.isImportDeclaration()).pop();
  760. if (lastImport) lastImport.insertAfter(ast);
  761. importMap.runtimeIsSlot = isSlot;
  762. return isSlot;
  763. });
  764. } else {
  765. let sourceName;
  766. importNames.forEach((name) => {
  767. state.set(name, () => {
  768. if (!sourceName) sourceName = (0, __babel_helper_module_imports.addNamespace)(path, "vue", { ensureLiveReference: true });
  769. return __babel_types.memberExpression(sourceName, __babel_types.identifier(name));
  770. });
  771. });
  772. const helpers = {};
  773. const { enableObjectSlots = true } = state.opts;
  774. if (enableObjectSlots) state.set("@vue/babel-plugin-jsx/runtimeIsSlot", () => {
  775. if (helpers.runtimeIsSlot) return helpers.runtimeIsSlot;
  776. const isSlot = path.scope.generateUidIdentifier("isSlot");
  777. const { object: objectName } = state.get("isVNode")();
  778. const ast = template.ast`
  779. function ${isSlot.name}(s) {
  780. return typeof s === 'function' || (Object.prototype.toString.call(s) === '[object Object]' && !${objectName.name}.isVNode(s));
  781. }
  782. `;
  783. const nodePaths = path.get("body");
  784. const lastImport = nodePaths.filter((p) => p.isVariableDeclaration() && p.node.declarations.some((d) => {
  785. var _d$id;
  786. return ((_d$id = d.id) === null || _d$id === void 0 ? void 0 : _d$id.name) === sourceName.name;
  787. })).pop();
  788. if (lastImport) lastImport.insertAfter(ast);
  789. return isSlot;
  790. });
  791. }
  792. const { opts: { pragma = "" }, file } = state;
  793. if (pragma) state.set("createVNode", () => __babel_types.identifier(pragma));
  794. if (file.ast.comments) for (const comment of file.ast.comments) {
  795. const jsxMatches = JSX_ANNOTATION_REGEX.exec(comment.value);
  796. if (jsxMatches) state.set("createVNode", () => __babel_types.identifier(jsxMatches[1]));
  797. }
  798. }
  799. } } })
  800. });
  801. });
  802. var src_default = plugin;
  803. //#endregion
  804. module.exports = src_default;