postcss / postcss-nested

PostCSS plugin to unwrap nested rules like how Sass does it.
MIT License
1.15k stars 66 forks source link

Strange behaviour on unwrapping nested elements #146

Closed talyguryn closed 2 years ago

talyguryn commented 2 years ago

Found strange behaviour on my code and tried to test on example. Correct me if I'm wrong please.

Code from readme

image

✅ OK for postcss-nested@5.0.1

image

❌ WRONG for postcss-nested@5.0.2-postcss-nested@5.0.6

image

.postcssrc.yml

plugins:
  # Consumes files by @import rule
  # https://github.com/postcss/postcss-import
  postcss-import: {}

  # Convert modern CSS into something most browsers can understand
  # https://github.com/csstools/postcss-preset-env
  postcss-preset-env:
    # Polyfill CSS features
    # https://github.com/csstools/postcss-preset-env#stage
    #
    # List of features with levels: https://cssdb.org/
    stage: 0

    # Define polyfills based on browsers you are supporting
    # https://github.com/csstools/postcss-preset-env#browsers
    browsers:
      - 'last 2 versions'
      - '> 1%'

    # Instruct all plugins to omit pre-polyfilled CSS
    # https://github.com/csstools/postcss-preset-env#preserve
    preserve: false

  # Nested rules unwrapper
  # https://github.com/postcss/postcss-nested
  #
  # As you know 'postcss-preset-env' plugin has an ability to process
  # 'postcss-nesting' feature but it does not work with BEM
  # Report: https://github.com/csstools/postcss-preset-env/issues/40
  postcss-nested: {}

  # Compression tool
  # https://github.com/cssnano/cssnano
#  cssnano: {}
ai commented 2 years ago

Can you post an input example as a text, so I can copy paste it?

talyguryn commented 2 years ago
Tested code
.phone {
    &_title {
        width: 500px;
        @media (max-width: 500px) {
            width: auto;
        }
        body.is_dark & {
            color: white;
        }
    }
    img {
        display: block;
    }
}

.title {
  font-size: var(--font);

  @at-root html {
      --font: 16px
  }
}

hm, sorry. might be not postcss-nested problem. i think this is my bad with the wrong order of plugins.

image

image

but it is still strange to get this unpacking by postcss-preset-env 🤔

ai commented 2 years ago

Seems like you are using postcss-nesting (another plugin, it used by postcss-preset-env). It based on CSS spec and has another syntax. You need @nest body.is-dark & (if your selector started not from & you need @nest prefix).