Skip to content

Commit eac1703

Browse files
authored
fix(external): execute transforms before JSX transform (#677)
1 parent cb80cec commit eac1703

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

src/babel.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ export default function({ types: t }) {
290290
// possibly others so we traverse from here or else
291291
// dynamic values in classNames could be incorrect
292292
path.traverse(jsxVisitors, state)
293+
294+
// Transpile external styles
295+
path.traverse(externalStylesVisitor, state)
293296
},
294297
exit({ node, scope }, state) {
295298
if (
@@ -306,10 +309,7 @@ export default function({ types: t }) {
306309
const importDeclaration = createReactComponentImportDeclaration(state)
307310
node.body.unshift(importDeclaration)
308311
}
309-
},
310-
311-
// Transpile external styles
312-
...externalStylesVisitor
312+
}
313313
}
314314
}
315315

test/external.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,28 @@ test('Makes sure that style nodes are not re-used', async t => {
113113

114114
t.snapshot(code)
115115
})
116+
117+
test('Make sure that it works with the new automatic transform', async t => {
118+
const { code } = await transformSource(
119+
`
120+
import { css } from "styled-jsx/css";
121+
122+
const A = css.resolve\`
123+
div {
124+
color: green;
125+
}
126+
\`;
127+
128+
export default function IndexPage() {
129+
return JSON.stringify(A);
130+
}
131+
`,
132+
{
133+
babelrc: false,
134+
presets: [['@babel/preset-react', { runtime: 'automatic' }]],
135+
plugins: [plugin]
136+
}
137+
)
138+
139+
t.snapshot(code)
140+
})

test/snapshots/external.js.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,20 @@ Generated by [AVA](https://ava.li).
225225
<p className={`jsx-${styles.__hash}`}>test</p>␊
226226
<_JSXStyle id={styles.__hash}>{styles}</_JSXStyle>␊
227227
</div>);`
228+
229+
## Make sure that it works with the new automatic transform
230+
231+
> Snapshot 1
232+
233+
`import { jsx as _jsx } from "react/jsx-runtime";␊
234+
import _JSXStyle from "styled-jsx/style";␊
235+
const A = {␊
236+
styles: /*#__PURE__*/_jsx(_JSXStyle, {␊
237+
id: "2723508961",␊
238+
children: "div.jsx-2723508961{color:green;}"␊
239+
}),␊
240+
className: "jsx-2723508961"␊
241+
};␊
242+
export default function IndexPage() {␊
243+
return JSON.stringify(A);␊
244+
}`

test/snapshots/external.js.snap

159 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)