Gulp - Cleaning up Generated Files
In this chapter let us study about cleaning up generated files. As we are automatically generating the files, make sure that unnecessary files should be deleted before running your build. This procedure is called cleaning. The del plugin can be used for this purpose.
Installing del plugins
In your command line install the plugin by entering below command:
npm install del --save-dev
Declare dependencies and create tasks
In your configuration file gulpfile.js, declare the dependencies as shown below:
var del = require('del');
Next, you can create task as shown below:
gulp.task('clean:build', function() {
return del.sync('build');
});
The above task will clean entire build. The clean task clears any image catches and removes any old files present in build.
It is possible to clean only specific file or folder and leave some of them untouched as illustrated in below code:
gulp.task('clean:build', function() {
//return del.sync('build');
return del([
'build/temp/',
// instructs to clean temp folder
'!build/package.json'
// negate to instruct not to clean package.json file
]);
});
In the above task, only the temp folder will be cleaned leaving package.json untouched.