{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-module-boundary-types */\nimport { ConfigAPI } from '@babel/core'\nimport addJSXAttribute, {\n  Attribute,\n} from '@svgr/babel-plugin-add-jsx-attribute'\nimport removeJSXAttribute from '@svgr/babel-plugin-remove-jsx-attribute'\nimport removeJSXEmptyExpression from '@svgr/babel-plugin-remove-jsx-empty-expression'\nimport replaceJSXAttributeValue, {\n  Value,\n} from '@svgr/babel-plugin-replace-jsx-attribute-value'\nimport svgDynamicTitle from '@svgr/babel-plugin-svg-dynamic-title'\nimport svgEmDimensions from '@svgr/babel-plugin-svg-em-dimensions'\nimport transformReactNativeSVG from '@svgr/babel-plugin-transform-react-native-svg'\nimport transformSvgComponent, {\n  Options as TransformOptions,\n} from '@svgr/babel-plugin-transform-svg-component'\n\nexport interface Options extends TransformOptions {\n  ref?: boolean\n  titleProp?: boolean\n  descProp?: boolean\n  expandProps?: boolean | 'start' | 'end'\n  dimensions?: boolean\n  icon?: boolean | string | number\n  native?: boolean\n  svgProps?: { [key: string]: string }\n  replaceAttrValues?: { [key: string]: string }\n}\n\nconst getAttributeValue = (value: string) => {\n  const literal =\n    typeof value === 'string' && value.startsWith('{') && value.endsWith('}')\n  return { value: literal ? value.slice(1, -1) : value, literal }\n}\n\nconst propsToAttributes = (props: { [key: string]: string }): Attribute[] => {\n  return Object.keys(props).map((name) => {\n    const { literal, value } = getAttributeValue(props[name])\n    return { name, literal, value }\n  })\n}\n\nfunction replaceMapToValues(replaceMap: { [key: string]: string }): Value[] {\n  return Object.keys(replaceMap).map((value) => {\n    const { literal, value: newValue } = getAttributeValue(replaceMap[value])\n    return { value, newValue, literal }\n  })\n}\n\nconst plugin = (_: ConfigAPI, opts: Options) => {\n  let toRemoveAttributes = ['version']\n  let toAddAttributes: Attribute[] = []\n\n  if (opts.svgProps) {\n    toAddAttributes = [...toAddAttributes, ...propsToAttributes(opts.svgProps)]\n  }\n\n  if (opts.ref) {\n    toAddAttributes = [\n      ...toAddAttributes,\n      {\n        name: 'ref',\n        value: 'ref',\n        literal: true,\n      },\n    ]\n  }\n\n  if (opts.titleProp) {\n    toAddAttributes = [\n      ...toAddAttributes,\n      {\n        name: 'aria-labelledby',\n        value: 'titleId',\n        literal: true,\n      },\n    ]\n  }\n\n  if (opts.descProp) {\n    toAddAttributes = [\n      ...toAddAttributes,\n      {\n        name: 'aria-describedby',\n        value: 'descId',\n        literal: true,\n      },\n    ]\n  }\n\n  if (opts.expandProps) {\n    toAddAttributes = [\n      ...toAddAttributes,\n      {\n        name: 'props',\n        spread: true,\n        position:\n          opts.expandProps === 'start' || opts.expandProps === 'end'\n            ? opts.expandProps\n            : undefined,\n      },\n    ]\n  }\n\n  if (!opts.dimensions) {\n    toRemoveAttributes = [...toRemoveAttributes, 'width', 'height']\n  }\n\n  const plugins: any[] = [\n    [transformSvgComponent, opts],\n    ...(opts.icon !== false && opts.dimensions\n      ? [\n          [\n            svgEmDimensions,\n            opts.icon !== true\n              ? { width: opts.icon, height: opts.icon }\n              : opts.native\n              ? { width: 24, height: 24 }\n              : {},\n          ],\n        ]\n      : []),\n    [\n      removeJSXAttribute,\n      { elements: ['svg', 'Svg'], attributes: toRemoveAttributes },\n    ],\n    [\n      addJSXAttribute,\n      { elements: ['svg', 'Svg'], attributes: toAddAttributes },\n    ],\n    removeJSXEmptyExpression,\n  ]\n\n  if (opts.replaceAttrValues) {\n    plugins.push([\n      replaceJSXAttributeValue,\n      { values: replaceMapToValues(opts.replaceAttrValues) },\n    ])\n  }\n\n  if (opts.titleProp) {\n    plugins.push(svgDynamicTitle)\n  }\n\n  if (opts.descProp) {\n    plugins.push([svgDynamicTitle, { tag: 'desc' }, 'desc'])\n  }\n\n  if (opts.native) {\n    plugins.push(transformReactNativeSVG)\n  }\n\n  return { plugins }\n}\n\nexport default plugin\n"],"names":[],"mappings":";;;;;;;;;;;AA6BA,MAAM,iBAAA,GAAoB,CAAC,KAAkB,KAAA;AAC3C,EAA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