Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp source from another task

Tags:

gulp

Right now I am creating a temporary file in Gulp which precompiles my templates. After that, I uglify and concat them into one javascript file. But I'd rather not have a temporary files. Is there a way to have a 'dynamic' sources in Gulp? I tried combining streams, but to no avail.

Example code:

gulp.task('template', function () {
  gulp.src('./src/tpl/*.hbs')
    .pipe(ember())
    .pipe(concat('tpl.js')) // make sure to only do concat after
    .pipe(gulp.dest('tmp'));
});

gulp.task('uglify', ['template'], function() {
  gulp.src(['./tmp/tpl.js', './src/js/app.js'])
    .pipe(uglify())
    .pipe(concat('epg.js'))
    .pipe(gulp.dest('dist'))
});
like image 777
Peter Briers Avatar asked Jun 30 '14 16:06

Peter Briers


2 Answers

Tasks can't be piped into other tasks directly, but you can use event-stream to merge the streams:

var es = require('event-stream');
function template() {
  return gulp.src('./src/tpl/*.hbs')
    .pipe(ember());
}

function appjs() {
  return gulp.src(['./src/js/app.js']);
}

gulp.task('uglify', function() {
  return es.merge(template(), appjs())
    .pipe(uglify())
    .pipe(concat('epg.js'))
    .pipe(gulp.dest('dist'));
});
like image 175
Ben Avatar answered Nov 13 '22 06:11

Ben


So theoretically you should be able to do this with gulp version 3.8.0 or higher. Here is the changelog with an example of the feature.

From the changelog:

gulp.src is now a writable passthrough, this means you can use it to add files to your pipeline at any point

So your task could be like this:

gulp.task('template', function () {
  return gulp.src('./src/tpl/*.hbs')
    .pipe(ember())
    .pipe(concat('tpl.js'))
    .pipe(gulp.src('./src/js/app.js', { passthrough: true }))
    .pipe(uglify())
    .pipe(concat('epg.js'))
    .pipe(gulp.dest('dist'))
});

Unfortunately, there seems to be some kind of bug where it has trouble handling certain globs, see the issues mentioned here and here. Hopefully they'll get that resolved soon. This is preventing me from using this in my project, but in yours you are only adding a single file, so try it out and maybe it will work.

If it doesn't work, try to use merge-stream https://github.com/gulpjs/gulp/blob/master/docs/recipes/using-multiple-sources-in-one-task.md

Edited to include the passthrough option to gulp.src mentioned by Bjorn Tipling

like image 23
RustyToms Avatar answered Nov 13 '22 06:11

RustyToms