@@ -8,21 +8,30 @@ function babelPluginReactLive(babel, options) {
88 ? JSON . parse ( fs . readFileSync ( options . prettierPath , 'utf-8' ) )
99 : { }
1010
11- const wrappCodeInside = ( code ) => {
12- const formattedCode = prettier . format ( code , {
11+ const convertCodeToJSX = ( children ) => {
12+ // So prettier can do its work,
13+ // we wrap our JSX children in a fragment.
14+ // But we also remvoe it afterwards.
15+ let code =
16+ children . length > 1
17+ ? `<>${ children . join ( '\n' ) } </>`
18+ : children . join ( '\n' )
19+
20+ code = prettier . format ( code , {
1321 ...prettierrc ,
1422 parser : 'babel' ,
1523 } )
1624
25+ // Prettier adds a leading ;
26+ // And we also escape `
27+ code = code . replace ( / ^ ; / , '' ) . replace ( / ` / g, '\\`' )
28+
29+ if ( children . length > 1 ) {
30+ code = code . replace ( / ^ < > | < \/ > $ | ^ \s { 2 } / gm, '' )
31+ }
32+
1733 return t . jsxExpressionContainer (
18- t . templateLiteral (
19- [
20- t . templateElement ( {
21- raw : formattedCode . replace ( / ^ ; / , '' ) . replace ( / ` / g, '\\`' ) ,
22- } ) ,
23- ] ,
24- [ ]
25- )
34+ t . templateLiteral ( [ t . templateElement ( { raw : code } ) ] , [ ] )
2635 )
2736 }
2837
@@ -129,13 +138,7 @@ function babelPluginReactLive(babel, options) {
129138 )
130139 }
131140
132- path . node . children = [
133- wrappCodeInside (
134- children . length > 1
135- ? `<>${ children . join ( '\n' ) } </>`
136- : children . join ( '\n' )
137- ) ,
138- ]
141+ path . node . children = [ convertCodeToJSX ( children ) ]
139142
140143 path . replaceWith ( t . identifier ( path . toString ( ) ) )
141144 }
0 commit comments