-
-
Notifications
You must be signed in to change notification settings - Fork 428
/
Copy pathindex.js
36 lines (33 loc) · 870 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
32
33
34
35
36
import { getProps, getImport, getExport } from './util'
function defaultTemplate(
{ template },
opts,
{ imports, componentName, props, jsx, exports },
) {
return template.ast`${imports}
const ${componentName} = (${props}) => ${jsx}
${exports}
`
}
const plugin = (api, opts) => ({
visitor: {
Program(path) {
const { types: t } = api
const template = opts.template || defaultTemplate
const body = template(api, opts, {
componentName: t.identifier(opts.state.componentName),
props: getProps(api, opts),
imports: getImport(api, opts),
exports: getExport(api, opts),
jsx: path.node.body[0].expression,
})
if (Array.isArray(body)) {
path.node.body = body
} else {
path.node.body = [body]
}
path.replaceWith(path.node)
},
},
})
export default plugin