Creating Tasks in Gulp

Gulp requires a special file gulpfile.js, placed in the project's root folder. This file contains tasks for Gulp. These tasks are JavaScript functions.

Let's create a test task for warm-up. First, let's create the specified file and connect our library to it:

gulpfile.jslet gulp = require('gulp');

Let's create a function for our first task and export it:

javascriptlet gulp = require('gulp');

function task(cb) {
  console.log('my first task completed!');
  cb(); // a special callback, more on that later
}

exports.default = task;

To run our task, while in the project folder, execute the following command in the command line:

terminalgulp
Задание 1

Create a task that outputs the current time to the console. Run this task via the command line.

Explanation of cb

You've probably noticed that a callback function is passed as a parameter to the task function, and it must be called at the end of the task:

javascriptfunction task(cb) {
  console.log('my first task');
  cb(); // call the callback
}

This is done to notify Gulp that the task is completed and it's possible to proceed to the next task.

Don't overthink it - just call the callback at the end. In the next lessons, when our task returns something via return, calling the callback won't be necessary.