Open theboyWhoCriedWoolf opened 5 years ago
I'd double check the plugin is being added for your production build... if you use something like the "env" setting in your babel config and change the presets or plugins at all you have to recompose all of them. Do you have a reproduction repo?
Hey there, I created a small reproduction repo of the issue where I simply created & exported a button. You can find the related code here:
https://github.com/3nvi/styled-components-css-rollup
You can verify that it's not working by visiting the following CodeSandbox project, where the repo is installed & utilized:
https://codesandbox.io/s/dank-https-8mee9
In addition, I also found the following 2 threads on Spectrum that unfortunately received no answer:
Any help from the SC team is greatly appreciated.
Thanks in advance for your time!
Ok I figured it out.
For anyone facing a similar problem, the issue derives from the way your code is transformed. babel-plugin-styled-components
tries to apply the changes to jsx
elements only. Thus, if you have another process transpiling your code before your code reaches the babel-loader
, then the babel-plugin-styled-components
plugin won't take effect and your css
prop will not be working.
For me, it was because I was using typescript
with jsx: react
in my tsconfig.json
. Because of the fact that I had to convert .ts
files to .js
, I was running the typescript compiler before the babel transformations, which was also turning <div>
to React.createElement('div')
. That transformation caused the issue.
I resolved it by not allowing typescript to make any changes to JSX (i.e. jsx: preserve
) and handling the transformation in babel through @babel/preset-react
preset. This fixed it, since now babel (as well as all the presets & plugins) received the original JSX code and the plugin can apply its changes properly.
Hope it helps!
You saved my day @3nvi Thanks for posting how you resolved your issue. Finally I was able to make my TS library run with the SC css prop as well. If anyone stumbles upon this issue and needs another example as reference, check out react-table-library which uses Rollup + TS + SC css prop via babel-plugin-styled-components.
@3nvi Almost 3 years later you've saved my day as well, thank you!
Cheers 😃
I am creating a styled-system using styled-components. Everything during the development works correctly, after using Rollup to generate my reusable library, CSS prop no longer works.
When importing this into a different library the Button component comes out red.
I am using Rollup with a simple configuration which uses
babel-plugin-styled-component
.Could this be something to do with Rollup? I would assume that once transpiled this should work anywhere?