-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (31 loc) · 1.06 KB
/
index.js
File metadata and controls
36 lines (31 loc) · 1.06 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
module.exports = function (babel) {
return {
visitor: {
CallExpression: function (path) {
if (path.node.callee.name === 'h' && path.node.arguments.length > 3) {
var args = path.node.arguments
path.node.arguments = args.slice(0, 2)
path.node.arguments.push(babel.types.arrayExpression(args.slice(2)))
}
},
JSXOpeningElement: function (path, meta) {
meta.file.set('hasJSX', true)
},
Program: {
enter: function (path, meta) {
meta.file.set('hasJSX', false)
},
exit: function (path, meta) {
// if it doesn't have JSX or "h" is already imported, stop here
if (!meta.file.get('hasJSX') || path.scope.hasBinding('h')) {
return
}
var vdomImportDeclaration = babel.types.importDeclaration([
babel.types.importDefaultSpecifier(babel.types.identifier('h'))
], babel.types.stringLiteral('virtual-dom/h'))
path.node.body.unshift(vdomImportDeclaration)
}
}
}
}
}