{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { parse } from 'svg-parser'\nimport hastToBabelAst from '@svgr/hast-util-to-babel-ast'\nimport { transformFromAstSync, createConfigItem } from '@babel/core'\nimport svgrBabelPreset, {\n  Options as SvgrPresetOptions,\n} from '@svgr/babel-preset'\nimport type { Plugin, Config } from '@svgr/core'\n\nconst getJsxRuntimeOptions = (config: Config): Partial<SvgrPresetOptions> => {\n  if (config.jsxRuntimeImport) {\n    return {\n      importSource: config.jsxRuntimeImport.source,\n      jsxRuntimeImport: config.jsxRuntimeImport,\n    }\n  }\n  switch (config.jsxRuntime) {\n    case null:\n    case undefined:\n    case 'classic':\n      return {\n        jsxRuntime: 'classic',\n        importSource: 'react',\n        jsxRuntimeImport: { namespace: 'React', source: 'react' },\n      }\n    case 'classic-preact':\n      return {\n        jsxRuntime: 'classic',\n        importSource: 'preact/compat',\n        jsxRuntimeImport: { specifiers: ['h'], source: 'preact' },\n      }\n    case 'automatic':\n      return { jsxRuntime: 'automatic' }\n    default:\n      throw new Error(`Unsupported \"jsxRuntime\" \"${config.jsxRuntime}\"`)\n  }\n}\n\nconst jsxPlugin: Plugin = (code, config, state) => {\n  const filePath = state.filePath || 'unknown'\n  const hastTree = parse(code)\n\n  const babelTree = hastToBabelAst(hastTree)\n\n  const svgPresetOptions: SvgrPresetOptions = {\n    ref: config.ref,\n    titleProp: config.titleProp,\n    descProp: config.descProp,\n    expandProps: config.expandProps,\n    dimensions: config.dimensions,\n    icon: config.icon,\n    native: config.native,\n    svgProps: config.svgProps,\n    replaceAttrValues: config.replaceAttrValues,\n    typescript: config.typescript,\n    template: config.template,\n    memo: config.memo,\n    exportType: config.exportType,\n    namedExport: config.namedExport,\n    ...getJsxRuntimeOptions(config),\n    state,\n  }\n\n  const result = transformFromAstSync(babelTree, code, {\n    caller: {\n      name: 'svgr',\n    },\n    presets: [\n      createConfigItem([svgrBabelPreset, svgPresetOptions], {\n        type: 'preset',\n      }),\n    ],\n    filename: filePath,\n    babelrc: false,\n    configFile: false,\n    code: true,\n    ast: false,\n    // @ts-ignore\n    inputSourceMap: false,\n    ...(config.jsx && config.jsx.babelConfig),\n  })\n\n  if (!result?.code) {\n    throw new Error(`Unable to generate SVG file`)\n  }\n\n  return result.code\n}\n\nexport default jsxPlugin\n"],"names":["parse","transformFromAstSync","createConfigItem"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,MAAM,oBAAA,GAAuB,CAAC,MAA+C,KAAA;AAC3E,EAAA,IAAI,OAAO,gBAAkB,EAAA;AAC3B,IAAO,OAAA;AAAA,MACL,YAAA,EAAc,OAAO,gBAAiB,CAAA,MAAA;AAAA,MACtC,kBAAkB,MAAO,CAAA,gBAAA;AAAA,KAC3B,CAAA;AAAA,GACF;AACA,EAAA,QAAQ,OAAO,UAAY;AAAA,IACzB,KAAK,IAAA,CAAA;AAAA,IACL,KAAK,KAAA,CAAA,CAAA;AAAA,IACL,KAAK,SAAA;AACH,MAAO,OAAA;AAAA,QACL,UAAY,EAAA,SAAA;AAAA,QACZ,YAAc,EAAA,OAAA;AAAA,QACd,gBAAkB,EAAA,EAAE,SAAW,EAAA,OAAA,EAAS,QAAQ,OAAQ,EAAA;AAAA,OAC1D,CAAA;AAAA,IACF,KAAK,gBAAA;AACH,MAAO,OAAA;AAAA,QACL,UAAY,EAAA,SAAA;AAAA,QACZ,YAAc,EAAA,eAAA;AAAA,QACd,kBAAkB,EAAE,UAAA,EAAY,CAAC,GAAG,CAAA,EAAG,QAAQ,QAAS,EAAA;AAAA,OAC1D,CAAA;AAAA,IACF,KAAK,WAAA;AACH,MAAO,OAAA,EAAE,YAAY,WAAY,EAAA,CAAA;AAAA,IACnC;AACE,MAAA,MAAM,IAAI,KAAA,CAAM,CAA6B,0BAAA,EAAA,MAAA,CAAO,UAAa,CAAA,CAAA,CAAA,CAAA,CAAA;AAAA,GACrE;AACF,CAAA,CAAA;AAEA,MAAM,SAAoB,GAAA,CAAC,IAAM,EAAA,MAAA,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