Skip to content

Commit

Permalink
fix(external): execute transforms before JSX transform (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer authored Nov 14, 2020
1 parent cb80cec commit eac1703
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ export default function({ types: t }) {
// possibly others so we traverse from here or else
// dynamic values in classNames could be incorrect
path.traverse(jsxVisitors, state)

// Transpile external styles
path.traverse(externalStylesVisitor, state)
},
exit({ node, scope }, state) {
if (
Expand All @@ -306,10 +309,7 @@ export default function({ types: t }) {
const importDeclaration = createReactComponentImportDeclaration(state)
node.body.unshift(importDeclaration)
}
},

// Transpile external styles
...externalStylesVisitor
}
}
}

Expand Down
25 changes: 25 additions & 0 deletions test/external.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,28 @@ test('Makes sure that style nodes are not re-used', async t => {

t.snapshot(code)
})

test('Make sure that it works with the new automatic transform', async t => {
const { code } = await transformSource(
`
import { css } from "styled-jsx/css";
const A = css.resolve\`
div {
color: green;
}
\`;
export default function IndexPage() {
return JSON.stringify(A);
}
`,
{
babelrc: false,
presets: [['@babel/preset-react', { runtime: 'automatic' }]],
plugins: [plugin]
}
)

t.snapshot(code)
})
17 changes: 17 additions & 0 deletions test/snapshots/external.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,20 @@ Generated by [AVA](https://ava.li).
<p className={`jsx-${styles.__hash}`}>test</p>␊
<_JSXStyle id={styles.__hash}>{styles}</_JSXStyle>␊
</div>);`

## Make sure that it works with the new automatic transform

> Snapshot 1
`import { jsx as _jsx } from "react/jsx-runtime";␊
import _JSXStyle from "styled-jsx/style";␊
const A = {␊
styles: /*#__PURE__*/_jsx(_JSXStyle, {␊
id: "2723508961",␊
children: "div.jsx-2723508961{color:green;}"␊
}),␊
className: "jsx-2723508961"␊
};␊
export default function IndexPage() {␊
return JSON.stringify(A);␊
}`
Binary file modified test/snapshots/external.js.snap
Binary file not shown.

0 comments on commit eac1703

Please sign in to comment.