question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Watching in Gulp: maintaining task and watch in separate places

See original GitHub issue

I’ve started using gulp for everything. I love it. It’s simple and easy to get started. However, with any non-trivial project that gulpfile.js doesn’t take long to get beastly.

One thing I’ve done to deal with this is to separate tasks into separate files (gulp/tasks/sass.js, gulp/tasks/jsx.js, gulp/tasks/ghpages.js, gulp/tasks/watch.js, …) and use require-dir to require all the tasks into my gulpfile.js. I really like this way of doing things.

But any time I add or edit a task, I have to remember to keep the watch updated as well. The “so-fresh-so-clean-snob” inside of me doesn’t like the gulp.watch(files, [task]) being so disconnected from the related task.

I played with using a gulp/config.js file to list all my src values, used in the tasks, and then looped over in my watch task so at least I only have to list my files in one place. But this goes against a core gulp philosophy: code over configuration.

I’d like to define my gulp.watch(files, [task]) in the task it’s related to:

var gulp = require('gulp');
var sass = require('gulp-sass');

gulp.task('sass', function () {
  gulp.src('./src/scss/styles.scss')
    .pipe(sass())
    .pipe(gulp.dest('./dist'));

  if (watch) { /* some kind of option defined somewhere */
    gulp.watch('./src/scss/**/*.scss', [sass]);
  }
});

I’ve played around with yargs or commander but then I feel the burden of whether or not I’m going to document supported arguments.

I’m just curious what others out there are doing?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
thasmocommented, Feb 11, 2016

With Gulp 4 I use something like this, simplified:

var task = function() {
    if(gutil.env.watch && !gulp.lastRun(task)) {
        gulp.watch('**', task);
    }

    return gulp.src('**').pipe(...);
};

Which can be called with gulp task --watch and could be more simplified maybe with a helper function.

For paths I’m also using a global config object.

You can also have a look at the current state of my approach(es): https://github.com/thasmo/gulp.boilerplate

0reactions
phatedcommented, Jul 29, 2018

Now that gulp.watch behaves the same as the rest of the task system, I don’t think it makes much sense to put inside a task. You can just put it outside the task system and start the watcher based on a flag or something.

So I’m going to close this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I keep watching my tasks on gulp 4? - Stack Overflow
Does work. When I run gulp watch, the console says that the watch started, but when I change anything in the sass file...
Read more >
Watching Files | gulp.js
The watch() API connects globs to tasks using a file system watcher. It watches for changes to files that match the globs and...
Read more >
Solved: How to stop Gulp from watching for changes when us...
Solved: Hi Team, I am using gulp with frontend-maven-plugin. All my tasks defined in gulp are working fine. There is just one BIG...
Read more >
Gulp for Beginners | CSS-Tricks
Luckily, we can tell Gulp to automatically run the sass task whenever a file is saved through a process called “watching”. Watching Sass...
Read more >
Super simple Gulp tutorial for beginners - freeCodeCamp
The Watch task will watch the files that you tell it to for any changes. Once it detects a change, it will run...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found