@@ -13,6 +13,11 @@ const buildSvg = template(`
13
13
var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; };
14
14
` ) ;
15
15
16
+ const buildSvgWithDefaults = template ( `
17
+ var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; };
18
+ SVG_NAME.defaultProps = SVG_DEFAULT_PROPS_CODE;
19
+ ` ) ;
20
+
16
21
let ignoreRegex ;
17
22
18
23
export default ( { types : t } ) => ( {
@@ -52,12 +57,40 @@ export default ({ types: t }) => ({
52
57
53
58
const svgCode = traverse . removeProperties ( parsedSvgAst . program . body [ 0 ] . expression ) ;
54
59
55
- const svgReplacement = buildSvg ( {
60
+ const opts = {
56
61
SVG_NAME : importIdentifier ,
57
62
SVG_CODE : svgCode ,
58
- } ) ;
63
+ } ;
64
+
65
+ // Move props off of element and into defaultProps
66
+ if ( svgCode . openingElement . attributes . length > 1 ) {
67
+ const keepProps = [ ] ;
68
+ const defaultProps = [ ] ;
59
69
60
- path . replaceWith ( svgReplacement ) ;
70
+ svgCode . openingElement . attributes . forEach ( ( prop ) => {
71
+ if ( prop . type === 'JSXSpreadAttribute' ) {
72
+ keepProps . push ( prop ) ;
73
+ } else {
74
+ defaultProps . push (
75
+ t . objectProperty (
76
+ t . identifier ( prop . name . name ) ,
77
+ prop . value ,
78
+ )
79
+ ) ;
80
+ }
81
+ } ) ;
82
+
83
+ svgCode . openingElement . attributes = keepProps ;
84
+ opts . SVG_DEFAULT_PROPS_CODE = t . objectExpression ( defaultProps ) ;
85
+ }
86
+
87
+ if ( opts . SVG_DEFAULT_PROPS_CODE ) {
88
+ const svgReplacement = buildSvgWithDefaults ( opts ) ;
89
+ path . replaceWithMultiple ( svgReplacement ) ;
90
+ } else {
91
+ const svgReplacement = buildSvg ( opts ) ;
92
+ path . replaceWith ( svgReplacement ) ;
93
+ }
61
94
}
62
95
} ,
63
96
} ,
0 commit comments