1

Gulp task 'Copy images' copies ALL image files on the first gulp run. Then, task 'Watch source files' watches the images source folder and imagesWatcher.on('add) intercepts added files. How I can call 'Copy images' from the 'watcher.on()' callback?

Note that gulp task names in camel case are an unwanted limitation. I know that gulp.run was removed in gulp4, however here it could be very handy.

gulp.task('Copy images', () => {
   // ...
})

gulp.task('Watch source files', () => {

    let imagesWatcher = gulp.watch('src/images/**/*.*');
    imagesWatcher.on('add', function(path, stats) {
        // How to call 'Copy images'?
    });
})
fleveillee
  • 55
  • 1
  • 6
Takeshi Tokugawa YD
  • 670
  • 5
  • 40
  • 124
  • This is extremely easy with function names - in your unwanted camelCase or similar. Why don't you want to use them? – Mark Jul 14 '18 at 17:07
  • @Mark, Because of readability. Camel case is for machine, but I want that all task names will be for human. Modern IDE can automatically create the list of gulp tasks, so there is camel case will be unnatural. Can you image the menu bar of your soft with camel case menu items? ... however, I can accept the solution with aliases. – Takeshi Tokugawa YD Jul 15 '18 at 02:22

1 Answers1

4

Just make your 'Copy images' task into a function (this is the especially nice thing about gulp4) so:

function Copy_Images() {
  return gulp.src(paths.images.src)
    .pipe(newer(paths.images.dist))
    .pipe(gulp.dest(paths.images.dist));
}

function Watch_Source_Files() {
    let imagesWatcher = gulp.watch('src/images/**/*.*');
    imagesWatcher.on('add', function(path, stats) {
        // How to call 'Copy images'?
        Copy_Images();
    });
}

It is that easy with function names. Not sure what you mean by "solution with aliases" in this context.

fleveillee
  • 55
  • 1
  • 6
Mark
  • 143,421
  • 24
  • 428
  • 436