Skip to content

Commit 619c329

Browse files
authored
Merge pull request #189 from developit/collapse-whitespace
Collapse whitespace when { terse: true }
2 parents ed1c620 + e47bea6 commit 619c329

File tree

1 file changed

+6
-4
lines changed
  • packages/babel-plugin-transform-jsx-to-htm

1 file changed

+6
-4
lines changed

packages/babel-plugin-transform-jsx-to-htm/index.mjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import jsx from '@babel/plugin-syntax-jsx';
44
* @param {Babel} babel
55
* @param {object} [options]
66
* @param {string} [options.tag='html'] The tagged template "tag" function name to produce.
7-
* @param {boolean} [options.terse=false] Output `<//>` for closing component tags
7+
* @param {boolean} [options.terse=false] Collapse whitespace and use `<//>` for closing component tags
88
* @param {string | boolean | object} [options.import=false] Import the tag automatically
99
*/
1010
export default function jsxToHtmBabelPlugin({ types: t }, options = {}) {
1111
const tagString = options.tag || 'html';
1212
const tag = dottedIdentifier(tagString);
1313
const importDeclaration = tagImport(options.import || false);
14+
const terse = options.terse === true;
1415

1516
function tagImport(imp) {
1617
if (imp === false) {
@@ -118,8 +119,9 @@ export default function jsxToHtmBabelPlugin({ types: t }, options = {}) {
118119
for (let i = 0; i < children.length; i++) {
119120
let child = children[i];
120121
if (t.isStringLiteral(child)) {
121-
// @todo - expose `whitespace: true` option?
122-
escapeText(child.value);
122+
let value = child.value;
123+
if (terse) value = value.replace(/\n+/g, ' ');
124+
escapeText(value);
123125
}
124126
else if (t.isJSXElement(child)) {
125127
processNode(child);
@@ -131,7 +133,7 @@ export default function jsxToHtmBabelPlugin({ types: t }, options = {}) {
131133

132134
if (!isFragment) {
133135
if (isComponentName(name)) {
134-
if (options.terse) {
136+
if (terse) {
135137
raw('<//>');
136138
}
137139
else {

0 commit comments

Comments
 (0)