|
1 | 1 | const { compile } = require('vue-template-compiler');
|
2 | 2 |
|
3 |
| -module.exports = function(content) { |
4 |
| - const { render: renderFunction } = compile(content, { |
5 |
| - whitespace: 'condense' |
| 3 | +function transformChildren(value) { |
| 4 | + const chilldren = value.reduce((acc, child) => { |
| 5 | + if(child.text) { |
| 6 | + acc.push(`_v('${child.text}')`); |
| 7 | + } else { |
| 8 | + const args = [`'${child.tag}'`]; |
| 9 | + |
| 10 | + if(Object.keys(child.attrsMap).length) { |
| 11 | + const data = []; |
| 12 | + |
| 13 | + if(child.staticClass) { |
| 14 | + data.push(`staticClass:${child.staticClass}`); |
| 15 | + } |
| 16 | + |
| 17 | + if(child.staticStyle) { |
| 18 | + data.push(`staticStyle:${child.staticStyle}`); |
| 19 | + } |
| 20 | + |
| 21 | + if(child.attrsList.length) { |
| 22 | + const attrs = child.attrsList.reduce((v, attr) => ({ |
| 23 | + ...v, |
| 24 | + [attr.name]: attr.value, |
| 25 | + }), {}); |
| 26 | + |
| 27 | + data.push(`attrs:${JSON.stringify(attrs)}`); |
| 28 | + } |
| 29 | + |
| 30 | + if(data.length) { |
| 31 | + args.push(`{${data.join()}}`); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + if(child.children.length) { |
| 36 | + args.push(transformChildren(child.children)); |
| 37 | + } |
| 38 | + |
| 39 | + acc.push(`_c(${args.join()})`); |
| 40 | + } |
| 41 | + |
| 42 | + return acc; |
| 43 | + }, []); |
| 44 | + |
| 45 | + return `[${chilldren.join()}]`; |
| 46 | +} |
| 47 | + |
| 48 | +const stringify = (value) => value.filter((item) => item).join(); |
| 49 | + |
| 50 | +module.exports = (result) => { |
| 51 | + const { ast } = compile(result, { |
| 52 | + preserveWhitespace: false, |
6 | 53 | });
|
7 | 54 |
|
| 55 | + const children = ast.children.length |
| 56 | + ? `children.concat(${transformChildren(ast.children)})` |
| 57 | + : 'children'; |
| 58 | + |
| 59 | + const attrs = Object.keys(ast.attrsMap).length |
| 60 | + ? `attrs: Object.assign(${JSON.stringify(ast.attrsMap)}, attrs)` |
| 61 | + : 'attrs'; |
| 62 | + |
| 63 | + const classNames = stringify([ast.staticClass, 'classNames', 'staticClass']); |
| 64 | + const styles = stringify([ast.staticStyle, 'style', 'staticStyle']); |
| 65 | + |
8 | 66 | return `
|
9 | 67 | module.exports = {
|
10 | 68 | functional: true,
|
11 |
| - render(_h, { _c }) {${renderFunction}} |
| 69 | + render(_h, { _c, _v, data, children = [] }) { |
| 70 | + const { |
| 71 | + class: classNames, |
| 72 | + staticClass, |
| 73 | + style, |
| 74 | + staticStyle, |
| 75 | + attrs = {}, |
| 76 | + ...rest |
| 77 | + } = data; |
| 78 | +
|
| 79 | + return _c('svg', { |
| 80 | + class: [${classNames}], |
| 81 | + style: [${styles}], |
| 82 | + ${attrs}, |
| 83 | + ...rest, |
| 84 | + }, ${children}) |
| 85 | + } |
12 | 86 | }
|
13 | 87 | `;
|
14 | 88 | }
|
0 commit comments