在 Gulp 中定义 "global" 行为(测量任务持续时间)

Defining "global" behavior in Gulp (measuring task duration)

我正在努力将我们从 ant 转移到 gulp,并且作为工作的一部分,我想将计时统计信息写入 Graphite。我们也在 ant 中执行此操作(不知道如何,无论如何,题外话)。我的问题是,我宁愿不必为我们拥有的每个任务(我们有超过 60 个)手动添加一些或其他插件,而是有某种全局行为,对于每个任务,在任务之前 运行 计时器启动,当它发出完成信号时,我们将一些数据推送到 Graphite(通过 statsd)。

有人可以为我指出正确的方向吗?我在文档/食谱中找不到任何特别有用的东西...

我们 运行宁 gulp@4

您可以使用 NPM gulp-duration 包,而不是将计时代码添加到您的众多任务中。

它的使用示例片段如下所示:

function rebundle() {
 var uglifyTimer = duration('uglify time')
 var bundleTimer = duration('bundle time')

 return bundler.bundle()
   .pipe(source('bundle.js'))
   .pipe(bundleTimer)
   // start just before uglify recieves its first file 
   .once('data', uglifyTimer.start)
   .pipe(uglify())
   .pipe(uglifyTimer)
   .pipe(gulp.dest('example/'))
}

gulp-持续时间的duration函数:

Creates a new pass-through duration stream. When this stream is closed, it will log the amount of time since its creation to your terminal.

然后将允许您记录任务的持续时间。

虽然这不是全局行为解决方案,但至少您可以在 gulp 文件中指定计时代码,而不必修改所有 60 多个任务。