-
-
Notifications
You must be signed in to change notification settings - Fork 140
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
displayName is broken since v1.10.3 #261
Comments
It seems that the plugin only works if I use |
This is a duplicate of #243, I think. My current suspicion is something happening inside of @babel/core |
+1 for @babel/core changes // styled-components.ts
import * as styledComponents from 'styled-components';
import { Theme } from 'theme';
const {
default: styled,
css,
createGlobalStyle,
keyframes,
ThemeProvider,
} = styledComponents as styledComponents.ThemedStyledComponentsModule<Theme>;
export { css, createGlobalStyle, keyframes, ThemeProvider };
export default styled; When I replace imports to the original lib in the target component displayName start work correctly // import styled from 'utils/styled-components/styled-components'; <- displayName don't generates
import styled from 'styled-components'; // <- displayName generates In the beginning, I blame // styled-components.js
import * as styledComponents from 'styled-components';
const { default: styled, css, createGlobalStyle, keyframes, ThemeProvider } = styledComponents;
export { css, createGlobalStyle, keyframes, ThemeProvider };
export default styled; Then I try to add ignoring for babel transpile of my styled-components reexport file and everything start working🎉 (except the types actually 😞) // babel.config.js
module.exports ={
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: [
['babel-plugin-styled-components', { ssr: false, displayName: true }],
'@babel/plugin-transform-react-constant-elements',
'@babel/plugin-proposal-export-default-from',
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-proposal-object-rest-spread',
// eslint-disable-next-line no-magic-numbers
['@babel/plugin-transform-runtime', { corejs: 2 }],
],
overrides: [
{
test: /\.(ts|tsx)$/,
presets: ['@babel/preset-typescript'],
plugins: [
'babel-plugin-ts-optchain',
// eslint-disable-next-line no-magic-numbers
['@babel/plugin-transform-runtime', { corejs: 2 }],
],
},
],
ignore: ['./**/styled-components.js'],
}; I think the reason in that side effect: And suspicion to // babel.config.js
module.exports ={
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: [
['babel-plugin-styled-components', { ssr: false, displayName: true }],
],
}; In that case nothing work with the same simple styled-components reexport // styled-components.js
import * as styledComponents from 'styled-components';
const { default: styled, css, createGlobalStyle, keyframes, ThemeProvider } = styledComponents;
export { css, createGlobalStyle, keyframes, ThemeProvider };
export default styled; |
I found the workaround solution for that case. The reason in // babel.config.js
module.exports = {
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: [
['babel-plugin-styled-components', { ssr: false, displayName: true }],
'@babel/plugin-transform-react-constant-elements',
'@babel/plugin-proposal-export-default-from',
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-proposal-object-rest-spread',
// eslint-disable-next-line no-magic-numbers
['@babel/plugin-transform-runtime', { corejs: 2 }],
],
overrides: [
// --- The Saver:
{
test: /styled-components\.(ts|tsx)$/,
presets: ['@babel/preset-react', '@babel/preset-typescript'],
},
// ---
{
test: /\.(ts|tsx)$/,
presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'],
plugins: [
'babel-plugin-ts-optchain',
// eslint-disable-next-line no-magic-numbers
['@babel/plugin-transform-runtime', { corejs: 2 }],
],
},
],
}; // styled-components.ts
import * as styledComponents from 'styled-components';
import { Theme } from 'theme';
const {
default: styled,
css,
createGlobalStyle,
keyframes,
ThemeProvider,
} = styledComponents as styledComponents.ThemedStyledComponentsModule<Theme>;
export { css, createGlobalStyle, keyframes, ThemeProvider };
export default styled; |
I have the same snippet of typing from the styled-components documentation, I can try this override and see if it works for me also. Nice work @baybara-pavel :) |
@frisbee09 I hope it helps you to 🙂 |
On further inspection, could this be broken due to #230 It might be that the plugin currently does not support a typed theme as specified in the This would also mean we're seeing the same issue as found in #238 |
same issue here, although we don't use typescript, we just reexported |
If you are using something like create-react-app, try this instead: https://styled-components.com/docs/tooling#babel-macro |
Is anyone else still experiencing this? I'm not getting component displayNames in my SC classes on I tried downgrading to |
@BadDox Yeah this is still affecting me. The babel macro alternative works, but I've been unable to get that to work in a mono-repo situation where my components are in a different package. Also I'm not sold on the idea of having to use a completely different import in that case. |
Also running into this. Seems like I'm only running into issues when using ts-loader in my webpack config. Example minimal repo reproducing the issue: https://github.com/bradtgmurray/babel-styled-components-test-repo This branch fixes the issue by flipping from ts-loader to using babel to transpile my typescript. https://github.com/bradtgmurray/babel-styled-components-test-repo/compare/fixed?expand=1 |
@bradtgmurray That's really interesting. I'm not using TS at all and I have the problem. I have a minimal example built on a create-react-app in this thread: rebassjs/rebass#434 (comment) |
@bradtgmurray I just created a PR (#279) for another issue related to |
While using next.js. I fixed the issue by manually adding into
This is my
|
import styled from 'styled-components' But I don't know what the side effects are |
…, force displayName: true option Bug description: styled-components/babel-plugin-styled-components#261
Based on comparing 1.10.2 and 1.10.3 It seems that the problem starts here:
The local name is ignored when import statements are used and it is not cached.
importLocalName is used in isStyles.
isStyled is used here
and is always false in my environment, so addConfig is never called and no displayName is configured. By removing
the displayname will be rendered again. |
I couldn't find a working solution either. const Content = styled.div.attrs({ 'data-testid': 'Content' })`
margin-top: 21px;
` |
We were struggling with this as well. For us, I noticed the same thing as @baybara-pavel, that when we used
This is a double-win IMO, since folks on our team would accidentally use the original import all the time. Now I can get rid of that, and I have a much better debugging experience (and still retain types!). 🎊 |
Yes, I think best practice here changed and was updated in the docs. I too migrated to the new approach. There’s a reason why all three of us re-exported to use SC with typescript; it was in the docs before!
… On 8 Jun 2021, at 21:47, Mark Godwin ***@***.***> wrote:
We were struggling with this as well. For us, I noticed the same thing as @baybara-pavel, that when we used import styled from 'styled-components' rather than import styled from 'src/styledComponents' the debug names appeared correctly in the DOM. We use typescript over here and originally defined a file that re-exported the constants from the original import for typing purposes. However, looking at the docs on the styled components site, it's actually not recommended or necessary to re-export everything from a src/styledComponents file anymore! https://styled-components.com/docs/api#styling-components
That's it! We're able to use styled-components just by using any original import.
import styled, { createGlobalStyle, css } from 'styled-components';
// theme is now fully typed
This is a double-win IMO, since folks on our team would accidentally use the original import all the time. Now I can get rid of that, and I have a much better debugging experience (and still retain types!). 🎊
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
It still doesn't work for most cases, since `styled` is not imported from `styled-components` for typing reasons. More info: styled-components/babel-plugin-styled-components#261 (comment) There is a suggestion here about how to stick with importing `styled` from `styled-components` and fix typing issue by augmenting `DefaultTheme` type. https://styled-components.com/docs/api#styling-components
We continue to have this issue with styles not being applied to the correct element. Have yet to find any solution other then to roll back to Please help! No idea how to fix this and been limping along with 1.10.3 for months now. |
Using
|
Nevermind, for me, I accidentally had |
Hello. Starting with version 1.10.3, the plugin stops adding displayName to generated classes. This option worked in 1.10.2 and does not work in all subsequent versions. Here is a repository with a small example to verify this:
https://github.com/psixdev/babel-plugin-styled-components-bug
After loading the page, I get this html:
If I used version 1.10.2, it would be different:
Is this a bug in the library, or is something wrong in my application?
The text was updated successfully, but these errors were encountered: