Skip to content

[Feature Request] Remove Unused Rules from @-rules #724

@KazWolfe

Description

@KazWolfe

CSS inlining, by definition, will already remove unused rules by virtue of them simply... not being inlined. However, media queries and other @-rules can't be inlined.

Take, for example, the following document:

<html>
  <head>
    <style>
      h1 { color: blue; }
      h2 { color: green; }

      @media (prefers-color-scheme: dark) {
         h1 { color: orange !important; }
         h2 { color: yellow !important; }
      }
    </style>
  </head>
  <body>
    <h1>Big Text</h1>
  </body>
</html>

This gets optimized to the following:

<html>
  <head>
    <style>
      @media (prefers-color-scheme: dark) {
        h1 { color: orange !important; }
        h2 { color: yellow !important; } /* not necessary! target for removal */
      }
    </style>
  </head>
  <body>
    <h1 style="color: blue;">Big Text</h1>
  </body>
</html>

Note that the h2 declaration is completely unused here, and can be stripped. This would allow developers to import full(er) CSS libraries and have things intelligently removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions