Convert gulp watch in gulp@3.9.1 to gulp@4

前端 未结 3 1934
一向
一向 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条回答
  • 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.

    0 讨论(0)
  • 2021-01-03 16:37

    I just ran into this a couple days ago myself. What worked for me was to run each task in its own gulp.watch() with the gulp.series() on the watch task call instead of the watch task itself. For example:

    gulp.task('watch', function() {
      gulp.watch(src.js, gulp.series('minify-js'));
      gulp.watch(src.css, gulp.series('minify-css'));
    });
    
    0 讨论(0)
  • 2021-01-03 16:39

    I would recommend converting your minify-js, minify-css, clean-scripts and clean-css tasks to functions:

    var dest = {
      js: 'js/dest/some-dir/**/*.js',
      css: 'css/dest/some-dir/**/*.css'
    };
    
    function cleanCss() {
      return gulp.src(dest.css)
                 .pipe(clean({read:false, force: true});
    });
    
    function minifyCss() {
      return gulp.src(src.css)
                 .pipe(cssmin())
                 .pipe(gulp.dest(dest.css));
    });
    
    function cleanScripts() {
      return gulp.src(dest.js)
                 .pipe(clean({read:false, force: true});
    });
    
    function minifyJs() {
       return gulp.src(src.js)
           .pipe(uglify())
           .pipe(gulp.dest(dest.js));
    });
    
    var minifyJsAndClean = gulp.series(minifyJs, cleanScripts);
    var minifyCssAndClean = gulp.series(minifyCss, cleanCss);
    var watchers = function (done) {
       gulp.watch(src.js, minifyJs);
       gulp.watch(src.css, minifyCss);
       done();
    }
    
    gulp.task('watch', gulp.series(minifyJsAndClean, minifyCssAndClean, watchers));
    
    0 讨论(0)
提交回复
热议问题