Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const buildSvg = template(`
var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; };
`);

const buildSvgWithDefaults = template(`
var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; };
SVG_NAME.defaultProps = SVG_DEFAULT_PROPS_CODE;
`);

let ignoreRegex;

export default ({ types: t }) => ({
Expand Down Expand Up @@ -45,12 +50,40 @@ export default ({ types: t }) => ({

const svgCode = traverse.removeProperties(parsedSvgAst.program.body[0].expression);

const svgReplacement = buildSvg({
const opts = {
SVG_NAME: importIdentifier,
SVG_CODE: svgCode,
});
};

// Move props off of element and into defaultProps
if (svgCode.openingElement.attributes.length > 1) {
const keepProps = [];
const defaultProps = [];

path.replaceWith(svgReplacement);
svgCode.openingElement.attributes.forEach((prop) => {
if (prop.type === 'JSXSpreadAttribute') {
keepProps.push(prop);
} else {
defaultProps.push(
t.objectProperty(
t.identifier(prop.name.name),
prop.value,
)
);
}
});

svgCode.openingElement.attributes = keepProps;
opts.SVG_DEFAULT_PROPS_CODE = t.objectExpression(defaultProps);
}

if (opts.SVG_DEFAULT_PROPS_CODE) {
const svgReplacement = buildSvgWithDefaults(opts);
path.replaceWithMultiple(svgReplacement);
} else {
const svgReplacement = buildSvg(opts);
path.replaceWith(svgReplacement);
}
}
},
},
Expand Down