-
Notifications
You must be signed in to change notification settings - Fork 27.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Port babel-plugin-styled-components to SWC #30802
Comments
Unclear if this is relevant, but it looks like SWC fixed the compiler to not break with styled-components. Still probably not enough to close this issue for customization/optionality, but maybe if updated, swc can compile SC at a base level now? |
The linked issue is unrelated as that one is about the minifier. This issue is about implementing the transforms that are critical for running styled-components correctly when doing SSR. |
## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint` Related to: #30802
Posted an update on the feedback thread: #30174 (comment). It's available to try now. |
Awesome ⭐ I'm a bit confused on how to actually implement. Not sure if this is on a "normal" I'm running version This actually works, but am getting no displayNames (component names) on the components. {
styledComponent: true
} This works as expected. Do you guys have an estimate on when this is not experimental any more? {
experimental: {
styledComponent: true
}
} |
Right now is available only on Thanks to @kdy1 and @timneutkens for this contribution! I'll wait for |
Ahh this explains why css prop failed.. I thought it was supported. Is it planned later? |
Hi @kdy1, are you planning to publish the styled-components swc plugin to npm as well? Would be cool if it could also be used with the swc-loader |
Wait you mean like Emotion's css prop? I've been using it on next 12 already. You can change the JSX pragma trough jsconfig.json |
Thank you @timneutkens, we're all very excited about the work you all have been doing with SWC. Our main blocker right now is 'referring to other components' feature of styled-components. I'm not exactly sure what transform this is using, but I wanted to flag it in case it's not on your roadmap already. |
That feature doesn't seem to rely on any compilation, it's just leveraging tagged template literals. Are you running into an issue with that feature or just checking if it's supported? |
Do you expect to support https://xstyled.dev/docs/migrate-from-styled-components/#babel-plugin |
Sorry for the late response. I finally managed to handle issue emails and saw this. But it will require some more work because currently only |
Nice, thank you @kdy1! From what I see, |
When opting in to use SWC I noticed that media-queries like below have a new behaviour. Before, the query would take precedence but now the const P = styled(BaseP)`
margin-bottom: 60px;
${mq.breakpoint('sm')} {
margin: auto auto 120px;
}
`;
const CenterP = styled(P)`
text-align: center;
margin: 23px auto;
`; |
@KimPaow That has happened randomly in our applications even with babel. Styled-components FAQ has a solution: https://styled-components.com/docs/faqs#how-can-i-override-styles-with-higher-specificity |
@roxeteer That seems to be working, thanks for the tip! |
@roxeteer It's not random. Media queries don't increase specificity. The wrapping styled-component overrides the wrapped one because the rules come later. @KimPaow The behavior you describe happens with Babel too, it's not related to SWC. https://codesandbox.io/s/stupefied-grass-e0cox?file=/src/App.js |
@agriffis I think it's related to the fact only |
@agriffis It's random in the sense that it depends on the order that styled-components outputs the rules, and it doesn't seem to be constant. |
@roxeteer Hmm, that really shouldn't happen... 😦 I understand you might not have a reproducer handy, but if you do, I would love to see it and an issue at https://github.com/styled-components/styled-components/issues. The specificity hack in the s-c doc shouldn't be necessary in this case. (Asking as a contributor to styled-components.) @kdy1 Are you referring to transpilation of tagged template literals? In theory that's only an optimization and shouldn't change behavior. Or do you mean something else? |
I have still same issue on the nextjs 13.4.1 even though I configured such as document of nextjs.
|
I know this is probably not very relevant to the issue at hand, but I'll give you my 2 cents. It's been a very long time since this issue was opened. At some point, I just decided not to use SWC on the project that used the |
We did the same thing. The issue in this ticket wasn't the motivator for switching, exactly, but switching means it became mostly a legacy issue |
@thexpand Can you just tell me if in dev you can trace the file where your style is? |
Hey guys, I'm using the newest Next.js with the SWC compiler and styled-components work fine, but I don't see the more useful class names in the developer tools, which is supposed to be controlled with the |
Honestly, the fact that this ticket has been open for almost two years should tell you everything you need to know: the Next.js team just doesn't care about supporting Styled Components. Similarly, don't expect the Styled Components devs to help out in any way: when I asked the guy who answered basically said that he was the only developer left, and he could barely keep the library maintained on his own, so he doesn't have time to help out other projects. Sadly, in 2023, if you want to use Next.js, you either have to abandon Styled Components (as I did), or live with a flawed integration. |
This got me to a solution for getting the
The following are my // next.config.js
module.exports = {
return {
env,
useFileSystemPublicRoutes: false,
typescript: {
// TODO: Remove once project is fully typed
ignoreBuildErrors: true,
},
compiler: {
styledComponents: true,
},
}
}
|
@Lwdthe1 If you have |
That's what I heard elsewhere, but that's not true. If I remove However, it does appear the |
In case someone else faces the same issue, I've fixed it by doing what this comment suggested: #46605 (comment) Sounds ridiculous, but it works. |
May I know why minify is not supporting at all? I am seeing standalone/.build folder js contents not getting minified. |
Hey @kdy1 👋 As referred to by @2Pacalypse- and @d-nakajima, how come that this is the case?
Is there any chance you can fix this behavior going forward, and allow us to also use the following style of importing as well? import { styled } from 'styled-components'; (This is the recommended way as of https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-named-as-default.md) |
I misread it. It's a third-party eslint rule |
Filed swc-project/plugins#224 anyway, but I have lots of tasks with higher priority |
This implements `minify` option in styled-components plugin. The minification algorithm and unit tests are ported from `babel-plugin-styled-components`. Note that CSS code in helper functions (`keyframes`, `css`, etc.) are currently not always processed. I think there is a bug in `is_helper` function, so I'll fix it in another pull request later. fixes swc-project#143 cf. vercel/next.js#30802
…roject#237) This implements `transpileTemplateLiterals` option in styled-components plugin. cf. vercel/next.js#30802
This implements `pure` option in styled-components plugin. cf. vercel/next.js#30802
### What? Update swc crates. Diff: swc-project/swc@09b3003...c566b73 ### Why? To fix bugs, and improve minification. ### How? turbopack counterpart: vercel/turborepo#6472 - Closes PACK-1972 - Closes vercel#30802 --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Can confirm that |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
This Babel plugin is the most used customization of
.babelrc
.The plugin is split up in multiple separate transforms:
https://github.com/styled-components/babel-plugin-styled-components/blob/a26774e599c82c72e8793989f60310d29c181fac/src/index.js
The defaults are here: https://github.com/styled-components/babel-plugin-styled-components/blob/950692b92855f17609e4281d5081e3d2acbf4b6b/src/utils/options.js
The critical one which makes styled-components not have hydration issues with Next.js is
displayNameAndId
. All the other ones seem to be optimizations. Because of this I'd start by porting thedisplayNameAndId
transform and then check what else can be ported.Looking at the defaults the only transform that is disabled by default is the pure annotations one.
List of transforms:
styledComponents: true
styledComponents: true
, disabled in production to reduce file sizeThe text was updated successfully, but these errors were encountered: