Skip to content

Duplicate styles in nested at-rules are not merged #1847

@michaelabrahamian

Description

@michaelabrahamian

Describe the bug
When there are duplicate styles used in nested at-rules, they are not merged.

This is unexpected and not quite obvious to developers, and results in additional CSS code being generated. Nesting at-rules like this is valid CSS: https://css-tricks.com/can-you-nest-media-and-support-queries/

There is also a comment about this Compiled limitation in the mergeDuplicateAtRules function:

 * Plugin to remove duplicate children found in at-rules.
 * Currently does not handle nested at-rules.

To Reproduce
Steps to reproduce the behavior:

Add and run the unit test to the file merge-duplicate-at-rules.test.ts:

it('should remove duplicate children when nested at-rules', () => {
  const actual = transform`
    @supports not (height: 1lh) {
      @media (min-width:500px) {
        color: red;
        color: green;
      }
    }

    @supports not (height: 1lh) {
      @media (min-width:500px){
        color: red;
        color: blue;
      }
    }
  `;

  expect(actual).toMatchInlineSnapshot(`
    "
          @supports not (height: 1lh) {
            @media (min-width:500px) {
              color: red;
              color: green;
            }
            @media (min-width:500px){
              color: red;
              color: blue;
            }
          }
        "
  `);
});

Expected behavior
Duplicate styles should be merged. In the above example, it should end up with:

@supports not (height: 1lh) {
    @media (min-width:500px) {
      color: red;
      color: green;
      color: blue;
    }
  }

At a minimum, some improved guidance and lint rules to make people aware would be really helpful.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛Something isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions