Convert gulp watch in gulp@3.9.1 to gulp@4

前端 未结 3 1955
一向
一向 2021-01-03 15:41

We are switching from gulp@3.9.1 to gulp@4 and are having trouble switching over. When we run gulp watch, we are getting the following

3条回答
  •  Happy的楠姐
    2021-01-03 16:22

    gulp.task('minify-css', gulp.series('clean-css', function() {
      return gulp.src(src.css)
        .pipe(cssmin())
        .pipe(gulp.dest(dest.css));
    }));
    
    gulp.task('minify-js', gulp.series('clean-scripts', function() {
       return gulp.src(src.js)
           .pipe(uglify())
           .pipe(gulp.dest(dest.js));
    }));
    
    gulp.task('watch', gulp.series('minify-js', 'minify-css', function() {
      gulp.watch(src.js, gulp.series('minify-js'));
      gulp.watch(src.css, gulp.series('minify-css'));
    }));
    

    As @Abdaylan suggested, I also advocate switching to functions. Nevertheless, so you can see where your code was wrong, I have fixed it here. Gulp 4 does not use this syntax:

    gulp.task('someTask', ['task1', 'task2'], function () {});  // gulp 3
    

    Gulp 4:

    gulp.task('someTask', gulp.series('task1', 'task2', function () {}));  // gulp 4 with string tasks
    

    or gulp.parallel. So you can use your gulp.task syntax (rather than named functions) if you modify them to use the signatures that gulp 4 supports as I did in your modified code at the top of this answer.

    Gulp 4 with named functions:

    gulp.task(someTask, gulp.series(task1, task2, function () {}));  // gulp 4 with named functions
    

    So with named functions, the tasks are not referred to as strings.

    See also task never defined for other potential problems when migrating from gulp3 to gulp4 with the same error message.

提交回复
热议问题