Skip to content

Commit b7a5800

Browse files
JSX whitespace fix.
1 parent d0c2c5b commit b7a5800

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/vanillajsx.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const babelPluginVanillaJSX: babel.PluginItem = {
2525
else if (child.type === 'JSXExpressionContainer' || child.type === 'JSXSpreadChild')
2626
path.replaceWith(child.expression);
2727
else if (child.type === 'JSXText')
28-
path.replaceWith(t.stringLiteral(child.value));
28+
path.replaceWith(t.stringLiteral(trimJsxWhitespace(child.value)));
2929
return;
3030
}
3131

@@ -98,7 +98,7 @@ function pushChildren(parent: babel.types.ObjectExpression, path: babel.NodePath
9898
if (c.type === 'JSXElement') children.push(c);
9999
else if (c.type === 'JSXFragment') children.push(c);
100100
else if (c.type === 'JSXSpreadChild') children.push(t.spreadElement(c.expression));
101-
else if (c.type === 'JSXText') children.push(t.stringLiteral(c.value));
101+
else if (c.type === 'JSXText') children.push(t.stringLiteral(trimJsxWhitespace(c.value)));
102102
else if (c.expression.type !== 'JSXEmptyExpression') children.push(c.expression);
103103
}
104104

@@ -124,3 +124,10 @@ function convertMember(v: babel.types.JSXMemberExpression): babel.types.MemberEx
124124
t.identifier(v.property.name)
125125
);
126126
}
127+
128+
function trimJsxWhitespace(str: string): string {
129+
return (str
130+
.replace(/^ *\n+ */mg, '')
131+
.replace(/ *\n+ *$/mg, '')
132+
.replace(/ *\n+ */mg, ' '));
133+
}

0 commit comments

Comments
 (0)