Gulp - Combining Tasks
Task enables a modular approach to configuring Gulp. We need to create task for each dependency which we would add up as we find and install other plugins. The Gulp task will have following structure:
gulp.task('task-name', function() {
//do stuff here
});
Where task-name is a string name and function() that performs your task. The gulp.task registers the function as a task within name and specifies the dependencies on other tasks.
Installing Plugins
Let's take one plugin called minify-css to merge and minify all CSS scripts and it can be installed by using npm as shown below:
npm install gulp-minify-css --save-dev
To work with gulp-minify-css plugin, you need to install another plugin called gulp-autoprefixer as shown in the below line:
npm install gulp-autoprefixer --save-dev
To concatenate the CSS files, install the gulp-concat as shown in the below command:
npm install gulp-concat --save-dev
After installation of plugins, you need to write dependencies in your configuration file as shown below:
var autoprefix = require('gulp-autoprefixer');
var minifyCSS = require('gulp-minify-css');
var concat = require('gulp-concat');
Adding Task to Gulp file
We need to create task for each dependency which we would add up as we install the plugins. The Gulp task will have following structure:
gulp.task('styles', function() {
gulp.src(['src/styles/*.css'])
.pipe(concat('styles.css'))
.pipe(autoprefix('last 2 versions'))
.pipe(minifyCSS())
.pipe(gulp.dest('build/styles/'));
});
The concat plugin concatenates the CSS files and autoprefix plugin indicates current and previous versions of all browsers. It minifies all CSS scripts from src folder and copied to build folder by calling dest method with an argument which represents the target directory.
To run the task, use the following command in your project directory:
gulp styles
Similarly we will use another plugin called gulp-imagemin to compress the image file which can be installed using below command:
npm install gulp-imagemin --save-dev
You can add dependencies to your configuration file as shown below:
var imagemin = require('gulp-imagemin');
You can create the task for above defined dependency as shown below:
gulp.task('imagemin', function() {
var img_src = 'src/images/**/*',
img_dest = 'build/images';
gulp.src(img_src)
.pipe(changed(img_dest))
.pipe(imagemin())
.pipe(gulp.dest(img_dest));
});
The images are located in src/images/**/* which are saved in the img_src object. It is piped to other function created by the imagemin constructor. It compresses the images from src folder and copied to build folder by calling dest method with an argument which represents the target directory.
To run the task, use the following command in your project directory:
gulp imagemin
Combining Multiple Tasks
You can run multiple tasks at a time by creating default task in the configuration file as shown below:
gulp.task('default', ['imagemin', 'styles'], function() {
});
Now Gulp file is setup and ready to execute. Just run the following command in your project directory to run the above combined tasks:
gulp
Run the task by using the above command, you will get to see below result in the command prompt:
C:\work>gulp [16:08:51] Using gulpfile C:\work\gulpfile.js [16:08:51] Starting 'imagemin'... [16:08:51] Finished 'imagemin' after 20 ms [16:08:51] Starting 'styles'... [16:08:51] Finished 'styles' after 13 ms [16:08:51] Starting 'default'... [16:08:51] Finished 'default' after 6.13 ms [16:08:51] gulp-imagemin: Minified 0 images