【问题标题】:How to get task name inside task in gulp如何在 gulp 中的任务中获取任务名称
【发布时间】:2014-11-27 01:56:17
【问题描述】:

我正在使用 gulp-plumber + gulp-notify 并希望将任务名称作为标题放入 gulp-notify 中。 以下是我写的代码,先谢谢了。

gulp.task('SOMETASK', function() {
    return gulp.src(sourcePaths)
        .pipe(plumber({errorHandler: notify.onError({
            message: "<%= error.message %>",
            title: "I WANT TO PUT TASK NAME HERE"
        })}))
        // omitted below

});

【问题讨论】:

    标签: javascript gulp


    【解决方案1】:
    gulp.Gulp.prototype.__runTask = gulp.Gulp.prototype._runTask;
    gulp.Gulp.prototype._runTask = function(task) {
      this.currentTask = task;
      this.__runTask(task);
    }
    
    gulp.task("someTask", function(){
      console.log( this.currentTask.name );
    }
    

    【讨论】:

    • 这改变了吗?我在 currentTask 上未定义
    • @JimmyRare - 如果您观察 currentTask 变量的范围,它会起作用。如果您在此块之前设置var currentTask;,然后在这两个位置将this.currentTask 更改为currentTask,则可以正常工作。这段代码应该在另一个对象的范围内(因此this 引用)。
    • 在 gulp 4.0.0 中不起作用
    【解决方案2】:
    gulp.task('SOMETASK',function() {
        console.log('Task name:', this.seq.slice(-1)[0]) // Task name: SOMETASK
    })
    

    【讨论】:

    • 其实并不是对所有情况都有效,即嵌套情况。
    • 这将获取启动进程“默认”的第一个任务名称
    • 它返回解决所有依赖关系后将执行的第一个任务,而不是当前任务。
    【解决方案3】:

    如果你想对 Gulp 进行猴子补丁,以下将适用于 Gulp 版本3.9.0

    var _gulpStart = gulp.Gulp.prototype.start;
    
    var _runTask = gulp.Gulp.prototype._runTask;
    
    gulp.Gulp.prototype.start = function (taskName) {
        this.currentStartTaskName = taskName;
    
        _gulpStart.apply(this, arguments);
    };
    
    gulp.Gulp.prototype._runTask = function (task) {
        this.currentRunTaskName = task.name;
    
        _runTask.apply(this, arguments);
    };
    
    gulp.task('jscs', function () {
        console.log('this.currentStartTaskName: ' + this.currentStartTaskName);
        console.log('this.currentRunTaskName: ' + this.currentRunTaskName);
    });
    
    gulp.task('jshint', function () {
        console.log('this.currentStartTaskName: ' + this.currentStartTaskName);
        console.log('this.currentRunTaskName: ' + this.currentRunTaskName);
    });
    
    gulp.task('build', ['jshint', 'jscs']);
    

    运行gulp build 将产生以下控制台输出:

    c:\project>gulp 构建
    [16:38:54] 使用 gulpfile c:\project\gulpfile.js
    [16:38:54] 开始'jshint'...
    this.currentStartTaskName: 构建
    this.currentRunTaskName: jshint
    [16:38:54] 244 μs 后完成“jshint”
    [16:38:54] 开始 'jscs'...
    this.currentStartTaskName: 构建
    this.currentRunTaskName: jscs
    [16:38:54] 152 μs 后完成“jscs”
    [16:38:54] 开始“构建”...
    [16:38:54] 3.54 μs 后完成“构建”

    【讨论】:

    • 这增加了关于启动任务和运行任务之间区别的有用信息。
    【解决方案4】:
    let aCallerName = (new Error().stack.match(/ at [^(]+ /g)).map(s => s.replace(/(?: at | )/g,''));
    console.log( aCallerName ); 
    

    可能是 aCallerName[0] 或 aCallerName[1]....

    【讨论】:

      【解决方案5】:

      在回调之外定义任务名称并在需要时引用它。

      var taskName = 'SOMETASK';
      
      gulp.task(taskName, function() {
          return gulp.src(sourcePaths)
              .pipe(plumber({errorHandler: notify.onError({
                  message: "<%= error.message %>",
                  title: taskName
              })}));
      });
      

      【讨论】:

      • 如果它是嵌套的,这将无济于事......假设我们有 compilewatchdevproduction 任务。 dev == [compile, watch]production == [compile]。如果任务名称是 dev,那么 compile 实际上会做一些额外的 sourcemap 工作
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 2017-03-13
      相关资源
      最近更新 更多