GulpJS

Why ?

The JavaScript Task Runner

Gruntfile.js (configuration)

module.exports = function(grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    concat: {
      options: {
        separator: ';'
      },
      dist: {
        src: ['src/**/*.js'],
        dest: 'dist/<%= pkg.name %>.js'
      }
    },
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
      },
      dist: {
        files: {
          'dist/<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>']
        }
      }
    },
    qunit: {
      files: ['test/**/*.html']
    },
    jshint: {
      files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
      options: {
        // options here to override JSHint defaults
        globals: {
          jQuery: true,
          console: true,
          module: true,
          document: true
        }
      }
    },
    watch: {
      files: ['<%= jshint.files %>'],
      tasks: ['jshint', 'qunit']
    }
  });

  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-qunit');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-concat');

  grunt.registerTask('test', ['jshint', 'qunit']);

  grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']);

};

Stats...

Name Plugins Stars Followers URL
Grunt 3 638 8 661 44 000 click !
Gulp 777 9 373 7 058 click !
Justin Bieber 0 0 56 200 000 click !

Gulp

  • Uses streams
  • Code, not configuration
  • Strict rules for plugins

Gulpfile.js (code)

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');

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

// Not all tasks need to use streams
// A gulpfile is just another node program and you can use all packages available on npm
gulp.task('clean', function(cb) {
  // You can use multiple globbing patterns as you would with `gulp.src`
  del(['build'], cb);
});

gulp.task('scripts', ['clean'], function() {
  // Minify and copy all JavaScript (except vendor scripts)
  // with sourcemaps all the way down
  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'));
});

// Copy all static images
gulp.task('images', ['clean'], function() {
  return gulp.src(paths.images)
    // Pass in options to the task
    .pipe(imagemin({optimizationLevel: 5}))
    .pipe(gulp.dest('build/img'));
});

// Rerun the task when a file changes
gulp.task('watch', function() {
  gulp.watch(paths.scripts, ['scripts']);
  gulp.watch(paths.images, ['images']);
});

// The default task (called when you run `gulp` from cli)
gulp.task('default', ['watch', 'scripts', 'images']);

Moar gulp

Node Streams

A stream is an abstract interface implemented by various objects in Node. For example a request to an HTTP server is a stream, as is stdout. Streams are readable, writable, or both.

Streams

Orchestrator

https://github.com/orchestrator/orchestrator

A module for sequencing and executing tasks and dependencies in maximum concurrency

Vinyl

https://github.com/wearefractal/vinyl

 

  • embeds file description
    • path
    • content
  • Works with files, streams, buffers

Gulp API

gulp.task(name[, deps], fn)

  • Define a task
  • Can have dependencies
var gulp = require('gulp');

gulp.task('one', function(cb) {
    cb(err);
});

gulp.task('two', ['one'], function() {
    // task 'one' is done now
});

gulp.task('default', ['one', 'two']);

gulp.watch(glob [, opts], tasks)

Watch files and do something when a file changes.

gulp.watch('js/**/*.js', 
    ['uglify','reload']);

gulp.watch(glob [, opts, cb])

gulp.src(globs[, options])

Takes a glob and represents a file structure.

gulp.dest(path[, options])

  • Writes everything to the given output
  • Returns the given everything
gulp.src('./client/templates/*.jade')
  .pipe(jade())
  .pipe(gulp.dest('./build/templates'))
  .pipe(minify())
  .pipe(gulp.dest('./build/minified_templates'));

gulp -t

Display the tasks tree

The Future

Finally !

Links

Questions ?

  • Are tasks running in parallel ?
    • Yep, you can use runSequence to force a serial execution
  • Is it fast ?
    • I don't know, I added links about this (@florentduveau told me about a big project switching from grunt to gulp to improve the build time by 50%)

GulpJS

By Siegfried Ehret

GulpJS

Some stuff about gulpJS

  • 1,685
Loading comments...

More from Siegfried Ehret