vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.77k stars 6.21k forks source link

CSS Modules duplicates `composes` dependencies onto page, re-declaring classes #15683

Open privatenumber opened 10 months ago

privatenumber commented 10 months ago

Describe the bug

I'm facing a problem with the composes feature in CSS Modules using LightningCSS. It seems to duplicate the base class it's composing, causing a re-declaration. Consequently, this re-declaration overwrites styles from components loaded earlier that also composes the same base class, dominating in specificity and altering their intended look.

Disabling LightningCSS resolves the issue, but there are other problems with PostCSS (e.g., https://github.com/vitejs/vite/issues/10079).

Reproduction

https://stackblitz.com/edit/vitejs-vite-vf7fy6?file=vite.config.js

Steps to reproduce

Just load the StackBlitz.

For context, I'm encountering this problem in a Vue.js app. But the reproduction minimizes it down to just JS + CSS Modules.

System Info

N/A

Used Package Manager

npm

Logs

No response

Validations

stackblitz[bot] commented 10 months ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

privatenumber commented 10 months ago

I actually managed to reproduce this with PostCSS too: https://stackblitz.com/edit/vitejs-vite-vhmmza?file=comp-a%2Findex.js

In both cases, it seems like anything CSSModules imports is handled outside of Vite and so shared dependencies are duplicated, creating unpredictable behavior based on what gets loaded on the page first.