Skip to content

Fix hoisting of empty @layer statements that follow layer blocks - #613

Merged
romainmenke merged 1 commit into
postcss:masterfrom
madogiwa0124:fix/layer-order-after-content-layers
Oct 9, 2026
Merged

romainmenke merged 1 commit into
postcss:masterfrom
madogiwa0124:fix/layer-order-after-content-layers

Conversation

@madogiwa0124

@madogiwa0124 madogiwa0124 commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Problem

An empty @layer name; statement that comes after @layer name { ... } blocks is moved to the top of the bundle, which changes the cascade layer order.

/* pkg.css */
@layer base { html { box-sizing: border-box; } }
@layer utils { .hidden { display: none; } }
@layer overrides;

/* app.css */
@import "./pkg.css";
@layer overrides { body { background-color: #e5e5e5; } }

Output before this change:

@layer overrides;
@layer base { ... }
@layer utils { ... }
@layer overrides { body { background-color: #e5e5e5; } }

overrides now appears first, so it gets the lowest priority instead of the highest.

Cause

In lib/parse-statements.js, encounteredNonImportNodes was only set in the else branch for non at-rule nodes. At-rules that don't become a statement (such as @layer name { ... }, @media, @font-face) never set the flag. A later empty @layer name; was still seen as part of the import prelude, classified as a layer statement, and put in beforeBundle.

Fix

Set encounteredNonImportNodes for any non-comment node that does not become an import / charset / layer statement. Empty @layer statements before @import (the case fixed in #574 for #567) are handled the same as before.

Tests

🤖 Generated with Claude Code

`encounteredNonImportNodes` was only set for non at-rule nodes, so
at-rules such as `@layer name { ... }` did not mark the end of the
import prelude. A later empty `@layer name;` was then treated as a
prelude statement and moved before all other content, changing the
cascade layer order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@RyanZim RyanZim left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this looks correct, but would love to have a second review here @romainmenke.

@RyanZim
RyanZim requested a review from romainmenke October 9, 2026 02:03

@romainmenke romainmenke left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the fix is good :)

I also added some tests in https://github.com/romainmenke/css-import-tests to verify but hadn't yet gotten around to reporting back here.

Thank you @madogiwa0124

@romainmenke
romainmenke merged commit 1bfb5c6 into postcss:master Oct 9, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants