19 Mar.2018

Example of gulp file

 Example of gulp file

For a long time, I use gulp in my work to automate several processes, such as compile SASS to CSS, merge different images into a sprite, make CSS styles universal for all browsers. In this short article, I just want to share the file that I use, maybe it will be useful to someone. I will not describe each line step by step, the purpose of this article is to make a cheat sheet, not a manual.

A package.json file is the best way to manage locally installed npm packages.

  • Lists the packages that your project depends on
  • Allows you to specify the versions of a package that your project can use using semantic versioning rules
  • Makes your build reproducible and therefore much easier to share with other developers

A gulpfile.js file is used to configure or define tasks and load Gulp plugins.

An easy way to use this.

  1. Put both files, for example, in the directory of your theme
  2. Install project dependencies with npm install.
  3. Run Gulp with gulp watch.

More detailed documentation on the official website.

Here is also a similar article with an example for grunt.

Also, I did not have time to try webpack, so I'll be happy if someone else shares an example of a similar file doing similar tasks, I mean SASS to CSS, Post CSS, Images to Sprite. Thanks in advance.

    "name": "makedrupaleasy",
    "version": "1.0.2",
    "author": "Ruslan P",
    "homepage": "https://makedrupaleasy.com",
    "private": true,
    "devDependencies": {
        "autoprefixer": "^9.7.4",
        "css-mqpacker": "^7.0.0",
        "gulp": "^3.9.1",
        "gulp-imagemin": "^4.1.0",
        "gulp-postcss": "^7.0.1",
        "gulp-sass": "^4.0.1",
        "gulp.spritesmith": "^6.11.0"
    "main": "gulpfile.js"
'use strict';
var gulp = require('gulp'),
  imagemin = require('gulp-imagemin'),
  spritesmith = require('gulp.spritesmith'),
  sass = require('gulp-sass'),
  sourcemaps = require('gulp-sourcemaps'),
  postcss = require('gulp-postcss'),
  autoprefixer = require('autoprefixer'),
  mqpacker = require('css-mqpacker');
gulp.task('sprite', function () {
  var spriteData = gulp.src('images/sprite-src/*.png').pipe(spritesmith({
    imgName: 'sprite.png',
    imgPath: '../images/sprite.png',
    cssName: '_sprite.scss',
    padding: 2
gulp.task('imagemin', function () {
  return gulp.src('images/*')
gulp.task('sass', function () {
      outputStyle: 'expanded',
      precision: 10
    }).on('error', sass.logError))
        sort: true
        overrideBrowserslist: ['last 2 versions']
gulp.task('imagemin', function () {
  gulp.watch(['images/**'], ['imagemin']);
gulp.task('watch', function () {
  gulp.watch(['images/sprite-src/*.png', './scss/**/*.scss'], ['sprite', 'sass']);

Ruslan Piskarov

PHP/WEB Developer / Drupal Expert. More than 11 years of experience delivering Drupal based General Purpose solutions for different sectors such as Job Boards, Product Portfolios, Geo Coding, Real Estate solutions, E-Commerce, Classifieds, Corporate and online Magazines/Newspapers.