NiLL
NiLL

Reputation: 13843

How to start Gulp task with params?

I need to apply a build task for specific files. For finding them, I use the typical template. But I can't understood how to pass the arguments (file path) from gulp.src.

Desirable solution.

gulp.task('bundles', function() {
  gulp.src('bundles/**/*.js').
    pipe(gulp.start('build', file.path));
});

gulp.task('build', function (path) {
  // use here
});

Upvotes: 5

Views: 4122

Answers (3)

joemaller
joemaller

Reputation: 20556

First off, gulp.task('build', function (path) won't ever work. The only valid argument for gulp tasks is a callback to signal asynchronous task completion. If you tried to do run the above, gulp would expect path to be a function and the task would never complete unless that function was called. In this example, the 'build' task should be a regular function called from the 'bundles' pipe, not a task.

The better question would be: How do I run a custom function inside a gulp pipe? Plugins like gulp-tap might get you close, but it's not difficult to create what is essentially an inline gulp plugin to call your function.

Gulp pipes receive a through2 object stream containing a vinyl file object, an encoding and a callback. Here's a basic skeleton for calling any arbitrary function against the files in a gulp pipe:

var gulp = require('gulp');
var through = require('through2');

gulp.task('stack', function() {
  return gulp.src('./src/*.js')
    .pipe(through.obj(function(file, enc, cb) {
      // file.path is the full path to the file
      myBuildFunction(file.path);
      cb(null, file);
    }))
    .pipe(gulp.dest('./build/'));
})

This can be incredibly powerful. To modify the file's contents, just change the file.contents buffer. To rename or relocate the file, change file.path. Everything can be done in gulp's native pipes.

Upvotes: 0

Olivier Clément
Olivier Clément

Reputation: 764

Question is a bit stale and I am not sure I totally understand what you're trying to achieve here, but I think what you're looking for is lazypipe

You might want to clarify your question if that's not what you're looking for

Example Usage:

var lazypipe = require('lazypipe'),
g = require('gulp-load-plugins')({lazy: true}),

jsTransformPipe = lazypipe()
    .pipe(g.jshint) // <-- Notice the notation: g.jshint, not g.jshint()
    .pipe(g.concat, 'bundle.js'), // <-- Notice how the param is passed to g.concat, as a second param to .pipe()

jsSourcePipe = lazypipe()
    .pipe(gulp.src, './**/*.js');

gulp.task('bundle', function() {
    jsSourcePipe()
        .pipe(jsTransformPipe()) // <-- You execute the lazypipe by calling it as a function
        .pipe(gulp.dest('../build/');
});

With lazypipe you basically create a pipe for future use; hope this help

Upvotes: 1

soenguy
soenguy

Reputation: 1361

(Can't comment because of rep, sorry)

I assume that your sample code isn't filled with everything, but why don't you merge those tasks and use your gulp.src() in your build task instead of calling another task.

Maybe it's useful for you but with what you're showing I can't find an explanation for why you do this instead of simply going with something like :

gulp.task('build', function (path) {
    gulp.src('bundles/**/*.js)
    //Your code for this task
});

Of course, it removes the bundles task, but it's not useful as is. Don't hesitate to comment if I'm wrong and I'll try to help you as much as I can.

Upvotes: 1

Related Questions