index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _babelPluginAddJsxAttribute = _interopRequireDefault(require("@svgr/babel-plugin-add-jsx-attribute"));
  5. var _babelPluginRemoveJsxAttribute = _interopRequireDefault(require("@svgr/babel-plugin-remove-jsx-attribute"));
  6. var _babelPluginRemoveJsxEmptyExpression = _interopRequireDefault(require("@svgr/babel-plugin-remove-jsx-empty-expression"));
  7. var _babelPluginReplaceJsxAttributeValue = _interopRequireDefault(require("@svgr/babel-plugin-replace-jsx-attribute-value"));
  8. var _babelPluginSvgDynamicTitle = _interopRequireDefault(require("@svgr/babel-plugin-svg-dynamic-title"));
  9. var _babelPluginSvgEmDimensions = _interopRequireDefault(require("@svgr/babel-plugin-svg-em-dimensions"));
  10. var _babelPluginTransformReactNativeSvg = _interopRequireDefault(require("@svgr/babel-plugin-transform-react-native-svg"));
  11. var _babelPluginTransformSvgComponent = _interopRequireDefault(require("@svgr/babel-plugin-transform-svg-component"));
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. function getAttributeValue(value) {
  14. const literal = typeof value === 'string' && value.startsWith('{') && value.endsWith('}');
  15. return {
  16. value: literal ? value.slice(1, -1) : value,
  17. literal
  18. };
  19. }
  20. function propsToAttributes(props) {
  21. return Object.keys(props).map(name => {
  22. const {
  23. literal,
  24. value
  25. } = getAttributeValue(props[name]);
  26. return {
  27. name,
  28. literal,
  29. value
  30. };
  31. });
  32. }
  33. function replaceMapToValues(replaceMap) {
  34. return Object.keys(replaceMap).map(value => {
  35. const {
  36. literal,
  37. value: newValue
  38. } = getAttributeValue(replaceMap[value]);
  39. return {
  40. value,
  41. newValue,
  42. literal
  43. };
  44. });
  45. }
  46. const plugin = (api, opts) => {
  47. let toRemoveAttributes = ['version'];
  48. let toAddAttributes = [];
  49. if (opts.svgProps) {
  50. toAddAttributes = [...toAddAttributes, ...propsToAttributes(opts.svgProps)];
  51. }
  52. if (opts.ref) {
  53. toAddAttributes = [...toAddAttributes, {
  54. name: 'ref',
  55. value: 'svgRef',
  56. literal: true
  57. }];
  58. }
  59. if (opts.titleProp) {
  60. toAddAttributes = [...toAddAttributes, {
  61. name: 'aria-labelledby',
  62. value: 'titleId',
  63. literal: true
  64. }];
  65. }
  66. if (opts.expandProps) {
  67. toAddAttributes = [...toAddAttributes, {
  68. name: 'props',
  69. spread: true,
  70. position: opts.expandProps
  71. }];
  72. }
  73. if (!opts.dimensions) {
  74. toRemoveAttributes = [...toRemoveAttributes, 'width', 'height'];
  75. }
  76. const plugins = [[_babelPluginTransformSvgComponent.default, opts], ...(opts.icon && opts.dimensions ? [_babelPluginSvgEmDimensions.default] : []), [_babelPluginRemoveJsxAttribute.default, {
  77. elements: ['svg', 'Svg'],
  78. attributes: toRemoveAttributes
  79. }], [_babelPluginAddJsxAttribute.default, {
  80. elements: ['svg', 'Svg'],
  81. attributes: toAddAttributes
  82. }], _babelPluginRemoveJsxEmptyExpression.default];
  83. if (opts.replaceAttrValues) {
  84. plugins.push([_babelPluginReplaceJsxAttributeValue.default, {
  85. values: replaceMapToValues(opts.replaceAttrValues)
  86. }]);
  87. }
  88. if (opts.titleProp) {
  89. plugins.push(_babelPluginSvgDynamicTitle.default);
  90. }
  91. if (opts.native) {
  92. if (opts.native.expo) {
  93. plugins.push([_babelPluginTransformReactNativeSvg.default, opts.native]);
  94. } else {
  95. plugins.push(_babelPluginTransformReactNativeSvg.default);
  96. }
  97. }
  98. return {
  99. plugins
  100. };
  101. };
  102. var _default = plugin;
  103. exports.default = _default;