index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = jsxPlugin;
  4. var _svgParser = require("svg-parser");
  5. var _hastUtilToBabelAst = _interopRequireDefault(require("@svgr/hast-util-to-babel-ast"));
  6. var _core = require("@babel/core");
  7. var _babelPreset = _interopRequireDefault(require("@svgr/babel-preset"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function jsxPlugin(code, config, state) {
  10. const filePath = state.filePath || 'unknown';
  11. const hastTree = (0, _svgParser.parse)(code);
  12. const babelTree = (0, _hastUtilToBabelAst.default)(hastTree);
  13. const {
  14. code: generatedCode
  15. } = (0, _core.transformFromAstSync)(babelTree, code, {
  16. caller: {
  17. name: 'svgr'
  18. },
  19. presets: [(0, _core.createConfigItem)([_babelPreset.default, { ...config,
  20. state
  21. }], {
  22. type: 'preset'
  23. })],
  24. filename: filePath,
  25. babelrc: false,
  26. configFile: false,
  27. code: true,
  28. ast: false,
  29. inputSourceMap: false,
  30. ...(config.jsx && config.jsx.babelConfig)
  31. });
  32. return generatedCode;
  33. }