index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. var core = require('@babel/core');
  3. const elements = ["svg", "Svg"];
  4. const createTagElement = (tag, children = [], attributes = []) => {
  5. const eleName = core.types.jsxIdentifier(tag);
  6. return core.types.jsxElement(
  7. core.types.jsxOpeningElement(eleName, attributes),
  8. core.types.jsxClosingElement(eleName),
  9. children
  10. );
  11. };
  12. const createTagIdAttribute = (tag) => core.types.jsxAttribute(
  13. core.types.jsxIdentifier("id"),
  14. core.types.jsxExpressionContainer(core.types.identifier(`${tag}Id`))
  15. );
  16. const addTagIdAttribute = (tag, attributes) => {
  17. const existingId = attributes.find(
  18. (attribute) => core.types.isJSXAttribute(attribute) && attribute.name.name === "id"
  19. );
  20. if (!existingId) {
  21. return [...attributes, createTagIdAttribute(tag)];
  22. }
  23. existingId.value = core.types.jsxExpressionContainer(
  24. core.types.isStringLiteral(existingId.value) ? core.types.logicalExpression("||", core.types.identifier(`${tag}Id`), existingId.value) : core.types.identifier(`${tag}Id`)
  25. );
  26. return attributes;
  27. };
  28. const plugin = () => ({
  29. visitor: {
  30. JSXElement(path, state) {
  31. const tag = state.opts.tag || "title";
  32. if (!elements.length)
  33. return;
  34. const openingElement = path.get("openingElement");
  35. const openingElementName = openingElement.get("name");
  36. if (!elements.some(
  37. (element) => openingElementName.isJSXIdentifier({ name: element })
  38. )) {
  39. return;
  40. }
  41. const getTagElement = (existingTitle) => {
  42. var _a;
  43. const tagExpression = core.types.identifier(tag);
  44. if (existingTitle) {
  45. existingTitle.openingElement.attributes = addTagIdAttribute(
  46. tag,
  47. existingTitle.openingElement.attributes
  48. );
  49. }
  50. const conditionalTitle = core.types.conditionalExpression(
  51. tagExpression,
  52. createTagElement(
  53. tag,
  54. [core.types.jsxExpressionContainer(tagExpression)],
  55. existingTitle ? existingTitle.openingElement.attributes : [createTagIdAttribute(tag)]
  56. ),
  57. core.types.nullLiteral()
  58. );
  59. if ((_a = existingTitle == null ? void 0 : existingTitle.children) == null ? void 0 : _a.length) {
  60. return core.types.jsxExpressionContainer(
  61. core.types.conditionalExpression(
  62. core.types.binaryExpression(
  63. "===",
  64. tagExpression,
  65. core.types.identifier("undefined")
  66. ),
  67. existingTitle,
  68. conditionalTitle
  69. )
  70. );
  71. }
  72. return core.types.jsxExpressionContainer(conditionalTitle);
  73. };
  74. let tagElement = null;
  75. const hasTitle = path.get("children").some((childPath) => {
  76. if (childPath.node === tagElement)
  77. return false;
  78. if (!childPath.isJSXElement())
  79. return false;
  80. const name = childPath.get("openingElement").get("name");
  81. if (!name.isJSXIdentifier())
  82. return false;
  83. if (name.node.name !== tag)
  84. return false;
  85. tagElement = getTagElement(childPath.node);
  86. childPath.replaceWith(tagElement);
  87. return true;
  88. });
  89. tagElement = tagElement || getTagElement();
  90. if (!hasTitle) {
  91. path.node.children.unshift(tagElement);
  92. path.replaceWith(path.node);
  93. }
  94. }
  95. }
  96. });
  97. module.exports = plugin;
  98. //# sourceMappingURL=index.js.map