With a Gulp task file and associated package.json file in place, we should be good to go, right? Well, not quite—yes, our processor has been used on demos throughout the book to great effect. But there is more that we can do: our Gulp file should never be static; we should always look to review it periodically, to ensure it is working at optimal efficiency.
Our Gulp file does have a few issues we need to address, so let's look at these now:
style.min.css filename as the output; this isn't going to suit all requirements, so we should change this to make it dynamic.cssnano, which is compressing every .css file it sees; this isn't necessary, so needs to be changed.cssnano that should be run as a task within PostCSS is causing issues—even though it would make sense to run it this way, it needs to be run independently, to satisfy our needs.Over the next few pages, we will explore ways of fixing and improving our Gulp task file—it's key to understand that whilst many of these changes are specific to our task file, they are ones that may crop up for your future projects. Above all, it is essential that we should continually review our production process to ensure it is working as needed.
Let's begin the process of fixing and improving our Gulp file before we put it to test on a sample site.
It has to be said that there are a few issues we need to resolve—the key here is that none of them will stop our compilation process; we should consider them more as rough edges on a diamond, which need polishing to make our process sparkle (pun intended!).
Okay, let's get cracking: there are a few changes to make, so we will start with the key task, which compiles the source file:
gulpfile.js file from the T48 – existing processor folder within the code download that accompanies this book; go ahead and save it as gulpfile.js at the root of our project area.autoprefixer support in the file—you should find it there but commented out on line 5; go ahead and remove the comment..pipe(postcss([ rucksack({ fallbacks: true, autoprefixer: true }) ]))We're not going to include fallback support, and will take care of autoprefixer separately, so for now, alter it as shown:
.pipe(postcss([ rucksack(), autoprefixer() ]))
var stylerules = {
"color-no-invalid-hex": 2,
"declaration-colon-space-before": [2, "never"],
"indentation": [2, 2],
"number-leading-zero": [2, "always"]
};gulp.task('lint', ['styles'], function() {
return gulp.src("dest/*.css")
.pipe(postcss([ stylelint({ "rules": stylerules }),
reporter({ clearMessages: true })
]))
});cssnano line at line 38; we're splitting the task into two, and this will be handled in a new task.gulp.task('rename', ['lint'], function () {.pipe(rename(renameFunction))sourcemap, we have one alteration to make—on or around line 47, change this line as shown:gulp.task('sourcemap', ['rename'], function () {
return gulp.src(sourceMapLocation)sourcemap task:gulp.task('minifyCSS', ['sourcemap'], function () {
return gulp.src('dest/*.min.css')
.pipe(cssnano({ autoprefixer: false }))
.pipe(gulp.dest("dest/"));
});['styles', 'lint' , 'rename' , 'sourcemap', 'minifyCSS']
gulp.task('default', ['styles', 'lint' , 'rename' , 'minifyCSS', 'sourcemap']);
var watcher = gulp.watch('src/*.css', ['default']);
watcher.on('change', function(event) {stylerules declaration added in step 4, go ahead and add these extra lines:var renameFunction = function (path) {
path.extname = ".min.css";
return path;
};
var sourceMapLocation = ['dest/*.css', '!dest/*.min.css'];We now have an updated Gulp task file—we now need to copy the style.css from the src folder under T49 – fixing issues in Gulpfile to the src folder at the root of our project area. If all is well, we should have something akin to this in the dest folder of our project area when we compile our file, and a file named style.css.map in the maps folder:

At this point, I am sure you will have a few questions about some of the changes we've made—the demo highlights a few key points, so it's worth taking time out to explore these in more detail.
Throughout the course of our demo, we made a number of changes to our Gulp task file—the key thing to note is that none of them are compulsory. Our task file worked perfectly well prior to making the changes, so if they aren't compulsory, why are we making them?
The answer to this is simple—using a task runner such as Gulp is about automating processes so that you arrive at just the content you need. We had that, but the task runner produced extra files, didn't compress them as expected, and our Gulp file contained tasks that had multiple steps within the same task. The work we completed was about adding polish to the process—although our Gulp task file worked, we explored how we could improve on it by tweaking some of the processes.
We kicked off with changes to how vendor prefixes were added—our existing task completed this as part of compiling using the Rucksack plugin. The Rucksack plugin was to provide fallback support—I'm not a fan of working with older browsers, so we don't need it. This makes it less beneficial to incorporate vendor prefix support from such a large plugin, thus support is not enabled.
There is another plugin available for PostCSS that handles vendor prefixes—doiuse, available at https://github.com/anandthakker/doiuse. Just another option to try!
The lint-styles task worked well—the changes we made focus on making the code easier to read in the task file. We moved the configuration block to the start of the file, and rearranged the format of the task; this means that we should not have to change the task, even though we may change the configuration!
Most of the remaining changes focus on splitting multiple roles into single tasks, and correcting some anomalies in the output. Our compilation process produced a minified file with the right extension, but also minified the original source file. We also had two source map files produced in a similar fashion—this is clearly not ideal! The changes we made now mean that our original source file is not minified, but only one minified file is produced, and that we have a single uncompressed style sheet created during the process.
Perfect, we now have a polished compilation process, which is producing the right files at the appropriate point; what next? Well, we can now add additional functionality to our compilation process. Using a task runner such as Gulp is about automating menial tasks, so let's explore what we might achieve in more detail.