Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a Gulp task to be the default

I have the following task in my gulpFile, created by someone else on my team:

gulp.task('serve', [], function(){
   gulp.run(
    'fonts',
    'browsersync',
    'watch'
  );
});

I would like to leave it alone, but I also wanted to map the default task to this task. So I tried:

gulp.task('default',['serve']);

It appeared to work, in that the server runs, but for some reason the "watch" task is not happening, I am not getting browser refresh on changes.

It all works as planned if I run "gulp serve" but not "gulp". What did I do wrong?

EDIT: Here's the watch task:

gulp.task('watch', ['styles', 'browsersync'], function() { //'default'
  gulp.watch(
    [
      './app/assets/sass/**/*.scss',
      './app/modules/**/*.scss'
    ], ['styles']);

  gulp.watch([
    './app/**/*.js',
    './app/**/*.html'
  ], function() {
    reload();
  });

});
like image 970
Steve Avatar asked Feb 11 '15 13:02

Steve


People also ask

How do you define a task in gulp?

gulp. task('task-name', function() { // Stuff here }); task-name refers to the name of the task, which would be used whenever you want to run a task in Gulp. You can also run the same task in the command line by writing gulp task-name .

Is gulp better than Webpack?

Webpack vs Gulp - and the winner is...In terms of usability, Gulp is the winner here: it's much easier to define and execute your tasks. On the other hand, Webpack's configuration options are much more flexible + it's developing very fast and has a community growing in size with every day.

How do I run gulp locally?

Install Gulp into your local project To install Gulp locally, navigate to your project directory and run npm install gulp . You can save it to your package. json dependencies by running npm install gulp --save-dev . Once you have Gulp installed locally, you can then proceed to create your gulpfile.

What is each task in Gulp?

Each gulp task is an asynchronous JavaScript function - a function that accepts an error-first callback or returns a stream, promise, event emitter, child process, or observable (more on that later). Due to some platform limitations, synchronous tasks aren't supported, though there is a pretty nifty alternative.

Why is my default gulp task exiting prematurely?

It may be the case that your 'default' task is exiting prematurely, because gulp.run does not execute synchronously. One way or another, Gulp is confused about which tasks need to wait on what, when. You're using two completely different tools to manage your run-sequence.

Why is my gulp run not working?

I suspect your main problem is that you are mixing the usage of array task dependencies and gulp.run, but either way, gulp.run is "doing it wrong". You may also want to take a look at the Gulp docs regarding how to ensure that asynchronous tasks are executed in series: github.com/gulpjs/gulp/blob/master/docs/…

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.


1 Answers

Try updating your default task to include the watch task in the array argument instead of running it inside serve. Like so:

gulp.task('default', ['serve', 'watch']);

If you checkout the Gulp documentation on asynchronous task support, particularly the last example, you'll see that you can require a dependent task to finish before the designated task is supposed to start.

var gulp = require('gulp');

// takes in a callback so the engine knows when it'll be done
gulp.task('one', function(cb) {
    // do stuff -- async or otherwise
    cb(err); // if err is not null and not undefined, the run will stop, and note that it failed
});

// identifies a dependent task must be complete before this one begins
gulp.task('two', ['one'], function() {
    // task 'one' is done now
});

gulp.task('default', ['one', 'two']);
like image 152
Seth Avatar answered Sep 20 '22 14:09

Seth