forked from gregberge/svgr
/
index.js
31 lines (27 loc) 路 835 Bytes
/
index.js
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
import { parse } from 'svg-parser'
import hastToBabelAst from '@svgr/hast-util-to-babel-ast'
import { transformFromAstSync, createConfigItem } from '@babel/core'
import svgrBabelPreset from '@svgr/babel-preset'
export default function jsxPlugin(code, config, state) {
const filePath = state.filePath || 'unknown'
const hastTree = parse(code)
const babelTree = hastToBabelAst(hastTree)
const { code: generatedCode } = transformFromAstSync(babelTree, code, {
caller: {
name: 'svgr',
},
presets: [
createConfigItem([svgrBabelPreset, { ...config, state }], {
type: 'preset',
}),
],
filename: filePath,
babelrc: false,
configFile: false,
code: true,
ast: false,
inputSourceMap: false,
...(config.jsx && config.jsx.babelConfig),
})
return generatedCode
}