Skip to content

Commit 80ac40f

Browse files
committed
fix(babel-plugin-transform-svg): support template that only return a single node
Closes #223
1 parent 00a2625 commit 80ac40f

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

packages/babel-plugin-transform-svg-component/src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ const plugin = (api, opts) => ({
1616
Program(path) {
1717
const { types: t } = api
1818
const template = opts.template || defaultTemplate
19-
path.node.body = template(api, opts, {
19+
const body = template(api, opts, {
2020
componentName: t.identifier(opts.state.componentName),
2121
props: getProps(api, opts),
2222
imports: getImport(api, opts),
2323
exports: getExport(api, opts),
2424
jsx: path.node.body[0].expression,
2525
})
26+
if (Array.isArray(body)) {
27+
path.node.body = body
28+
} else {
29+
path.node.body = [body]
30+
}
2631
path.replaceWith(path.node)
2732
},
2833
},

packages/babel-plugin-transform-svg-component/src/index.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ export default MyComponent;"
4545
`)
4646
})
4747

48+
it('should handle template that does not return an array', () => {
49+
const { code } = testPlugin('<svg><div /></svg>', {
50+
template: ({ template }, opts, { jsx }) => template.ast`${jsx}`,
51+
state: { componentName: 'SvgComponent' },
52+
})
53+
expect(code).toMatchInlineSnapshot(`"<svg><div /></svg>;"`)
54+
})
55+
4856
it('should work with ref', () => {
4957
const { code } = testPlugin('<svg><div /></svg>', {
5058
state: { componentName: 'SvgComponent' },

0 commit comments

Comments
 (0)