index.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. const elements = ['svg', 'Svg'];
  5. const plugin = ({
  6. types: t
  7. }) => ({
  8. visitor: {
  9. JSXOpeningElement: {
  10. enter(path) {
  11. if (!elements.some(element => path.get('name').isJSXIdentifier({
  12. name: element
  13. }))) return;
  14. const requiredAttributes = ['width', 'height'];
  15. const attributeValue = '1em';
  16. path.get('attributes').forEach(attributePath => {
  17. if (!attributePath.isJSXAttribute()) return;
  18. const index = requiredAttributes.indexOf(attributePath.node.name.name);
  19. if (index === -1) return;
  20. const value = attributePath.get('value');
  21. value.replaceWith(t.stringLiteral(attributeValue));
  22. requiredAttributes.splice(index, 1);
  23. });
  24. requiredAttributes.forEach(attribute => {
  25. path.pushContainer('attributes', t.jsxAttribute(t.jsxIdentifier(attribute), t.stringLiteral(attributeValue)));
  26. });
  27. }
  28. }
  29. }
  30. });
  31. var _default = plugin;
  32. exports.default = _default;