|
| 1 | +import jsx from '@babel/plugin-syntax-jsx'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @param {Babel} babel |
| 5 | + * @param {object} [options] |
| 6 | + * @param {string} [options.tag='html'] The tagged template "tag" function name to produce. |
| 7 | + * @param {string} [options.html=false] If `true`, output HTML-like instead of XML-like (no self-closing tags, etc). |
| 8 | + */ |
| 9 | +export default function jsxToHtmBabelPlugin({ types: t }, options = {}) { |
| 10 | + const tag = dottedIdentifier(options.tag || 'html'); |
| 11 | + const htmlOutput = !!options.html; |
| 12 | + |
| 13 | + function dottedIdentifier(keypath) { |
| 14 | + const path = keypath.split('.'); |
| 15 | + let out; |
| 16 | + for (let i = 0; i < path.length; i++) { |
| 17 | + const ident = t.identifier(path[i]); |
| 18 | + out = i === 0 ? ident : t.memberExpression(out, ident); |
| 19 | + } |
| 20 | + return out; |
| 21 | + } |
| 22 | + |
| 23 | + let quasis = []; |
| 24 | + let expressions = []; |
| 25 | + let buffer = ''; |
| 26 | + |
| 27 | + function expr(value) { |
| 28 | + commit(true); |
| 29 | + expressions.push(value); |
| 30 | + } |
| 31 | + |
| 32 | + function raw(str) { |
| 33 | + buffer += str; |
| 34 | + } |
| 35 | + |
| 36 | + function escapeText(text) { |
| 37 | + if (text.indexOf('<') < 0) { |
| 38 | + return raw(text); |
| 39 | + } |
| 40 | + return expr(t.stringLiteral(text)); |
| 41 | + } |
| 42 | + |
| 43 | + function escapePropValue(node) { |
| 44 | + const value = node.value; |
| 45 | + |
| 46 | + if (value.match(/^.*$/u)) { |
| 47 | + if (value.indexOf('"') < 0) { |
| 48 | + return raw(`"${value}"`); |
| 49 | + } |
| 50 | + else if (value.indexOf("'") < 0) { |
| 51 | + return raw(`'${value}'`); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return expr(t.stringLiteral(node.value)); |
| 56 | + } |
| 57 | + |
| 58 | + function commit(force) { |
| 59 | + if (!buffer && !force) return; |
| 60 | + quasis.push(t.templateElement({ |
| 61 | + raw: buffer, |
| 62 | + cooked: buffer |
| 63 | + })); |
| 64 | + buffer = ''; |
| 65 | + } |
| 66 | + |
| 67 | + function processNode(node, path, isRoot) { |
| 68 | + const open = node.openingElement; |
| 69 | + const { name } = open.name; |
| 70 | + |
| 71 | + if (name.match(/^[A-Z]/)) { |
| 72 | + raw('<'); |
| 73 | + expr(t.identifier(name)); |
| 74 | + } |
| 75 | + else { |
| 76 | + raw('<'); |
| 77 | + raw(name); |
| 78 | + } |
| 79 | + |
| 80 | + if (open.attributes) { |
| 81 | + for (let i = 0; i < open.attributes.length; i++) { |
| 82 | + const attr = open.attributes[i]; |
| 83 | + raw(' '); |
| 84 | + if (t.isJSXSpreadAttribute(attr)) { |
| 85 | + raw('...'); |
| 86 | + expr(attr.argument); |
| 87 | + continue; |
| 88 | + } |
| 89 | + const { name, value } = attr; |
| 90 | + raw(name.name); |
| 91 | + if (value) { |
| 92 | + raw('='); |
| 93 | + if (value.expression) { |
| 94 | + expr(value.expression); |
| 95 | + } |
| 96 | + else if (t.isStringLiteral(value)) { |
| 97 | + escapePropValue(value); |
| 98 | + } |
| 99 | + else { |
| 100 | + expr(value); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + const children = t.react.buildChildren(node); |
| 107 | + if (htmlOutput || children && children.length !== 0) { |
| 108 | + raw('>'); |
| 109 | + for (let i = 0; i < children.length; i++) { |
| 110 | + let child = children[i]; |
| 111 | + if (t.isStringLiteral(child)) { |
| 112 | + // @todo - expose `whitespace: true` option? |
| 113 | + escapeText(child.value); |
| 114 | + } |
| 115 | + else if (t.isJSXElement(child)) { |
| 116 | + processNode(child); |
| 117 | + } |
| 118 | + else { |
| 119 | + expr(child); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if (name.match(/^[A-Z]/)) { |
| 124 | + raw('</'); |
| 125 | + expr(t.identifier(name)); |
| 126 | + raw('>'); |
| 127 | + } |
| 128 | + else { |
| 129 | + raw('</'); |
| 130 | + raw(name); |
| 131 | + raw('>'); |
| 132 | + } |
| 133 | + } |
| 134 | + else { |
| 135 | + raw('/>'); |
| 136 | + } |
| 137 | + |
| 138 | + if (isRoot) { |
| 139 | + commit(); |
| 140 | + const template = t.templateLiteral(quasis, expressions); |
| 141 | + const replacement = t.taggedTemplateExpression(tag, template); |
| 142 | + path.replaceWith(replacement); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return { |
| 147 | + name: 'transform-jsx-to-htm', |
| 148 | + inherits: jsx, |
| 149 | + visitor: { |
| 150 | + JSXElement(path) { |
| 151 | + let quasisBefore = quasis.slice(); |
| 152 | + let expressionsBefore = expressions.slice(); |
| 153 | + let bufferBefore = buffer; |
| 154 | + |
| 155 | + buffer = ''; |
| 156 | + quasis.length = 0; |
| 157 | + expressions.length = 0; |
| 158 | + |
| 159 | + processNode(path.node, path, true); |
| 160 | + |
| 161 | + quasis = quasisBefore; |
| 162 | + expressions = expressionsBefore; |
| 163 | + buffer = bufferBefore; |
| 164 | + } |
| 165 | + } |
| 166 | + }; |
| 167 | +} |
0 commit comments