Skip to content
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

(...).createGlobalStyle.withConfig is not a function #315

Closed
luizwhite opened this issue Feb 23, 2021 · 4 comments · Fixed by #340 · May be fixed by tiff-es/tiffanyPortfolio-gatsby-styledcomp#237 or tiff-es/tiffanyPortfolio-gatsby-styledcomp#246

Comments

@luizwhite
Copy link

luizwhite commented Feb 23, 2021

Similar to this comment workaround, when i use createGlobalStyle like this:

import * as styled from 'styled-components';
export const GlobalStyle = styled.createGlobalStyle``;, i get the error (...).createGlobalStyle.withConfig is not a function on a Next.js / Typescript project

The bug is resolved when I use the solution presented in the comment above, using CommonJS require:
const createGlobalStyle = require('styled-components').createGlobalStyle
export const GlobalStyle = createGlobalStyle``;

Is it a bug or a setup issue?

@kitten
Copy link
Member

kitten commented Feb 23, 2021

The targeting of the Babel plug-in isn't quite that precise, so it never associates the styled-components import with your local names.

So when you import the entire namespace as styled the Babel plugin ticks off all its boxes:

  • You're importing styled-components and its locally bound
  • You have a variable called styled that's called using a tag as styled.NAME

To prevent this you can import the createGlobalStyle helper separately and not pull in the entire namespace, I believe.

This does look like a bug since this detector here is faulty:

localName = specifier.node.local.name

@luizwhite
Copy link
Author

luizwhite commented Feb 23, 2021

Yup, thx @kitten!
I was just using that import style because of this issue with prettier formatting a createGlobalStyle component through vscode-styled-components plugin

@mqklin
Copy link

mqklin commented Feb 26, 2021

Same error for me, simple reproduce:

import * as Styled from 'styled-components';
console.log(Styled.css``); // css.withConfig is not a function

But if I do import {css} from 'styled-components'; error is gone

@quantizor
Copy link
Collaborator

Can y'all try out 1.13.2-test.1 and let me know if it fixes your issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment