-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (70 loc) · 2.04 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const { compile } = require('vue-template-compiler');
function transformChildren(value) {
const chilldren = value.reduce((acc, child) => {
if(child.text) {
acc.push(`_v('${child.text}')`);
} else {
const args = [`'${child.tag}'`];
if(Object.keys(child.attrsMap).length) {
const data = [];
if(child.staticClass) {
data.push(`staticClass:${child.staticClass}`);
}
if(child.staticStyle) {
data.push(`staticStyle:${child.staticStyle}`);
}
if(child.attrsList.length) {
const attrs = child.attrsList.reduce((v, attr) => ({
...v,
[attr.name]: attr.value,
}), {});
data.push(`attrs:${JSON.stringify(attrs)}`);
}
if(data.length) {
args.push(`{${data.join()}}`);
}
}
if(child.children.length) {
args.push(transformChildren(child.children));
}
acc.push(`_c(${args.join()})`);
}
return acc;
}, []);
return `[${chilldren.join()}]`;
}
const stringify = (value) => value.filter((item) => item).join();
module.exports = (result) => {
const { ast } = compile(result, {
preserveWhitespace: false,
});
const children = ast.children.length
? `children.concat(${transformChildren(ast.children)})`
: 'children';
const attrs = Object.keys(ast.attrsMap).length
? `attrs: Object.assign(${JSON.stringify(ast.attrsMap)}, attrs)`
: 'attrs';
const classNames = stringify([ast.staticClass, 'classNames', 'staticClass']);
const styles = stringify([ast.staticStyle, 'style', 'staticStyle']);
return `
module.exports = {
functional: true,
render(_h, { _c, _v, data, children = [] }) {
const {
class: classNames,
staticClass,
style,
staticStyle,
attrs = {},
...rest
} = data;
return _c('svg', {
class: [${classNames}],
style: [${styles}],
${attrs},
...rest,
}, ${children})
}
}
`;
}