Skip to content

Eliminating unused classes #127

Open

Description

@BaoDelta

Let's say I have the following code:

/* test.css */

.roundedBorder {
  border-radius: 5px;
}

.redBorder {
  border: 1px solid red;
}

.roundedAndRedBorder {
  composes: roundedBorder;
  composes: redBorder;
}
/* test.js */
import styles from './test.css'

console.log(styles)

When I run the code with style loader and css loader, the output is:

{
  "roundedBorder": "test__roundedBorder",
  "redBorder": "test__redBorder",
  "roundedAndRedBorder": "test__roundedAndRedBorder test__roundedBorder test__redBorder"
}

and the style element added into DOM is:

<style type="text/css">
.test__roundedBorder {
  border-radius: 5px;
}

.test__redBorder {
  border: 1px solid red;
}

.test__roundedAndRedBorder {
}
</style>

The class roundedAndRedBorder is used here only to compose the two other classes. Therefore, I think we should not have the class test__roundedAndRedBorder in the output. What do you think?

Activity

  1. joeybaker commented on Mar 22, 2016

    @joeybaker

    @BaoDelta It's not a bad idea. But, you could use something like cssnano to eliminate the empty classes. Though, it's a good argument that removing empty classes could be added to CSS Module's list of core modules. I can't think of any "unsafe" behavior that would occur.

  2. BaoDelta commented on Mar 22, 2016

    @BaoDelta
    Author

    Thanks for your answer @joeybaker

    If I add minimize to css-loader query, the class test__roundedAndRedBorder is not in the style element anymore but is still referenced in styles.roundedAndRedBorder. In addition, if I have a css file with only compose-only classes, then I will have an empty style element in the DOM.

    My situation is that I'm using purecss in my project but I want to hide that fact from my React components. They should not know which css framework I'm using so that I can change and tweak the css. Therefore, I have a lot of css modules with compose-only classes like this:

    /* Button.css */
    
    .button {
      composes: pure-button from global;
    }

    That produces a lot of empty and unused real classes.

  3. joeybaker commented on Mar 22, 2016

    @joeybaker

    Hmm… why wouldn't you want to have styles.roundedAndRedBorder? That's the shortcut CSS Modules gives you for referencing both classes at once, no?

  4. BaoDelta commented on Mar 22, 2016

    @BaoDelta
    Author

    @joeybaker I want to have styles.roundedAndRedBorder but I expect it to be just "test__roundedBorder test__redBorder" instead of "test__roundedAndRedBorder test__roundedBorder test__redBorder"

  5. joeybaker commented on Mar 22, 2016

    @joeybaker

    Ah, I see. That's totally fair.

  6. navgarcha commented on May 11, 2016

    @navgarcha

    +1

  7. richardgorman commented on Dec 9, 2016

    @richardgorman

    Is there any progress on this?

  8. tivac commented on Dec 9, 2016

    @tivac

    I've been doing that in modular-css and it's really nice. 👍

  9. Benno007 commented on Mar 9, 2017

    @Benno007

    I agree, we have many levels of composition in our themed components and we end up with 5 or 6 empty classes and empty classes on the element attributes. The minimize flag on css loader DOES remove them from the CSS, but they are not removed from the HTML when its rendered. It would make our DOM considerably smaller to have these unused classes removed. Is there any way to achieve this?

  10. nicksyndicate commented on May 22, 2017

    @nicksyndicate

    A lot of empty class names rendered in html, anybody PR ?

  11. changed the title [-]Should compose-only classes be hidden?[/-] [+]Eliminating unused classes[/+] on May 31, 2017
  12. bitttttten commented on Jul 28, 2018

    @bitttttten

    Is it not possible to use getLocalIdent for this? It must be able to evaluate if the declaration is empty but I have had no luck finding out how to yet.

  13. added 2 commits that reference this issue on May 9, 2021
    dec5b56
    91954d8
  14. added a commit that references this issue on Jun 9, 2021
    9c23834
  15. felixsanz commented on Jul 29, 2021

    @felixsanz

    5 years old, has anyone come up with a solution?

  16. sjoqvist commented on Aug 16, 2021

    @sjoqvist

    5 years old, has anyone come up with a solution?

    @felixsanz Yes, I have proposed a solution (css-modules/postcss-modules-scope#32). It's currently being reviewed by @alexander-akait. If it's approved, then an option should also be added to css-loader to enable the feature.

  17. alexander-akait commented on Aug 16, 2021

    @alexander-akait
    Member

    Yep, need to find time on review 😞 still in my TODO

  18. GitProdEnv commented on Feb 2, 2022

    @GitProdEnv

    Any update on this?

  19. zzuieliyaoli commented on Mar 25, 2025

    @zzuieliyaoli

    hi guys, any update on this?

  20. malinindev commented on Jun 26, 2025

    @malinindev

    Hey guys, I've created a package for detecting unused css - it provides a simple cli command.
    Please check it out: https://www.npmjs.com/package/check-unused-css

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions