Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Empty blocks within @layer rule are not deleted #1594

Open
Yoskutik opened this issue Mar 31, 2024 · 3 comments
Open

[Bug]: Empty blocks within @layer rule are not deleted #1594

Yoskutik opened this issue Mar 31, 2024 · 3 comments

Comments

@Yoskutik
Copy link

Yoskutik commented Mar 31, 2024

Describe the bug

If my CSS code contains several instances of using @layer rule with the same layer name, after the minification CSS nano will keep each usage of it.

This code

@layer components {
  .O82 { display: flex; }
}
@layer components {
  .tK5 { border: 0; }
}
@layer components {
  .tK1 { font-size: inherit; }
}

Is compiled into this (I added whitespaces)

@layer components {
  .O82 { display: flex }

  .tK5 { border: 0 }

  .tK1 { font-size: inherit }
}

@layer components {}

@layer components {}

Expected behaviour

The following @layer components {} blocks do not affect the styles, therefore they can be removed

Steps to reproduce

The steps are described above

Version

6.1.2

Preset

default

Environment

System:
    OS: macOS 14.3.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 113.86 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh

Package details

├─┬ css-loader@6.10.0 -> ./node_modules/.pnpm/css-loader@6.10.0_webpack@5.91.0/node_modules/css-loader
│ ├─┬ icss-utils@5.1.0 -> ./node_modules/.pnpm/icss-utils@5.1.0_postcss@8.4.38/node_modules/icss-utils
│ │ └── postcss@8.4.38 -> ./node_modules/.pnpm/postcss@8.4.38/node_modules/postcss
│ ├─┬ postcss-modules-extract-imports@3.0.0 -> ./node_modules/.pnpm/postcss-modules-extract-imports@3.0.0_postcss@8.4.38/node_modules/postcss-modules-extract-imports
│ │ └── postcss@8.4.38 -> ./node_modules/.pnpm/postcss@8.4.38/node_modules/postcss
│ ├─┬ postcss-modules-local-by-default@4.0.4 -> ./node_modules/.pnpm/postcss-modules-local-by-default@4.0.4_postcss@8.4.38/node_modules/postcss-modules-local-by-default
│ │ └── postcss@8.4.38 -> ./node_modules/.pnpm/postcss@8.4.38/node_modules/postcss
│ ├─┬ postcss-modules-scope@3.1.1 -> ./node_modules/.pnpm/postcss-modules-scope@3.1.1_postcss@8.4.38/node_modules/postcss-modules-scope
│ │ └── postcss@8.4.38 -> ./node_modules/.pnpm/postcss@8.4.38/node_modules/postcss
│ ├─┬ postcss-modules-values@4.0.0 -> ./node_modules/.pnpm/postcss-modules-values@4.0.0_postcss@8.4.38/node_modules/postcss-modules-values
│ │ └── postcss@8.4.38 -> ./node_modules/.pnpm/postcss@8.4.38/node_modules/postcss
│ └── postcss@8.4.38 -> ./node_modules/.pnpm/postcss@8.4.38/node_modules/postcss
└─┬ css-minimizer-webpack-plugin@6.0.0 -> ./node_modules/.pnpm/css-minimizer-webpack-plugin@6.0.0_webpack@5.91.0/node_modules/css-minimizer-webpack-plugin
  ├─┬ cssnano@6.1.2 -> ./node_modules/.pnpm/cssnano@6.1.2_postcss@8.4.38/node_modules/cssnano
  │ └── postcss@8.4.38 -> ./node_modules/.pnpm/postcss@8.4.38/node_modules/postcss
  └── postcss@8.4.38 -> ./node_modules/.pnpm/postcss@8.4.38/node_modules/postcss

Additional context

No response

@ludofischer
Copy link
Collaborator

This happens because deleting an empty layer might change the layer priority (see closed issue #1544). To prevent keeping useless empty layers, cssnano should remember all the layer names it has seen before, but currently it does not do it.

@Yoskutik
Copy link
Author

Yoskutik commented Apr 1, 2024

When why do you merge the styled into a single layer declaration? If you have to keep the layers, isn't better not to merge them?

@ludofischer
Copy link
Collaborator

When why do you merge the styled into a single layer declaration? If you have to keep the layers, isn't better not to merge them?

As far as I remember, the code that merges is the same for all at-rules, and predates even existence of @layer declarations. When layers became more widespread, a fix was applied to preserve empty at-rules only for the @layer case.

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

No branches or pull requests

2 participants