3.4 KiB
3.4 KiB
PostCSS Nesting 
PostCSS Nesting lets you nest style rules inside each other, following the CSS Nesting specification.
a, b {
color: red;
& c, & d {
color: white;
}
}
/* becomes */
a, b {
color: red;
}
a c, a d, b c, b d {
color: white;
}
Usage
Add PostCSS Nesting to your build tool:
npm install postcss-nesting --save-dev
Node
Use PostCSS Nesting to process your CSS:
import postcssNesting from 'postcss-nesting';
postcssNesting.process(YOUR_CSS);
PostCSS
Add PostCSS to your build tool:
npm install postcss --save-dev
Use PostCSS Nesting as a plugin:
import postcss from 'gulp-postcss';
import postcssNesting from 'postcss-nesting';
postcss([
postcssNesting(/* options */)
]).process(YOUR_CSS);
Webpack
Add PostCSS Loader to your build tool:
npm install postcss-loader --save-dev
Use PostCSS Nesting in your Webpack configuration:
import postcssNesting from 'postcss-nesting';
export default {
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
{ loader: 'css-loader', options: { importLoaders: 1 } },
{ loader: 'postcss-loader', options: {
ident: 'postcss',
plugins: () => [
postcssNesting(/* options */)
]
} }
]
}
]
}
}
Gulp
Add Gulp PostCSS to your build tool:
npm install gulp-postcss --save-dev
Use PostCSS Nesting in your Gulpfile:
import postcss from 'gulp-postcss';
import postcssNesting from 'postcss-nesting';
gulp.task('css', () => gulp.src('./src/*.css').pipe(
postcss([
postcssNesting(/* options */)
])
).pipe(
gulp.dest('.')
));
Grunt
Add Grunt PostCSS to your build tool:
npm install grunt-postcss --save-dev
Use PostCSS Nesting in your Gruntfile:
import postcssNesting from 'postcss-nesting';
grunt.loadNpmTasks('grunt-postcss');
grunt.initConfig({
postcss: {
options: {
use: [
postcssNesting(/* options */)
]
},
dist: {
src: '*.css'
}
}
});