-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
67 lines (66 loc) · 1.91 KB
/
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
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
module.exports = babel => {
const t = babel.types
return {
inherits: require('babel-plugin-syntax-jsx'),
visitor: {
Program (path) {
path.traverse({
'VariableDeclaration' (path) {
if (path.node.declarations.length !== 1 ||
!t.isVariableDeclarator(path.node.declarations[0]) ||
!t.isArrowFunctionExpression(path.node.declarations[0].init)) {
return
}
const jsxChecker = {
hasJsx: false
}
path.traverse({
JSXElement () {
this.hasJsx = true
}
}, jsxChecker)
if (!jsxChecker.hasJsx) {
return
}
const name = path.node.declarations[0].id.name
const params = [t.identifier('h'), ...path.node.declarations[0].init.params]
const body = path.node.declarations[0].init.body
const isDevEnv = process.env.NODE_ENV === 'development'
const props = [
t.objectProperty(
t.identifier('functional'),
t.booleanLiteral(true)
),
t.objectProperty(
t.identifier('render'),
t.arrowFunctionExpression(params, body)
)
]
if (isDevEnv) {
props.unshift(
t.objectProperty(
t.identifier('name'),
t.stringLiteral(name)
)
)
}
path.replaceWith(
t.variableDeclaration(
'const',
[
t.variableDeclarator(
t.identifier(name),
t.objectExpression(
props
)
)
]
),
[]
)
}
})
}
}
}
}