transform-vue-jsx.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. var __importDefault = (this && this.__importDefault) || function (mod) {
  22. return (mod && mod.__esModule) ? mod : { "default": mod };
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. exports.transformJSXElement = void 0;
  26. const t = __importStar(require("@babel/types"));
  27. const utils_1 = require("./utils");
  28. const buildProps_1 = __importDefault(require("./buildProps"));
  29. /**
  30. * Get children from Array of JSX children
  31. * @param paths Array<JSXText | JSXExpressionContainer | JSXElement | JSXFragment>
  32. * @returns Array<Expression | SpreadElement>
  33. */
  34. const getChildren = (paths, state) => paths
  35. .map((path) => {
  36. if (path.isJSXText()) {
  37. const transformedText = utils_1.transformJSXText(path);
  38. if (transformedText) {
  39. return t.callExpression(utils_1.createIdentifier(state, 'createTextVNode'), [transformedText]);
  40. }
  41. return transformedText;
  42. }
  43. if (path.isJSXExpressionContainer()) {
  44. const expression = utils_1.transformJSXExpressionContainer(path);
  45. if (t.isIdentifier(expression)) {
  46. const { name } = expression;
  47. const { referencePaths = [] } = path.scope.getBinding(name) || {};
  48. referencePaths.forEach((referencePath) => {
  49. utils_1.walksScope(referencePath, name, 2 /* DYNAMIC */);
  50. });
  51. }
  52. return expression;
  53. }
  54. if (t.isJSXSpreadChild(path)) {
  55. return utils_1.transformJSXSpreadChild(path);
  56. }
  57. if (path.isCallExpression()) {
  58. return path.node;
  59. }
  60. if (path.isJSXElement()) {
  61. return transformJSXElement(path, state);
  62. }
  63. throw new Error(`getChildren: ${path.type} is not supported`);
  64. }).filter(((value) => (value !== undefined
  65. && value !== null
  66. && !t.isJSXEmptyExpression(value))));
  67. const transformJSXElement = (path, state) => {
  68. const children = getChildren(path.get('children'), state);
  69. const { tag, props, isComponent, directives, patchFlag, dynamicPropNames, slots, } = buildProps_1.default(path, state);
  70. const { optimize = false } = state.opts;
  71. const slotFlag = path.getData('slotFlag') || 1 /* STABLE */;
  72. // @ts-ignore
  73. const createVNode = t.callExpression(utils_1.createIdentifier(state, 'createVNode'), [
  74. tag,
  75. props,
  76. (children.length || slots) ? (isComponent
  77. ? t.objectExpression([
  78. !!children.length && t.objectProperty(t.identifier('default'), t.arrowFunctionExpression([], t.arrayExpression(utils_1.buildIIFE(path, children)))),
  79. ...(slots ? (t.isObjectExpression(slots)
  80. ? slots.properties
  81. : [t.spreadElement(slots)]) : []),
  82. optimize && t.objectProperty(t.identifier('_'), t.numericLiteral(slotFlag)),
  83. ].filter(Boolean))
  84. : t.arrayExpression(children)) : t.nullLiteral(),
  85. !!patchFlag && optimize && t.numericLiteral(patchFlag),
  86. !!dynamicPropNames.size && optimize
  87. && t.arrayExpression([...dynamicPropNames.keys()].map((name) => t.stringLiteral(name))),
  88. ].filter(Boolean));
  89. if (!directives.length) {
  90. return createVNode;
  91. }
  92. return t.callExpression(utils_1.createIdentifier(state, 'withDirectives'), [
  93. createVNode,
  94. t.arrayExpression(directives),
  95. ]);
  96. };
  97. exports.transformJSXElement = transformJSXElement;
  98. exports.default = () => ({
  99. JSXElement: {
  100. exit(path, state) {
  101. path.replaceWith(transformJSXElement(path, state));
  102. },
  103. },
  104. });