question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CSS Modules - composed styles are duplicated

See original GitHub issue

Describe the bug

Importing a css module that’s been composed ends up duplicating styles.

In the reproduction linked below, our App.tsx has 2 div elements with texts that have different styles. The styles are under the styles folder. The first text is styled using originalText style from original.module.css, and should have a 32px font size. The second one should have a 14px font size, since it’s styled using smallText from small.module.css, which composes originalText and then overrides the font size. However, it doesn’t work as expected.

The originalText style from original.module.css is imported first, then smallText style is imported, then the originalText style is imported once again, overriding the font size provided by smallText.

You can see this in action if you inspect the div element in dev tools and check out the styles applied.

Reproduction

https://codesandbox.io/s/vite-react-ts-forked-dy91of?file=/src/App.tsx

System Info

System:
    OS: macOS 12.2.1
    CPU: (8) arm64 Apple M1
    Memory: 127.45 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.2 - /usr/local/bin/node
    Yarn: 1.22.17 - /opt/homebrew/bin/yarn
    npm: 8.1.2 - /usr/local/bin/npm
  Browsers:
    Brave Browser: 99.1.36.117
    Chrome: 99.0.4844.83
    Firefox: 97.0.1
    Safari: 15.3
  npmPackages:
    @vitejs/plugin-react: ^1.2.0 => 1.2.0 
    vite: ^2.8.6 => 2.8.6

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
sapphi-redcommented, Apr 22, 2022

I think this problem cannot be solved with Vite due to the architecture. Vite processes each css import individually.

  • original.module.css will include .originalText
  • small.module.css will include .originalText and .smallText

To dedupe .originalText, it will need to process all css files at once because it needs to know depency graph.

One way it may solve this is to rewrite composes into a import with JS. But I feel this is no longer “CSSModules”.

1reaction
desmond-tuiyotcommented, Mar 30, 2022

I’m not particularly familiar with CSS Modules, but it works fine when you change their import order.

import originalStyles from './styles/original.module.css';
import smallStyles from './styles/small.module.css'; // import later for higher priority

In this small example it does have a workaround. But when I’m composing classes across many different css modules in my actual project, I have no way of controlling the import order myself. That’s entirely in vite territory.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I fix CSS modules copy of styles problem?
Problem is duplicate of styles. These components have same classnames but I see duplicate of styles. They are totally same. How can I...
Read more >
Reusing Styles - Tailwind CSS
If the styles you need to reuse only need to be reused within a single file, multi-cursor editing and loops are the simplest...
Read more >
Specificity - CSS: Cascading Style Sheets - MDN Web Docs
Create a separate, short style sheet containing only important declarations specifically overriding any important declarations you were unable ...
Read more >
Sass: @extend
Sass's @extend rule solves this. It's written @extend <selector> , and it tells Sass that one selector should inherit the styles of another....
Read more >
Goodbye CSS Modules, Hello TailwindCSS - Polytomic
The existing styling approach used CSS Modules without a design system. ... class names and IDs do not prevent duplicate CSS in other...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found