Gulp run alternative

后端 未结 3 1934
旧时难觅i
旧时难觅i 2021-01-13 14:31

Everytime I run gulp, I see this message gulp.run() has been deprecated. Use task dependencies or gulp.watch task triggering instead.

Example code:

相关标签:
3条回答
  • 2021-01-13 15:18

    You shouldn't use run. Here is an alternative (to address that part of your answer), but not what you need to do:

    gulp
        .start('default')
        .once('task_stop', function(){
            //do other stuff.
         });
    

    If you really must fire an ad hoc task, but can literally use run...You can use .start with the task name, and also attach to the task_stop handler to fire something when the task is complete. This is nice when writing tests for gulp tasks, but that's really it.

    however in day to day gulp usage, this is an antipattern.

    Normally, you build smaller tasks and composite them. This is the right way. See this:

    var gulp = require('gulp'),
        runSequence = require('run-sequence');
    
    function a(){
      //gulpstuff
    } 
    function b(){
      //gulpstuff
    }
    
    function d(callback){
      runSequence('a', 'b', callback)
    }
    
    gulp
        .task('a', a) // gulp a -runs a
        .task('b', b) // gulp b runs b
        .task('c', ['a', 'b']) //gulp c runs a and b at the same time
        .task('d', d); //gulp d runs a, then b.
    

    basically if c or d was a watch task, you'd achieve the same goal of firing the already registered smaller gulp tasks without .run

    0 讨论(0)
  • 2021-01-13 15:18

    You can always just use plain old javascript functions. From what i've read this is considered to be a more "gulp-ish" way of doing things.

    I ran into a similar situation once and basically solved it with something like this:

    var watch = require('gulp-watch');
    watch(['public/**/*.js','!public/**/*.min.js'], function(){
        compress();
        bsReload();
    });
    

    And then these functions are basically wrappers around the guts of what would have been your original gulp tasks:

    var compress = function () {
      return gulp.src("stuff/**")
              .pipe(gulp-compress())
              .pipe(gulp.dest("./the_end/");
    };
    

    Its easy to become caught up in the idea that one has to use gulp tasks for everything otherwise you are "doing it wrong" but if you need to use something like this go for it.

    If you also want a gulp task with the same functionality then whip up something like this:

    gulp.task("compress", function () {
      return compress();
    });
    

    and you can still take advantage of gulp task dependecies when using the same code if you need it somewhere else.

    0 讨论(0)
  • 2021-01-13 15:19

    gulp.run() was deprecated because people were using it as a crutch. You are using it as a crutch!

    I'm not sure why you're using gulp-watch, the built in gulp.watch would be far more appropriate for what you're using it for. Have a look at the documentation for .watch: https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulpwatchglob--opts-tasks-or-gulpwatchglob--opts-cb

    Here's what you should have written. Please understand why you're using it instead of just copying it:

    gulp.watch(['public/**/*.js','!public/**/*.min.js'], ['compressjs', 'bs-reload'])
    
    0 讨论(0)
提交回复
热议问题