Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/babel-plugin-htm/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ export default function htmBabelPlugin({ types: t }, options = {}) {
const statics = path.node.quasi.quasis.map(e => e.value.raw);
const expr = path.node.quasi.expressions;
currentExpressions = expr;
path.replaceWith(html(statics, ...expr.map((p, i) => `$$$_h_[${i}]`)));
let jsx = html(statics, ...expr.map((p, i) => `$$$_h_[${i}]`));
jsx = Array.isArray(jsx) ? t.arrayExpression(jsx) : jsx;
path.replaceWith(jsx);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ function build(statics) {
str.replace(/(?:'.*?'|".*?"|([A-Z]))/g, (s, c) => c ? ':::'+c : s) + (a ? '</'+name+'>' : '')
))
.trim();
return Function('h', '$_h', 'return ' + walk((TEMPLATE.content || TEMPLATE).firstChild));
const rootEls = Array.from((TEMPLATE.content || TEMPLATE).children);
if (rootEls.length > 1) {
return Function('h', '$_h', 'return [' + rootEls.map(walk).join(',') + ']');
}
return Function('h', '$_h', 'return ' + walk(rootEls[0]));
}

/** Traverse a DOM tree and serialize it to hyperscript function calls */
Expand Down
12 changes: 12 additions & 0 deletions test/babel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ describe('htm/babel', () => {
).toBe(`var name="world";h("div",{id:"hello"},["hello, ",name]);`);
});

test('multiple root level elements ', () => {
expect(
transform('html`<div id=hello>hello</div><span>hola</span>`;', {
babelrc: false,
compact: true,
plugins: [
htmBabelPlugin
]
}).code
).toBe(`[h("div",{id:"hello"},["hello"]),h("span",{},["hola"])];`);
});

test('inline vnode transformation: (pragma:false)', () => {
expect(
transform('var name="world",vnode=html`<div id=hello>hello, ${name}</div>`;', {
Expand Down