Gulp

4.0.0-alpha.5 · active · verified Wed Apr 22

Gulp is a popular, open-source streaming build system for automating time-consuming development tasks, primarily in web development. It leverages Node.js streams to process files in memory, which often results in faster build times compared to tools that write intermediate files to disk. The current stable version is 5.0.1. Gulp differentiates itself from alternatives like Grunt by favoring a 'code-over-configuration' approach, allowing developers to define tasks using standard JavaScript functions and Node.js APIs, offering greater flexibility and direct control over the build process. It boasts a strong ecosystem with thousands of plugins available via npm to handle various file transformations and manipulations. Its platform-agnostic nature makes it suitable for projects across different languages and environments.

Common errors

Warnings

Install

Imports

Quickstart

This `gulpfile.js` demonstrates defining Gulp 4 tasks using plain functions, composing them with `gulp.series` and `gulp.parallel`, and using `gulp.src` and `gulp.dest` with popular plugins for cleaning, image optimization, CoffeeScript compilation, minification, concatenation, and sourcemap generation. It also includes a `watch` task for development.

var gulp = require('gulp');
var coffee = require('gulp-coffee');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var imagemin = require('gulp-imagemin');
var sourcemaps = require('gulp-sourcemaps');
var del = require('del'); // A separate npm package for deleting files

var paths = {
  scripts: ['client/js/**/*.coffee', '!client/external/**/*.coffee'],
  images: 'client/img/**/*'
};

// Clean the build directory
function clean() {
  return del(['build']);
}

// Optimize images
function images() {
  return gulp.src(paths.images)
    .pipe(imagemin({ optimizationLevel: 5 }))
    .pipe(gulp.dest('build/img'));
}

// Compile CoffeeScript, minify, concat, and add sourcemaps
function scripts() {
  return gulp.src(paths.scripts)
    .pipe(sourcemaps.init())
    .pipe(coffee())
    .pipe(uglify())
    .pipe(concat('all.min.js'))
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('build/js'));
}

// Watch for file changes and re-run tasks
function watch() {
  gulp.watch(paths.scripts, scripts);
  gulp.watch(paths.images, images);
}

// Define main build task: clean, then run scripts and images in parallel
gulp.task('build', gulp.series(
  clean,
  gulp.parallel(scripts, images)
));

// Expose individual tasks for CLI if needed
gulp.task(clean);
gulp.task(watch);

// Default task runs the 'build' task
gulp.task('default', gulp.series('build'));

view raw JSON →