Skip to content

Instantly share code, notes, and snippets.

@OverZealous
Last active January 3, 2016 18:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save OverZealous/8503130 to your computer and use it in GitHub Desktop.
Save OverZealous/8503130 to your computer and use it in GitHub Desktop.
Lazy Pipe for Gulp
var combine = require('stream-combiner');
function lazypipe() {
var createPipeline = function(tasks) {
var build = function() {
return combine.apply(null, tasks.map(function(t) {
return t.task.apply(null, t.args);
}));
};
build.pipe = function(task) {
if(!task) {
throw "Invalid call to lazypipe().pipe(): no task specified";
} else if(typeof task !== 'function') {
throw "Invalid call to lazypipe().pipe(): task is not a function";
}
return createPipeline(tasks.concat({
task: task,
args: Array.prototype.slice.call(arguments, 1)
}));
}
return build;
};
return createPipeline([]);
}
module.exports = lazypipe;
var jsHintTasks = lazypipe()
.pipe(jshint)
.pipe(jshint.reporter, 'jshint-stylish');
// this is OK, because lazypipes are immutable now
var jsTasks = jsHintTasks
.pipe(gulp.dest, 'build/js');
var cssTasks = lazypipe()
.pipe(recess, recessConfig)
.pipe(less)
.pipe(autoprefixer);
@OverZealous
Copy link
Author

Also, you can use a raw function if you need, like this:

var foo = lazypipe().pipe(function() {
        return jshint({globals: { angular: true }});
    })
    .pipe(jshint.reporter, 'jshint-stylish');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment