Throughout this book, we've explored a number of different plugins and concepts to help construct a processor; over the last few pages, we've brought together some of those concepts as the final version of our processor—at least one we can start using in anger.
There is one key step left to complete—we've compiled code for simple exercises, this works well, but doesn't really represent the kind of processes we might go through as developers! For this, we need to construct a real-world example, and put our processor through its paces.
As luck would have it, there is an example web page we can use from the code download that accompanies this book—let's take a look at putting its style sheet code through our processor. We'll begin by running the normal tasks we've done before, but will add a selection of plugins to make for a more realistic example:
T55 - testing our processor folder from the code download that accompanies this book; go ahead and save it to the root of our project area.gulpfile.js and package.json files from within this sub-folder to the root of our project area.npm install postcss-nesting --save-dev npm install postcss-short-color --save-dev npm install postcss-pixrem
site.css file from within the css – completed version folder under T55 – testing our processor, to the src folder at the root of our project area.gulp at the prompt and press Enter—wait for it to complete compiling.dest folder to the css folder within T55 – testing our processor.
Try resizing the browser window, or enabling Responsive Design mode in your browser (if supported)—we should see that content automatically flows or resizes, according to the size you set for the browser window. Overall, a successful result!
The question is—what happened here? If we take a look at our code, the sharp-eyed should spot the addition of three plugins, plus a lot more code in the compiled version; let's take a moment to digest the results of our exercise.
If we look through our Gulp task file carefully, there should not be much in there that comes as a surprise—many of the tasks used are ones we have used on many occasions throughout the book.
The key here, though, is that whilst we can run the standard processor that we've already used before, it's unlikely to suit all occasions. It's more likely that we can use it as our base (as stated earlier), then add any extra plugins as needed. The great thing about this is that most of the configuration work is done—it keeps a consistent approach to our work. All that remains is to install any plugins that we don't already have in place—we of course have most of them, but need to install three additional ones, as highlighted here:
gulp.task('styles', function () {
return gulp.src('src/*.css')
.pipe(postcss([ rucksack({ fallbacks: true }), autoprefixer(), shortcolor, nesting, pixrem ]))
.pipe(gulp.dest('dest/'));
});These have to be accompanied with the relevant calls at the top of our Gulp task file:
var nesting = require('postcss-nesting');
var shortcolor = require('postcss-short-color');
var pixrem = require('pixrem');In turn, these plugins are as follows:
@font-face.background-color and color attributes in a shorthand form that is later transpiled by the plugin. You can see more about this plugin at https://github.com/jonathantneal/postcss-short-color.We can of course add others, and continue converting our code—there are other instances where Nesting can be applied, such as in the rules that control styling for our navigation. The key here, though, is that success is measured in how much we have to change our processor's default setup—in this instance, we didn't have to change it at all! We of course added extra plugins that required a change to one line of code in the processor, but none of the other tasks required any changes at all.
It's at this stage that we have effectively completed the journey to create our processor—well, strictly speaking, our journey should always be considered without end; this will help ensure our tool remains up to date. This aside, though, there are some useful tips we can use when creating our processor, so let's take a moment to cover these in more detail.