-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathplugin.js
More file actions
178 lines (160 loc) · 4.72 KB
/
plugin.js
File metadata and controls
178 lines (160 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import cssToObj from './css-to-obj';
import {hyphenToCamel, namespaceToCamel} from './camelize';
export default function (babel) {
const t = babel.types;
// converts
// <svg stroke-width="5" xmlns:xlink="asdf">
// to
// <svg strokeWidth="5" xmlnsXlink="asdf">
const camelizeVisitor = {
JSXAttribute(path) {
if (t.isJSXNamespacedName(path.node.name)) {
path.node.name = t.jSXIdentifier(
namespaceToCamel(path.node.name.namespace.name, path.node.name.name.name)
);
} else if (t.isJSXIdentifier(path.node.name)) {
path.node.name.name = hyphenToCamel(path.node.name.name);
}
}
};
// converts
// <tag style="text-align: center; width: 50px">
// to
// <tag style={{textAlign: 'center', width: '50px'}}>
const styleAttrVisitor = {
JSXAttribute(path) {
if (t.isJSXIdentifier(path.node.name) && path.node.name.name === 'style') {
let csso = cssToObj(path.node.value.value);
let properties = Object.keys(csso).map(prop => t.objectProperty(
t.identifier(hyphenToCamel(prop)),
t.stringLiteral(csso[prop])
));
path.node.value = t.jSXExpressionContainer(
t.objectExpression(properties)
);
return;
}
}
};
// Flow props from Output component to root SVG
// converts
// <svg width="50">
// to
// <svg width={this.props.width ? this.props.width : "50"}>
const attrVisitor = {
JSXAttribute(path) {
if (!t.isJSXIdentifier(path.node.name)) return;
// don't handle style attr. It needs to be an object
if (path.node.name.name === 'style') return;
// else
const valueExpression = t.memberExpression(
t.memberExpression(
t.thisExpression(),
t.identifier('props')
),
t.identifier(hyphenToCamel(path.node.name.name))
);
path.node.value = t.jSXExpressionContainer(
t.conditionalExpression(
valueExpression,
valueExpression,
t.stringLiteral(path.node.value.value)
)
);
}
};
// converts
// <svg>
// to
// <svg {...this.props}>
// after passing through attributes visitors
const svgVisitor = {
JSXOpeningElement(path) {
if (t.isJSXIdentifier(path.node.name) && path.node.name.name.toLowerCase() === 'svg') {
path.traverse(camelizeVisitor);
path.traverse(attrVisitor);
path.traverse(styleAttrVisitor);
// add spread props
path.node.attributes.push(
t.jSXSpreadAttribute(
t.memberExpression(
t.thisExpression(),
t.identifier('props')
)
)
);
} else {
// don't ignore style attr transformations for other nodes
path.traverse(camelizeVisitor);
path.traverse(styleAttrVisitor);
}
}
};
// returns
// export default class SVG extends React.Component {
// render() {
// return ${input_svg_node}
// }
// }
const getExport = function (svg, className = 'SVG') {
return t.exportDefaultDeclaration(
t.classDeclaration(
t.identifier(className),
t.memberExpression(
t.identifier('React'),
t.identifier('Component')
),
t.classBody(
[
t.classMethod(
'method',
t.identifier('render'),
[],
t.blockStatement(
[t.returnStatement(svg)]
)
)
]
),
[]
)
);
}
// converts
// <svg/>
// to
// import React from 'react';
// export default class SVG extends React.Component { render() { <svg/> }}
// after passing through other visitors
const programVisitor = {
Program(path) {
if (path.node.body.length === 0) throw new Error('No Content in SVG file');
if (path.node.body.length > 1) throw new Error('There is more than one root Element. We don\'t support this yet.');
let node = path.node.body[0];
if (!t.isExpressionStatement(node)) return;
if (!t.isJSXElement(node.expression)) return;
if (!t.isJSXIdentifier(node.expression.openingElement.name)) return;
if (node.expression.openingElement.name.name !== 'svg') return;
path.traverse(svgVisitor);
// replace the entire node to
// export default class extends React {
// render () {
// SVG_NODE
// }
// }
path.node.body[0] = getExport(node.expression);
// add import react statement
path.node.body.unshift(
t.importDeclaration(
[
t.importDefaultSpecifier(t.identifier('React'))
],
t.stringLiteral('react')
)
);
}
}
return {
visitor: programVisitor
};
}