vnode.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  2. const require_runtime = require('../../_virtual/_rolldown/runtime.js');
  3. const require_error = require('../error.js');
  4. let vue = require("vue");
  5. let _vue_shared = require("@vue/shared");
  6. //#region ../../packages/utils/vue/vnode.ts
  7. const SCOPE = "utils/vue/vnode";
  8. let PatchFlags = /* @__PURE__ */ function(PatchFlags) {
  9. PatchFlags[PatchFlags["TEXT"] = 1] = "TEXT";
  10. PatchFlags[PatchFlags["CLASS"] = 2] = "CLASS";
  11. PatchFlags[PatchFlags["STYLE"] = 4] = "STYLE";
  12. PatchFlags[PatchFlags["PROPS"] = 8] = "PROPS";
  13. PatchFlags[PatchFlags["FULL_PROPS"] = 16] = "FULL_PROPS";
  14. PatchFlags[PatchFlags["HYDRATE_EVENTS"] = 32] = "HYDRATE_EVENTS";
  15. PatchFlags[PatchFlags["STABLE_FRAGMENT"] = 64] = "STABLE_FRAGMENT";
  16. PatchFlags[PatchFlags["KEYED_FRAGMENT"] = 128] = "KEYED_FRAGMENT";
  17. PatchFlags[PatchFlags["UNKEYED_FRAGMENT"] = 256] = "UNKEYED_FRAGMENT";
  18. PatchFlags[PatchFlags["NEED_PATCH"] = 512] = "NEED_PATCH";
  19. PatchFlags[PatchFlags["DYNAMIC_SLOTS"] = 1024] = "DYNAMIC_SLOTS";
  20. PatchFlags[PatchFlags["HOISTED"] = -1] = "HOISTED";
  21. PatchFlags[PatchFlags["BAIL"] = -2] = "BAIL";
  22. return PatchFlags;
  23. }({});
  24. function isFragment(node) {
  25. return (0, vue.isVNode)(node) && node.type === vue.Fragment;
  26. }
  27. function isText(node) {
  28. return (0, vue.isVNode)(node) && node.type === vue.Text;
  29. }
  30. function isComment(node) {
  31. return (0, vue.isVNode)(node) && node.type === vue.Comment;
  32. }
  33. const TEMPLATE = "template";
  34. function isTemplate(node) {
  35. return (0, vue.isVNode)(node) && node.type === TEMPLATE;
  36. }
  37. function isValidElementNode(node) {
  38. return (0, vue.isVNode)(node) && !isFragment(node) && !isComment(node);
  39. }
  40. /**
  41. * get a valid child node (not fragment nor comment)
  42. * @param node {VNode} node to be searched
  43. * @param depth {number} depth to be searched
  44. */
  45. function getChildren(node, depth) {
  46. if (isComment(node)) return;
  47. if (isFragment(node) || isTemplate(node)) return depth > 0 ? getFirstValidNode(node.children, depth - 1) : void 0;
  48. return node;
  49. }
  50. const getFirstValidNode = (nodes, maxDepth = 3) => {
  51. if ((0, _vue_shared.isArray)(nodes)) return getChildren(nodes[0], maxDepth);
  52. else return getChildren(nodes, maxDepth);
  53. };
  54. function renderIf(condition, ...args) {
  55. return condition ? renderBlock(...args) : (0, vue.createCommentVNode)("v-if", true);
  56. }
  57. function renderBlock(...args) {
  58. return (0, vue.openBlock)(), (0, vue.createBlock)(...args);
  59. }
  60. const getNormalizedProps = (node) => {
  61. if (!(0, vue.isVNode)(node)) {
  62. require_error.debugWarn(SCOPE, "[getNormalizedProps] must be a VNode");
  63. return {};
  64. }
  65. const raw = node.props || {};
  66. const type = ((0, vue.isVNode)(node.type) ? node.type.props : void 0) || {};
  67. const props = {};
  68. Object.keys(type).forEach((key) => {
  69. if ((0, _vue_shared.hasOwn)(type[key], "default")) props[key] = type[key].default;
  70. });
  71. Object.keys(raw).forEach((key) => {
  72. props[(0, _vue_shared.camelize)(key)] = raw[key];
  73. });
  74. return props;
  75. };
  76. const flattedChildren = (children) => {
  77. const vNodes = (0, _vue_shared.isArray)(children) ? children : [children];
  78. const result = [];
  79. vNodes.forEach((child) => {
  80. if ((0, _vue_shared.isArray)(child)) result.push(...flattedChildren(child));
  81. else if ((0, vue.isVNode)(child) && child.component?.subTree) result.push(child, ...flattedChildren(child.component.subTree));
  82. else if ((0, vue.isVNode)(child) && (0, _vue_shared.isArray)(child.children)) result.push(...flattedChildren(child.children));
  83. else if ((0, vue.isVNode)(child) && child.shapeFlag === 2) result.push(...flattedChildren(child.type()));
  84. else result.push(child);
  85. });
  86. return result;
  87. };
  88. //#endregion
  89. exports.PatchFlags = PatchFlags;
  90. exports.flattedChildren = flattedChildren;
  91. exports.getFirstValidNode = getFirstValidNode;
  92. exports.getNormalizedProps = getNormalizedProps;
  93. exports.isComment = isComment;
  94. exports.isFragment = isFragment;
  95. exports.isTemplate = isTemplate;
  96. exports.isText = isText;
  97. exports.isValidElementNode = isValidElementNode;
  98. exports.renderBlock = renderBlock;
  99. exports.renderIf = renderIf;
  100. //# sourceMappingURL=vnode.js.map