Skip to content

Commit

Permalink
Add fragments to lib/babel
Browse files Browse the repository at this point in the history
  • Loading branch information
finnp committed Mar 20, 2018
1 parent 8be9029 commit 52b25df
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions lib/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ module.exports = (babel) => {
[t.stringLiteral(text)]
)

/**
* Returns a node that creates a fragment.
*/
const createFragment = (text) =>
t.callExpression(
t.memberExpression(t.identifier('document'), t.identifier('createDocumentFragment')),
[]
)

/**
* Returns a node that sets a DOM property.
*/
Expand Down Expand Up @@ -154,8 +163,10 @@ module.exports = (babel) => {
const expressions = path.node.expressions
const expressionPlaceholders = expressions.map((expr, i) => getPlaceholder(i))

const root = hyperx(transform, { comments: true }).apply(null,
[quasis].concat(expressionPlaceholders))
const root = hyperx(transform, {
comments: true,
createFragment: children => transform('nanohtml-fragment', {}, children)
}).apply(null, [quasis].concat(expressionPlaceholders))

/**
* Convert placeholders used in the template string back to the AST nodes
Expand Down Expand Up @@ -188,12 +199,16 @@ module.exports = (babel) => {

const result = []

// Use the SVG namespace for svg elements.
if (SVG_TAGS.includes(tag)) {
state.svgNamespaceId.used = true
result.push(t.assignmentExpression('=', id, createNsElement(state.svgNamespaceId, tag)))
if (tag === 'nanohtml-fragment') {
result.push(t.assignmentExpression('=', id, createFragment()))
} else {
result.push(t.assignmentExpression('=', id, createElement(tag)))
// Use the SVG namespace for svg elements.
if (SVG_TAGS.includes(tag)) {
state.svgNamespaceId.used = true
result.push(t.assignmentExpression('=', id, createNsElement(state.svgNamespaceId, tag)))
} else {
result.push(t.assignmentExpression('=', id, createElement(tag)))
}
}

Object.keys(props).forEach((propName) => {
Expand Down

0 comments on commit 52b25df

Please sign in to comment.