Skip to content

Commit

Permalink
fix(babel-plugin-transform-svg): support template that only return a …
Browse files Browse the repository at this point in the history
…single node

Closes #223
  • Loading branch information
gregberge committed Nov 8, 2018
1 parent 00a2625 commit 80ac40f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/babel-plugin-transform-svg-component/src/index.js
Expand Up @@ -16,13 +16,18 @@ const plugin = (api, opts) => ({
Program(path) {
const { types: t } = api
const template = opts.template || defaultTemplate
path.node.body = template(api, opts, {
const body = template(api, opts, {
componentName: t.identifier(opts.state.componentName),
props: getProps(api, opts),
imports: getImport(api, opts),
exports: getExport(api, opts),
jsx: path.node.body[0].expression,
})
if (Array.isArray(body)) {
path.node.body = body
} else {
path.node.body = [body]
}
path.replaceWith(path.node)
},
},
Expand Down
Expand Up @@ -45,6 +45,14 @@ export default MyComponent;"
`)
})

it('should handle template that does not return an array', () => {
const { code } = testPlugin('<svg><div /></svg>', {
template: ({ template }, opts, { jsx }) => template.ast`${jsx}`,
state: { componentName: 'SvgComponent' },
})
expect(code).toMatchInlineSnapshot(`"<svg><div /></svg>;"`)
})

it('should work with ref', () => {
const { code } = testPlugin('<svg><div /></svg>', {
state: { componentName: 'SvgComponent' },
Expand Down

0 comments on commit 80ac40f

Please sign in to comment.