Using Gulp For Automating Tasks

Image by Neha Gupta

Introduction

Task Runners: A task runner is a tool that automates repetitive tasks in the development workflow, such as minification, compilation, unit testing, linting, and more. It helps developers save time and effort by automating these tasks, allowing them to focus on writing code and building features.

One popular task runner is Gulp, which is built on Node.js. Gulp uses a streaming approach to process files, which makes it fast and efficient. Here’s how Gulp can help you with minification, optimization, and other tasks:

Gulp Setup For Minification Tasks

We’re setting up Gulp and creating tasks to minify JavaScript and CSS files. You can follow a similar setup and add additional tasks tailored to your project’s needs.

Step 1: Install Gulp globally and as a Development dependency

First, you need to install Gulp globally and also as a development dependency to your Node.js project.To install run the following command in your terminal:

# Command to install globally
npm install -g gulp-cli
# Command to install as a project dependency
npm install --save-dev gulp

Step 2: Install Gulp Plugins for Minifying JavaScript and CSS

We’ll use gulp-uglify for JavaScript minification and gulp-clean-css for CSS minification. Run the following commands in your project's root directory:

npm install --save-dev gulp-uglify
npm install --save-dev gulp-clean-css

Step 3: Create a gulpfile.js

In your project’s root directory, create a gulpfile by the following name gulpfile.js. This file will contain your Gulp tasks.

Step 4: Define a Task for Minifying JavaScript and CSS Files

Add the following code in gulpfile.js for minifying JavaScript and CSS files

const gulp = require('gulp');
const uglify = require('gulp-uglify');
const cssnano = require('gulp-cssnano');

// Minify JavaScript files
function minifyJs() {
return gulp.src('src/**/*.js') // Source files (replace 'src' with your source directory)
.pipe(uglify()) // Minify the files
.pipe(gulp.dest('dist')); // Destination directory
}

// Minify CSS files
function minifyCss() {
return gulp.src('src/**/*.css') // Source files (replace 'src' with your source directory)
.pipe(cssnano()) // Minify the files
.pipe(gulp.dest('dist')); // Destination directory
}
// Export the task so it can be run using the `gulp` command
exports.minifyCss = minifyCss;
exports.minifyJs = minifyJs;
  • This task uses the gulp.src() method to specify the source files (all JavaScript files in the src directory)
  • The gulp-uglify plugin to minify the files.
  • The gulp-cssnano plugin to minify the files
  • The gulp.dest() method to specify the destination directory (the dist directory in this example).

Step 4: Run the Task

  • Run following command to minify JavaScript files and CSS files
# Minifying JavaScript
gulp minifyJs

# Minifying CSS
gulp minifyCss

Step 5: Define a Default Task (Optional)

A default task can execute both the CSS and JavaScript minification jobs simultaneously. Add this code to your gulpfile.js to create a default task:

// Define a default task that runs both the JavaScript and CSS minification tasks
function defaultTask(cb) {
gulp.series(minifyJs, minifyCss)(cb);
}

exports.default = defaultTask;

This task uses the gulp.series() method to run the minifyJs and minifyCss tasks sequentially.

  • Run the Default Tasks

Now you can run the tasks using the gulp command in your terminal. To minify the JavaScript and CSS files, run the following command:

gulp

Automating to Watch for Changes

You can use watch() command to watch for Gulp tasks changes. Here's how you can modify your gulpfile.js to watch for changes and automate the workflow.

// Additonal changes in `gulpfile.js` to watch for changes in JavaScript and CSS files
function watchFiles() {
gulp.watch('src/**/*.js', minifyJs); // Watch for changes in JavaScript files and run the `minifyJs` task
gulp.watch('src/**/*.css', minifyCss); // Watch for changes in CSS files and run the `minifyCss` task
}

exports.watch = watchFiles; // Export the watch task so it can be run using the `gulp` command
  • Added a function watchFiles that watches for changes in your JavaScript and CSS files and runs the corresponding tasks when a change is detected.
  • watchFiles function uses the gulp.watch() method to watch for changes in your JavaScript and CSS files (src/**/*.js and src/**/*.css respectively) and runs the minifyJs and minifyCss tasks when a change is detected.

Run the Watch Task

To watch for changes and automatically run the tasks, run the following command:

gulp watch

Gulp will now automatically execute the minifyJs and minifyCss tasks anytime you make changes to your JavaScript or CSS files, as this will initiate the watch task.

Conclusion

You can use Gulp task runner for your day-to-day manual activities and automate the flow so that you can focus more on development.

Queries and Doubts

Thanks for the read :) Hope you have enjoyed reading it 🤩 and learned something new today.

If you have any doubts or queries feel free to drop a comment or

⁍ Connect with me on my 🔗Topmate 💬.

⁍ You can also reach out to me on my 🔗LinkedIn.

⁍ Please clap for this post if you enjoyed reading it 📗 and follow for more interesting articles.

⁍ You can also support me and my writings by treating me to a nice virtual cup of coffee ☕️.