« Back to Index

Playing around with http://gulpjs.com/

View original Gist on GitHub

1. Gulp Basic Streaming.js

var gulp    = require('gulp');
var size    = require('gulp-filesize');
var stream  = require('stream');
var util    = require('util');
var styles  = 'Assets/Styles';
var scripts = 'Assets/Scripts';

function aNewWritableStream() {
    stream.Writable.call(this, { objectMode: true }); // make Stream behave like stream of objects instead of a Buffer with a set size

    this.on('finish', function() {
        console.log('Stream has finished, no more events can come through without causing an error');
    });
}

util.inherits(aNewWritableStream, stream.Writable);

aNewWritableStream.prototype._write = function(chunk, encoding, next) {
    console.log(chunk.contents.toString(encoding)); // logs content of file
    next();
};

// We need mulitiple Streams to be created for us to pipe the data through to
var writableStuff1 = new aNewWritableStream();
var writableStuff2 = new aNewWritableStream();

gulp.task('filesizes', function() {
    gulp.src(styles + '/**/*.css').pipe(size()).pipe(writableStuff1);
    gulp.src(scripts + '/**/*.js').pipe(size()).pipe(writableStuff2);
});

gulp.task('default', function() {
    gulp.watch([
        styles + '/**/*.css',
        scripts + '/**/*.js',
        './gulpfile.js'
    ], function() {
        gulp.run('filesizes');
    });
});

2. Gulp Basic Async.js

var gulp    = require('gulp');

gulp.task('test', function(cb) {
    cb(null); // when the async task is done we execute our callback
});

gulp.task('default', function() {
    gulp.watch([
        './gulpfile.js'
    ], function() {
        gulp.run('test', doSomethingAsync);
    });
});

function doSomethingAsync(err) {
    if (err) console.log('Error: ' + err);

    console.log('Our async task is complete and this is the callback to do something else after the async task');
}