Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pipe to another task in Gulp?

I try to DRY my gulpfile. There I have small duplication of code I not comfortable with. How can this be made better?

gulp.task('scripts', function() {
  return gulp.src('src/scripts/**/*.coffee')
    .pipe(coffeelint())
    .pipe(coffeelint.reporter())
    .pipe(coffee())
    .pipe(gulp.dest('dist/scripts/'))
    .pipe(gulp.src('src/index.html'))  // this
    .pipe(includeSource())             // needs
    .pipe(gulp.dest('dist/'))          // DRY
});

gulp.task('index', function() {
  return gulp.src('src/index.html')
    .pipe(includeSource())
    .pipe(gulp.dest('dist/'))
});

I got index as a separate task, since I need to watch src/index.html to livereload. But I'm also watching my .coffee sources and when they change, I need to update src/index.html as well.

How can I pipe to index in scripts?

like image 961
srigi Avatar asked May 04 '14 16:05

srigi


People also ask

What does pipe do in Gulp?

pipe() method attaches a Writable stream to the readable, causing it to switch automatically into flowing mode and push all of its data to the attached Writable.

How do you write a gulp task?

If you just want a task that runs some function, then just do that: gulp. task('my-custom-task', function () { myCustomFunction('foo', 'bar'); });

What is Gulp series?

series() Combines task functions and/or composed operations into larger operations that will be executed one after another, in sequential order.

How to run multiple tasks at a time in Gulp?

You can run multiple tasks at a time by creating default task in the configuration file as shown in the following code − Gulp file is set up and ready to execute. Run the following command in your project directory to run the above combined tasks −

What is the difference between “gulp” and “task-name”?

Where “task-name” is a string name and “function ()” performs your task. The “gulp.task” registers the function as a task within the name and specifies the dependencies on other tasks. Let's take one plugin called minify-css to merge and minify all CSS scripts. It can be installed by using npm as shown in the following command −

What is gulp and how to use it?

The Beginner’s Guide to Gulp. What is gulp and how can you use it in… | by Emmanuel Akhigbe | The Startup | Medium Gulp is a free Javascript toolkit that helps you automate the running of tasks that are part of your workflow; this in turn helps you focus on the actual development of your project.

How do I register a task publicly in Gulp?

To register a task publicly, export it from your gulpfile. // The `clean` function is not exported so it can be considered a private task. // It can still be used within the `series ()` composition. // The `build` function is exported so it is public and can be run with the `gulp` command. // It can also be used within the `series ()` composition.


2 Answers

gulp enables you to order a series of tasks based on arguments.

Example:

gulp.task('second', ['first'], function() {
   // this occurs after 'first' finishes
});

Try the following code, you will be running the task 'index' to run both tasks:

gulp.task('scripts', function() {
  return gulp.src('src/scripts/**/*.coffee')
    .pipe(coffeelint())
    .pipe(coffeelint.reporter())
    .pipe(coffee())
    .pipe(gulp.dest('dist/scripts/'));
});

gulp.task('index', ['scripts'], function() {
  return gulp.src('src/index.html')
    .pipe(includeSource())
    .pipe(gulp.dest('dist/'))
});

The task index will now require scripts to be finished before it runs the code inside it's function.

like image 113
SteveLacy Avatar answered Nov 07 '22 05:11

SteveLacy


If you look into the Orchestrator source, particularly the .start() implementation you will see that if the last parameter is a function it will treat it as a callback.

I wrote this snippet for my own tasks:

  gulp.task( 'task1', () => console.log(a) )
  gulp.task( 'task2', () => console.log(a) )
  gulp.task( 'task3', () => console.log(a) )
  gulp.task( 'task4', () => console.log(a) )
  gulp.task( 'task5', () => console.log(a) )

  function runSequential( tasks ) {
    if( !tasks || tasks.length <= 0 ) return;

    const task = tasks[0];
    gulp.start( task, () => {
        console.log( `${task} finished` );
        runSequential( tasks.slice(1) );
    } );
  }
  gulp.task( "run-all", () => runSequential([ "task1", "task2", "task3", "task4", "task5" ));
like image 29
Assaf Moldavsky Avatar answered Nov 07 '22 03:11

Assaf Moldavsky