index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. var core = require('@babel/core');
  3. const elementToComponent = {
  4. svg: "Svg",
  5. circle: "Circle",
  6. clipPath: "ClipPath",
  7. ellipse: "Ellipse",
  8. g: "G",
  9. linearGradient: "LinearGradient",
  10. radialGradient: "RadialGradient",
  11. line: "Line",
  12. path: "Path",
  13. pattern: "Pattern",
  14. polygon: "Polygon",
  15. polyline: "Polyline",
  16. rect: "Rect",
  17. symbol: "Symbol",
  18. text: "Text",
  19. textPath: "TextPath",
  20. tspan: "TSpan",
  21. use: "Use",
  22. defs: "Defs",
  23. stop: "Stop",
  24. mask: "Mask",
  25. image: "Image",
  26. foreignObject: "ForeignObject"
  27. };
  28. const plugin = () => {
  29. function replaceElement(path, state) {
  30. const namePath = path.get("openingElement").get("name");
  31. if (!namePath.isJSXIdentifier())
  32. return;
  33. const { name } = namePath.node;
  34. const component = elementToComponent[name];
  35. if (component) {
  36. namePath.replaceWith(core.types.jsxIdentifier(component));
  37. if (path.has("closingElement")) {
  38. const closingNamePath = path.get("closingElement").get("name");
  39. closingNamePath.replaceWith(core.types.jsxIdentifier(component));
  40. }
  41. state.replacedComponents.add(component);
  42. return;
  43. }
  44. state.unsupportedComponents.add(name);
  45. path.remove();
  46. }
  47. const svgElementVisitor = {
  48. JSXElement(path, state) {
  49. if (!path.get("openingElement").get("name").isJSXIdentifier({ name: "svg" })) {
  50. return;
  51. }
  52. replaceElement(path, state);
  53. path.traverse(jsxElementVisitor, state);
  54. }
  55. };
  56. const jsxElementVisitor = {
  57. JSXElement(path, state) {
  58. replaceElement(path, state);
  59. }
  60. };
  61. const importDeclarationVisitor = {
  62. ImportDeclaration(path, state) {
  63. if (path.get("source").isStringLiteral({ value: "react-native-svg" })) {
  64. state.replacedComponents.forEach((component) => {
  65. if (path.get("specifiers").some(
  66. (specifier) => specifier.get("local").isIdentifier({ name: component })
  67. )) {
  68. return;
  69. }
  70. path.pushContainer(
  71. "specifiers",
  72. core.types.importSpecifier(core.types.identifier(component), core.types.identifier(component))
  73. );
  74. });
  75. } else if (path.get("source").isStringLiteral({ value: "expo" })) {
  76. path.pushContainer(
  77. "specifiers",
  78. core.types.importSpecifier(core.types.identifier("Svg"), core.types.identifier("Svg"))
  79. );
  80. } else {
  81. return;
  82. }
  83. if (state.unsupportedComponents.size && !path.has("trailingComments")) {
  84. const componentList = [...state.unsupportedComponents].join(", ");
  85. path.addComment(
  86. "trailing",
  87. ` SVGR has dropped some elements not supported by react-native-svg: ${componentList} `
  88. );
  89. }
  90. }
  91. };
  92. return {
  93. visitor: {
  94. Program(path, state) {
  95. state.replacedComponents = /* @__PURE__ */ new Set();
  96. state.unsupportedComponents = /* @__PURE__ */ new Set();
  97. path.traverse(svgElementVisitor, state);
  98. path.traverse(importDeclarationVisitor, state);
  99. }
  100. }
  101. };
  102. };
  103. module.exports = plugin;
  104. //# sourceMappingURL=index.js.map