index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. var addJSXAttribute = require('@svgr/babel-plugin-add-jsx-attribute');
  3. var removeJSXAttribute = require('@svgr/babel-plugin-remove-jsx-attribute');
  4. var removeJSXEmptyExpression = require('@svgr/babel-plugin-remove-jsx-empty-expression');
  5. var replaceJSXAttributeValue = require('@svgr/babel-plugin-replace-jsx-attribute-value');
  6. var svgDynamicTitle = require('@svgr/babel-plugin-svg-dynamic-title');
  7. var svgEmDimensions = require('@svgr/babel-plugin-svg-em-dimensions');
  8. var transformReactNativeSVG = require('@svgr/babel-plugin-transform-react-native-svg');
  9. var transformSvgComponent = require('@svgr/babel-plugin-transform-svg-component');
  10. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  11. var addJSXAttribute__default = /*#__PURE__*/_interopDefaultLegacy(addJSXAttribute);
  12. var removeJSXAttribute__default = /*#__PURE__*/_interopDefaultLegacy(removeJSXAttribute);
  13. var removeJSXEmptyExpression__default = /*#__PURE__*/_interopDefaultLegacy(removeJSXEmptyExpression);
  14. var replaceJSXAttributeValue__default = /*#__PURE__*/_interopDefaultLegacy(replaceJSXAttributeValue);
  15. var svgDynamicTitle__default = /*#__PURE__*/_interopDefaultLegacy(svgDynamicTitle);
  16. var svgEmDimensions__default = /*#__PURE__*/_interopDefaultLegacy(svgEmDimensions);
  17. var transformReactNativeSVG__default = /*#__PURE__*/_interopDefaultLegacy(transformReactNativeSVG);
  18. var transformSvgComponent__default = /*#__PURE__*/_interopDefaultLegacy(transformSvgComponent);
  19. const getAttributeValue = (value) => {
  20. const literal = typeof value === "string" && value.startsWith("{") && value.endsWith("}");
  21. return { value: literal ? value.slice(1, -1) : value, literal };
  22. };
  23. const propsToAttributes = (props) => {
  24. return Object.keys(props).map((name) => {
  25. const { literal, value } = getAttributeValue(props[name]);
  26. return { name, literal, value };
  27. });
  28. };
  29. function replaceMapToValues(replaceMap) {
  30. return Object.keys(replaceMap).map((value) => {
  31. const { literal, value: newValue } = getAttributeValue(replaceMap[value]);
  32. return { value, newValue, literal };
  33. });
  34. }
  35. const plugin = (_, opts) => {
  36. let toRemoveAttributes = ["version"];
  37. let toAddAttributes = [];
  38. if (opts.svgProps) {
  39. toAddAttributes = [...toAddAttributes, ...propsToAttributes(opts.svgProps)];
  40. }
  41. if (opts.ref) {
  42. toAddAttributes = [
  43. ...toAddAttributes,
  44. {
  45. name: "ref",
  46. value: "ref",
  47. literal: true
  48. }
  49. ];
  50. }
  51. if (opts.titleProp) {
  52. toAddAttributes = [
  53. ...toAddAttributes,
  54. {
  55. name: "aria-labelledby",
  56. value: "titleId",
  57. literal: true
  58. }
  59. ];
  60. }
  61. if (opts.descProp) {
  62. toAddAttributes = [
  63. ...toAddAttributes,
  64. {
  65. name: "aria-describedby",
  66. value: "descId",
  67. literal: true
  68. }
  69. ];
  70. }
  71. if (opts.expandProps) {
  72. toAddAttributes = [
  73. ...toAddAttributes,
  74. {
  75. name: "props",
  76. spread: true,
  77. position: opts.expandProps === "start" || opts.expandProps === "end" ? opts.expandProps : void 0
  78. }
  79. ];
  80. }
  81. if (!opts.dimensions) {
  82. toRemoveAttributes = [...toRemoveAttributes, "width", "height"];
  83. }
  84. const plugins = [
  85. [transformSvgComponent__default["default"], opts],
  86. ...opts.icon !== false && opts.dimensions ? [
  87. [
  88. svgEmDimensions__default["default"],
  89. opts.icon !== true ? { width: opts.icon, height: opts.icon } : opts.native ? { width: 24, height: 24 } : {}
  90. ]
  91. ] : [],
  92. [
  93. removeJSXAttribute__default["default"],
  94. { elements: ["svg", "Svg"], attributes: toRemoveAttributes }
  95. ],
  96. [
  97. addJSXAttribute__default["default"],
  98. { elements: ["svg", "Svg"], attributes: toAddAttributes }
  99. ],
  100. removeJSXEmptyExpression__default["default"]
  101. ];
  102. if (opts.replaceAttrValues) {
  103. plugins.push([
  104. replaceJSXAttributeValue__default["default"],
  105. { values: replaceMapToValues(opts.replaceAttrValues) }
  106. ]);
  107. }
  108. if (opts.titleProp) {
  109. plugins.push(svgDynamicTitle__default["default"]);
  110. }
  111. if (opts.descProp) {
  112. plugins.push([svgDynamicTitle__default["default"], { tag: "desc" }, "desc"]);
  113. }
  114. if (opts.native) {
  115. plugins.push(transformReactNativeSVG__default["default"]);
  116. }
  117. return { plugins };
  118. };
  119. module.exports = plugin;
  120. //# sourceMappingURL=index.js.map