0

I have a function that I want to apply to only the changed file with gulp 4.0 watcher e.g. the current task would be:

gulp.watch([
    paths.sourceFolder + "/**/*.js",
    paths.sourceFolder + "/**/*.html",
    paths.sourceFolder + "/**/*.less",
    paths.lessFolder + "/**/*.less",
], {}

).on('change', gulp.series(lint));

This works, gives output and when it fails e.g. lint errors it continues to watch for more changes. However when I try to reformat this to run just on the single file no matter what I do it simply falls over on error and doesn't give task detail output in the console e.g.

gulp.watch([
    paths.sourceFolder + "/**/*.js",
    paths.sourceFolder + "/**/*.html",
    paths.sourceFolder + "/**/*.less",
    paths.lessFolder + "/**/*.less",
], {}).on('change', (path) => {
    lint(someDoneFunction, path)
});

How can I make this function run for a single file and retain the same output and error handling as using gulp.series()?

1 Answers1

0

Use your function in the watch callback instead of attaching it to the chokidar instance.

gulp.watch(
    [
        paths.sourceFolder + "/**/*.js",
        paths.sourceFolder + "/**/*.html",
        paths.sourceFolder + "/**/*.less",
        paths.lessFolder + "/**/*.less",
    ],
    gulp.series(lint, something, somethingElse)
);

According to the latest watch documentation, when you access the chokidar instance directly, as you do when you use on('change',...), you loose most of the task system integrations.

  • Problem is that doesn't pass any information about which individual file changed for the tasks to work on. – ian.c Oct 26 '18 at 09:47