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 `@layer` bundling misses selectors inside media queries

See original GitHub issue

Describe the bug

@import 'layers.css' layer(demo);

where layers.css contains:

h1 {
  color: red;
}

@media (min-width: 100px) {
  h1 {
    color: green;
  }
}

incorrectly produces:

@layer demo {
  h1 {
    color: red;
  }
}

@media (min-width: 100px) {
  h1 {
    color: green;
  }
}

instead of (this is just one correct way):

@layer demo {
  h1 {
    color: red;
  }

  @media (min-width: 100px) {
    h1 {
      color: green;
    }
  }
}

This means any media queries are unlayered, giving them the top most precedence and specificity over styles in layers.

Vanilla Vite example: https://stackblitz.com/edit/vitejs-vite-vlemhc?file=layers.css,style.css Demo: https://vitejs-vite-vlemhc--5173.local.webcontainer.io/

I’d like a way to disable it for now, but don’t see one.

Reproduction

https://stackblitz.com/edit/vitejs-vite-vlemhc?file=layers.css,style.css

System Info

System:
    OS: macOS 12.4
    CPU: (16) x64 Intel(R) Xeon(R) W-2140B CPU @ 3.20GHz
    Memory: 799.40 MB / 64.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 18.2.0 - /usr/local/bin/node
    npm: 8.9.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 103.0.5060.134
    Chrome Canary: 106.0.5201.0
    Firefox: 102.0.1
    Firefox Developer Edition: 102.0
    Firefox Nightly: 103.0a1
    Safari: 15.5
    Safari Technology Preview: 15.4
  npmPackages:
    vite: 3.0.2 => 3.0.2

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
argyleinkcommented, Sep 6, 2022

bug is fixed 👍🏻

4reactions
romainmenkecommented, Jul 25, 2022

I suspect this will be fixed by : https://github.com/postcss/postcss-import/pull/496 Haven’t verified this but it seems to be the same issue and vite uses postcss-import.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is wrong with media queries on different selectors?
Why don't media queries work when element styles are defined with a different list of selectors? For example, I have HTML page: <div...
Read more >
Safari Technology Preview Release Notes - Apple Developer
CSS. Added support for media queries level 4 including range queries ... Fixed :hover with descendant selector invalidating correctly in shadow trees ...
Read more >
Twitter
CSS @layer bundling misses selectors inside media queries. Describe the bug @import 'layers.css' layer(demo); where layers.css contains: h1 { color: red; } ...
Read more >
Using Tailwind CSS in production - LogRocket Blog
Bundling is the process of a bundler(like webpack, parcel, or snowpack) to combine different groups of assets such as HTML, CSS, JavaScript, and ......
Read more >
Using media queries - CSS: Cascading Style Sheets | MDN
A media query is composed of an optional media type and any number of media feature expressions, which may optionally be combined in...
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