Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the documentation: explain how to watch files from the command line #2322

Comments

@AeonFr
Copy link

AeonFr commented Apr 20, 2019

Hello. I just did a fresh install on node and gulp.

I'm following the documentation at https://gulpjs.com/docs/en/getting-started/watching-files

The code samples explain how to set up a gulpfile to watch files. Yet, after trying to run gulp, gulp --watch or any other command using the sample gulpfile, I encounter an error:

[15:54:06] Task never defined: default

It would be nice if the site explained how to use the code sample it provides. For newbies like me.

A google search points out on that documentation article as first result, and other tutorials seem to be old, that's why I find this necessary.

For now I will continue using the gulp-watch npm package.

Thanks.

@AeonFr
Copy link
Author

AeonFr commented Apr 20, 2019

I don't know if this is best practice, but it works (without using gulp-watch or any other package)

const gulp = require('gulp');


function css(cb){
  const postcss = require('gulp-postcss')
  
  cb();
  return gulp.src('src/style.css')
    // ...
    .pipe(postcss([
      // ...
      require('autoprefixer'),
      // ...
    ]))
    // ...
    .pipe(gulp.dest('./src'))

}

gulp.task('default', function() {
  gulp.watch(['./src/style.css'], {ignoreInitial: false}, css)
})

The key is to use gulp.watch inside a function and use the first argument of the watch function as a callable.

@phated
Copy link
Member

phated commented Apr 27, 2019

@AeonFr that's not quite right. I've updated the examples in that tutorial article so they all export a default task.

@AeonFr
Copy link
Author

AeonFr commented May 4, 2019

Thanks @phated.

The key point is to call the cb() callback function as soon as the process is finished, yet in my code right now the callback is called immediately when the function starts. This looks as if the process finishes in a breeze.

Just for the sake of learning (bc right now it's working), how would one use the cb() callback properly?

It would be nice if the documentation had at least one example with a proper exports.default and a body that finishes with the cb(). It's not necessary to include it in all of the examples (but I guess it doesn't hurt nobody).

Again, thanks for replying.
Loving gulp so far ♥

@phated
Copy link
Member

phated commented May 4, 2019

@AeonFr The Async Completion guide explains the usage of an error-first callback (if you aren't able to return a stream, but you are already doing that so you don't need to use the callback).

@AeonFr
Copy link
Author

AeonFr commented May 4, 2019

Got it! Thanks, removing cb() does the trick!

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