-
Notifications
You must be signed in to change notification settings - Fork 0
/
taro-jsx-transform.js
54 lines (48 loc) · 1.9 KB
/
taro-jsx-transform.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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
var core = require("@babel/core");
let types = core.types;
var util = require('util')
function _default() {
return {
visitor: {
JSXElement: function(path) {
let opening = path.node.openingElement;
let attrs = opening.attributes;
let elementTypeName = opening.name.name;
let children = path.node.children;
if (elementTypeName.charAt(0) == elementTypeName.charAt(0).toUpperCase()) {
path.replaceWithSourceString(`Taro.createComponent(${elementTypeName})`);
} else {
path.replaceWithSourceString(`Taro.create("${elementTypeName}")`);
}
let attrsNode = types.objectExpression([]);
for (let attr of attrs) {
let attrName = attr.name.name;
let attrValue = attr.value;
if (attrValue == null) {
attrValue = types.booleanLiteral(true);
}
if (types.isJSXExpressionContainer(attrValue)) {
attrValue = attrValue.expression;
}
attrsNode.properties.push(types.objectProperty(types.stringLiteral(attrName), attrValue));
}
let callArgs = path.node.arguments;
callArgs.push(attrsNode);
for (let child of children) {
callArgs.push(child)
}
},
JSXText: function(path) {
path.replaceWith(types.stringLiteral(path.node.value));
},
JSXExpressionContainer: function(path) {
path.replaceWith(path.node.expression)
}
},
};
}