callstack / linaria

Zero-runtime CSS in JS library
https://linaria.dev
MIT License
11.5k stars 414 forks source link

The same CSS styles are applied multiple times to an element on the build mode #678

Open dmitry-pokhmelnov opened 3 years ago

dmitry-pokhmelnov commented 3 years ago

Environment

Description

I have some components loaded dynamically by using dynamic import() and React. lazy. Dynamic loading produces a CSS chunk for each component in the production build mode If I have a test component as a part of other components that loads dynamically, I get CSS chunks for each dynamically loaded component with exactly the same className for my test component

Issues

The same CSS selectors are in different CSS chunks; The same CSS styles are applied at least 2 or more times; It makes impossible to override CSS styles with styled(SomeComponent) because duplicated styles override each other and sometimes have a higher priority.

image

Styled-components don't have this issue because they generate a unique classNames

Question

Based on my current understanding, this issue might be resolved if we apply chunk name to class name produced by linaria. It will result in generating a unique class name selector for each CSS chunk. Do you think it's possible to apply a chunk name for each CSS selector produced by linaria?

Reproducible Demo

https://github.com/dmitry-pokhmelnov/bug-repro-linaria

jevangelista-quizlet commented 3 years ago

just FYI. I tried the demo with linaria 2.0.0-rc.3 and the behaviour is still there image

DiFuks commented 2 years ago

The reason for this problem is that webpack generates a css file for every chunk loaded with lazy-load. But after switching between pages, react does not remove already included css files. This issue is very similar to this one https://stackoverflow.com/questions/66531412/dynamically-load-and-unload-content-of-css-file-in-javascript-react

gajraj-gan commented 1 year ago

Any update on this ? This is happening to me while I am using *.module.scss