styled-components / styled-components

Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
https://styled-components.com
MIT License
40.09k stars 2.47k forks source link

createGlobalStyle not working version >= 5.3.1 #4285

Closed favger closed 1 month ago

favger commented 1 month ago

I've initiated a new project utilizing Vite, React, and Typescript, and I've opted for styled-components for UI development. However, I'm encountering difficulties in running createGlobalStyle. Interestingly, when I downgrade styled-components to version 5.3.0, the issue resolves, but ideally, I'd prefer to utilize the latest version of the library.

My Package.json:

    "dependencies": {
            ...
        "react": "^18.2.0",
        "react-dom": "^18.2.0",
        "styled-components": "^6.8.1"
    },
    "devDependencies": {
            ...
        "typescript": "^5.2.2",
        "vite": "^5.0.8"
    }

Has anyone else experienced this problem, and are there any known resolutions or workarounds available?

favger commented 1 month ago

I understand the issue. In vite.config.ts, resolve.MainFields is set by default to ['browser', 'module', 'jsnext:main', 'jsnext'], but it seems it's not working because I left it as an empty array. This means styled-components determines whether it runs in the browser or on the server from here.

To enable createGlobalStyle to work, you can either remove mainFields or set it to ['browser', 'module', 'jsnext:main', 'jsnext'].