-
-
Notifications
You must be signed in to change notification settings - Fork 414
/
index.ts
140 lines (124 loc) · 3.62 KB
/
index.ts
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
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { ConfigAPI } from '@babel/core'
import addJSXAttribute, {
Attribute,
} from '@svgr/babel-plugin-add-jsx-attribute'
import removeJSXAttribute from '@svgr/babel-plugin-remove-jsx-attribute'
import removeJSXEmptyExpression from '@svgr/babel-plugin-remove-jsx-empty-expression'
import replaceJSXAttributeValue, {
Value,
} from '@svgr/babel-plugin-replace-jsx-attribute-value'
import svgDynamicTitle from '@svgr/babel-plugin-svg-dynamic-title'
import svgEmDimensions from '@svgr/babel-plugin-svg-em-dimensions'
import transformReactNativeSVG from '@svgr/babel-plugin-transform-react-native-svg'
import transformSvgComponent, {
Options as TransformOptions,
} from '@svgr/babel-plugin-transform-svg-component'
export interface Options extends TransformOptions {
ref?: boolean
titleProp?: boolean
expandProps?: boolean | 'start' | 'end'
dimensions?: boolean
icon?: boolean | string | number
native?: boolean
svgProps?: { [key: string]: string }
replaceAttrValues?: { [key: string]: string }
}
const getAttributeValue = (value: string) => {
const literal =
typeof value === 'string' && value.startsWith('{') && value.endsWith('}')
return { value: literal ? value.slice(1, -1) : value, literal }
}
const propsToAttributes = (props: { [key: string]: string }): Attribute[] => {
return Object.keys(props).map((name) => {
const { literal, value } = getAttributeValue(props[name])
return { name, literal, value }
})
}
function replaceMapToValues(replaceMap: { [key: string]: string }): Value[] {
return Object.keys(replaceMap).map((value) => {
const { literal, value: newValue } = getAttributeValue(replaceMap[value])
return { value, newValue, literal }
})
}
const plugin = (_: ConfigAPI, opts: Options) => {
let toRemoveAttributes = ['version']
let toAddAttributes: Attribute[] = []
if (opts.svgProps) {
toAddAttributes = [...toAddAttributes, ...propsToAttributes(opts.svgProps)]
}
if (opts.ref) {
toAddAttributes = [
...toAddAttributes,
{
name: 'ref',
value: 'ref',
literal: true,
},
]
}
if (opts.titleProp) {
toAddAttributes = [
...toAddAttributes,
{
name: 'aria-labelledby',
value: 'titleId',
literal: true,
},
]
}
if (opts.expandProps) {
toAddAttributes = [
...toAddAttributes,
{
name: 'props',
spread: true,
position:
opts.expandProps === 'start' || opts.expandProps === 'end'
? opts.expandProps
: undefined,
},
]
}
if (!opts.dimensions) {
toRemoveAttributes = [...toRemoveAttributes, 'width', 'height']
}
const plugins: any[] = [
[transformSvgComponent, opts],
...(opts.icon !== false && opts.dimensions
? [
[
svgEmDimensions,
opts.icon !== true
? { width: opts.icon, height: opts.icon }
: opts.native
? { width: 24, height: 24 }
: {},
],
]
: []),
[
removeJSXAttribute,
{ elements: ['svg', 'Svg'], attributes: toRemoveAttributes },
],
[
addJSXAttribute,
{ elements: ['svg', 'Svg'], attributes: toAddAttributes },
],
removeJSXEmptyExpression,
]
if (opts.replaceAttrValues) {
plugins.push([
replaceJSXAttributeValue,
{ values: replaceMapToValues(opts.replaceAttrValues) },
])
}
if (opts.titleProp) {
plugins.push(svgDynamicTitle)
}
if (opts.native) {
plugins.push(transformReactNativeSVG)
}
return { plugins }
}
export default plugin