We've seen that adapting code to use nesting is a simple principle, but the real art is getting the balance right, many developers fall into the trap of nesting everything in their code when using the technique for the first time.
With this in mind, let's explore how we can convert our code to using PostCSS. We'll start by installing the postcss-nesting plugin, which will perform most of the work for us:
npm install --save-dev postcss-nesting

gulpfile.js from the project area, ready for editing.var cssnano = require('cssnano');
var nesting = require('postcss-nesting');autoprefixer task needs to be altered—this time around, we will start with compiling our nested code and adding the appropriate vendor prefixes. Alter the first line of this task, as indicated:gulp.task('autoprefixer', function() {
return gulp.src('src/*.css') .pipe(postcss([ autoprefixer, nesting({ /* options */ }) ]))cssnano plugin from Chapter 2, Creating Variables and Mixins. Go ahead and add this at line 20:.pipe(postcss([ cssnano() ]))
gulp.task("lint-styles", ['autoprefixer'], function() {sass task, so go ahead and remove it in its entirety, and from the default task entry—we should be left with this:gulp.task('default', ['lint-styles', 'autoprefixer', 'rename']);autoprefixer task, we'll run it once the lint-styles task has been completed:gulp.task('rename', ['lint-styles'], function () {At this stage, our gulp task file is now ready for use. We can begin to convert our style sheet to use PostCSS nesting as a replacement for SASS. Let's make a start on converting it, as part of the next exercise.
Altering our code to use PostCSS is very simple. Even if it requires a few changes, the format does not change significantly when compared to processors such as SASS; let's take a look at what is involved:
style.scss from the Tutorial6A folder in the code download that accompanies this book—save it to the src folder of our project area. Rename it to style.css.@nest immediately before &:, as indicated—this is required to allow the postcss-nesting plugin to correctly compile each nesting statement:@nest &:before, &:after {@nest & immediately before h2, as shown:@nest & h2 {@nest immediately before &., as shown:@nest &.page1 {Repeat step 4 for lines 65, 69 and 73.
@nest immediately before &., as shown:@nest &.invisible {@nest immediately before ul, as shown:@nest & ul {@nest immediately before & li, as shown:@nest & li {@nest immediately before &., as shown:@nest &:after {Repeat the same process for lines 150 and 155.
@nest immediately before &., as shown:
@nest &.up {Repeat the same process for lines 183 and 187, then save the file.
Our style sheet is now converted; to prove it works, we need to run it through PostCSS, so let's do that now as part of the next exercise.
With the changes made to our code, we need to compile it—let's go ahead and do that now, using the same process we saw back in Chapter 2, Creating Variables and Mixins:
gulp

dest folder of our project area should reveal the relevant compiled CSS and source map files, produced by PostCSS.Tutorial7 folder from the code download that accompanies this book—save this to our project area.dest folder from our project area to the css folder under Tutorial7—if all is well, our demo should continue to work, but without the dependency of SASS.Try previewing the results in a browser—if all is well, we should see the same results appear as before, but this time using PostCSS, and without the dependency on SASS. We can now apply the same techniques to any project, safe in the knowledge that using the postcss-nesting plugin will allow us to compile to valid CSS code—or will it?