•  


GitHub - postcss/postcss-nested: PostCSS plugin to unwrap nested rules like how Sass does it.
Skip to content

PostCSS plugin to unwrap nested rules like how Sass does it.

License

Notifications You must be signed in to change notification settings

postcss/postcss-nested

Repository files navigation

PostCSS Nested

PostCSS plugin to unwrap nested rules closer to Sass syntax.

.
phone
 {
    
&
_title
 {
        
width
:
 500
px
;
        
@media
 (
max-width
:
 500
px
) {
            
width
:
 auto;
        }
        
body
.
is_dark
 &
 {
            
color
:
 white;
        }
    }
    
img
 {
        
display
:
 block;
    }
}

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

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

  }
}

will be processed to:

.
phone_title
 {
    
width
:
 500
px
;
}
@media
 (
max-width
:
 500
px
) {
    .
phone_title
 {
        
width
:
 auto;
    }
}
body
.
is_dark
 .
phone_title
 {
    
color
:
 white;
}
.
phone
 img
 {
    
display
:
 block;
}

.
title
 {
  
font-size
:
 var
(
--font
);
}
html
 {
  
--font
:
 16
px

}

Related plugins:

  • Use postcss-current-selector after this plugin if you want to use current selector in properties or variables values.
  • Use postcss-nested-ancestors before this plugin if you want to reference any ancestor element directly in your selectors with ^& .

Alternatives:

Sponsored by Evil Martians

Usage

Step 1: Install plugin:

npm install --save-dev postcss postcss-nested

Step 2: Check your project for existing PostCSS config: postcss.config.js in the project root, "postcss" section in package.json or postcss in bundle config.

If you do not use PostCSS, add it according to official docs and set this plugin in settings.

Step 3: Add the plugin to plugins list:

module.exports = {
  plugins: [
+
   require('postcss-nested'),

    require('autoprefixer')
  ]
}

Options

bubble

By default, plugin will bubble only @media , @supports and @layer at-rules. Use this option to add your custom at-rules to this list.

postcss
(
[
 require
(
'postcss-nested'
)
(
{
 bubble
: 
[
'phone'
]
 }
)
 ]
)
/* input */

a
 {
  
color
:
 white;
  
@phone
 {
    
color
:
 black;
  }
}
/* output */

a
 {
  
color
:
 white;
}
@phone
 {
  
a
 {
    
color
:
 black;
  }
}

unwrap

By default, plugin will unwrap only @font-face , @keyframes and @document at-rules. You can add your custom at-rules to this list by unwrap option:

postcss
(
[
 require
(
'postcss-nested'
)
(
{
 unwrap
: 
[
'phone'
]
 }
)
 ]
)
/* input */

a
 {
  
color
:
 white;
  
@phone
 {
    
color
:
 black;
  }
}
/* output */

a
 {
  
color
:
 white;
}
@phone
 {
  
color
:
 black;
}

preserveEmpty

By default, plugin will strip out any empty selector generated by intermediate nesting levels. You can set preserveEmpty to true to preserve them.

.
a
 {
    .
b
 {
        
color
:
 black;
    }
}

Will be compiled to:

.
a
 { }
.
a
 .
b
 {
    
color
:
 black;
}

This is especially useful if you want to export the empty classes with postcss-modules .

rootRuleName

The plugin supports the SCSS custom at-rule @at-root which breaks rule blocks out of their nested position. If you want, you can choose a new custom name for this rule in your code.

postcss
(
[
 require
(
'postcss-nested'
)
(
{
 rootRuleName
: 
'_escape-nesting'
 }
)
 ]
)
/* input */

.
a
 {
  
color
:
 white;
  
@_escape-nesting
 {
    .
b
 { 
color
:
 black; }
  }
}
/* output */

.
a
 { 
color
:
 white; }
.
b
 { 
color
:
 black; }
- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본